haltest: battery: remove registering callback at UnregisterChangedEventP 09/255009/4
authorYoungjae Cho <y0.cho@samsung.com>
Thu, 11 Mar 2021 07:52:46 +0000 (16:52 +0900)
committerYoungjae Cho <y0.cho@samsung.com>
Mon, 15 Mar 2021 04:47:18 +0000 (13:47 +0900)
Battery api preserves event callback internally, and doesn't allow
registering an additional callback. Therefore registering callback
in UnregisterChangedEventP generates error as the callback had been
registered at the test right before, the RegisterChangedEventP.
To prevent this, UnregisterChangedEventP reuses callback registered
from RegisterChangedEventP. For the case failing RegisterChangedEventP,
skip the UnregisterChangedEventP.

Change-Id: I92836f2bb68658fd3fd05bfe5755491d2da6d15e
Signed-off-by: Youngjae Cho <y0.cho@samsung.com>
haltest/battery.cpp

index 973f926..fcb238c 100644 (file)
@@ -17,8 +17,14 @@ class BATTERY : public testing::Test {
                bool battery_supported;
 };
 
+// maintained within a test suite
+static bool changed_callback_registered = false;
+
 // dummy
-static void __changed_cb(struct battery_info *info, void *data) {}
+static void __changed_cb(struct battery_info *info, void *data)
+{
+       EXPECT_NE(info, nullptr) << "Failed to receive battery information";
+}
 
 TEST_F(BATTERY, GetBackendP)
 {
@@ -43,7 +49,9 @@ TEST_F(BATTERY, RegisterChangedEventP)
        }
 
        ret = hal_device_battery_register_changed_event(__changed_cb, NULL);
-       EXPECT_EQ(ret, 0) << "Failed to register changed callback";
+       ASSERT_EQ(ret, 0) << "Failed to register changed callback";
+
+       changed_callback_registered = true;
 }
 
 TEST_F(BATTERY, UnregisterChangedEventP)
@@ -55,13 +63,15 @@ TEST_F(BATTERY, UnregisterChangedEventP)
                return;
        }
 
-       ret = hal_device_battery_register_changed_event(__changed_cb, NULL);
-       ASSERT_EQ(ret, 0) << "Failed to register changed callback";
-
-       if (ret == 0) {
-               ret = hal_device_battery_unregister_changed_event(__changed_cb);
-               EXPECT_EQ(ret, 0) << "Failed to unregister changed callback";
+       if (!changed_callback_registered) {
+               SKIP_MESSAGE("Unable to test unregister as there is no registered callback");
+               return;
        }
+
+       ret = hal_device_battery_unregister_changed_event(__changed_cb);
+       ASSERT_EQ(ret, 0) << "Failed to unregister changed callback";
+
+       changed_callback_registered = false;
 }
 
 TEST_F(BATTERY, GetCurrentStateP)