blob: 5c9c8c3fbc6e4dbba091c73b18f451dd73dcd4c5 [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
34 Mode getTimeMode()
35 {
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 }
59 void notifyPgoodChanged(bool pgood)
60 {
61 manager.onPgoodChanged(pgood);
62 }
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
72 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 YUddd54422017-04-18 16:38:44 +080077TEST_F(TestManager, DISABLED_pgoodChange)
Lei YU7f4fca52017-02-23 15:15:51 +080078{
79 notifyPgoodChanged(true);
80 EXPECT_TRUE(hostOn());
81 notifyPgoodChanged(false);
82 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{
87 // When host is off, property change will be notified to listners
88 EXPECT_FALSE(hostOn());
Lei YUa5003ce2017-02-24 15:35:25 +080089
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 YU7f4fca52017-02-23 15:15:51 +080096 notifyPropertyChanged("time_mode", "MANUAL");
97 notifyPropertyChanged("time_owner", "HOST");
Lei YUa5003ce2017-02-24 15:35:25 +080098
Lei YU7f4fca52017-02-23 15:15:51 +080099 EXPECT_EQ("", getRequestedMode());
100 EXPECT_EQ("", getRequestedOwner());
Lei YU7f4fca52017-02-23 15:15:51 +0800101
Lei YU7f4fca52017-02-23 15:15:51 +0800102 // When host is on, property changes are saved as requested ones
Lei YUa5003ce2017-02-24 15:35:25 +0800103 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 YU7f4fca52017-02-23 15:15:51 +0800116
117
118 // When host becomes off, the requested mode/owner shall be notified
119 // to listners, and be cleared
Lei YUa5003ce2017-02-24 15:35:25 +0800120 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 YU7f4fca52017-02-23 15:15:51 +0800125 notifyPgoodChanged(false);
Lei YUa5003ce2017-02-24 15:35:25 +0800126
Lei YU7f4fca52017-02-23 15:15:51 +0800127 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 YUddd54422017-04-18 16:38:44 +0800136TEST_F(TestManager, DISABLED_propertyChangedAndChangedbackWhenHostOn)
Lei YUa5003ce2017-02-24 15:35:25 +0800137{
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 YUa7417132017-02-23 15:24:05 +0800180// TODO: if gmock is ready, add case to test
181// updateNtpSetting() and updateNetworkSetting()
182
Lei YU415b9642017-02-09 11:37:26 +0800183}
184}