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