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);
+}
--- /dev/null
+/*
+ * 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);
+}