blob: 935590e0d428cc612a2700828c828802c0b7b2e0 [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"
6
7namespace phosphor
8{
9namespace time
10{
11
12class TestManager : public testing::Test
13{
14 public:
15 sdbusplus::bus::bus bus;
16 Manager manager;
17
18 TestManager()
19 : bus(sdbusplus::bus::new_default()),
20 manager(bus)
21 {
22 // Empty
23 }
24
25 // Proxies for Manager's private members and functions
26 Mode getTimeMode()
27 {
28 return manager.timeMode;
29 }
30 Owner getTimeOwner()
31 {
32 return manager.timeOwner;
33 }
34 Mode convertToMode(const std::string& mode)
35 {
36 return Manager::convertToMode(mode);
37 }
38 Owner convertToOwner(const std::string& owner)
39 {
40 return Manager::convertToOwner(owner);
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
65TEST_F(TestManager, empty)
66{
Lei YU7f4fca52017-02-23 15:15:51 +080067 EXPECT_FALSE(hostOn());
68 EXPECT_EQ("", getRequestedMode());
69 EXPECT_EQ("", getRequestedOwner());
Lei YU415b9642017-02-09 11:37:26 +080070 EXPECT_EQ(Mode::NTP, getTimeMode());
71 EXPECT_EQ(Owner::BMC, getTimeOwner());
72}
73
74TEST_F(TestManager, convertToMode)
75{
76 EXPECT_EQ(Mode::NTP, convertToMode("NTP"));
77 EXPECT_EQ(Mode::MANUAL, convertToMode("MANUAL"));
78
79 // All unrecognized strings are mapped to Ntp
80 EXPECT_EQ(Mode::NTP, convertToMode(""));
81 EXPECT_EQ(Mode::NTP, convertToMode("Manual"));
82 EXPECT_EQ(Mode::NTP, convertToMode("whatever"));
83}
84
85
86TEST_F(TestManager, convertToOwner)
87{
88 EXPECT_EQ(Owner::BMC, convertToOwner("BMC"));
89 EXPECT_EQ(Owner::HOST, convertToOwner("HOST"));
90 EXPECT_EQ(Owner::SPLIT, convertToOwner("SPLIT"));
91 EXPECT_EQ(Owner::BOTH, convertToOwner("BOTH"));
92
93 // All unrecognized strings are mapped to Bmc
94 EXPECT_EQ(Owner::BMC, convertToOwner(""));
95 EXPECT_EQ(Owner::BMC, convertToOwner("Split"));
96 EXPECT_EQ(Owner::BMC, convertToOwner("xyz"));
97}
98
Lei YU7f4fca52017-02-23 15:15:51 +080099TEST_F(TestManager, pgoodChange)
100{
101 notifyPgoodChanged(true);
102 EXPECT_TRUE(hostOn());
103 notifyPgoodChanged(false);
104 EXPECT_FALSE(hostOn());
105}
106
107TEST_F(TestManager, propertyChange)
108{
109 // When host is off, property change will be notified to listners
110 EXPECT_FALSE(hostOn());
111 notifyPropertyChanged("time_mode", "MANUAL");
112 notifyPropertyChanged("time_owner", "HOST");
113 EXPECT_EQ("", getRequestedMode());
114 EXPECT_EQ("", getRequestedOwner());
115 // TODO: if gmock is ready, check mocked listners shall receive notifies
116
117 notifyPgoodChanged(true);
118 // When host is on, property changes are saved as requested ones
119 notifyPropertyChanged("time_mode", "MANUAL");
120 notifyPropertyChanged("time_owner", "HOST");
121 EXPECT_EQ("MANUAL", getRequestedMode());
122 EXPECT_EQ("HOST", getRequestedOwner());
123
124
125 // When host becomes off, the requested mode/owner shall be notified
126 // to listners, and be cleared
127 notifyPgoodChanged(false);
128 // TODO: if gmock is ready, check mocked listners shall receive notifies
129 EXPECT_EQ("", getRequestedMode());
130 EXPECT_EQ("", getRequestedOwner());
131
132 // When host is on, and invalid property is changed,
133 // verify the code asserts because it shall never occur
134 notifyPgoodChanged(true);
135 ASSERT_DEATH(notifyPropertyChanged("invalid property", "whatever"), "");
136}
137
Lei YU415b9642017-02-09 11:37:26 +0800138}
139}