Mike Frysinger | 1dad097 | 2019-02-23 18:36:37 -0500 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "dev-install/dev_install.h" |
| 6 | |
Mike Frysinger | 60260f6 | 2019-02-24 02:28:23 -0500 | [diff] [blame^] | 7 | #include <unistd.h> |
| 8 | |
| 9 | #include <istream> |
| 10 | #include <sstream> |
| 11 | #include <string> |
| 12 | |
Mike Frysinger | 1dad097 | 2019-02-23 18:36:37 -0500 | [diff] [blame] | 13 | #include <gmock/gmock.h> |
| 14 | #include <gtest/gtest.h> |
| 15 | |
| 16 | using ::testing::_; |
| 17 | using ::testing::Return; |
| 18 | |
| 19 | namespace dev_install { |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | class DevInstallMock : public DevInstall { |
| 24 | public: |
| 25 | MOCK_METHOD1(Exec, int(const std::vector<const char*>&)); |
Mike Frysinger | ee5af6e | 2019-02-23 23:47:03 -0500 | [diff] [blame] | 26 | MOCK_CONST_METHOD0(IsDevMode, bool()); |
Mike Frysinger | 60260f6 | 2019-02-24 02:28:23 -0500 | [diff] [blame^] | 27 | MOCK_METHOD2(PromptUser, bool(std::istream&, const std::string&)); |
Mike Frysinger | 1dad097 | 2019-02-23 18:36:37 -0500 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | class DevInstallTest : public ::testing::Test { |
Mike Frysinger | ee5af6e | 2019-02-23 23:47:03 -0500 | [diff] [blame] | 31 | public: |
| 32 | void SetUp() override { |
| 33 | // Set the default to dev mode enabled. Most tests want that. |
| 34 | ON_CALL(dev_install_, IsDevMode()).WillByDefault(Return(true)); |
| 35 | } |
| 36 | |
Mike Frysinger | 1dad097 | 2019-02-23 18:36:37 -0500 | [diff] [blame] | 37 | protected: |
| 38 | DevInstallMock dev_install_; |
| 39 | }; |
| 40 | |
| 41 | } // namespace |
| 42 | |
| 43 | // Check default run through. |
| 44 | TEST_F(DevInstallTest, Run) { |
| 45 | EXPECT_CALL(dev_install_, Exec(_)).WillOnce(Return(1234)); |
| 46 | EXPECT_EQ(1234, dev_install_.Run()); |
| 47 | } |
| 48 | |
Mike Frysinger | ee5af6e | 2019-02-23 23:47:03 -0500 | [diff] [blame] | 49 | // Systems not in dev mode should abort. |
| 50 | TEST_F(DevInstallTest, NonDevMode) { |
| 51 | EXPECT_CALL(dev_install_, IsDevMode()).WillOnce(Return(false)); |
| 52 | EXPECT_CALL(dev_install_, Exec(_)).Times(0); |
| 53 | EXPECT_EQ(2, dev_install_.Run()); |
| 54 | } |
| 55 | |
Mike Frysinger | 60260f6 | 2019-02-24 02:28:23 -0500 | [diff] [blame^] | 56 | namespace { |
| 57 | |
| 58 | class PromptUserTest : public ::testing::Test { |
| 59 | protected: |
| 60 | DevInstall dev_install_; |
| 61 | }; |
| 62 | |
| 63 | } // namespace |
| 64 | |
| 65 | // The --yes flag should pass w/out prompting the user. |
| 66 | TEST_F(PromptUserTest, Forced) { |
| 67 | dev_install_.SetYesForTest(true); |
| 68 | std::stringstream stream(""); |
| 69 | EXPECT_TRUE(dev_install_.PromptUser(stream, "")); |
| 70 | } |
| 71 | |
| 72 | // EOF input should fail. |
| 73 | TEST_F(PromptUserTest, Eof) { |
| 74 | std::stringstream stream(""); |
| 75 | EXPECT_FALSE(dev_install_.PromptUser(stream, "")); |
| 76 | } |
| 77 | |
| 78 | // Default input (hitting enter) should fail. |
| 79 | TEST_F(PromptUserTest, Default) { |
| 80 | std::stringstream stream("\n"); |
| 81 | EXPECT_FALSE(dev_install_.PromptUser(stream, "")); |
| 82 | } |
| 83 | |
| 84 | // Entering "n" should fail. |
| 85 | TEST_F(PromptUserTest, No) { |
| 86 | std::stringstream stream("n\n"); |
| 87 | EXPECT_FALSE(dev_install_.PromptUser(stream, "")); |
| 88 | } |
| 89 | |
| 90 | // Entering "y" should pass. |
| 91 | TEST_F(PromptUserTest, Yes) { |
| 92 | std::stringstream stream("y\n"); |
| 93 | EXPECT_TRUE(dev_install_.PromptUser(stream, "")); |
| 94 | } |
| 95 | |
Mike Frysinger | 1dad097 | 2019-02-23 18:36:37 -0500 | [diff] [blame] | 96 | } // namespace dev_install |