Save properties to persistent storage when host is on

1. When host is on, set properties as requested properties instead
of notify listeners;
2. When host becomes off, and requested properties are not empty, notify
the listners and reset the requested properties.

Add unit tests.

Change-Id: I9359c801c698df0c6e5eab43e12427bb5a6da611
Signed-off-by: Lei YU <mine260309@gmail.com>
diff --git a/test/TestManager.cpp b/test/TestManager.cpp
index 4d9ae73..935590e 100644
--- a/test/TestManager.cpp
+++ b/test/TestManager.cpp
@@ -39,10 +39,34 @@
         {
             return Manager::convertToOwner(owner);
         }
+        bool hostOn()
+        {
+            return manager.hostOn;
+        }
+        std::string getRequestedMode()
+        {
+            return manager.requestedMode;
+        }
+        std::string getRequestedOwner()
+        {
+            return manager.requestedOwner;
+        }
+        void notifyPropertyChanged(const std::string& key,
+                                   const std::string& value)
+        {
+            manager.onPropertyChanged(key, value);
+        }
+        void notifyPgoodChanged(bool pgood)
+        {
+            manager.onPgoodChanged(pgood);
+        }
 };
 
 TEST_F(TestManager, empty)
 {
+    EXPECT_FALSE(hostOn());
+    EXPECT_EQ("", getRequestedMode());
+    EXPECT_EQ("", getRequestedOwner());
     EXPECT_EQ(Mode::NTP, getTimeMode());
     EXPECT_EQ(Owner::BMC, getTimeOwner());
 }
@@ -72,5 +96,44 @@
     EXPECT_EQ(Owner::BMC, convertToOwner("xyz"));
 }
 
+TEST_F(TestManager, pgoodChange)
+{
+    notifyPgoodChanged(true);
+    EXPECT_TRUE(hostOn());
+    notifyPgoodChanged(false);
+    EXPECT_FALSE(hostOn());
+}
+
+TEST_F(TestManager, propertyChange)
+{
+    // When host is off, property change will be notified to listners
+    EXPECT_FALSE(hostOn());
+    notifyPropertyChanged("time_mode", "MANUAL");
+    notifyPropertyChanged("time_owner", "HOST");
+    EXPECT_EQ("", getRequestedMode());
+    EXPECT_EQ("", getRequestedOwner());
+    // TODO: if gmock is ready, check mocked listners shall receive notifies
+
+    notifyPgoodChanged(true);
+    // When host is on, property changes are saved as requested ones
+    notifyPropertyChanged("time_mode", "MANUAL");
+    notifyPropertyChanged("time_owner", "HOST");
+    EXPECT_EQ("MANUAL", getRequestedMode());
+    EXPECT_EQ("HOST", getRequestedOwner());
+
+
+    // When host becomes off, the requested mode/owner shall be notified
+    // to listners, and be cleared
+    notifyPgoodChanged(false);
+    // TODO: if gmock is ready, check mocked listners shall receive notifies
+    EXPECT_EQ("", getRequestedMode());
+    EXPECT_EQ("", getRequestedOwner());
+
+    // When host is on, and invalid property is changed,
+    // verify the code asserts because it shall never occur
+    notifyPgoodChanged(true);
+    ASSERT_DEATH(notifyPropertyChanged("invalid property", "whatever"), "");
+}
+
 }
 }