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 |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 34 | Mode getTimeMode() |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 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 | } |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame^] | 59 | void notifyOnHostState(bool hostOn) |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 60 | { |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame^] | 61 | manager.onHostState(hostOn); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 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 |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 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 | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame^] | 77 | TEST_F(TestManager, DISABLED_hostStateChange) |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 78 | { |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame^] | 79 | notifyOnHostState(true); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 80 | EXPECT_TRUE(hostOn()); |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame^] | 81 | notifyOnHostState(false); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 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 |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 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); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 95 | |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 96 | notifyPropertyChanged( |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 97 | "TimeSyncMethod", |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 98 | "xyz.openbmc_project.Time.Synchronization.Method.Manual"); |
| 99 | notifyPropertyChanged( |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 100 | "TimeOwner", |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 101 | "xyz.openbmc_project.Time.Owner.Owners.Host"); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 102 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 103 | EXPECT_EQ("", getRequestedMode()); |
| 104 | EXPECT_EQ("", getRequestedOwner()); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 105 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 106 | // When host is on, property changes are saved as requested ones |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame^] | 107 | notifyOnHostState(true); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 108 | |
| 109 | // Check mocked listeners shall not receive notifications |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 110 | EXPECT_CALL(listener1, onModeChanged(Mode::Manual)).Times(0); |
| 111 | EXPECT_CALL(listener1, onOwnerChanged(Owner::Host)).Times(0); |
| 112 | EXPECT_CALL(listener2, onModeChanged(Mode::Manual)).Times(0); |
| 113 | EXPECT_CALL(listener2, onOwnerChanged(Owner::Host)).Times(0); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 114 | |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 115 | notifyPropertyChanged( |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 116 | "TimeSyncMethod", |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 117 | "xyz.openbmc_project.Time.Synchronization.Method.NTP"); |
| 118 | notifyPropertyChanged( |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 119 | "TimeOwner", |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 120 | "xyz.openbmc_project.Time.Owner.Owners.Split"); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 121 | |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 122 | EXPECT_EQ("xyz.openbmc_project.Time.Synchronization.Method.NTP", |
| 123 | getRequestedMode()); |
| 124 | EXPECT_EQ("xyz.openbmc_project.Time.Owner.Owners.Split", |
| 125 | getRequestedOwner()); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 126 | |
| 127 | |
| 128 | // When host becomes off, the requested mode/owner shall be notified |
| 129 | // to listners, and be cleared |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 130 | EXPECT_CALL(listener1, onModeChanged(Mode::NTP)).Times(1); |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 131 | EXPECT_CALL(listener1, onOwnerChanged(Owner::Split)).Times(1); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 132 | EXPECT_CALL(listener2, onModeChanged(Mode::NTP)).Times(1); |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 133 | EXPECT_CALL(listener2, onOwnerChanged(Owner::Split)).Times(1); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 134 | |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame^] | 135 | notifyOnHostState(false); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 136 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 137 | EXPECT_EQ("", getRequestedMode()); |
| 138 | EXPECT_EQ("", getRequestedOwner()); |
| 139 | |
| 140 | // When host is on, and invalid property is changed, |
| 141 | // verify the code asserts because it shall never occur |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame^] | 142 | notifyOnHostState(true); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 143 | ASSERT_DEATH(notifyPropertyChanged("invalid property", "whatever"), ""); |
| 144 | } |
| 145 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 146 | TEST_F(TestManager, DISABLED_propertyChangedAndChangedbackWhenHostOn) |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 147 | { |
| 148 | // Property is now MANUAL/HOST |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 149 | notifyPropertyChanged( |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 150 | "TimeSyncMethod", |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 151 | "xyz.openbmc_project.Time.Synchronization.Method.Manual"); |
| 152 | notifyPropertyChanged( |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 153 | "TimeOwner", |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 154 | "xyz.openbmc_project.Time.Owner.Owners.Host"); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 155 | |
| 156 | // Set host on |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame^] | 157 | notifyOnHostState(true); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 158 | |
| 159 | // Check mocked listeners shall not receive notifications |
| 160 | EXPECT_CALL(listener1, onModeChanged(_)).Times(0); |
| 161 | EXPECT_CALL(listener1, onOwnerChanged(_)).Times(0); |
| 162 | EXPECT_CALL(listener2, onModeChanged(_)).Times(0); |
| 163 | EXPECT_CALL(listener2, onOwnerChanged(_)).Times(0); |
| 164 | |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 165 | notifyPropertyChanged( |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 166 | "TimeSyncMethod", |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 167 | "xyz.openbmc_project.Time.Synchronization.Method.NTP"); |
| 168 | notifyPropertyChanged( |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 169 | "TimeOwner", |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 170 | "xyz.openbmc_project.Time.Owner.Owners.Split"); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 171 | |
| 172 | // Saved as requested mode/owner |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 173 | EXPECT_EQ("xyz.openbmc_project.Time.Synchronization.Method.NTP", |
| 174 | getRequestedMode()); |
| 175 | EXPECT_EQ("xyz.openbmc_project.Time.Owner.Owners.Split", |
| 176 | getRequestedOwner()); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 177 | |
| 178 | // Property changed back to MANUAL/HOST |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 179 | notifyPropertyChanged( |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 180 | "TimeSyncMethod", |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 181 | "xyz.openbmc_project.Time.Synchronization.Method.Manual"); |
| 182 | notifyPropertyChanged( |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 183 | "TimeOwner", |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 184 | "xyz.openbmc_project.Time.Owner.Owners.Host"); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 185 | |
| 186 | // Requested mode/owner shall be updated |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 187 | EXPECT_EQ("xyz.openbmc_project.Time.Synchronization.Method.Manual", |
| 188 | getRequestedMode()); |
| 189 | EXPECT_EQ("xyz.openbmc_project.Time.Owner.Owners.Host", |
| 190 | getRequestedOwner()); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 191 | |
| 192 | // Because the latest mode/owner is the same as when host is off, |
| 193 | // The listeners shall not be notified, and requested mode/owner |
| 194 | // shall be cleared |
| 195 | EXPECT_CALL(listener1, onModeChanged(_)).Times(0); |
| 196 | EXPECT_CALL(listener1, onOwnerChanged(_)).Times(0); |
| 197 | EXPECT_CALL(listener2, onModeChanged(_)).Times(0); |
| 198 | EXPECT_CALL(listener2, onOwnerChanged(_)).Times(0); |
| 199 | |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame^] | 200 | notifyOnHostState(false); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 201 | |
| 202 | EXPECT_EQ("", getRequestedMode()); |
| 203 | EXPECT_EQ("", getRequestedOwner()); |
| 204 | } |
| 205 | |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 206 | // TODO: if gmock is ready, add case to test |
| 207 | // updateNtpSetting() and updateNetworkSetting() |
| 208 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 209 | } |
| 210 | } |