Merge branch 'tizen_5.5' into tizen
[platform/core/uifw/capi-ui-sticker.git] / tests / src / sticker_consumer_unittests.cc
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <gtest/gtest.h>
18 #include <sticker_data.h>
19 #include <sticker_consumer.h>
20
21 #include <glib.h>
22 #include <string>
23 #include <chrono>
24 #include <thread>
25
26 #include "app_common_mock.h"
27 #include "cynara_mock.h"
28 #include "gio_mock.h"
29
30 static bool callback_result = false;
31 static sticker_data_h g_dh = NULL;
32 static sticker_consumer_h g_ch = NULL;
33
34 namespace {
35
36 // cynara
37 int __cynara_initialize(cynara** c, const cynara_configuration* conf) {
38     *c = (void *)0x1;
39     return 0;
40 }
41
42 int __cynara_finish(cynara* c) {
43     return 0;
44 }
45
46 int __cynara_check(cynara* c, const char* client, const char* client_session,
47     const char* user,
48     const char* privilege) {
49     return CYNARA_API_ACCESS_ALLOWED;
50 }
51
52 // app common
53 int __fake_app_get_id(char** app_id) {
54     if (app_id)
55         *app_id = strdup("org.tizen.hello");
56
57     return 0;
58 }
59
60 // gio
61 GDBusConnection* __fake_g_bus_get_sync(GBusType type,
62         GCancellable* cancel, GError** err) {
63     return (GDBusConnection*)calloc(1, 4);
64 }
65
66 const gchar * __fake_g_dbus_connection_get_unique_name(GDBusConnection *connection) {
67     return "hello";
68 }
69
70 guint __fake_g_bus_watch_name(
71     GBusType bus_type,
72     const gchar* name,
73     GBusNameWatcherFlags flags,
74     GBusNameAppearedCallback name_appeared_handler,
75     GBusNameVanishedCallback name_vanished_handler,
76     gpointer user_data,
77     GDestroyNotify user_data_free_func)
78 {
79     name_appeared_handler(nullptr, "appear test", "test owner", user_data);
80     name_vanished_handler(nullptr, "vanish test", user_data);
81
82     return 1;
83 }
84
85 guint __fake_g_dbus_connection_signal_subscribe(GDBusConnection* connection,
86                                     const gchar* sender,
87                                     const gchar* interface_name,
88                                     const gchar* member,
89                                     const gchar* object_path,
90                                     const gchar* arg0,
91                                     GDBusSignalFlags     flags,
92                                     GDBusSignalCallback  callback,
93                                     gpointer             user_data,
94                                     GDestroyNotify       user_data_free_func)
95 {
96     return 2;
97 }
98
99 guint __fake_g_bus_watch_name_on_connection(GDBusConnection* connection,
100     const gchar* name,
101     GBusNameWatcherFlags flags,
102     GBusNameAppearedCallback name_appeared_handler,
103     GBusNameVanishedCallback  name_vanished_handler,
104     gpointer user_data,
105     GDestroyNotify user_data_free_func)
106 {
107     name_appeared_handler(nullptr, "appear test", "test owner", user_data);
108     name_vanished_handler(nullptr, "vanish test", user_data);
109     return 1;
110 }
111
112 GDBusMessage*  __fake_g_dbus_message_new_method_call(
113         const gchar* a, const gchar* b, const gchar* c, const gchar* d) {
114     return (GDBusMessage*)calloc(1, 4);
115 }
116
117 void __fake_g_dbus_message_set_body(GDBusMessage * m,
118         GVariant* a) {
119 }
120
121 GDBusMessage* __fake_g_dbus_connection_send_message_with_reply_sync(
122         GDBusConnection* conn, GDBusMessage* m, GDBusSendMessageFlags f, gint a,
123         volatile guint32* b, GCancellable* c, GError* *d) {
124     return (GDBusMessage*)calloc(1, 4);
125 }
126
127 void __fake_g_object_unref(gpointer o) {
128 }
129
130 GVariant* __fake_g_dbus_message_get_body(GDBusMessage* m) {
131     return g_variant_new("(u)", 10);
132 }
133
134 void __fake_g_dbus_connection_signal_unsubscribe(GDBusConnection* c,
135         guint a) {
136 }
137
138 void __fake_g_bus_unwatch_name(guint a) {
139 }
140
141 class StickerConsumerTest : public testing::Test {
142     public:
143         virtual void SetUp() {
144             // cynara
145             cynara_initialize_fake.custom_fake = __cynara_initialize;
146             cynara_finish_fake.custom_fake = __cynara_finish;
147             cynara_check_fake.custom_fake = __cynara_check;
148
149             // app-common
150             app_get_id_fake.custom_fake = __fake_app_get_id;
151
152             // gio
153             g_bus_watch_name_on_connection_fake.custom_fake = __fake_g_bus_watch_name_on_connection;
154             g_dbus_message_set_body_fake.custom_fake = __fake_g_dbus_message_set_body;
155             g_bus_get_sync_fake.custom_fake = __fake_g_bus_get_sync;
156             g_dbus_connection_get_unique_name_fake.custom_fake = __fake_g_dbus_connection_get_unique_name;
157             g_bus_watch_name_fake.custom_fake = __fake_g_bus_watch_name;
158             g_dbus_message_new_method_call_fake.custom_fake = __fake_g_dbus_message_new_method_call;
159             g_dbus_connection_send_message_with_reply_sync_fake.custom_fake = __fake_g_dbus_connection_send_message_with_reply_sync;
160             g_object_unref_fake.custom_fake = __fake_g_object_unref;
161             g_dbus_message_get_body_fake.custom_fake = __fake_g_dbus_message_get_body;
162             g_dbus_connection_signal_unsubscribe_fake.custom_fake = __fake_g_dbus_connection_signal_unsubscribe;
163             g_bus_unwatch_name_fake.custom_fake = __fake_g_bus_unwatch_name;
164             g_dbus_connection_signal_subscribe_fake.custom_fake = __fake_g_dbus_connection_signal_subscribe;
165
166             sticker_consumer_create(&g_ch);
167             sticker_data_create(&g_dh);
168             sticker_data_set_uri(g_dh, STICKER_DATA_URI_WEB_RESOURCE, "www.samsung.com");
169             sticker_data_add_keyword(g_dh, "smile");
170             sticker_data_add_keyword(g_dh, "face");
171             sticker_data_add_keyword(g_dh, "cute");
172             sticker_data_set_group_name(g_dh, "tizen");
173             sticker_data_set_thumbnail(g_dh, "/res/test.png");
174             sticker_data_set_description(g_dh, "TCT Test");
175         }
176         virtual void TearDown() {
177             sticker_data_destroy(g_dh);
178             sticker_consumer_destroy(g_ch);
179             g_dh = NULL;
180             g_ch = NULL;
181         }
182 };
183
184 TEST_F(StickerConsumerTest, utc_sticker_consumer_create_ERROR_NONE)
185 {
186     sticker_consumer_h sc_h = NULL;
187     int ret = sticker_consumer_create(&sc_h);
188     EXPECT_EQ(ret, STICKER_ERROR_NONE);
189 }
190
191 /**
192  * @function        utc_sticker_consumer_p
193  * @description     Positive UTC of the function that creates sticker consumer handle
194  * @parameter       NA
195  */
196 TEST_F(StickerConsumerTest, utc_sticker_consumer_create_p)
197 {
198     sticker_consumer_h sc_h = NULL;
199     int ret = sticker_consumer_create(&sc_h);
200     EXPECT_EQ(ret, STICKER_ERROR_NONE);
201 }
202
203 /**
204  * @function        utc_sticker_consumer_n
205  * @description     Negative UTC of the function that creates sticker consumer handle
206  * @parameter       NA
207  */
208 TEST_F(StickerConsumerTest, utc_sticker_consumer_n)
209 {
210     int ret = sticker_consumer_create(NULL);
211     EXPECT_EQ(ret, STICKER_ERROR_INVALID_PARAMETER);
212 }
213
214 TEST_F(StickerConsumerTest, utc_sticker_consumer_destroy_p)
215 {
216     sticker_consumer_h sc_h = NULL;
217     int ret = sticker_consumer_create(&sc_h);
218     ASSERT_EQ(ret, STICKER_ERROR_NONE);
219
220     ret = sticker_consumer_destroy(sc_h);
221     EXPECT_EQ(ret, STICKER_ERROR_NONE);
222 }
223
224 TEST_F(StickerConsumerTest, utc_sticker_consumer_destroy_n)
225 {
226     int ret = sticker_consumer_destroy(NULL);
227     EXPECT_EQ(ret, STICKER_ERROR_INVALID_PARAMETER);
228 }
229
230 static void sticker_data_foreach_cb(sticker_data_h data_handle, void *user_data)
231 {
232     callback_result = true;
233 }
234
235 /**
236  * @testcase        utc_sticker_consumer_data_foreach_all_p
237  * @since_tizen     5.5
238  * @description     Positive UTC of the function that retrieves all sticker data.
239  */
240 TEST_F(StickerConsumerTest, utc_sticker_consumer_data_foreach_all_p)
241 {
242     int result = 0;
243     callback_result = false;
244     int ret = sticker_consumer_data_foreach_all(g_ch, 0, 1000, &result, sticker_data_foreach_cb, NULL);
245     EXPECT_EQ(ret, STICKER_ERROR_NONE);
246 }
247
248 /**
249  * @testcase        utc_sticker_consumer_data_foreach_all_n
250  * @since_tizen     5.5
251  * @description     Negative UTC of the function that retrieves all sticker data.
252  */
253 TEST_F(StickerConsumerTest, utc_sticker_consumer_data_foreach_all_n)
254 {
255     int result = 0;
256     int ret = sticker_consumer_data_foreach_all(g_ch, -1, 0, &result, sticker_data_foreach_cb, NULL);
257     EXPECT_EQ(ret, STICKER_ERROR_INVALID_PARAMETER);
258 }
259
260 /**
261  * @testcase        utc_sticker_consumer_data_foreach_by_keyword_n
262  * @since_tizen     5.5
263  * @description     Negative UTC of the function that retrieves all sticker data using keyword.
264  */
265 TEST_F(StickerConsumerTest, utc_sticker_consumer_data_foreach_by_keyword_n)
266 {
267     int result = 0;
268     int ret = sticker_consumer_data_foreach_by_keyword(g_ch, -1, 0, &result, "cute", sticker_data_foreach_cb, NULL);
269     EXPECT_EQ(ret, STICKER_ERROR_INVALID_PARAMETER);
270 }
271
272 /**
273  * @testcase        utc_sticker_consumer_data_foreach_by_group_n
274  * @since_tizen     5.5
275  * @description     Negative UTC of the function that retrieves all sticker data using group.
276  */
277 TEST_F(StickerConsumerTest, utc_sticker_consumer_data_foreach_by_group_n)
278 {
279     int result = 0;
280     int ret = sticker_consumer_data_foreach_by_group(g_ch, -1, 0, &result, "tizen", sticker_data_foreach_cb, NULL);
281     EXPECT_EQ(ret, STICKER_ERROR_INVALID_PARAMETER);
282 }
283
284 /**
285  * @testcase        utc_sticker_consumer_data_foreach_by_type_n
286  * @since_tizen     5.5
287  * @description     Negative UTC of the function that retrieves all sticker data using type.
288  */
289 TEST_F(StickerConsumerTest, utc_sticker_consumer_data_foreach_by_type_n)
290 {
291     int result = 0;
292     int ret = sticker_consumer_data_foreach_by_type(g_ch, -1, 0, &result, STICKER_DATA_URI_WEB_RESOURCE, sticker_data_foreach_cb, NULL);
293     EXPECT_EQ(ret, STICKER_ERROR_INVALID_PARAMETER);
294 }
295
296 /**
297  * @testcase        utc_sticker_consumer_group_list_foreach_all_n
298  * @since_tizen     5.5
299  * @description     Negative UTC of the function that retrieves all group name.
300  */
301 TEST_F(StickerConsumerTest, utc_sticker_consumer_group_list_foreach_all_n)
302 {
303     int ret = sticker_consumer_group_list_foreach_all(g_ch, NULL, NULL);
304     EXPECT_EQ(ret, STICKER_ERROR_INVALID_PARAMETER);
305 }
306
307 /**
308  * @testcase        utc_sticker_consumer_keyword_list_foreach_all_n
309  * @since_tizen     5.5
310  * @description     Negative UTC of the function that retrieves all keyword.
311  */
312 TEST_F(StickerConsumerTest, utc_sticker_consumer_keyword_list_foreach_all_n)
313 {
314     int ret = sticker_consumer_keyword_list_foreach_all(g_ch, NULL, NULL);
315     EXPECT_EQ(ret, STICKER_ERROR_INVALID_PARAMETER);
316 }
317
318 } // namespace