Add new unittest TCs
[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 "unittest/mock/system_info_mock.h"
26 #include "unittest/mock/aul_mock.h"
27 #include "unittest/mock/cynara_mock.h"
28 #include "unittest/mock/fcntl_mock.h"
29 #include "unittest/mock/tzplatform_config_mock.h"
30 #include "unittest/mock/db_mock.h"
31 #include "unittest/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 class WidgetServiceTest : public ::testing::Test {
154   public:
155     virtual void SetUp() {
156       system_info_get_platform_bool_fake.custom_fake = __fake_system_info_get_platform_bool;
157       aul_widget_service_set_disable_fake.custom_fake = __fake_aul_widget_service_set_disable;
158       cynara_initialize_fake.custom_fake = __fake_cynara_initialize;
159       cynara_check_fake.custom_fake = __fake_cynara_check;
160       cynara_finish_fake.custom_fake = __fake_cynara_finish;
161       open_fake.custom_fake = __fake_open;
162       read_fake.custom_fake = __fake_read;
163       chmod_fake.custom_fake = __fake_chmod;
164       chown_fake.custom_fake = __fake_chown;
165       aul_app_com_create_fake.custom_fake = __fake_aul_app_com_create;
166       aul_app_com_leave_fake.custom_fake = __fake_aul_app_com_leave;
167       aul_widget_instance_update_fake.custom_fake = __fake_aul_widget_instance_update;
168       aul_widget_instance_foreach_fake.custom_fake = __fake_aul_widget_instance_foreach;
169       aul_widget_instance_get_content_fake.custom_fake = __fake_aul_widget_instance_get_content;
170       aul_widget_instance_count_fake.custom_fake = __fake_aul_widget_instance_count;
171       tzplatform_mkpath_fake.custom_fake = __fake_tzplatform_mkpath;
172       getuid_fake.custom_fake = __fake_getuid;
173       pkgmgrinfo_pkginfo_get_usr_pkginfo_fake.custom_fake = __fake_pkgmgrinfo_pkginfo_get_usr_pkginfo;
174       pkgmgrinfo_pkginfo_get_mainappid_fake.custom_fake = __fake_pkgmgrinfo_pkginfo_get_mainappid;
175     }
176     virtual void TearDown() {
177     }
178 };
179
180 /* internal api */
181 TEST_F(WidgetServiceTest, CheckDBIntegrity) {
182   int ret;
183
184   // create db
185   ret = widget_service_check_db_integrity(false);
186   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
187
188   // insert default data
189   insert_default_data();
190 }
191
192 TEST_F(WidgetServiceTest, SetDisabled) {
193   int ret;
194
195   ret = widget_service_set_widget_disabled("org.tizen.test_widget", true);
196   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
197 }
198
199 TEST_F(WidgetServiceTest, GetDisabled) {
200   int ret = WIDGET_ERROR_NONE;
201   bool is_disabled;
202
203   ret = widget_service_get_widget_disabled("org.tizen.test_widget",
204       &is_disabled);
205   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
206 }
207
208 void _widget_disable_event_cb(const char *widget_id, bool is_disabled, void *user_data) {
209 }
210
211 TEST_F(WidgetServiceTest, SetDisabledEventCallback) {
212   int ret;
213
214   ret = widget_service_set_disable_event_cb(_widget_disable_event_cb, NULL);
215   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
216 }
217
218 TEST_F(WidgetServiceTest, UnsetDisabledEventCallback) {
219   int ret;
220
221   ret = widget_service_set_disable_event_cb(_widget_disable_event_cb, NULL);
222   ret = widget_service_unset_disable_event_cb();
223   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
224 }
225
226 TEST_F(WidgetServiceTest, GetSize) {
227   int ret;
228   int width, height;
229
230   ret = widget_service_get_size(WIDGET_SIZE_TYPE_4x4, &width, &height);
231   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
232 }
233
234 TEST_F(WidgetServiceTest, GetSizeType) {
235   int ret;
236   widget_size_type_e type;
237
238   ret = widget_service_get_size_type(175, 175, &type);
239   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
240 }
241
242 TEST_F(WidgetServiceTest, GetNeedOfMouseEvent) {
243   int ret;
244   bool need_of_event;
245
246   ret = widget_service_get_need_of_mouse_event("org.tizen.test_widget", WIDGET_SIZE_TYPE_4x2,
247         &need_of_event);
248   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
249 }
250
251 TEST_F(WidgetServiceTest, GetNeedOfTouchEffect) {
252   int ret;
253   bool need_of_event;
254
255   ret = widget_service_get_need_of_touch_effect("org.tizen.test_widget", WIDGET_SIZE_TYPE_4x2,
256         &need_of_event);
257   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
258 }
259
260 TEST_F(WidgetServiceTest, GetNeedOfFrame) {
261   int ret;
262   bool need_of_frame;
263
264   ret = widget_service_get_need_of_frame("org.tizen.test_widget", WIDGET_SIZE_TYPE_4x2,
265         &need_of_frame);
266   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
267 }
268
269 TEST_F(WidgetServiceTest, TriggerUpdate) {
270   int ret;
271
272   ret = widget_service_trigger_update("org.tizen.test_widget",
273         "org.tizen.test_widget", NULL, 1);
274   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
275 }
276
277 TEST_F(WidgetServiceTest, ChangePeriod) {
278   int ret;
279
280   ret = widget_service_change_period("org.tizen.test_widget",
281         "org.tizen.test_widget", 1.0);
282   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
283 }
284
285 int _widget_list_cb(const char *pkgid, const char *widget_id, int is_prime, void *user_data) {
286   return 0;
287 }
288
289 TEST_F(WidgetServiceTest, GetWidgetList) {
290   int ret;
291
292   ret = widget_service_get_widget_list(_widget_list_cb, NULL);
293   ASSERT_EQ(ret, 1);
294 }
295
296 TEST_F(WidgetServiceTest, GetMainAppId) {
297   char *app_id = NULL;
298
299   app_id = widget_service_get_main_app_id("org.tizen.test_widget");
300   ASSERT_STREQ(app_id, "org.tizen.mainappid");
301 }
302
303 int _widget_list_by_pkgid_cb(const char *widget_id, int is_prime, void *user_data) {
304   return 0;
305 }
306
307 TEST_F(WidgetServiceTest, GetWidgetListByPkgId) {
308   int ret;
309
310   ret = widget_service_get_widget_list_by_pkgid("org.tizen.test_widget", _widget_list_by_pkgid_cb, NULL);
311   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
312 }
313
314 TEST_F(WidgetServiceTest, GetWidgetId) {
315   char *widget_id = NULL;
316
317   widget_id = widget_service_get_widget_id("org.tizen.test_appid");
318   ASSERT_STREQ(widget_id, "org.tizen.test_widget");
319 }
320
321 TEST_F(WidgetServiceTest, GetAppIdOfSetupApp) {
322   char *app_id = NULL;
323
324   app_id = widget_service_get_app_id_of_setup_app("org.tizen.test_widget");
325   ASSERT_STREQ(app_id, "org.tizen.setup_appid");
326 }
327
328 TEST_F(WidgetServiceTest, GetPackageId) {
329   char *package_id = NULL;
330
331   package_id = widget_service_get_package_id("org.tizen.test_widget");
332   ASSERT_STREQ(package_id, "org.tizen.test_pkgid");
333 }
334
335 TEST_F(WidgetServiceTest, GetName) {
336   char *name = NULL;
337
338   name = widget_service_get_name("org.tizen.test_widget", "en-us");
339   ASSERT_STREQ(name, "TestWidget");
340 }
341
342 TEST_F(WidgetServiceTest, GetPreviewImagePath) {
343   char *path = NULL;
344
345   path = widget_service_get_preview_image_path("org.tizen.test_widget", WIDGET_SIZE_TYPE_4x2);
346   ASSERT_STREQ(path, "/unittest/preview.png");
347 }
348
349 TEST_F(WidgetServiceTest, GetIcon) {
350   char *path = NULL;
351
352   path = widget_service_get_icon("org.tizen.test_pkgid", "en-us");
353   ASSERT_STREQ(path, "/unittest/icon.png");
354 }
355
356 TEST_F(WidgetServiceTest, GetNodisplay) {
357   int ret = WIDGET_ERROR_NONE;
358
359   ret = widget_service_get_nodisplay("org.tizen.test_widget");
360   ASSERT_EQ(ret, 2);
361 }
362
363 TEST_F(WidgetServiceTest, GetSupportedSizes) {
364   int ret;
365   int cnt = 10;
366   int *w;
367   int *h;
368
369   ret = widget_service_get_supported_sizes("org.tizen.test_widget", &cnt, &w, &h);
370   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
371 }
372
373 TEST_F(WidgetServiceTest, GetSupportedSizeTypes) {
374   int ret;
375   int cnt = 10;
376   int *types;
377
378   ret = widget_service_get_supported_size_types("org.tizen.test_widget", &cnt, &types);
379   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
380 }
381
382 int _widget_instance_list_cb(const char *widget_id, const char *instance_id, void *user_data) {
383   return 0;
384 }
385
386 TEST_F(WidgetServiceTest, GetInstanceList) {
387   int ret;
388   struct instance_cb cb_data;
389
390   cb_data.widget_id = "widget_id";
391   cb_data.cb = NULL;
392   cb_data.data = NULL;
393   cb_data.cnt = 0;
394
395   ret = widget_service_get_widget_instance_list("org.tizen.test_widget",
396         _widget_instance_list_cb, &cb_data);
397   ASSERT_TRUE(ret > 0);
398 }
399
400 int _widget_lifecycle_event_cb(const char *widget_id, widget_lifecycle_event_e lifecycle_event,
401     const char *widget_instance_id, void *user_data) {
402   return 0;
403 }
404
405 TEST_F(WidgetServiceTest, SetLifecycleEventCb) {
406   int ret;
407
408   ret = widget_service_set_lifecycle_event_cb("org.tizen.test_widget",
409         _widget_lifecycle_event_cb, NULL);
410   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
411 }
412
413
414 TEST_F(WidgetServiceTest, UnsetLifecycleEventCb) {
415   int ret;
416
417   ret = widget_service_unset_lifecycle_event_cb("org.tizen.test_widget", NULL);
418   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
419 }
420
421 TEST_F(WidgetServiceTest, GetContent) {
422   int ret;
423   bundle *b;
424
425   ret = widget_service_get_content_of_widget_instance("org.tizen.test_widget", "org.tizen.test_widget", &b);
426   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
427 }
428
429 /* internal api */
430 TEST_F(WidgetServiceTest, GetWidgetMaxCount) {
431   int ret;
432
433   ret = widget_service_get_widget_max_count("org.tizen.test_widget");
434   ASSERT_EQ(ret, 3);
435 }
436
437 /* internal api */
438 TEST_F(WidgetServiceTest, GetInstanceCount) {
439   int ret;
440
441   ret = widget_service_get_instance_count("org.tizen.test_widget", NULL, NULL);
442   ASSERT_EQ(ret, WIDGET_ERROR_NONE);
443 }
444
445 }