Add unit test for RequestBroker 97/235897/8 accepted/tizen/unified/20200618.130042 submit/tizen/20200618.082339
authorIlho Kim <ilho159.kim@samsung.com>
Wed, 10 Jun 2020 01:57:09 +0000 (10:57 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Thu, 18 Jun 2020 03:56:30 +0000 (03:56 +0000)
Change-Id: I75cbcf4d1571697519aa331b7581875f8092fb2a
Signed-off-by: Ilho Kim <ilho159.kim@samsung.com>
test/unit_tests/mock/gio_mock.cc
test/unit_tests/mock/gio_mock.h
test/unit_tests/mock/mock_hook.h
test/unit_tests/test_request_broker.cc [new file with mode: 0644]

index 874dd8d..4e048fc 100644 (file)
@@ -47,3 +47,53 @@ extern "C" void g_dbus_message_set_body(GDBusMessage* arg0, GVariant* arg1) {
 extern "C" GVariant* g_dbus_message_get_body(GDBusMessage* arg0) {
   return MOCK_HOOK_P1(GioMock, g_dbus_message_get_body, arg0);
 }
+
+extern "C" gboolean g_dbus_connection_emit_signal(GDBusConnection* arg0,
+    const gchar* arg1, const gchar* arg2, const gchar* arg3, const gchar* arg4,
+    GVariant* arg5, GError** arg6) {
+  return MOCK_HOOK_P7(GioMock, g_dbus_connection_emit_signal,
+      arg0, arg1, arg2, arg3, arg4, arg5, arg6);
+}
+
+extern "C" GDBusNodeInfo* g_dbus_node_info_new_for_xml(
+    const gchar* arg0, GError** arg1) {
+  return MOCK_HOOK_P2(GioMock, g_dbus_node_info_new_for_xml, arg0, arg1);
+}
+
+extern "C" guint g_dbus_connection_register_object(GDBusConnection* arg0,
+    const gchar* arg1, GDBusInterfaceInfo* arg2,
+    const GDBusInterfaceVTable* arg3, gpointer arg4,
+    GDestroyNotify arg5, GError** arg6) {
+  return MOCK_HOOK_P7(GioMock, g_dbus_connection_register_object,
+      arg0, arg1, arg2, arg3, arg4, arg5, arg6);
+}
+
+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" 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,
+    gpointer arg8, GDestroyNotify arg9) {
+  return MOCK_HOOK_P10(GioMock, g_dbus_connection_signal_subscribe,
+      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,
+      arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
+}
+
+extern "C" GDBusMessage* g_dbus_connection_send_message_with_reply_finish(
+  GDBusConnection* arg0, GAsyncResult* arg1, GError** arg2) {
+  return MOCK_HOOK_P3(GioMock, g_dbus_connection_send_message_with_reply_finish,
+      arg0, arg1, arg2);
+}
index 25f7919..a85f992 100755 (executable)
@@ -28,6 +28,10 @@ class GioMock : public virtual ModuleMock {
 
   MOCK_METHOD3(g_bus_get_sync,
       GDBusConnection*(GBusType, GCancellable*, GError**));
+  MOCK_METHOD7(g_bus_own_name_on_connection,
+      guint(GDBusConnection*, const gchar*, GBusNameOwnerFlags,
+          GBusNameAcquiredCallback, GBusNameLostCallback,
+          gpointer, GDestroyNotify));
 
   MOCK_METHOD4(g_dbus_message_new_method_call,
       GDBusMessage*(const gchar*, const gchar*, const gchar*, const gchar*));
@@ -37,6 +41,27 @@ class GioMock : public virtual ModuleMock {
   MOCK_METHOD7(g_dbus_connection_send_message_with_reply_sync,
       GDBusMessage*(GDBusConnection*, GDBusMessage*, GDBusSendMessageFlags,
           gint, volatile guint32*, GCancellable*, GError**));
+  MOCK_METHOD7(g_dbus_connection_emit_signal,
+      gboolean(GDBusConnection*, const gchar*, const gchar*, const gchar*,
+          const gchar*, GVariant*, GError**));
+  MOCK_METHOD7(g_dbus_connection_register_object,
+      guint(GDBusConnection*, const gchar*, GDBusInterfaceInfo*,
+          const GDBusInterfaceVTable*, gpointer, GDestroyNotify, GError**));
+  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_METHOD3(g_dbus_connection_send_message_with_reply_finish,
+      GDBusMessage*(GDBusConnection*, GAsyncResult*, GError**));
+
+  MOCK_METHOD2(g_dbus_node_info_new_for_xml,
+      GDBusNodeInfo*(const gchar*, GError**));
+
+  MOCK_METHOD2(g_dbus_method_invocation_return_value,
+      GDBusNodeInfo*(GDBusMethodInvocation*, GVariant*));
 };
 
 #endif  // UNIT_TESTS_MOCK_GIO_MOCK_H_
index 9d2a3d8..af27bba 100644 (file)
     TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3, p4, p5, p6)
 #define MOCK_HOOK_P7(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6, p7)                \
     TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3, p4, p5, p6, p7)
+#define MOCK_HOOK_P8(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6, p7, p8)            \
+    TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3, p4, p5, p6, p7, p8)
+#define MOCK_HOOK_P10(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10)  \
+    TestFixture::GetMock<MOCK_CLASS>().f(                                      \
+        p1, p2, p3, p4, p5, p6, p7, p8, p9, p10)
 
 #endif  // UNIT_TESTS_MOCK_MOCK_HOOK_H_
