Improve gmock tests 21/281521/3
authorInkyun Kil <inkyun.kil@samsung.com>
Tue, 20 Sep 2022 01:20:10 +0000 (10:20 +0900)
committerInkyun Kil <inkyun.kil@samsung.com>
Tue, 20 Sep 2022 05:25:17 +0000 (14:25 +0900)
- Add unittest
- Improve coverage
  - lines......: 56.6% (787 of 1390 lines)
  - functions..: 68.4% (67 of 98 functions)

Change-Id: I37b70b67b7e25b62471480e06c7f49bea7002ce8
Signed-off-by: Inkyun Kil <inkyun.kil@samsung.com>
tests/mock/gio_mock.cc
tests/mock/gio_mock.h
tests/unit_tests/src/test_eventsystem.cc

index b0f7aa9d1d5c32dec38c3d36f18aa0f5909abcbe..a0d209b8ee3a205c86b354d5b053fec0078a7ca8 100644 (file)
 #include "mock_hook.h"
 #include "test_fixture.h"
 
+extern "C" gboolean g_dbus_is_interface_name(const gchar* arg0) {
+  return MOCK_HOOK_P1(GioMock, g_dbus_is_interface_name, arg0);
+}
+
 extern "C" GDBusConnection* g_bus_get_sync(GBusType type,
     GCancellable* cancellable, GError** error) {
   return MOCK_HOOK_P3(GioMock, g_bus_get_sync, type, cancellable, error);
 }
 
