d86edec6b609b75dade3923ef1d247cf7a24ca5b
[platform/core/appfw/widget-service.git] / unittest / src / test_widget_service.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 <gmock/gmock.h>
19 #include <stdio.h>
20 #include <sqlite3.h>
21
22 #include "include/widget_service.h"
23 #include "include/widget_errno.h"
24 #include "include/widget_service_internal.h"
25 #include "mock/system_info_mock.h"
26 #include "mock/aul_mock.h"
27 #include "mock/cynara_mock.h"
28 #include "mock/fcntl_mock.h"
29 #include "mock/tzplatform_config_mock.h"
30 #include "mock/db_mock.h"
31 #include "mock/pkgmgr_info_mock.h"
32
33 namespace {
34 int __fake_system_info_get_platform_bool(const char* key, bool* value) {
35   *value = true;
36   return 0;
37 }
38
39 uid_t __fake_getuid(void) {
40   return (uid_t)0;
41 }
42
43 int __fake_aul_widget_service_set_disable(const char* widget_id, bool disabled) {
44   return 0;
45 }
46
47 int __fake_cynara_initialize(cynara** cyn, const cynara_configuration* conf) {
48   return CYNARA_API_SUCCESS;
49 }
50
51 int __fake_cynara_check(cynara* cyn, const char* client, const char* client_session,
52     const char* user, const char* privilege) {
53   return CYNARA_API_ACCESS_ALLOWED;
54 }
55
56 int __fake_cynara_finish(cynara* cyn) {
57   return 0;
58 }
59
60 int __fake_open(const char* path, int flag) {
61   return 0;
62 }
63
64 ssize_t __fake_read(int fd, void* buf, size_t size) {
65   return 0;
66 }
67
68 int __fake_chown(const char *pathname, uid_t owner, gid_t group) {
69   return 0;
70 }
71 int __fake_chmod(const char *pathname, mode_t mode) {
72   return 0;
73 }
74
75 int __fake_aul_app_com_create(const char *endpoint, aul_app_com_permission_h permission,
76     app_com_cb callback, void *user_data, aul_app_com_connection_h *connection) {
77   aul_app_com_connection_h *conn;
78
79   conn = (aul_app_com_connection_h *)malloc(sizeof(connection));
80   *connection = conn;
81   free(conn);
82   return 0;
83 }
84
85 int __fake_aul_app_com_leave(aul_app_com_connection_h connection) {
86   return 0;
87 }
88
89 int __fake_aul_widget_instance_update(const char *widget_id, const char *instance_id, bundle *b) {
90   return 0;
91 }
92
93 int __fake_aul_widget_instance_get_content(const char *widget_id,
94                 const char *instance_id, char **content) {
95   bundle *b;
96   bundle_raw *raw;
97   int len;
98
99   b = bundle_create();
100   bundle_add_str(b, "testkey", "testvalue");
101   bundle_encode(b, &raw, &len);
102
103   *content = strdup((const char *)raw);
104
105   bundle_free(b);
106   return 0;
107 }
108
109 int __fake_aul_widget_instance_count(const char *widget_id) {
110   return 0;
111 }
112
113 struct instance_cb {
114   const char  *widget_id;
115   widget_instance_list_cb cb;
116   void *data;
117   int cnt;
118 };
119
120 struct widget_instance_info_s {
121   int period;
122   bool exists;
123   const char *instance_id;
124 };
125
126 int __fake_aul_widget_instance_foreach(const char *widget_id, aul_widget_instance_foreach_cb cb,
127     void *data) {
128   struct instance_cb *cb_data = (struct instance_cb *)data;
129   cb_data->cnt = 5;
130
131   if (cb_data->data != NULL) {
132     struct widget_instance_info_s *instance_info = (struct widget_instance_info_s *)cb_data->data;
133     instance_info->exists = true;
134   }
135
136   return 0;
137 }
138
139 const char *__fake_tzplatform_mkpath(enum tzplatform_variable id, const char *path) {
140   return get_db_path();
141 }
142
143 int __fake_pkgmgrinfo_pkginfo_get_usr_pkginfo(const char *pkgid,
144     uid_t uid, pkgmgrinfo_pkginfo_h *handle) {
145   return PMINFO_R_OK;
146 }
147
148 int __fake_pkgmgrinfo_pkginfo_get_mainappid(pkgmgrinfo_pkginfo_h handle, char **mainappid) {
149   *mainappid = (char *)"org.tizen.mainappid";
150   return PMINFO_R_OK;
151 }
152
153 int __fake_pkgmgrinfo_pkginfo_destroy_pkginfo(pkgmgrinfo_pkginfo_h handle) {
154   return PMINFO_R_OK;
155 }
156
157 class WidgetServiceTest : public ::testing::Test {
158   public:
159     virtual void SetUp() {
160       system_info_get_platform_bool_fake.custom_fake = __fake_system_info_get_platform_bool;
161       aul_widget_service_set_disable_fake.custom_fake = __fake_aul_widget_service_set_disable;
162       cynara_initialize_fake.custom_fake = __fake_cynara_initialize;
163       cynara_check_fake.custom_fake = __fake_cynara_check;
164       cynara_finish_fake.custom_fake = __fake_cynara_finish;
165       open_fake.custom_fake = __fake_open;
166       read_fake.custom_fake = __fake_read;
167       chmod_fake.custom_fake = __fake_chmod;
168       chown_fake.custom_fake = __fake_chown;
169       aul_app_com_create_fake.custom_fake = __fake_aul_app_com_create;
170       aul_app_com_leave_fake.custom_fake = __fake_aul_app_com_leave;
171       aul_widget_instance_update_fake.custom_fake = __fake_aul_widget_instance_update;
172       aul_widget_instance_foreach_fake.custom_fake = __fake_aul_widget_instance_foreach;
173       aul_widget_instance_get_content_fake.custom_fake = __fake_aul_widget_instance_get_content;
174       aul_widget_instance_count_fake.custom_fake = __fake_aul_widget_instance_count;
175       tzplatform_mkpath_fake.custom_fake = __fake_tzplatform_mkpath;
176       getuid_fake.custom_fake = __fake_getuid;
177       pkgmgrinfo_pkginfo_get_usr_pkginfo_fake.custom_fake = __fake_pkgmgrinfo_pkginfo_get_usr_pkginfo;
178       pkgmgrinfo_pkginfo_get_mainappid_fake.custom_fake = __fake_pkgmgrinfo_pkginfo_get_mainappid;
179       pkgmgrinfo_pkginfo_destroy_pkginfo_fake.custom_fake = __fake_pkgmgrinfo_pkginfo_destroy_pkginfo;
180     }
181     virtual void TearDown() {
182     }
183 };
184
185 /* internal api */
186 TEST_F(WidgetServiceTest, CheckDBIntegrity) {
187   int ret;
188
189   // create db
190   ret = widget_service_check_db_integrity(false);
191   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
192
193   // insert default data
194   insert_default_data();
195 }
196
197 TEST_F(WidgetServiceTest, SetDisabled) {
198   int ret;
199
200   ret = widget_service_set_widget_disabled("org.tizen.test_widget", true);
201   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
202 }
203
204 TEST_F(WidgetServiceTest, GetDisabled) {
205   int ret = WIDGET_ERROR_NONE;
206   bool is_disabled;
207
208   ret = widget_service_get_widget_disabled("org.tizen.test_widget",
209       &is_disabled);
210   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
211 }
212
213 void _widget_disable_event_cb(const char *widget_id, bool is_disabled, void *user_data) {
214 }
215
216 TEST_F(WidgetServiceTest, SetDisabledEventCallback) {
217   int ret;
218
219   ret = widget_service_set_disable_event_cb(_widget_disable_event_cb, NULL);
220   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
221 }
222
223 TEST_F(WidgetServiceTest, UnsetDisabledEventCallback) {
224   int ret;
225
226   ret = widget_service_set_disable_event_cb(_widget_disable_event_cb, NULL);
227   ret = widget_service_unset_disable_event_cb();
228   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
229 }
230
231 TEST_F(WidgetServiceTest, GetSize) {
232   int ret;
233   int width, height;
234
235   ret = widget_service_get_size(WIDGET_SIZE_TYPE_4x4, &width, &height);
236   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
237 }
238
239 TEST_F(WidgetServiceTest, GetSizeType) {
240   int ret;
241   widget_size_type_e type;
242
243   ret = widget_service_get_size_type(175, 175, &type);
244   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
245 }
246
247 TEST_F(WidgetServiceTest, GetNeedOfMouseEvent) {
248   int ret;
249   bool need_of_event;
250
251   ret = widget_service_get_need_of_mouse_event("org.tizen.test_widget", WIDGET_SIZE_TYPE_4x2,
252         &need_of_event);
253   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
254 }
255
256 TEST_F(WidgetServiceTest, GetNeedOfTouchEffect) {
257   int ret;
258   bool need_of_event;
259
260   ret = widget_service_get_need_of_touch_effect("org.tizen.test_widget", WIDGET_SIZE_TYPE_4x2,
261         &need_of_event);
262   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
263 }
264
265 TEST_F(WidgetServiceTest, GetNeedOfFrame) {
266   int ret;
267   bool need_of_frame;
268
269   ret = widget_service_get_need_of_frame("org.tizen.test_widget", WIDGET_SIZE_TYPE_4x2,
270         &need_of_frame);
271   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
272 }
273
274 TEST_F(WidgetServiceTest, TriggerUpdate) {
275   int ret;
276
277   ret = widget_service_trigger_update("org.tizen.test_widget",
278         "org.tizen.test_widget", NULL, 1);
279   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
280 }
281
282 TEST_F(WidgetServiceTest, ChangePeriod) {
283   int ret;
284
285   ret = widget_service_change_period("org.tizen.test_widget",
286         "org.tizen.test_widget", 1.0);
287   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
288 }
289
290 int _widget_list_cb(const char *pkgid, const char *widget_id, int is_prime, void *user_data) {
291   return 0;
292 }
293
294 TEST_F(WidgetServiceTest, GetWidgetList) {
295   int ret;
296
297   ret = widget_service_get_widget_list(_widget_list_cb, NULL);
298   ASSERT_EQ(ret, 1);
299 }
300
301 TEST_F(WidgetServiceTest, GetMainAppId) {
302   char *app_id = NULL;
303
304   app_id = widget_service_get_main_app_id("org.tizen.test_widget");
305   ASSERT_STREQ(app_id, "org.tizen.mainappid");
306 }
307
308 int _widget_list_by_pkgid_cb(const char *widget_id, int is_prime, void *user_data) {
309   return 0;
310 }
311
312 TEST_F(WidgetServiceTest, GetWidgetListByPkgId) {
313   int ret;
314
315   ret = widget_service_get_widget_list_by_pkgid("org.tizen.test_widget", _widget_list_by_pkgid_cb, NULL);
316   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
317 }
318
319 TEST_F(WidgetServiceTest, GetWidgetId) {
320   char *widget_id = NULL;
321
322   widget_id = widget_service_get_widget_id("org.tizen.test_appid");
323   ASSERT_STREQ(widget_id, "org.tizen.test_widget");
324 }
325
326 TEST_F(WidgetServiceTest, GetAppIdOfSetupApp) {
327   char *app_id = NULL;
328
329   app_id = widget_service_get_app_id_of_setup_app("org.tizen.test_widget");
330   ASSERT_STREQ(app_id, "org.tizen.setup_appid");
331 }
332
333 TEST_F(WidgetServiceTest, GetPackageId) {
334   char *package_id = NULL;
335
336   package_id = widget_service_get_package_id("org.tizen.test_widget");
337   ASSERT_STREQ(package_id, "org.tizen.test_pkgid");
338 }
339
340 TEST_F(WidgetServiceTest, GetName) {
341   char *name = NULL;
342
343   name = widget_service_get_name("org.tizen.test_widget", "en-us");
344   ASSERT_STREQ(name, "TestWidget");
345 }
346
347 TEST_F(WidgetServiceTest, GetPreviewImagePath) {
348   char *path = NULL;
349
350   path = widget_service_get_preview_image_path("org.tizen.test_widget", WIDGET_SIZE_TYPE_4x2);
351   ASSERT_STREQ(path, "/unittest/preview.png");
352 }
353
354 TEST_F(WidgetServiceTest, GetIcon) {
355   char *path = NULL;
356
357   path = widget_service_get_icon("org.tizen.test_pkgid", "en-us");
358   ASSERT_STREQ(path, "/unittest/icon.png");
359 }
360
361 TEST_F(WidgetServiceTest, GetNodisplay) {
362   int ret = WIDGET_ERROR_NONE;
363
364   ret = widget_service_get_nodisplay("org.tizen.test_widget");
365   ASSERT_EQ(ret, 2);
366 }
367
368 TEST_F(WidgetServiceTest, GetSupportedSizes) {
369   int ret;
370   int cnt = 10;
371   int *w;
372   int *h;
373
374   ret = widget_service_get_supported_sizes("org.tizen.test_widget", &cnt, &w, &h);
375   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
376 }
377
378 TEST_F(WidgetServiceTest, GetSupportedSizeTypes) {
379   int ret;
380   int cnt = 10;
381   int *types;
382
383   ret = widget_service_get_supported_size_types("org.tizen.test_widget", &cnt, &types);
384   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
385 }
386
387 int _widget_instance_list_cb(const char *widget_id, const char *instance_id, void *user_data) {
388   return 0;
389 }
390
391 TEST_F(WidgetServiceTest, GetInstanceList) {
392   int ret;
393   struct instance_cb cb_data;
394
395   cb_data.widget_id = "widget_id";
396   cb_data.cb = NULL;
397   cb_data.data = NULL;
398   cb_data.cnt = 0;
399
400   ret = widget_service_get_widget_instance_list("org.tizen.test_widget",
401         _widget_instance_list_cb, &cb_data);
402   ASSERT_TRUE(ret > 0);
403 }
404
405 int _widget_lifecycle_event_cb(const char *widget_id, widget_lifecycle_event_e lifecycle_event,
406     const char *widget_instance_id, void *user_data) {
407   return 0;
408 }
409
410 TEST_F(WidgetServiceTest, SetLifecycleEventCb) {
411   int ret;
412
413   ret = widget_service_set_lifecycle_event_cb("org.tizen.test_widget",
414         _widget_lifecycle_event_cb, NULL);
415   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
416 }
417
418
419 TEST_F(WidgetServiceTest, UnsetLifecycleEventCb) {
420   int ret;
421
422   ret = widget_service_unset_lifecycle_event_cb("org.tizen.test_widget", NULL);
423   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
424 }
425
426 TEST_F(WidgetServiceTest, GetContent) {
427   int ret;
428   bundle *b;
429
430   ret = widget_service_get_content_of_widget_instance("org.tizen.test_widget", "org.tizen.test_widget", &b);
431   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
432 }
433
434 /* internal api */
435 TEST_F(WidgetServiceTest, GetWidgetMaxCount) {
436   int ret;
437
438   ret = widget_service_get_widget_max_count("org.tizen.test_widget");
439   ASSERT_EQ(ret, 3);
440 }
441
442 /* internal api */
443 TEST_F(WidgetServiceTest, GetInstanceCount) {
444   int ret;
445
446   ret = widget_service_get_instance_count("org.tizen.test_widget", NULL, NULL);
447   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
448 }
449
450 }