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" |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 6 | #include "mocked_property_change_listener.hpp" |
| 7 | |
| 8 | using ::testing::_; |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 9 | |
| 10 | namespace phosphor |
| 11 | { |
| 12 | namespace time |
| 13 | { |
| 14 | |
| 15 | class TestManager : public testing::Test |
| 16 | { |
| 17 | public: |
| 18 | sdbusplus::bus::bus bus; |
| 19 | Manager manager; |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 20 | MockPropertyChangeListner listener1; |
| 21 | MockPropertyChangeListner listener2; |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 22 | |
| 23 | TestManager() |
| 24 | : bus(sdbusplus::bus::new_default()), |
| 25 | manager(bus) |
| 26 | { |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 27 | // Add two mocked listeners so that we can test |
| 28 | // the behavior related to listeners |
| 29 | manager.addListener(&listener1); |
| 30 | manager.addListener(&listener2); |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | // Proxies for Manager's private members and functions |
| 34 | Mode getTimeMode() |
| 35 | { |
| 36 | return manager.timeMode; |
| 37 | } |
| 38 | Owner getTimeOwner() |
| 39 | { |
| 40 | return manager.timeOwner; |
| 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 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 65 | TEST_F(TestManager, DISABLED_empty) |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 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 | 57eab12 | 2017-06-16 10:27:30 +0800 | [diff] [blame^] | 70 | |
| 71 | // Default mode/owner is MANUAL/BOTH |
| 72 | EXPECT_EQ(Mode::MANUAL, getTimeMode()); |
| 73 | EXPECT_EQ(Owner::BOTH, getTimeOwner()); |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 74 | } |
| 75 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 76 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 77 | TEST_F(TestManager, DISABLED_pgoodChange) |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 78 | { |
| 79 | notifyPgoodChanged(true); |
| 80 | EXPECT_TRUE(hostOn()); |
| 81 | notifyPgoodChanged(false); |
| 82 | EXPECT_FALSE(hostOn()); |
| 83 | } |
| 84 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 85 | TEST_F(TestManager, DISABLED_propertyChanged) |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 86 | { |
| 87 | // When host is off, property change will be notified to listners |
| 88 | EXPECT_FALSE(hostOn()); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 89 | |
| 90 | // Check mocked listeners shall receive notifications on property changed |
| 91 | EXPECT_CALL(listener1, onModeChanged(Mode::MANUAL)).Times(1); |
| 92 | EXPECT_CALL(listener1, onOwnerChanged(Owner::HOST)).Times(1); |
| 93 | EXPECT_CALL(listener2, onModeChanged(Mode::MANUAL)).Times(1); |
| 94 | EXPECT_CALL(listener2, onOwnerChanged(Owner::HOST)).Times(1); |
| 95 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 96 | notifyPropertyChanged("time_mode", "MANUAL"); |
| 97 | notifyPropertyChanged("time_owner", "HOST"); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 98 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 99 | EXPECT_EQ("", getRequestedMode()); |
| 100 | EXPECT_EQ("", getRequestedOwner()); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 101 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 102 | // When host is on, property changes are saved as requested ones |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 103 | notifyPgoodChanged(true); |
| 104 | |
| 105 | // Check mocked listeners shall not receive notifications |
| 106 | EXPECT_CALL(listener1, onModeChanged(Mode::MANUAL)).Times(0); |
| 107 | EXPECT_CALL(listener1, onOwnerChanged(Owner::HOST)).Times(0); |
| 108 | EXPECT_CALL(listener2, onModeChanged(Mode::MANUAL)).Times(0); |
| 109 | EXPECT_CALL(listener2, onOwnerChanged(Owner::HOST)).Times(0); |
| 110 | |
| 111 | notifyPropertyChanged("time_mode", "NTP"); |
| 112 | notifyPropertyChanged("time_owner", "SPLIT"); |
| 113 | |
| 114 | EXPECT_EQ("NTP", getRequestedMode()); |
| 115 | EXPECT_EQ("SPLIT", getRequestedOwner()); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 116 | |
| 117 | |
| 118 | // When host becomes off, the requested mode/owner shall be notified |
| 119 | // to listners, and be cleared |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 120 | EXPECT_CALL(listener1, onModeChanged(Mode::NTP)).Times(1); |
| 121 | EXPECT_CALL(listener1, onOwnerChanged(Owner::SPLIT)).Times(1); |
| 122 | EXPECT_CALL(listener2, onModeChanged(Mode::NTP)).Times(1); |
| 123 | EXPECT_CALL(listener2, onOwnerChanged(Owner::SPLIT)).Times(1); |
| 124 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 125 | notifyPgoodChanged(false); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 126 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 127 | EXPECT_EQ("", getRequestedMode()); |
| 128 | EXPECT_EQ("", getRequestedOwner()); |
| 129 | |
| 130 | // When host is on, and invalid property is changed, |
| 131 | // verify the code asserts because it shall never occur |
| 132 | notifyPgoodChanged(true); |
| 133 | ASSERT_DEATH(notifyPropertyChanged("invalid property", "whatever"), ""); |
| 134 | } |
| 135 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 136 | TEST_F(TestManager, DISABLED_propertyChangedAndChangedbackWhenHostOn) |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 137 | { |
| 138 | // Property is now MANUAL/HOST |
| 139 | notifyPropertyChanged("time_mode", "MANUAL"); |
| 140 | notifyPropertyChanged("time_owner", "HOST"); |
| 141 | |
| 142 | // Set host on |
| 143 | notifyPgoodChanged(true); |
| 144 | |
| 145 | // Check mocked listeners shall not receive notifications |
| 146 | EXPECT_CALL(listener1, onModeChanged(_)).Times(0); |
| 147 | EXPECT_CALL(listener1, onOwnerChanged(_)).Times(0); |
| 148 | EXPECT_CALL(listener2, onModeChanged(_)).Times(0); |
| 149 | EXPECT_CALL(listener2, onOwnerChanged(_)).Times(0); |
| 150 | |
| 151 | notifyPropertyChanged("time_mode", "NTP"); |
| 152 | notifyPropertyChanged("time_owner", "SPLIT"); |
| 153 | |
| 154 | // Saved as requested mode/owner |
| 155 | EXPECT_EQ("NTP", getRequestedMode()); |
| 156 | EXPECT_EQ("SPLIT", getRequestedOwner()); |
| 157 | |
| 158 | // Property changed back to MANUAL/HOST |
| 159 | notifyPropertyChanged("time_mode", "MANUAL"); |
| 160 | notifyPropertyChanged("time_owner", "HOST"); |
| 161 | |
| 162 | // Requested mode/owner shall be updated |
| 163 | EXPECT_EQ("MANUAL", getRequestedMode()); |
| 164 | EXPECT_EQ("HOST", getRequestedOwner()); |
| 165 | |
| 166 | // Because the latest mode/owner is the same as when host is off, |
| 167 | // The listeners shall not be notified, and requested mode/owner |
| 168 | // shall be cleared |
| 169 | EXPECT_CALL(listener1, onModeChanged(_)).Times(0); |
| 170 | EXPECT_CALL(listener1, onOwnerChanged(_)).Times(0); |
| 171 | EXPECT_CALL(listener2, onModeChanged(_)).Times(0); |
| 172 | EXPECT_CALL(listener2, onOwnerChanged(_)).Times(0); |
| 173 | |
| 174 | notifyPgoodChanged(false); |
| 175 | |
| 176 | EXPECT_EQ("", getRequestedMode()); |
| 177 | EXPECT_EQ("", getRequestedOwner()); |
| 178 | } |
| 179 | |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 180 | // TODO: if gmock is ready, add case to test |
| 181 | // updateNtpSetting() and updateNetworkSetting() |
| 182 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 183 | } |
| 184 | } |