1 #include "manager.hpp"
2 #include "mocked_property_change_listener.hpp"
3 #include "types.hpp"
4 
5 #include <sdbusplus/bus.hpp>
6 
7 #include <gtest/gtest.h>
8 
9 using ::testing::_;
10 
11 namespace phosphor
12 {
13 namespace time
14 {
15 
16 class TestManager : public testing::Test
17 {
18   public:
19     sdbusplus::bus::bus bus;
20     Manager manager;
21     MockPropertyChangeListner listener1;
22     MockPropertyChangeListner listener2;
23 
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     }
31 
32     // Proxies for Manager's private members and functions
33     Mode getTimeMode()
34     {
35         return manager.timeMode;
36     }
37     bool hostOn()
38     {
39         return manager.hostOn;
40     }
41     std::string getRequestedMode()
42     {
43         return manager.requestedMode;
44     }
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     }
53 };
54 
55 TEST_F(TestManager, DISABLED_empty)
56 {
57     EXPECT_FALSE(hostOn());
58     EXPECT_EQ("", getRequestedMode());
59 
60     // Default mode is MANUAL
61     EXPECT_EQ(Mode::Manual, getTimeMode());
62 }
63 
64 TEST_F(TestManager, DISABLED_hostStateChange)
65 {
66     notifyOnHostState(true);
67     EXPECT_TRUE(hostOn());
68     notifyOnHostState(false);
69     EXPECT_FALSE(hostOn());
70 }
71 
72 TEST_F(TestManager, DISABLED_propertyChanged)
73 {
74     // When host is off, property change will be notified to listeners
75     EXPECT_FALSE(hostOn());
76 
77     // Check mocked listeners shall receive notifications on property changed
78     EXPECT_CALL(listener1, onModeChanged(Mode::Manual)).Times(1);
79     EXPECT_CALL(listener2, onModeChanged(Mode::Manual)).Times(1);
80 
81     notifyPropertyChanged(
82         "TimeSyncMethod",
83         "xyz.openbmc_project.Time.Synchronization.Method.Manual");
84 
85     EXPECT_EQ("", getRequestedMode());
86 
87     // When host is on, property changes are saved as requested ones
88     notifyOnHostState(true);
89 
90     // Check mocked listeners shall not receive notifications
91     EXPECT_CALL(listener1, onModeChanged(Mode::Manual)).Times(0);
92     EXPECT_CALL(listener2, onModeChanged(Mode::Manual)).Times(0);
93 
94     notifyPropertyChanged(
95         "TimeSyncMethod",
96         "xyz.openbmc_project.Time.Synchronization.Method.NTP");
97 
98     EXPECT_EQ("xyz.openbmc_project.Time.Synchronization.Method.NTP",
99               getRequestedMode());
100 
101     // When host becomes off, the requested mode shall be notified
102     // to listeners, and be cleared
103     EXPECT_CALL(listener1, onModeChanged(Mode::NTP)).Times(1);
104     EXPECT_CALL(listener2, onModeChanged(Mode::NTP)).Times(1);
105 
106     notifyOnHostState(false);
107 
108     EXPECT_EQ("", getRequestedMode());
109 
110     // When host is on, and invalid property is changed,
111     // verify the code asserts because it shall never occur
112     notifyOnHostState(true);
113     ASSERT_DEATH(notifyPropertyChanged("invalid property", "whatever"), "");
114 }
115 
116 TEST_F(TestManager, DISABLED_propertyChangedAndChangedbackWhenHostOn)
117 {
118     // Property is now MANUAL/HOST
119     notifyPropertyChanged(
120         "TimeSyncMethod",
121         "xyz.openbmc_project.Time.Synchronization.Method.Manual");
122 
123     // Set host on
124     notifyOnHostState(true);
125 
126     // Check mocked listeners shall not receive notifications
127     EXPECT_CALL(listener1, onModeChanged(_)).Times(0);
128     EXPECT_CALL(listener2, onModeChanged(_)).Times(0);
129 
130     notifyPropertyChanged(
131         "TimeSyncMethod",
132         "xyz.openbmc_project.Time.Synchronization.Method.NTP");
133 
134     // Saved as requested mode
135     EXPECT_EQ("xyz.openbmc_project.Time.Synchronization.Method.NTP",
136               getRequestedMode());
137 
138     // Property changed back to MANUAL/HOST
139     notifyPropertyChanged(
140         "TimeSyncMethod",
141         "xyz.openbmc_project.Time.Synchronization.Method.Manual");
142 
143     // Requested mode shall be updated
144     EXPECT_EQ("xyz.openbmc_project.Time.Synchronization.Method.Manual",
145               getRequestedMode());
146 
147     // Because the latest mode is the same as when host is off,
148     // The listeners shall not be notified, and requested mode
149     // shall be cleared
150     EXPECT_CALL(listener1, onModeChanged(_)).Times(0);
151     EXPECT_CALL(listener2, onModeChanged(_)).Times(0);
152 
153     notifyOnHostState(false);
154 
155     EXPECT_EQ("", getRequestedMode());
156 }
157 
158 // TODO: if gmock is ready, add case to test
159 // updateNtpSetting() and updateNetworkSetting()
160 
161 } // namespace time
162 } // namespace phosphor
163