Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 1 | #include "manager.hpp" |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 2 | #include "mocked_property_change_listener.hpp" |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 3 | #include "types.hpp" |
| 4 | |
| 5 | #include <sdbusplus/bus.hpp> |
| 6 | |
| 7 | #include <gtest/gtest.h> |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 8 | |
| 9 | using ::testing::_; |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 10 | |
| 11 | namespace phosphor |
| 12 | { |
| 13 | namespace time |
| 14 | { |
| 15 | |
| 16 | class TestManager : public testing::Test |
| 17 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 18 | public: |
| 19 | sdbusplus::bus::bus bus; |
| 20 | Manager manager; |
| 21 | MockPropertyChangeListner listener1; |
| 22 | MockPropertyChangeListner listener2; |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 23 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 24 | TestManager() : bus(sdbusplus::bus::new_default()), manager(bus) |
| 25 | { |
| 26 | // Add two mocked listeners so that we can test |
| 27 | // the behavior related to listeners |
| 28 | manager.addListener(&listener1); |
| 29 | manager.addListener(&listener2); |
| 30 | } |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 31 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 32 | // Proxies for Manager's private members and functions |
| 33 | Mode getTimeMode() |
| 34 | { |
| 35 | return manager.timeMode; |
| 36 | } |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 37 | bool hostOn() |
| 38 | { |
| 39 | return manager.hostOn; |
| 40 | } |
| 41 | std::string getRequestedMode() |
| 42 | { |
| 43 | return manager.requestedMode; |
| 44 | } |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 45 | void notifyPropertyChanged(const std::string& key, const std::string& value) |
| 46 | { |
| 47 | manager.onPropertyChanged(key, value); |
| 48 | } |
| 49 | void notifyOnHostState(bool hostOn) |
| 50 | { |
| 51 | manager.onHostState(hostOn); |
| 52 | } |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 53 | }; |
| 54 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 55 | TEST_F(TestManager, DISABLED_empty) |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 56 | { |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 57 | EXPECT_FALSE(hostOn()); |
| 58 | EXPECT_EQ("", getRequestedMode()); |
Lei YU | 57eab12 | 2017-06-16 10:27:30 +0800 | [diff] [blame] | 59 | |
George Liu | 3c2f449 | 2020-04-12 11:35:57 +0800 | [diff] [blame] | 60 | // Default mode is MANUAL |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 61 | EXPECT_EQ(Mode::Manual, getTimeMode()); |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 62 | } |
| 63 | |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 64 | TEST_F(TestManager, DISABLED_hostStateChange) |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 65 | { |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 66 | notifyOnHostState(true); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 67 | EXPECT_TRUE(hostOn()); |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 68 | notifyOnHostState(false); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 69 | EXPECT_FALSE(hostOn()); |
| 70 | } |
| 71 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 72 | TEST_F(TestManager, DISABLED_propertyChanged) |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 73 | { |
Gunnar Mills | 7f25c53 | 2017-10-25 20:45:28 -0500 | [diff] [blame] | 74 | // When host is off, property change will be notified to listeners |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 75 | EXPECT_FALSE(hostOn()); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 76 | |
| 77 | // Check mocked listeners shall receive notifications on property changed |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 78 | EXPECT_CALL(listener1, onModeChanged(Mode::Manual)).Times(1); |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 79 | EXPECT_CALL(listener2, onModeChanged(Mode::Manual)).Times(1); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 80 | |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 81 | notifyPropertyChanged( |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 82 | "TimeSyncMethod", |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 83 | "xyz.openbmc_project.Time.Synchronization.Method.Manual"); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 84 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 85 | EXPECT_EQ("", getRequestedMode()); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 86 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 87 | // When host is on, property changes are saved as requested ones |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 88 | notifyOnHostState(true); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 89 | |
| 90 | // Check mocked listeners shall not receive notifications |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 91 | EXPECT_CALL(listener1, onModeChanged(Mode::Manual)).Times(0); |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 92 | EXPECT_CALL(listener2, onModeChanged(Mode::Manual)).Times(0); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 93 | |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 94 | notifyPropertyChanged( |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 95 | "TimeSyncMethod", |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 96 | "xyz.openbmc_project.Time.Synchronization.Method.NTP"); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 97 | |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 98 | EXPECT_EQ("xyz.openbmc_project.Time.Synchronization.Method.NTP", |
| 99 | getRequestedMode()); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 100 | |
George Liu | 3c2f449 | 2020-04-12 11:35:57 +0800 | [diff] [blame] | 101 | // When host becomes off, the requested mode shall be notified |
Gunnar Mills | 7f25c53 | 2017-10-25 20:45:28 -0500 | [diff] [blame] | 102 | // to listeners, and be cleared |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 103 | EXPECT_CALL(listener1, onModeChanged(Mode::NTP)).Times(1); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 104 | EXPECT_CALL(listener2, onModeChanged(Mode::NTP)).Times(1); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 105 | |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 106 | notifyOnHostState(false); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 107 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 108 | EXPECT_EQ("", getRequestedMode()); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 109 | |
| 110 | // When host is on, and invalid property is changed, |
| 111 | // verify the code asserts because it shall never occur |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 112 | notifyOnHostState(true); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 113 | ASSERT_DEATH(notifyPropertyChanged("invalid property", "whatever"), ""); |
| 114 | } |
| 115 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 116 | TEST_F(TestManager, DISABLED_propertyChangedAndChangedbackWhenHostOn) |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 117 | { |
| 118 | // Property is now MANUAL/HOST |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 119 | notifyPropertyChanged( |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 120 | "TimeSyncMethod", |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 121 | "xyz.openbmc_project.Time.Synchronization.Method.Manual"); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 122 | |
| 123 | // Set host on |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 124 | notifyOnHostState(true); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 125 | |
| 126 | // Check mocked listeners shall not receive notifications |
| 127 | EXPECT_CALL(listener1, onModeChanged(_)).Times(0); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 128 | EXPECT_CALL(listener2, onModeChanged(_)).Times(0); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 129 | |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 130 | notifyPropertyChanged( |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 131 | "TimeSyncMethod", |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 132 | "xyz.openbmc_project.Time.Synchronization.Method.NTP"); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 133 | |
George Liu | 3c2f449 | 2020-04-12 11:35:57 +0800 | [diff] [blame] | 134 | // Saved as requested mode |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 135 | EXPECT_EQ("xyz.openbmc_project.Time.Synchronization.Method.NTP", |
| 136 | getRequestedMode()); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 137 | |
| 138 | // Property changed back to MANUAL/HOST |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 139 | notifyPropertyChanged( |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 140 | "TimeSyncMethod", |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 141 | "xyz.openbmc_project.Time.Synchronization.Method.Manual"); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 142 | |
George Liu | 3c2f449 | 2020-04-12 11:35:57 +0800 | [diff] [blame] | 143 | // Requested mode shall be updated |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 144 | EXPECT_EQ("xyz.openbmc_project.Time.Synchronization.Method.Manual", |
| 145 | getRequestedMode()); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 146 | |
George Liu | 3c2f449 | 2020-04-12 11:35:57 +0800 | [diff] [blame] | 147 | // Because the latest mode is the same as when host is off, |
| 148 | // The listeners shall not be notified, and requested mode |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 149 | // shall be cleared |
| 150 | EXPECT_CALL(listener1, onModeChanged(_)).Times(0); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 151 | EXPECT_CALL(listener2, onModeChanged(_)).Times(0); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 152 | |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 153 | notifyOnHostState(false); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 154 | |
| 155 | EXPECT_EQ("", getRequestedMode()); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 156 | } |
| 157 | |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 158 | // TODO: if gmock is ready, add case to test |
| 159 | // updateNtpSetting() and updateNetworkSetting() |
| 160 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 161 | } // namespace time |
| 162 | } // namespace phosphor |