blob: 95f573499d50d932d9b616fdf53b1bc3386d8cbb [file] [log] [blame]
Lei YU415b9642017-02-09 11:37:26 +08001#include "manager.hpp"
Lei YUa5003ce2017-02-24 15:35:25 +08002#include "mocked_property_change_listener.hpp"
Gunnar Millsab4cc6a2018-09-14 14:42:39 -05003#include "types.hpp"
4
5#include <sdbusplus/bus.hpp>
6
7#include <gtest/gtest.h>
Lei YUa5003ce2017-02-24 15:35:25 +08008
9using ::testing::_;
Lei YU415b9642017-02-09 11:37:26 +080010
11namespace phosphor
12{
13namespace time
14{
15
16class TestManager : public testing::Test
17{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050018 public:
19 sdbusplus::bus::bus bus;
20 Manager manager;
21 MockPropertyChangeListner listener1;
22 MockPropertyChangeListner listener2;
Lei YU415b9642017-02-09 11:37:26 +080023
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050024 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 YU415b9642017-02-09 11:37:26 +080031
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050032 // Proxies for Manager's private members and functions
33 Mode getTimeMode()
34 {
35 return manager.timeMode;
36 }
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050037 bool hostOn()
38 {
39 return manager.hostOn;
40 }
41 std::string getRequestedMode()
42 {
43 return manager.requestedMode;
44 }
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050045 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 YU415b9642017-02-09 11:37:26 +080053};
54
Lei YUddd54422017-04-18 16:38:44 +080055TEST_F(TestManager, DISABLED_empty)
Lei YU415b9642017-02-09 11:37:26 +080056{
Lei YU7f4fca52017-02-23 15:15:51 +080057 EXPECT_FALSE(hostOn());
58 EXPECT_EQ("", getRequestedMode());
Lei YU57eab122017-06-16 10:27:30 +080059
George Liu3c2f4492020-04-12 11:35:57 +080060 // Default mode is MANUAL
Lei YUad143542017-07-25 14:27:07 +080061 EXPECT_EQ(Mode::Manual, getTimeMode());
Lei YU415b9642017-02-09 11:37:26 +080062}
63
Lei YUdebe1d82017-10-13 13:21:37 +080064TEST_F(TestManager, DISABLED_hostStateChange)
Lei YU7f4fca52017-02-23 15:15:51 +080065{
Lei YUdebe1d82017-10-13 13:21:37 +080066 notifyOnHostState(true);
Lei YU7f4fca52017-02-23 15:15:51 +080067 EXPECT_TRUE(hostOn());
Lei YUdebe1d82017-10-13 13:21:37 +080068 notifyOnHostState(false);
Lei YU7f4fca52017-02-23 15:15:51 +080069 EXPECT_FALSE(hostOn());
70}
71
Lei YUddd54422017-04-18 16:38:44 +080072TEST_F(TestManager, DISABLED_propertyChanged)
Lei YU7f4fca52017-02-23 15:15:51 +080073{
Gunnar Mills7f25c532017-10-25 20:45:28 -050074 // When host is off, property change will be notified to listeners
Lei YU7f4fca52017-02-23 15:15:51 +080075 EXPECT_FALSE(hostOn());
Lei YUa5003ce2017-02-24 15:35:25 +080076
77 // Check mocked listeners shall receive notifications on property changed
Lei YUad143542017-07-25 14:27:07 +080078 EXPECT_CALL(listener1, onModeChanged(Mode::Manual)).Times(1);
Lei YUad143542017-07-25 14:27:07 +080079 EXPECT_CALL(listener2, onModeChanged(Mode::Manual)).Times(1);
Lei YUa5003ce2017-02-24 15:35:25 +080080
Lei YUad143542017-07-25 14:27:07 +080081 notifyPropertyChanged(
Lei YU710d49b2017-08-01 17:10:17 +080082 "TimeSyncMethod",
Lei YUad143542017-07-25 14:27:07 +080083 "xyz.openbmc_project.Time.Synchronization.Method.Manual");
Lei YUa5003ce2017-02-24 15:35:25 +080084
Lei YU7f4fca52017-02-23 15:15:51 +080085 EXPECT_EQ("", getRequestedMode());
Lei YU7f4fca52017-02-23 15:15:51 +080086
Lei YU7f4fca52017-02-23 15:15:51 +080087 // When host is on, property changes are saved as requested ones
Lei YUdebe1d82017-10-13 13:21:37 +080088 notifyOnHostState(true);
Lei YUa5003ce2017-02-24 15:35:25 +080089
90 // Check mocked listeners shall not receive notifications
Lei YUad143542017-07-25 14:27:07 +080091 EXPECT_CALL(listener1, onModeChanged(Mode::Manual)).Times(0);
Lei YUad143542017-07-25 14:27:07 +080092 EXPECT_CALL(listener2, onModeChanged(Mode::Manual)).Times(0);
Lei YUa5003ce2017-02-24 15:35:25 +080093
Lei YUad143542017-07-25 14:27:07 +080094 notifyPropertyChanged(
Lei YU710d49b2017-08-01 17:10:17 +080095 "TimeSyncMethod",
Lei YUad143542017-07-25 14:27:07 +080096 "xyz.openbmc_project.Time.Synchronization.Method.NTP");
Lei YUa5003ce2017-02-24 15:35:25 +080097
Lei YUad143542017-07-25 14:27:07 +080098 EXPECT_EQ("xyz.openbmc_project.Time.Synchronization.Method.NTP",
99 getRequestedMode());
Lei YU7f4fca52017-02-23 15:15:51 +0800100
George Liu3c2f4492020-04-12 11:35:57 +0800101 // When host becomes off, the requested mode shall be notified
Gunnar Mills7f25c532017-10-25 20:45:28 -0500102 // to listeners, and be cleared
Lei YUa5003ce2017-02-24 15:35:25 +0800103 EXPECT_CALL(listener1, onModeChanged(Mode::NTP)).Times(1);
Lei YUa5003ce2017-02-24 15:35:25 +0800104 EXPECT_CALL(listener2, onModeChanged(Mode::NTP)).Times(1);
Lei YUa5003ce2017-02-24 15:35:25 +0800105
Lei YUdebe1d82017-10-13 13:21:37 +0800106 notifyOnHostState(false);
Lei YUa5003ce2017-02-24 15:35:25 +0800107
Lei YU7f4fca52017-02-23 15:15:51 +0800108 EXPECT_EQ("", getRequestedMode());
Lei YU7f4fca52017-02-23 15:15:51 +0800109
110 // When host is on, and invalid property is changed,
111 // verify the code asserts because it shall never occur
Lei YUdebe1d82017-10-13 13:21:37 +0800112 notifyOnHostState(true);
Lei YU7f4fca52017-02-23 15:15:51 +0800113 ASSERT_DEATH(notifyPropertyChanged("invalid property", "whatever"), "");
114}
115
Lei YUddd54422017-04-18 16:38:44 +0800116TEST_F(TestManager, DISABLED_propertyChangedAndChangedbackWhenHostOn)
Lei YUa5003ce2017-02-24 15:35:25 +0800117{
118 // Property is now MANUAL/HOST
Lei YUad143542017-07-25 14:27:07 +0800119 notifyPropertyChanged(
Lei YU710d49b2017-08-01 17:10:17 +0800120 "TimeSyncMethod",
Lei YUad143542017-07-25 14:27:07 +0800121 "xyz.openbmc_project.Time.Synchronization.Method.Manual");
Lei YUa5003ce2017-02-24 15:35:25 +0800122
123 // Set host on
Lei YUdebe1d82017-10-13 13:21:37 +0800124 notifyOnHostState(true);
Lei YUa5003ce2017-02-24 15:35:25 +0800125
126 // Check mocked listeners shall not receive notifications
127 EXPECT_CALL(listener1, onModeChanged(_)).Times(0);
Lei YUa5003ce2017-02-24 15:35:25 +0800128 EXPECT_CALL(listener2, onModeChanged(_)).Times(0);
Lei YUa5003ce2017-02-24 15:35:25 +0800129
Lei YUad143542017-07-25 14:27:07 +0800130 notifyPropertyChanged(
Lei YU710d49b2017-08-01 17:10:17 +0800131 "TimeSyncMethod",
Lei YUad143542017-07-25 14:27:07 +0800132 "xyz.openbmc_project.Time.Synchronization.Method.NTP");
Lei YUa5003ce2017-02-24 15:35:25 +0800133
George Liu3c2f4492020-04-12 11:35:57 +0800134 // Saved as requested mode
Lei YUad143542017-07-25 14:27:07 +0800135 EXPECT_EQ("xyz.openbmc_project.Time.Synchronization.Method.NTP",
136 getRequestedMode());
Lei YUa5003ce2017-02-24 15:35:25 +0800137
138 // Property changed back to MANUAL/HOST
Lei YUad143542017-07-25 14:27:07 +0800139 notifyPropertyChanged(
Lei YU710d49b2017-08-01 17:10:17 +0800140 "TimeSyncMethod",
Lei YUad143542017-07-25 14:27:07 +0800141 "xyz.openbmc_project.Time.Synchronization.Method.Manual");
Lei YUa5003ce2017-02-24 15:35:25 +0800142
George Liu3c2f4492020-04-12 11:35:57 +0800143 // Requested mode shall be updated
Lei YUad143542017-07-25 14:27:07 +0800144 EXPECT_EQ("xyz.openbmc_project.Time.Synchronization.Method.Manual",
145 getRequestedMode());
Lei YUa5003ce2017-02-24 15:35:25 +0800146
George Liu3c2f4492020-04-12 11:35:57 +0800147 // Because the latest mode is the same as when host is off,
148 // The listeners shall not be notified, and requested mode
Lei YUa5003ce2017-02-24 15:35:25 +0800149 // shall be cleared
150 EXPECT_CALL(listener1, onModeChanged(_)).Times(0);
Lei YUa5003ce2017-02-24 15:35:25 +0800151 EXPECT_CALL(listener2, onModeChanged(_)).Times(0);
Lei YUa5003ce2017-02-24 15:35:25 +0800152
Lei YUdebe1d82017-10-13 13:21:37 +0800153 notifyOnHostState(false);
Lei YUa5003ce2017-02-24 15:35:25 +0800154
155 EXPECT_EQ("", getRequestedMode());
Lei YUa5003ce2017-02-24 15:35:25 +0800156}
157
Lei YUa7417132017-02-23 15:24:05 +0800158// TODO: if gmock is ready, add case to test
159// updateNtpSetting() and updateNetworkSetting()
160
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500161} // namespace time
162} // namespace phosphor