Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 1 | #include <sdbusplus/bus.hpp> |
| 2 | #include <gtest/gtest.h> |
| 3 | |
| 4 | #include "types.hpp" |
| 5 | #include "manager.hpp" |
| 6 | |
| 7 | namespace phosphor |
| 8 | { |
| 9 | namespace time |
| 10 | { |
| 11 | |
| 12 | class TestManager : public testing::Test |
| 13 | { |
| 14 | public: |
| 15 | sdbusplus::bus::bus bus; |
| 16 | Manager manager; |
| 17 | |
| 18 | TestManager() |
| 19 | : bus(sdbusplus::bus::new_default()), |
| 20 | manager(bus) |
| 21 | { |
| 22 | // Empty |
| 23 | } |
| 24 | |
| 25 | // Proxies for Manager's private members and functions |
| 26 | Mode getTimeMode() |
| 27 | { |
| 28 | return manager.timeMode; |
| 29 | } |
| 30 | Owner getTimeOwner() |
| 31 | { |
| 32 | return manager.timeOwner; |
| 33 | } |
| 34 | Mode convertToMode(const std::string& mode) |
| 35 | { |
| 36 | return Manager::convertToMode(mode); |
| 37 | } |
| 38 | Owner convertToOwner(const std::string& owner) |
| 39 | { |
| 40 | return Manager::convertToOwner(owner); |
| 41 | } |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 42 | bool hostOn() |
| 43 | { |
| 44 | return manager.hostOn; |
| 45 | } |
| 46 | std::string getRequestedMode() |
| 47 | { |
| 48 | return manager.requestedMode; |
| 49 | } |
| 50 | std::string getRequestedOwner() |
| 51 | { |
| 52 | return manager.requestedOwner; |
| 53 | } |
| 54 | void notifyPropertyChanged(const std::string& key, |
| 55 | const std::string& value) |
| 56 | { |
| 57 | manager.onPropertyChanged(key, value); |
| 58 | } |
| 59 | void notifyPgoodChanged(bool pgood) |
| 60 | { |
| 61 | manager.onPgoodChanged(pgood); |
| 62 | } |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | TEST_F(TestManager, empty) |
| 66 | { |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 67 | EXPECT_FALSE(hostOn()); |
| 68 | EXPECT_EQ("", getRequestedMode()); |
| 69 | EXPECT_EQ("", getRequestedOwner()); |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 70 | EXPECT_EQ(Mode::NTP, getTimeMode()); |
| 71 | EXPECT_EQ(Owner::BMC, getTimeOwner()); |
| 72 | } |
| 73 | |
| 74 | TEST_F(TestManager, convertToMode) |
| 75 | { |
| 76 | EXPECT_EQ(Mode::NTP, convertToMode("NTP")); |
| 77 | EXPECT_EQ(Mode::MANUAL, convertToMode("MANUAL")); |
| 78 | |
| 79 | // All unrecognized strings are mapped to Ntp |
| 80 | EXPECT_EQ(Mode::NTP, convertToMode("")); |
| 81 | EXPECT_EQ(Mode::NTP, convertToMode("Manual")); |
| 82 | EXPECT_EQ(Mode::NTP, convertToMode("whatever")); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | TEST_F(TestManager, convertToOwner) |
| 87 | { |
| 88 | EXPECT_EQ(Owner::BMC, convertToOwner("BMC")); |
| 89 | EXPECT_EQ(Owner::HOST, convertToOwner("HOST")); |
| 90 | EXPECT_EQ(Owner::SPLIT, convertToOwner("SPLIT")); |
| 91 | EXPECT_EQ(Owner::BOTH, convertToOwner("BOTH")); |
| 92 | |
| 93 | // All unrecognized strings are mapped to Bmc |
| 94 | EXPECT_EQ(Owner::BMC, convertToOwner("")); |
| 95 | EXPECT_EQ(Owner::BMC, convertToOwner("Split")); |
| 96 | EXPECT_EQ(Owner::BMC, convertToOwner("xyz")); |
| 97 | } |
| 98 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 99 | TEST_F(TestManager, pgoodChange) |
| 100 | { |
| 101 | notifyPgoodChanged(true); |
| 102 | EXPECT_TRUE(hostOn()); |
| 103 | notifyPgoodChanged(false); |
| 104 | EXPECT_FALSE(hostOn()); |
| 105 | } |
| 106 | |
| 107 | TEST_F(TestManager, propertyChange) |
| 108 | { |
| 109 | // When host is off, property change will be notified to listners |
| 110 | EXPECT_FALSE(hostOn()); |
| 111 | notifyPropertyChanged("time_mode", "MANUAL"); |
| 112 | notifyPropertyChanged("time_owner", "HOST"); |
| 113 | EXPECT_EQ("", getRequestedMode()); |
| 114 | EXPECT_EQ("", getRequestedOwner()); |
| 115 | // TODO: if gmock is ready, check mocked listners shall receive notifies |
| 116 | |
| 117 | notifyPgoodChanged(true); |
| 118 | // When host is on, property changes are saved as requested ones |
| 119 | notifyPropertyChanged("time_mode", "MANUAL"); |
| 120 | notifyPropertyChanged("time_owner", "HOST"); |
| 121 | EXPECT_EQ("MANUAL", getRequestedMode()); |
| 122 | EXPECT_EQ("HOST", getRequestedOwner()); |
| 123 | |
| 124 | |
| 125 | // When host becomes off, the requested mode/owner shall be notified |
| 126 | // to listners, and be cleared |
| 127 | notifyPgoodChanged(false); |
| 128 | // TODO: if gmock is ready, check mocked listners shall receive notifies |
| 129 | EXPECT_EQ("", getRequestedMode()); |
| 130 | EXPECT_EQ("", getRequestedOwner()); |
| 131 | |
| 132 | // When host is on, and invalid property is changed, |
| 133 | // verify the code asserts because it shall never occur |
| 134 | notifyPgoodChanged(true); |
| 135 | ASSERT_DEATH(notifyPropertyChanged("invalid property", "whatever"), ""); |
| 136 | } |
| 137 | |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 138 | // TODO: if gmock is ready, add case to test |
| 139 | // updateNtpSetting() and updateNetworkSetting() |
| 140 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 141 | } |
| 142 | } |