-extern "C" GDBusMessage* g_dbus_connection_send_message_with_reply_sync(
-    GDBusConnection* conn, GDBusMessage* msg, GDBusSendMessageFlags flags,
-    gint timeout, volatile guint32* out_serial, GCancellable* cancellable,
-    GError** error) {
-  return MOCK_HOOK_P7(GioMock, g_dbus_connection_send_message_with_reply_sync,
-      conn, msg, flags, timeout, out_serial, cancellable, error);
-}
-
-extern "C" GDBusMessage* g_dbus_message_new_method_call(const gchar* arg0,
-    const gchar* arg1, const gchar* arg2, const gchar* arg3) {
-  return MOCK_HOOK_P4(GioMock, g_dbus_message_new_method_call, arg0, arg1, arg2,
-      arg3);
-}
-
-extern "C" void g_dbus_message_set_body(GDBusMessage* arg0, GVariant* arg1) {
-  return MOCK_HOOK_P2(GioMock, g_dbus_message_set_body, arg0, arg1);
-}
-
-extern "C" GVariant* g_dbus_message_get_body(GDBusMessage* arg0) {
-  return MOCK_HOOK_P1(GioMock, g_dbus_message_get_body, arg0);
-}
-
 extern "C" guint g_dbus_connection_signal_subscribe(GDBusConnection* arg0,
     const gchar* arg1, const gchar* arg2, const gchar* arg3, const gchar* arg4,
     const gchar* arg5, GDBusSignalFlags arg6, GDBusSignalCallback arg7,
@@ -55,36 +37,36 @@ extern "C" guint g_dbus_connection_signal_subscribe(GDBusConnection* arg0,
       arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
 }
 
-extern "C" void g_dbus_connection_send_message_with_reply(GDBusConnection* arg0,
-    GDBusMessage* arg1, GDBusSendMessageFlags arg2, gint arg3,
-    volatile guint32* arg4, GCancellable* arg5, GAsyncReadyCallback arg6,
-    gpointer arg7) {
-  return MOCK_HOOK_P8(GioMock, g_dbus_connection_send_message_with_reply,
+extern "C" guint g_bus_own_name_on_connection(GDBusConnection* arg0,
+    const gchar* arg1, GBusNameOwnerFlags arg2, GBusNameAcquiredCallback arg3,
+    GBusNameLostCallback arg4, gpointer arg5, GDestroyNotify arg6) {
+  return MOCK_HOOK_P7(GioMock, g_bus_own_name_on_connection,
+      arg0, arg1, arg2, arg3, arg4, arg5, arg6);
+}
+
+extern "C" GDBusProxy* g_dbus_proxy_new_sync(GDBusConnection* arg0,
+    GDBusProxyFlags arg1, GDBusInterfaceInfo* arg2, const gchar* arg3,
+    const gchar* arg4, const gchar* arg5, GCancellable* arg6, GError** arg7) {
+  return MOCK_HOOK_P8(GioMock, g_dbus_proxy_new_sync,
       arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
 }
 
-extern "C" guint g_bus_watch_name_on_connection(
-    GDBusConnection* arg0, const gchar* arg1, GBusNameWatcherFlags arg2,
-    GBusNameAppearedCallback arg3, GBusNameVanishedCallback arg4,
-    gpointer arg5, GDestroyNotify arg6) {
-  return MOCK_HOOK_P7(GioMock, g_bus_watch_name_on_connection,
-      arg0, arg1, arg2, arg3, arg4, arg5, arg6);
+extern "C" GVariant* g_dbus_proxy_call_sync(GDBusProxy* arg1,
+    const gchar* arg2, GVariant* arg3, GDBusCallFlags arg4, gint arg5,
+    GCancellable* arg6, GError** arg7) {
+  return MOCK_HOOK_P7(GioMock, g_dbus_proxy_call_sync, arg1, arg2,
+      arg3, arg4, arg5, arg6, arg7);
 }
 
-extern "C" void g_bus_unwatch_name(guint arg0) {
-  return MOCK_HOOK_P1(GioMock, g_bus_unwatch_name, arg0);
+extern "C" gboolean g_dbus_connection_emit_signal(GDBusConnection* arg1,
+    const gchar* arg2, const gchar* arg3, const gchar* arg4, const gchar* arg5,
+    GVariant* arg6, GError** arg7) {
+  return MOCK_HOOK_P7(GioMock, g_dbus_connection_emit_signal, arg1, arg2,
+      arg3, arg4, arg5, arg6, arg7);
 }
 
 extern "C" void g_dbus_connection_signal_unsubscribe(
     GDBusConnection* arg0, guint arg1) {
   return MOCK_HOOK_P2(GioMock, g_dbus_connection_signal_unsubscribe,
       arg0, arg1);
-}
-
-extern "C" const gchar* g_dbus_connection_get_unique_name(GDBusConnection* connection) {
-  return MOCK_HOOK_P1(GioMock, g_dbus_connection_get_unique_name, connection);
-}
-
-extern "C" gboolean g_source_remove(guint tag) {
-  return MOCK_HOOK_P1(GioMock, g_source_remove, tag);
-}
+}
\ No newline at end of file
index 97d29a8ec7eec511d1bc983b9617d2b4b8177820..6161609572b795f750cec5d96fb493639578928f 100644 (file)
 
 class GioMock : public virtual ModuleMock {
  public:
+  GioMock() {
+    using ::testing::_;
+    using ::testing::Return;
+    using ::testing::Invoke;
+
+    // static int dummy;
+    GDBusConnection* conn = (GDBusConnection*)g_object_new(G_TYPE_OBJECT, nullptr);
+
+    ON_CALL(*this, g_bus_get_sync(_, _, _))
+        .WillByDefault(Return(conn));
+  }
   virtual ~GioMock() {}
 
+  MOCK_METHOD1(g_dbus_is_interface_name, gboolean(const gchar *));
   MOCK_METHOD3(g_bus_get_sync,
       GDBusConnection*(GBusType, GCancellable*, GError**));
-  MOCK_METHOD4(g_dbus_message_new_method_call,
-      GDBusMessage*(const gchar*, const gchar*, const gchar*, const gchar*));
-  MOCK_METHOD2(g_dbus_message_set_body, void(GDBusMessage*, GVariant*));
-  MOCK_METHOD1(g_dbus_message_get_body, GVariant*(GDBusMessage*));
-  MOCK_METHOD7(g_dbus_connection_send_message_with_reply_sync,
-      GDBusMessage*(GDBusConnection*, GDBusMessage*, GDBusSendMessageFlags,
-          gint, volatile guint32*, GCancellable*, GError**));
+  MOCK_METHOD7(g_bus_own_name_on_connection,
+      guint(GDBusConnection*, const gchar*, GBusNameOwnerFlags,
+          GBusNameAcquiredCallback, GBusNameLostCallback,
+          gpointer, GDestroyNotify));
   MOCK_METHOD10(g_dbus_connection_signal_subscribe,
       guint(GDBusConnection*, const gchar*, const gchar*, const gchar*,
           const gchar*, const gchar*, GDBusSignalFlags, GDBusSignalCallback,
           gpointer, GDestroyNotify));
-  MOCK_METHOD8(g_dbus_connection_send_message_with_reply,
-      void(GDBusConnection*, GDBusMessage*, GDBusSendMessageFlags, gint,
-          volatile guint32*, GCancellable*, GAsyncReadyCallback, gpointer));
-  MOCK_METHOD7(g_bus_watch_name_on_connection,
-      guint(GDBusConnection*, const gchar*, GBusNameWatcherFlags,
-          GBusNameAppearedCallback, GBusNameVanishedCallback, gpointer,
-          GDestroyNotify));
-  MOCK_METHOD1(g_bus_unwatch_name, void(guint));
+  MOCK_METHOD8(g_dbus_proxy_new_sync,
+      GDBusProxy *(GDBusConnection*, GDBusProxyFlags, GDBusInterfaceInfo*,
+          const gchar*, const gchar*, const gchar*, GCancellable *, GError **));
+  MOCK_METHOD7(g_dbus_proxy_call_sync, GVariant* (GDBusProxy*, const gchar*,
+      GVariant*, GDBusCallFlags, gint, GCancellable*, GError**));
+  MOCK_METHOD7(g_dbus_connection_emit_signal, gboolean (GDBusConnection*,
+      const gchar*, const gchar*, const gchar*, const gchar*, GVariant*,
+      GError**));
   MOCK_METHOD2(g_dbus_connection_signal_unsubscribe,
           void(GDBusConnection*, guint));
-  MOCK_METHOD1(g_dbus_connection_get_unique_name, const gchar*(GDBusConnection*));
-  MOCK_METHOD1(g_source_remove, gboolean(guint));
 };
 
 #endif  // MOCK_GIO_MOCK_H_
index 976ff5459f32c2d9ff6276a427c699b3e82b57cf..2f67f11fa140494e49b1d4c0e7e81e6e59975df7 100644 (file)
@@ -28,6 +28,7 @@
 // using namespace tizen_base;
 using ::testing::_;
 using ::testing::DoAll;
+using ::testing::Invoke;
 using ::testing::Return;
 using ::testing::SetArgPointee;
 
@@ -45,7 +46,244 @@ class EventSystemTest : public TestFixture {
   }
 };
 
+static void callback(const char *event_name, bundle *data, void *user_data) {
+}
+
+static void system_callback(const char *event_name, bundle_raw *event_data,
+    int len, void *user_data) {
+}
+
+TEST_F(EventSystemTest, eventsystem_register_system_event) {
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_is_interface_name(_)).
+      WillOnce(Return(true));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_connection_signal_subscribe(_, _, _, _, _, _, _, _, _, _)).
+      WillOnce(Return(1));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_bus_own_name_on_connection(_, _, _, _, _, _, _)).
+      WillOnce(Return(1));
+
+  unsigned int id;
+  int ret = eventsystem_register_event("tizen.system.event.test", &id, callback,
+      nullptr);
+  EXPECT_EQ(ret, ES_R_OK);
+}
+
+TEST_F(EventSystemTest, eventsystem_register_user_event) {
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_is_interface_name(_)).
+      WillRepeatedly(Return(true));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_connection_signal_subscribe(_, _, _, _, _, _, _, _, _, _)).
+      WillOnce(Return(1));
+
+  unsigned int id;
+  int ret = eventsystem_register_event("event.test", &id, callback, nullptr);
+  EXPECT_EQ(ret, ES_R_OK);
+}
+
+TEST_F(EventSystemTest, eventsystem_unregister_event) {
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_is_interface_name(_)).
+      WillOnce(Return(true));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_connection_signal_subscribe(_, _, _, _, _, _, _, _, _, _)).
+      WillRepeatedly(Return(1));
+
+  unsigned int id;
+  int ret = eventsystem_register_event("tizen.system.event.test", &id, callback,
+      nullptr);
+  EXPECT_EQ(ret, ES_R_OK);
+
+  ret = eventsystem_unregister_event(id);
+  EXPECT_EQ(ret, ES_R_OK);
+}
+
+TEST_F(EventSystemTest, eventsystem_send_user_event) {
+  GDBusProxy* _proxy = (GDBusProxy*)g_object_new(G_TYPE_OBJECT, nullptr);
+  GDBusConnection* _conn = (GDBusConnection*)g_object_new(G_TYPE_OBJECT, nullptr);
+
+  EXPECT_CALL(GetMock<GioMock>(), g_bus_get_sync(_, _, _))
+        .WillRepeatedly(Return(_conn));
+
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_bus_own_name_on_connection(_, _, _, _, _, _, _)).
+      WillRepeatedly(Return(1));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_proxy_new_sync(_, _, _, _, _, _, _, _)).
+      WillRepeatedly(Return(_proxy));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_proxy_call_sync(_, _, _, _, _, _, _)).
+      WillRepeatedly(
+        Invoke([&](GDBusProxy* p, const gchar* method, GVariant* v,
+            GDBusCallFlags flag, gint msec, GCancellable* c, GError** e)
+          -> GVariant* {
+        *e = nullptr;
+        return g_variant_new("(i)", 1);
+      }));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_connection_emit_signal(_, _, _, _, _, _, _)).
+      WillRepeatedly(Return(TRUE));
+
+  bundle *data = bundle_create();
+  int ret = eventsystem_send_user_event("event.test", data, false);
+  EXPECT_EQ(ret, ES_R_OK);
+}
+
+TEST_F(EventSystemTest, eventsystem_send_system_event) {
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_is_interface_name(_)).
+      WillOnce(Return(true));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_bus_own_name_on_connection(_, _, _, _, _, _, _)).
+      WillRepeatedly(Return(1));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_connection_emit_signal(_, _, _, _, _, _, _)).
+      WillRepeatedly(Return(TRUE));
+
+  bundle *data = bundle_create();
+  int ret = eventsystem_send_system_event("tizen.system.event.test", data);
+  EXPECT_EQ(ret, ES_R_OK);
+}
+
+TEST_F(EventSystemTest, eventsystem_request_sending_system_event) {
+  GDBusProxy* _proxy = (GDBusProxy*)g_object_new(G_TYPE_OBJECT, nullptr);
+
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_bus_own_name_on_connection(_, _, _, _, _, _, _)).
+      WillRepeatedly(Return(1));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_proxy_new_sync(_, _, _, _, _, _, _, _)).
+      WillRepeatedly(Return(_proxy));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_proxy_call_sync(_, _, _, _, _, _, _)).
+      WillRepeatedly(
+        Invoke([&](GDBusProxy* p, const gchar* method, GVariant* v,
+            GDBusCallFlags flag, gint msec, GCancellable* c, GError** e)
+          -> GVariant* {
+        *e = nullptr;
+        return g_variant_new("(i)", 1);
+      }));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_connection_emit_signal(_, _, _, _, _, _, _)).
+      WillRepeatedly(Return(TRUE));
+
+  bundle *data = bundle_create();
+  int ret = eventsystem_request_sending_system_event("event.test", data);
+  EXPECT_EQ(ret, ES_R_OK);
+}
+
+TEST_F(EventSystemTest, eventsystem_register_application_event) {
+  GDBusProxy* _proxy = (GDBusProxy*)g_object_new(G_TYPE_OBJECT, nullptr);
+
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_is_interface_name(_)).
+      WillOnce(Return(true));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_connection_signal_subscribe(_, _, _, _, _, _, _, _, _, _)).
+      WillOnce(Return(1));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_bus_own_name_on_connection(_, _, _, _, _, _, _)).
+      WillRepeatedly(Return(1));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_proxy_new_sync(_, _, _, _, _, _, _, _)).
+      WillRepeatedly(Return(_proxy));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_proxy_call_sync(_, _, _, _, _, _, _)).
+      WillOnce(
+        Invoke([&](GDBusProxy* p, const gchar* method, GVariant* v,
+            GDBusCallFlags flag, gint msec, GCancellable* c, GError** e)
+          -> GVariant* {
+        *e = nullptr;
+        return g_variant_new("(i)", 1);
+      })).
+      WillOnce(
+        Invoke([&](GDBusProxy* p, const gchar* method, GVariant* v,
+            GDBusCallFlags flag, gint msec, GCancellable* c, GError** e)
+          -> GVariant* {
+        *e = nullptr;
+        return g_variant_new("(iis)", 1, 1, "a");
+      }));
+  unsigned int id;
+  int type;
+  int ret = eventsystem_register_application_event("tizen.system.event.test",
+      &id, &type, system_callback, NULL);
+  EXPECT_EQ(ret, ES_R_OK);
+}
+
+TEST_F(EventSystemTest, eventsystem_unregister_application_event) {
+ GDBusProxy* _proxy = (GDBusProxy*)g_object_new(G_TYPE_OBJECT, nullptr);
+
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_is_interface_name(_)).
+      WillOnce(Return(true));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_connection_signal_subscribe(_, _, _, _, _, _, _, _, _, _)).
+      WillOnce(Return(1));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_bus_own_name_on_connection(_, _, _, _, _, _, _)).
+      WillRepeatedly(Return(1));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_proxy_new_sync(_, _, _, _, _, _, _, _)).
+      WillRepeatedly(Return(_proxy));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_proxy_call_sync(_, _, _, _, _, _, _)).
+      WillOnce(
+        Invoke([&](GDBusProxy* p, const gchar* method, GVariant* v,
+            GDBusCallFlags flag, gint msec, GCancellable* c, GError** e)
+          -> GVariant* {
+        *e = nullptr;
+        return g_variant_new("(i)", 1);
+      })).
+      WillOnce(
+        Invoke([&](GDBusProxy* p, const gchar* method, GVariant* v,
+            GDBusCallFlags flag, gint msec, GCancellable* c, GError** e)
+          -> GVariant* {
+        *e = nullptr;
+        return g_variant_new("(iis)", 1, 1, "a");
+      }));
+
+  unsigned int id;
+  int type;
+  int ret = eventsystem_register_application_event("tizen.system.event.test",
+      &id, &type, system_callback, NULL);
+  EXPECT_EQ(ret, ES_R_OK);
+
+  ret = eventsystem_unregister_application_event(id);
+  EXPECT_EQ(ret, ES_R_OK);
+}
+
 TEST_F(EventSystemTest, eventsystem_keep_last_event_data) {
-  int ret = eventsystem_keep_last_event_data("test");
-  EXPECT_NE(ret, ES_R_OK);
+  GDBusProxy* _proxy = (GDBusProxy*)g_object_new(G_TYPE_OBJECT, nullptr);
+
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_is_interface_name(_)).
+      WillOnce(Return(true));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_connection_signal_subscribe(_, _, _, _, _, _, _, _, _, _)).
+      WillOnce(Return(1));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_bus_own_name_on_connection(_, _, _, _, _, _, _)).
+      WillRepeatedly(Return(1));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_proxy_new_sync(_, _, _, _, _, _, _, _)).
+      WillRepeatedly(Return(_proxy));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_proxy_call_sync(_, _, _, _, _, _, _)).
+      WillRepeatedly(
+        Invoke([&](GDBusProxy* p, const gchar* method, GVariant* v,
+            GDBusCallFlags flag, gint msec, GCancellable* c, GError** e)
+          -> GVariant* {
+        *e = nullptr;
+        return g_variant_new("(i)", 0);
+      }));
+
+  int ret = eventsystem_keep_last_event_data("tizen.system.event.test");
+  EXPECT_EQ(ret, ES_R_OK);
+}
+
+TEST_F(EventSystemTest, eventsystem_application_finalize) {
+  int ret = eventsystem_application_finalize();
+  EXPECT_EQ(ret, ES_R_OK);
 }