--- /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 <gio/gio.h>
+
+#include "gio_mock.h"
+#include "mock_hook.h"
+#include "test_fixture.h"
+
+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,
+ 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);
+}
+
+extern "C" gboolean g_dbus_message_to_gerror(
+ GDBusMessage* arg0, GError** arg1) {
+ return MOCK_HOOK_P2(GioMock, g_dbus_message_to_gerror,
+ arg0, arg1);
+}
+
+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" void g_bus_unwatch_name(guint arg0) {
+ return MOCK_HOOK_P1(GioMock, g_bus_unwatch_name, arg0);
+}
+
+extern "C" void g_dbus_connection_signal_unsubscribe(
+ GDBusConnection* arg0, guint arg1) {
+ return MOCK_HOOK_P2(GioMock, g_dbus_connection_signal_unsubscribe,
+ arg0, arg1);
+}
--- /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.
+ */
+
+#ifndef MOCK_GIO_MOCK_H_
+#define MOCK_GIO_MOCK_H_
+
+#include <gio/gio.h>
+#include <gmock/gmock.h>
+
+#include "module_mock.h"
+
+class GioMock : public virtual ModuleMock {
+ public:
+ virtual ~GioMock() {}
+
+ 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_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_message_to_gerror,
+ gboolean(GDBusMessage*, GError**));
+ 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_METHOD2(g_dbus_connection_signal_unsubscribe,
+ void(GDBusConnection*, guint));
+};
+
+#endif // MOCK_GIO_MOCK_H_
#include <stdlib.h>
#include <gtest/gtest.h>
#include <shortcut_manager.h>
+#include <stdio.h>
#include <memory>
#include "system_info_mock.h"
+#include "gio_mock.h"
#include "test_fixture.h"
using ::testing::_;
using ::testing::DoAll;
using ::testing::Return;
using ::testing::SetArgPointee;
-using ::testing::Invoke;
-class Mocks : public ::testing::NiceMock<SystemInfoMock> {};
+class Mocks : public ::testing::NiceMock<SystemInfoMock>,
+ public ::testing::NiceMock<GioMock> {};
class ShortcutTest : public TestFixture {
public:
virtual void TearDown() {
}
};
+
+TEST_F(ShortcutTest, shortcut_add_to_home) {
+ int ret;
+ bool is_supported = true;
+ GDBusConnection* _conn = (GDBusConnection*)g_object_new(G_TYPE_OBJECT, NULL);
+ GDBusMessage* _msg = (GDBusMessage*)g_object_new(G_TYPE_OBJECT, NULL);
+ GDBusMessage* _msg2 = (GDBusMessage*)g_object_new(G_TYPE_OBJECT, NULL);
+ GDBusMessage* _msg3 = (GDBusMessage*)g_object_new(G_TYPE_OBJECT, NULL);
+
+ EXPECT_CALL(GetMock<SystemInfoMock>(),
+ system_info_get_platform_bool(_, _)).
+ WillOnce(DoAll(SetArgPointee<1>(is_supported),
+ Return(0)));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_bus_get_sync(_, _, _)).
+ WillOnce(Return(_conn));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_message_new_method_call(_, _, _, _)).
+ WillOnce(Return(_msg)).
+ WillOnce(Return(_msg2));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_send_message_with_reply_sync(
+ _, _, _, _, _, _, _)).
+ WillOnce(Return(_msg3));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_send_message_with_reply(
+ _, _, _, _, _, _, _, _)).
+ Times(1);
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_message_to_gerror(_, _)).
+ WillOnce(Return(FALSE));
+
+ ret = shortcut_add_to_home("test_name", LAUNCH_BY_URI, "test_uri", "test_icon",
+ 0, NULL, NULL);
+
+ EXPECT_EQ(ret, SHORTCUT_ERROR_NONE);
+}
+
+TEST_F(ShortcutTest, shortcut_remove_from_home) {
+ int ret;
+ bool is_supported = true;
+ GDBusMessage* _msg = (GDBusMessage*)g_object_new(G_TYPE_OBJECT, NULL);
+ GDBusMessage* _msg2 = (GDBusMessage*)g_object_new(G_TYPE_OBJECT, NULL);
+ GDBusMessage* _msg3 = (GDBusMessage*)g_object_new(G_TYPE_OBJECT, NULL);
+
+ EXPECT_CALL(GetMock<SystemInfoMock>(),
+ system_info_get_platform_bool(_, _)).
+ WillOnce(DoAll(SetArgPointee<1>(is_supported),
+ Return(0)));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_message_new_method_call(_, _, _, _)).
+ WillOnce(Return(_msg)).
+ WillOnce(Return(_msg2));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_send_message_with_reply_sync(
+ _, _, _, _, _, _, _)).
+ WillOnce(Return(_msg3));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_send_message_with_reply(
+ _, _, _, _, _, _, _, _)).
+ Times(1);
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_message_to_gerror(_, _)).
+ WillOnce(Return(FALSE));
+
+ ret = shortcut_remove_from_home("test_name",
+ [](int ret, void* user_data) -> int {
+ return 0;
+ } ,NULL);
+
+ EXPECT_EQ(ret, SHORTCUT_ERROR_NONE);
+}
+
+TEST_F(ShortcutTest, shortcut_get_list) {
+ int ret;
+ bool is_supported = true;
+ GVariantBuilder *builder;
+ GVariant* body;
+ GVariant* reply_body;
+ GDBusMessage* _msg = (GDBusMessage*)g_object_new(G_TYPE_OBJECT, NULL);
+ GDBusMessage* _msg2 = (GDBusMessage*)g_object_new(G_TYPE_OBJECT, NULL);
+
+ body = g_variant_new("(sssss)",
+ "test_pkg", "test_icon", "test_name", "test_extra", "");
+
+ builder = g_variant_builder_new(G_VARIANT_TYPE("a(v)"));
+ g_variant_builder_add(builder, "(v)", body);
+
+ reply_body = g_variant_new("(ia(v))", 1, builder);
+
+ EXPECT_CALL(GetMock<SystemInfoMock>(),
+ system_info_get_platform_bool(_, _)).
+ WillOnce(DoAll(SetArgPointee<1>(is_supported),
+ Return(0)));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_message_new_method_call(_, _, _, _)).
+ WillOnce(Return(_msg));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_send_message_with_reply_sync(
+ _, _, _, _, _, _, _)).
+ WillOnce(Return(_msg2));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_message_get_body(_)).
+ WillOnce(Return(reply_body));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_message_to_gerror(_, _)).
+ WillOnce(Return(FALSE));
+
+ ret = shortcut_get_list("test_pkgid",
+ [](const char* package_name, const char* icon,
+ const char* name, const char* extra_key,
+ const char* extra_data, void* user_data) -> int {
+ return 0;
+ }, NULL);
+
+ g_variant_builder_unref(builder);
+ g_variant_unref(body);
+
+ EXPECT_EQ(ret, 1);
+}
+
+TEST_F(ShortcutTest, shortcut_set_request_cb) {
+ int ret;
+ bool is_supported = true;
+ GDBusMessage* _msg = (GDBusMessage*)g_object_new(G_TYPE_OBJECT, NULL);
+ GDBusMessage* _msg2 = (GDBusMessage*)g_object_new(G_TYPE_OBJECT, NULL);
+
+ EXPECT_CALL(GetMock<SystemInfoMock>(),
+ system_info_get_platform_bool(_, _)).
+ WillOnce(DoAll(SetArgPointee<1>(is_supported),
+ Return(0)));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_signal_subscribe(
+ _, _, _, _, _, _, _, _, _, _)).
+ WillOnce(Return(99));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_message_new_method_call(_, _, _, _)).
+ WillOnce(Return(_msg));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_send_message_with_reply_sync(
+ _, _, _, _, _, _, _)).
+ WillOnce(Return(_msg2));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_bus_watch_name_on_connection(
+ _, _, _, _, _, _, _)).
+ WillOnce(Return(88));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_message_to_gerror(_, _)).
+ WillOnce(Return(FALSE));
+
+ ret = shortcut_set_request_cb(
+ [](const char* package_name, const char* name,
+ int type, const char* content_info, const char* icon,
+ int pid, double period, int allow_duplicate,
+ void* user_data) -> int {
+ return 0;
+ } ,NULL);
+
+ EXPECT_EQ(ret, SHORTCUT_ERROR_NONE);
+}
+
+TEST_F(ShortcutTest, shortcut_unset_request_cb) {
+ bool is_supported = true;
+ GDBusMessage* _msg = (GDBusMessage*)g_object_new(G_TYPE_OBJECT, NULL);
+ GDBusMessage* _msg2 = (GDBusMessage*)g_object_new(G_TYPE_OBJECT, NULL);
+
+ EXPECT_CALL(GetMock<SystemInfoMock>(),
+ system_info_get_platform_bool(_, _)).
+ WillOnce(DoAll(SetArgPointee<1>(is_supported),
+ Return(0)));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_message_new_method_call(_, _, _, _)).
+ WillOnce(Return(_msg));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_send_message_with_reply_sync(
+ _, _, _, _, _, _, _)).
+ WillOnce(Return(_msg2));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_message_to_gerror(_, _)).
+ WillOnce(Return(FALSE));
+
+ shortcut_unset_request_cb();
+
+ EXPECT_EQ(get_last_result(), SHORTCUT_ERROR_NONE);
+}
+
+TEST_F(ShortcutTest, shortcut_set_remove_cb) {
+ int ret;
+ bool is_supported = true;
+ GDBusConnection* _conn = (GDBusConnection*)g_object_new(G_TYPE_OBJECT, NULL);
+ GDBusMessage* _msg = (GDBusMessage*)g_object_new(G_TYPE_OBJECT, NULL);
+ GDBusMessage* _msg2 = (GDBusMessage*)g_object_new(G_TYPE_OBJECT, NULL);
+
+ EXPECT_CALL(GetMock<SystemInfoMock>(),
+ system_info_get_platform_bool(_, _)).
+ WillOnce(DoAll(SetArgPointee<1>(is_supported),
+ Return(0)));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_bus_get_sync(_, _, _)).
+ WillOnce(Return(_conn));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_signal_subscribe(
+ _, _, _, _, _, _, _, _, _, _)).
+ WillOnce(Return(99));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_message_new_method_call(_, _, _, _)).
+ WillOnce(Return(_msg));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_send_message_with_reply_sync(
+ _, _, _, _, _, _, _)).
+ WillOnce(Return(_msg2));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_bus_watch_name_on_connection(
+ _, _, _, _, _, _, _)).
+ WillOnce(Return(88));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_message_to_gerror(_, _)).
+ WillOnce(Return(FALSE));
+
+ ret = shortcut_set_remove_cb(
+ [](const char* package_name, const char* name,
+ int sender_pid, void* user_data) -> int {
+ return 0;
+ } ,NULL);
+
+ EXPECT_EQ(ret, SHORTCUT_ERROR_NONE);
+}
+
+TEST_F(ShortcutTest, shortcut_unset_remove_cb) {
+ bool is_supported = true;
+ GDBusMessage* _msg = (GDBusMessage*)g_object_new(G_TYPE_OBJECT, NULL);
+ GDBusMessage* _msg2 = (GDBusMessage*)g_object_new(G_TYPE_OBJECT, NULL);
+
+ EXPECT_CALL(GetMock<SystemInfoMock>(),
+ system_info_get_platform_bool(_, _)).
+ WillOnce(DoAll(SetArgPointee<1>(is_supported),
+ Return(0)));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_message_new_method_call(_, _, _, _)).
+ WillOnce(Return(_msg));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_send_message_with_reply_sync(
+ _, _, _, _, _, _, _)).
+ WillOnce(Return(_msg2));
+
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_message_to_gerror(_, _)).
+ WillOnce(Return(FALSE));
+
+ shortcut_unset_remove_cb();
+
+ EXPECT_EQ(get_last_result(), SHORTCUT_ERROR_NONE);
+}