blob: aa7a6473036891d71ed05c07169f1e563d416521 [file] [log] [blame]
Lei YU415b9642017-02-09 11:37:26 +08001#include <sdbusplus/bus.hpp>
2#include <gtest/gtest.h>
3
4#include "types.hpp"
5#include "manager.hpp"
Lei YUa5003ce2017-02-24 15:35:25 +08006#include "mocked_property_change_listener.hpp"
7
8using ::testing::_;
Lei YU415b9642017-02-09 11:37:26 +08009
10namespace phosphor
11{
12namespace time
13{
14
15class TestManager : public testing::Test
16{
17 public:
18 sdbusplus::bus::bus bus;
19 Manager manager;
Lei YUa5003ce2017-02-24 15:35:25 +080020 MockPropertyChangeListner listener1;
21 MockPropertyChangeListner listener2;
Lei YU415b9642017-02-09 11:37:26 +080022
23 TestManager()
24 : bus(sdbusplus::bus::new_default()),
25 manager(bus)
26 {
Lei YUa5003ce2017-02-24 15:35:25 +080027 // Add two mocked listeners so that we can test
28 // the behavior related to listeners
29 manager.addListener(&listener1);
30 manager.addListener(&listener2);
Lei YU415b9642017-02-09 11:37:26 +080031 }
32
33 // Proxies for Manager's private members and functions
Lei YU710d49b2017-08-01 17:10:17 +080034 Mode getTimeMode()
Lei YU415b9642017-02-09 11:37:26 +080035 {
36 return manager.timeMode;
37 }
38 Owner getTimeOwner()
39 {
40 return manager.timeOwner;
41 }
Lei YU7f4fca52017-02-23 15:15:51 +080042 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 YUdebe1d82017-10-13 13:21:37 +080059 void notifyOnHostState(bool hostOn)
Lei YU7f4fca52017-02-23 15:15:51 +080060 {
Lei YUdebe1d82017-10-13 13:21:37 +080061 manager.onHostState(hostOn);
Lei YU7f4fca52017-02-23 15:15:51 +080062 }
Lei YU415b9642017-02-09 11:37:26 +080063};
64
Lei YUddd54422017-04-18 16:38:44 +080065TEST_F(TestManager, DISABLED_empty)
Lei YU415b9642017-02-09 11:37:26 +080066{
Lei YU7f4fca52017-02-23 15:15:51 +080067 EXPECT_FALSE(hostOn());
68 EXPECT_EQ("", getRequestedMode());
69 EXPECT_EQ("", getRequestedOwner());
Lei YU57eab122017-06-16 10:27:30 +080070
71 // Default mode/owner is MANUAL/BOTH
Lei YUad143542017-07-25 14:27:07 +080072 EXPECT_EQ(Mode::Manual, getTimeMode());
73 EXPECT_EQ(Owner::Both, getTimeOwner());
Lei YU415b9642017-02-09 11:37:26 +080074}
75
Lei YU415b9642017-02-09 11:37:26 +080076
Lei YUdebe1d82017-10-13 13:21:37 +080077TEST_F(TestManager, DISABLED_hostStateChange)
Lei YU7f4fca52017-02-23 15:15:51 +080078{
Lei YUdebe1d82017-10-13 13:21:37 +080079 notifyOnHostState(true);
Lei YU7f4fca52017-02-23 15:15:51 +080080 EXPECT_TRUE(hostOn());
Lei YUdebe1d82017-10-13 13:21:37 +080081 notifyOnHostState(false);
Lei YU7f4fca52017-02-23 15:15:51 +080082 EXPECT_FALSE(hostOn());
83}
84
Lei YUddd54422017-04-18 16:38:44 +080085TEST_F(TestManager, DISABLED_propertyChanged)
Lei YU7f4fca52017-02-23 15:15:51 +080086{
Gunnar Mills7f25c532017-10-25 20:45:28 -050087 // When host is off, property change will be notified to listeners
Lei YU7f4fca52017-02-23 15:15:51 +080088 EXPECT_FALSE(hostOn());
Lei YUa5003ce2017-02-24 15:35:25 +080089
90 // Check mocked listeners shall receive notifications on property changed
Lei YUad143542017-07-25 14:27:07 +080091 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 YUa5003ce2017-02-24 15:35:25 +080095
Lei YUad143542017-07-25 14:27:07 +080096 notifyPropertyChanged(
Lei YU710d49b2017-08-01 17:10:17 +080097 "TimeSyncMethod",
Lei YUad143542017-07-25 14:27:07 +080098 "xyz.openbmc_project.Time.Synchronization.Method.Manual");
99 notifyPropertyChanged(
Lei YU710d49b2017-08-01 17:10:17 +0800100 "TimeOwner",
Lei YUad143542017-07-25 14:27:07 +0800101 "xyz.openbmc_project.Time.Owner.Owners.Host");
Lei YUa5003ce2017-02-24 15:35:25 +0800102
Lei YU7f4fca52017-02-23 15:15:51 +0800103 EXPECT_EQ("", getRequestedMode());
104 EXPECT_EQ("", getRequestedOwner());
Lei YU7f4fca52017-02-23 15:15:51 +0800105
Lei YU7f4fca52017-02-23 15:15:51 +0800106 // When host is on, property changes are saved as requested ones
Lei YUdebe1d82017-10-13 13:21:37 +0800107 notifyOnHostState(true);
Lei YUa5003ce2017-02-24 15:35:25 +0800108
109 // Check mocked listeners shall not receive notifications
Lei YUad143542017-07-25 14:27:07 +0800110 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 YUa5003ce2017-02-24 15:35:25 +0800114
Lei YUad143542017-07-25 14:27:07 +0800115 notifyPropertyChanged(
Lei YU710d49b2017-08-01 17:10:17 +0800116 "TimeSyncMethod",
Lei YUad143542017-07-25 14:27:07 +0800117 "xyz.openbmc_project.Time.Synchronization.Method.NTP");
118 notifyPropertyChanged(
Lei YU710d49b2017-08-01 17:10:17 +0800119 "TimeOwner",
Lei YUad143542017-07-25 14:27:07 +0800120 "xyz.openbmc_project.Time.Owner.Owners.Split");
Lei YUa5003ce2017-02-24 15:35:25 +0800121
Lei YUad143542017-07-25 14:27:07 +0800122 EXPECT_EQ("xyz.openbmc_project.Time.Synchronization.Method.NTP",
123 getRequestedMode());
124 EXPECT_EQ("xyz.openbmc_project.Time.Owner.Owners.Split",
125 getRequestedOwner());
Lei YU7f4fca52017-02-23 15:15:51 +0800126
127
128 // When host becomes off, the requested mode/owner shall be notified
Gunnar Mills7f25c532017-10-25 20:45:28 -0500129 // to listeners, and be cleared
Lei YUa5003ce2017-02-24 15:35:25 +0800130 EXPECT_CALL(listener1, onModeChanged(Mode::NTP)).Times(1);
Lei YUad143542017-07-25 14:27:07 +0800131 EXPECT_CALL(listener1, onOwnerChanged(Owner::Split)).Times(1);
Lei YUa5003ce2017-02-24 15:35:25 +0800132 EXPECT_CALL(listener2, onModeChanged(Mode::NTP)).Times(1);
Lei YUad143542017-07-25 14:27:07 +0800133 EXPECT_CALL(listener2, onOwnerChanged(Owner::Split)).Times(1);
Lei YUa5003ce2017-02-24 15:35:25 +0800134
Lei YUdebe1d82017-10-13 13:21:37 +0800135 notifyOnHostState(false);
Lei YUa5003ce2017-02-24 15:35:25 +0800136
Lei YU7f4fca52017-02-23 15:15:51 +0800137 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 YUdebe1d82017-10-13 13:21:37 +0800142 notifyOnHostState(true);
Lei YU7f4fca52017-02-23 15:15:51 +0800143 ASSERT_DEATH(notifyPropertyChanged("invalid property", "whatever"), "");
144}
145
Lei YUddd54422017-04-18 16:38:44 +0800146TEST_F(TestManager, DISABLED_propertyChangedAndChangedbackWhenHostOn)
Lei YUa5003ce2017-02-24 15:35:25 +0800147{
148 // Property is now MANUAL/HOST
Lei YUad143542017-07-25 14:27:07 +0800149 notifyPropertyChanged(
Lei YU710d49b2017-08-01 17:10:17 +0800150 "TimeSyncMethod",
Lei YUad143542017-07-25 14:27:07 +0800151 "xyz.openbmc_project.Time.Synchronization.Method.Manual");
152 notifyPropertyChanged(
Lei YU710d49b2017-08-01 17:10:17 +0800153 "TimeOwner",
Lei YUad143542017-07-25 14:27:07 +0800154 "xyz.openbmc_project.Time.Owner.Owners.Host");
Lei YUa5003ce2017-02-24 15:35:25 +0800155
156 // Set host on
Lei YUdebe1d82017-10-13 13:21:37 +0800157 notifyOnHostState(true);
Lei YUa5003ce2017-02-24 15:35:25 +0800158
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 YUad143542017-07-25 14:27:07 +0800165 notifyPropertyChanged(
Lei YU710d49b2017-08-01 17:10:17 +0800166 "TimeSyncMethod",
Lei YUad143542017-07-25 14:27:07 +0800167 "xyz.openbmc_project.Time.Synchronization.Method.NTP");
168 notifyPropertyChanged(
Lei YU710d49b2017-08-01 17:10:17 +0800169 "TimeOwner",
Lei YUad143542017-07-25 14:27:07 +0800170 "xyz.openbmc_project.Time.Owner.Owners.Split");
Lei YUa5003ce2017-02-24 15:35:25 +0800171
172 // Saved as requested mode/owner
Lei YUad143542017-07-25 14:27:07 +0800173 EXPECT_EQ("xyz.openbmc_project.Time.Synchronization.Method.NTP",
174 getRequestedMode());
175 EXPECT_EQ("xyz.openbmc_project.Time.Owner.Owners.Split",
176 getRequestedOwner());
Lei YUa5003ce2017-02-24 15:35:25 +0800177
178 // Property changed back to MANUAL/HOST
Lei YUad143542017-07-25 14:27:07 +0800179 notifyPropertyChanged(
Lei YU710d49b2017-08-01 17:10:17 +0800180 "TimeSyncMethod",
Lei YUad143542017-07-25 14:27:07 +0800181 "xyz.openbmc_project.Time.Synchronization.Method.Manual");
182 notifyPropertyChanged(
Lei YU710d49b2017-08-01 17:10:17 +0800183 "TimeOwner",
Lei YUad143542017-07-25 14:27:07 +0800184 "xyz.openbmc_project.Time.Owner.Owners.Host");
Lei YUa5003ce2017-02-24 15:35:25 +0800185
186 // Requested mode/owner shall be updated
Lei YUad143542017-07-25 14:27:07 +0800187 EXPECT_EQ("xyz.openbmc_project.Time.Synchronization.Method.Manual",
188 getRequestedMode());
189 EXPECT_EQ("xyz.openbmc_project.Time.Owner.Owners.Host",
190 getRequestedOwner());
Lei YUa5003ce2017-02-24 15:35:25 +0800191
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 YUdebe1d82017-10-13 13:21:37 +0800200 notifyOnHostState(false);
Lei YUa5003ce2017-02-24 15:35:25 +0800201
202 EXPECT_EQ("", getRequestedMode());
203 EXPECT_EQ("", getRequestedOwner());
204}
205
Lei YUa7417132017-02-23 15:24:05 +0800206// TODO: if gmock is ready, add case to test
207// updateNtpSetting() and updateNetworkSetting()
208
Lei YU415b9642017-02-09 11:37:26 +0800209}
210}