diff --git a/test/unit_tests/test_request_broker.cc b/test/unit_tests/test_request_broker.cc
new file mode 100644 (file)
index 0000000..013e63d
--- /dev/null
@@ -0,0 +1,281 @@
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <bundle_cpp.h>
+#include <gtest/gtest.h>
+
+#include <cstdio>
+
+#include "theme/dbus/request_broker.h"
+#include "theme/dbus/request_handler.h"
+#include "theme/loader/theme_info.h"
+
+#include "unit_tests/mock/gio_mock.h"
+#include "unit_tests/mock/test_fixture.h"
+
+using ::testing::_;
+using ::testing::DoAll;
+using ::testing::Return;
+using ::testing::SetArgPointee;
+using ::testing::Invoke;
+
+static guint __g_dbus_connection_register_object_fake(
+    GDBusConnection *connection, const gchar *object_path,
+    GDBusInterfaceInfo *interface_info, const GDBusInterfaceVTable *vtable,
+    gpointer user_data, GDestroyNotify user_data_free_func, GError **error) {
+  tizen_base::Bundle b;
+  GVariant* param = g_variant_new("(is)",
+      static_cast<int>(ttm::dbus::Command::ADD),
+      reinterpret_cast<char*>(b.ToRaw().first.get()));
+  vtable->method_call(nullptr, nullptr, nullptr, nullptr,
+      nullptr, param, nullptr, user_data);
+  return 1;
+}
+
+static guint __g_dbus_connection_signal_subscribe_fake(
+    GDBusConnection *con, const gchar* sender, const gchar* interface,
+    const gchar* member, const gchar* object, const gchar* arg,
+    GDBusSignalFlags flags, GDBusSignalCallback callback,
+    gpointer user_data, GDestroyNotify noti) {
+  tizen_base::Bundle b;
+  GVariant* param = g_variant_new("(is)",
+      static_cast<int>(ttm::dbus::Command::REMOVED),
+      reinterpret_cast<char*>(b.ToRaw().first.get()));
+  callback(nullptr, nullptr, nullptr, nullptr, nullptr, param, user_data);
+  return 1;
+}
+
+static void __g_dbus_connection_send_message_with_reply_fake(
+    GDBusConnection *connection, GDBusMessage *message,
+    GDBusSendMessageFlags flags, gint timeout_msec,
+    volatile guint32 *out_serial, GCancellable *cancellable,
+    GAsyncReadyCallback callback, gpointer user_data) {
+  callback(nullptr, nullptr, user_data);
+}
+
+class TestRequestHandler : public ttm::dbus::IRequestHandler {
+ public:
+  tizen_base::Bundle OnRequest(ttm::dbus::Command cmd,
+      const tizen_base::Bundle& args) {
+    tizen_base::Bundle b;
+    return b;
+  }
+};
+
+class Mocks : public ::testing::NiceMock<GioMock> {};
+
+class RequestBrokerTest : public TestFixture {
+ public:
+  RequestBrokerTest() : TestFixture(std::make_unique<Mocks>()),
+      broker_(ttm::dbus::RequestBroker::GetInst()) {}
+  virtual ~RequestBrokerTest() {}
+
+  virtual void SetUp() {
+  }
+
+  virtual void TearDown() {
+  }
+
+  ttm::dbus::RequestBroker& broker_;
+};
+
+TEST_F(RequestBrokerTest, RegisterRequestFilter) {
+  ttm::dbus::RequestFilter filter(ttm::dbus::Command::ADD,
+      std::make_shared<TestRequestHandler>());
+  EXPECT_FALSE(filter.IsOneway());
+  int res = broker_.RegisterRequestFilter(std::move(filter));
+  EXPECT_EQ(res, 0);
+}
+
+TEST_F(RequestBrokerTest, EmitSignal) {
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_connection_emit_signal(_, _, _, _, _, _, _)).
+          WillOnce(Return(true));
+  tizen_base::Bundle b;
+  bool res = broker_.EmitSignal("test", ttm::dbus::Command::CHANGED, b);
+  EXPECT_EQ(res, true);
+}
+
+TEST_F(RequestBrokerTest, EmitSignal_N) {
+  GQuark quark = g_quark_from_static_string("test");
+  GError *error = g_error_new(quark, G_DBUS_ERROR_FAILED, "Error");
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_connection_emit_signal(_, _, _, _, _, _, _)).
+          WillOnce(DoAll(SetArgPointee<6>(error), Return(false)));
+  tizen_base::Bundle b;
+  bool res = broker_.EmitSignal("test", ttm::dbus::Command::CHANGED, b);
+  EXPECT_EQ(res, false);
+}
+
+TEST_F(RequestBrokerTest, Listen) {
+  GDBusNodeInfo *info = reinterpret_cast<GDBusNodeInfo*>(
+      malloc(sizeof(GDBusNodeInfo)));
+  info->ref_count = 10;
+  info->path = NULL;
+  info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
+      malloc(sizeof(GDBusInterfaceInfo)));
+  info->nodes = NULL;
+  info->annotations = NULL;
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_node_info_new_for_xml(_, _)).WillOnce(Return(info));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_connection_register_object(_, _, _, _, _, _, _)).
+          WillOnce(Invoke(__g_dbus_connection_register_object_fake));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_bus_own_name_on_connection(_, _, _, _, _, _, _)).WillOnce(Return(1));
+  EXPECT_EQ(broker_.Listen(), true);
+}
+
+TEST_F(RequestBrokerTest, Listen_N1) {
+  GQuark quark = g_quark_from_static_string("test");
+  GError *error = g_error_new(quark, G_DBUS_ERROR_FAILED, "Error");
+  EXPECT_CALL(GetMock<GioMock>(), g_dbus_node_info_new_for_xml(_, _)).
+      WillOnce(DoAll(SetArgPointee<1>(error), Return(nullptr)));
+  EXPECT_EQ(broker_.Listen(), false);
+}
+
+TEST_F(RequestBrokerTest, Listen_N2) {
+  GDBusNodeInfo *info = reinterpret_cast<GDBusNodeInfo*>(
+      malloc(sizeof(GDBusNodeInfo)));
+  info->ref_count = 10;
+  info->path = NULL;
+  info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
+      malloc(sizeof(GDBusInterfaceInfo)));
+  info->nodes = NULL;
+  info->annotations = NULL;
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_node_info_new_for_xml(_, _)).WillOnce(Return(info));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_connection_register_object(_, _, _, _, _, _, _)).
+          WillOnce(Return(0));
+  EXPECT_EQ(broker_.Listen(), false);
+}
+
+TEST_F(RequestBrokerTest, Listen_N3) {
+  GDBusNodeInfo *info = reinterpret_cast<GDBusNodeInfo*>(
+      malloc(sizeof(GDBusNodeInfo)));
+  info->ref_count = 10;
+  info->path = NULL;
+  info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
+      malloc(sizeof(GDBusInterfaceInfo)));
+  info->nodes = NULL;
+  info->annotations = NULL;
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_node_info_new_for_xml(_, _)).WillOnce(Return(info));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_connection_register_object(_, _, _, _, _, _, _)).
+          WillOnce(Return(1));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_bus_own_name_on_connection(_, _, _, _, _, _, _)).WillOnce(Return(0));
+  EXPECT_EQ(broker_.Listen(), false);
+}
+
+TEST_F(RequestBrokerTest, Subscribe) {
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_connection_signal_subscribe(_, _, _, _, _, _, _, _, _, _)).
+          WillOnce(Invoke(__g_dbus_connection_signal_subscribe_fake));
+  EXPECT_EQ(broker_.Subscribe(), true);
+}
+
+TEST_F(RequestBrokerTest, SendData_N1) {
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_message_new_method_call(_, _, _, _)).WillOnce(Return(nullptr));
+  tizen_base::Bundle b;
+  b = broker_.SendData(ttm::dbus::Command::ADD, b);
+  std::string result = b.GetString(CMD_RESULT_KEY);
+  EXPECT_EQ(result, "error");
+}
+
+TEST_F(RequestBrokerTest, SendData_N2) {
+  GDBusMessage* msg =
+      reinterpret_cast<GDBusMessage*>(g_object_new(G_TYPE_OBJECT, NULL));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_message_new_method_call(_, _, _, _)).WillOnce(Return(msg));
+  GQuark quark = g_quark_from_static_string("test");
+  GError *error = g_error_new(quark, G_DBUS_ERROR_FAILED, "Error");
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_connection_send_message_with_reply_sync(_, _, _, _, _, _, _)).
+          WillOnce(DoAll(SetArgPointee<6>(error), Return(nullptr)));
+  tizen_base::Bundle b;
+  b = broker_.SendData(ttm::dbus::Command::ADD, b);
+  std::string result = b.GetString(CMD_RESULT_KEY);
+  EXPECT_EQ(result, "error");
+}
+
+TEST_F(RequestBrokerTest, SendData_N3) {
+  GDBusMessage* msg =
+      reinterpret_cast<GDBusMessage*>(g_object_new(G_TYPE_OBJECT, NULL));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_message_new_method_call(_, _, _, _)).WillOnce(Return(msg));
+  msg = reinterpret_cast<GDBusMessage*>(g_object_new(G_TYPE_OBJECT, NULL));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_connection_send_message_with_reply_sync(_, _, _, _, _, _, _)).
+          WillOnce(Return(msg));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_message_get_body(_)).WillOnce(Return(nullptr));
+  tizen_base::Bundle b;
+  b = broker_.SendData(ttm::dbus::Command::ADD, b);
+  std::string result = b.GetString(CMD_RESULT_KEY);
+  EXPECT_EQ(result, "error");
+}
+
+TEST_F(RequestBrokerTest, SendDataAsync_N1) {
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_message_new_method_call(_, _, _, _)).WillOnce(Return(nullptr));
+  tizen_base::Bundle b;
+  broker_.SendDataAsync(ttm::dbus::Command::ADD, b);
+}
+
+TEST_F(RequestBrokerTest, OnResultReceivedCb) {
+  GDBusMessage* msg =
+      reinterpret_cast<GDBusMessage*>(g_object_new(G_TYPE_OBJECT, NULL));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_message_new_method_call(_, _, _, _)).WillOnce(Return(msg));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_connection_send_message_with_reply(_, _, _, _, _, _, _, _)).
+          WillOnce(Invoke(__g_dbus_connection_send_message_with_reply_fake));
+  msg = reinterpret_cast<GDBusMessage *>(g_object_new(G_TYPE_OBJECT, NULL));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_connection_send_message_with_reply_finish(_, _, _)).
+          WillOnce(Return(msg));
+  tizen_base::Bundle b;
+  ttm::loader::ThemeInfo ti;
+  b.Add(CMD_RESULT_KEY, "ok");
+  b.Add(CMD_GET_KEY,
+      reinterpret_cast<char*>(ti.Serialize().ToRaw().first.get()));
+  b.Add(CMD_GET_IDS_KEY, std::vector<std::string>());
+  GVariant *gv = g_variant_new("(s)", b.ToRaw().first.get());
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_message_get_body(_)).WillOnce(Return(gv));
+  broker_.SendDataAsync(ttm::dbus::Command::ADD, b);
+}
+
+TEST_F(RequestBrokerTest, OnResultReceivedCb_N) {
+  GDBusMessage* msg =
+      reinterpret_cast<GDBusMessage*>(g_object_new(G_TYPE_OBJECT, NULL));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_message_new_method_call(_, _, _, _)).WillOnce(Return(msg));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_connection_send_message_with_reply(_, _, _, _, _, _, _, _)).
+          WillOnce(Invoke(__g_dbus_connection_send_message_with_reply_fake));
+  GQuark quark = g_quark_from_static_string("test");
+  GError *error = g_error_new(quark, G_DBUS_ERROR_FAILED, "Error");
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_connection_send_message_with_reply_finish(_, _, _)).
+          WillOnce(DoAll(SetArgPointee<2>(error), Return(nullptr)));
+  tizen_base::Bundle b;
+  broker_.SendDataAsync(ttm::dbus::Command::ADD, b);
+}