Use gmock on property_change_listener

1. Mock property_change_listener;
2. Make unit test link against gmock
3. Update TestManager to use mocked property change listener.
4. Fix an issue found by the updated test case, that when the properties
are changed during host is on, and changed back to the same as before,
when host becomes off, the properties shall remain the same and listeners
shall not be notified.

Change-Id: I815b59cb23edfcac910c2a8c26ea5a71e872d92f
Signed-off-by: Lei YU <mine260309@gmail.com>
diff --git a/test/TestManager.cpp b/test/TestManager.cpp
index 1e4096e..bbaca96 100644
--- a/test/TestManager.cpp
+++ b/test/TestManager.cpp
@@ -3,6 +3,9 @@
 
 #include "types.hpp"
 #include "manager.hpp"
+#include "mocked_property_change_listener.hpp"
+
+using ::testing::_;
 
 namespace phosphor
 {
@@ -14,12 +17,17 @@
     public:
         sdbusplus::bus::bus bus;
         Manager manager;
+        MockPropertyChangeListner listener1;
+        MockPropertyChangeListner listener2;
 
         TestManager()
             : bus(sdbusplus::bus::new_default()),
               manager(bus)
         {
-            // Empty
+            // Add two mocked listeners so that we can test
+            // the behavior related to listeners
+            manager.addListener(&listener1);
+            manager.addListener(&listener2);
         }
 
         // Proxies for Manager's private members and functions
@@ -104,28 +112,48 @@
     EXPECT_FALSE(hostOn());
 }
 
-TEST_F(TestManager, propertyChange)
+TEST_F(TestManager, propertyChanged)
 {
     // When host is off, property change will be notified to listners
     EXPECT_FALSE(hostOn());
+
+    // Check mocked listeners shall receive notifications on property changed
+    EXPECT_CALL(listener1, onModeChanged(Mode::MANUAL)).Times(1);
+    EXPECT_CALL(listener1, onOwnerChanged(Owner::HOST)).Times(1);
+    EXPECT_CALL(listener2, onModeChanged(Mode::MANUAL)).Times(1);
+    EXPECT_CALL(listener2, onOwnerChanged(Owner::HOST)).Times(1);
+
     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());
+    notifyPgoodChanged(true);
+
+    // Check mocked listeners shall not receive notifications
+    EXPECT_CALL(listener1, onModeChanged(Mode::MANUAL)).Times(0);
+    EXPECT_CALL(listener1, onOwnerChanged(Owner::HOST)).Times(0);
+    EXPECT_CALL(listener2, onModeChanged(Mode::MANUAL)).Times(0);
+    EXPECT_CALL(listener2, onOwnerChanged(Owner::HOST)).Times(0);
+
+    notifyPropertyChanged("time_mode", "NTP");
+    notifyPropertyChanged("time_owner", "SPLIT");
+
+    EXPECT_EQ("NTP", getRequestedMode());
+    EXPECT_EQ("SPLIT", getRequestedOwner());
 
 
     // When host becomes off, the requested mode/owner shall be notified
     // to listners, and be cleared
+    EXPECT_CALL(listener1, onModeChanged(Mode::NTP)).Times(1);
+    EXPECT_CALL(listener1, onOwnerChanged(Owner::SPLIT)).Times(1);
+    EXPECT_CALL(listener2, onModeChanged(Mode::NTP)).Times(1);
+    EXPECT_CALL(listener2, onOwnerChanged(Owner::SPLIT)).Times(1);
+
     notifyPgoodChanged(false);
-    // TODO: if gmock is ready, check mocked listners shall receive notifies
+
     EXPECT_EQ("", getRequestedMode());
     EXPECT_EQ("", getRequestedOwner());
 
@@ -135,6 +163,50 @@
     ASSERT_DEATH(notifyPropertyChanged("invalid property", "whatever"), "");
 }
 
+TEST_F(TestManager, propertyChangedAndChangedbackWhenHostOn)
+{
+    // Property is now MANUAL/HOST
+    notifyPropertyChanged("time_mode", "MANUAL");
+    notifyPropertyChanged("time_owner", "HOST");
+
+    // Set host on
+    notifyPgoodChanged(true);
+
+    // Check mocked listeners shall not receive notifications
+    EXPECT_CALL(listener1, onModeChanged(_)).Times(0);
+    EXPECT_CALL(listener1, onOwnerChanged(_)).Times(0);
+    EXPECT_CALL(listener2, onModeChanged(_)).Times(0);
+    EXPECT_CALL(listener2, onOwnerChanged(_)).Times(0);
+
+    notifyPropertyChanged("time_mode", "NTP");
+    notifyPropertyChanged("time_owner", "SPLIT");
+
+    // Saved as requested mode/owner
+    EXPECT_EQ("NTP", getRequestedMode());
+    EXPECT_EQ("SPLIT", getRequestedOwner());
+
+    // Property changed back to MANUAL/HOST
+    notifyPropertyChanged("time_mode", "MANUAL");
+    notifyPropertyChanged("time_owner", "HOST");
+
+    // Requested mode/owner shall be updated
+    EXPECT_EQ("MANUAL", getRequestedMode());
+    EXPECT_EQ("HOST", getRequestedOwner());
+
+    // Because the latest mode/owner is the same as when host is off,
+    // The listeners shall not be notified, and requested mode/owner
+    // shall be cleared
+    EXPECT_CALL(listener1, onModeChanged(_)).Times(0);
+    EXPECT_CALL(listener1, onOwnerChanged(_)).Times(0);
+    EXPECT_CALL(listener2, onModeChanged(_)).Times(0);
+    EXPECT_CALL(listener2, onOwnerChanged(_)).Times(0);
+
+    notifyPgoodChanged(false);
+
+    EXPECT_EQ("", getRequestedMode());
+    EXPECT_EQ("", getRequestedOwner());
+}
+
 // TODO: if gmock is ready, add case to test
 // updateNtpSetting() and updateNetworkSetting()