2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
18 #include <gmock/gmock.h>
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"
34 int __fake_system_info_get_platform_bool(const char* key, bool* value) {
39 uid_t __fake_getuid(void) {
43 int __fake_aul_widget_service_set_disable(const char* widget_id, bool disabled) {
47 int __fake_cynara_initialize(cynara** cyn, const cynara_configuration* conf) {
48 return CYNARA_API_SUCCESS;
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;
56 int __fake_cynara_finish(cynara* cyn) {
60 int __fake_open(const char* path, int flag) {
64 ssize_t __fake_read(int fd, void* buf, size_t size) {
68 int __fake_chown(const char *pathname, uid_t owner, gid_t group) {
71 int __fake_chmod(const char *pathname, mode_t mode) {
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;
79 conn = (aul_app_com_connection_h *)malloc(sizeof(connection));
85 int __fake_aul_app_com_leave(aul_app_com_connection_h connection) {
89 int __fake_aul_widget_instance_update(const char *widget_id, const char *instance_id, bundle *b) {
93 int __fake_aul_widget_instance_get_content(const char *widget_id,
94 const char *instance_id, char **content) {
100 bundle_add_str(b, "testkey", "testvalue");
101 bundle_encode(b, &raw, &len);
103 *content = strdup((const char *)raw);
109 int __fake_aul_widget_instance_count(const char *widget_id) {
114 const char *widget_id;
115 widget_instance_list_cb cb;
120 struct widget_instance_info_s {
123 const char *instance_id;
126 int __fake_aul_widget_instance_foreach(const char *widget_id, aul_widget_instance_foreach_cb cb,
128 struct instance_cb *cb_data = (struct instance_cb *)data;
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;
139 const char *__fake_tzplatform_mkpath(enum tzplatform_variable id, const char *path) {
140 return get_db_path();
143 int __fake_pkgmgrinfo_pkginfo_get_usr_pkginfo(const char *pkgid,
144 uid_t uid, pkgmgrinfo_pkginfo_h *handle) {
148 int __fake_pkgmgrinfo_pkginfo_get_mainappid(pkgmgrinfo_pkginfo_h handle, char **mainappid) {
149 *mainappid = (char *)"org.tizen.mainappid";
153 int __fake_pkgmgrinfo_pkginfo_destroy_pkginfo(pkgmgrinfo_pkginfo_h handle) {
157 class WidgetServiceTest : public ::testing::Test {
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;
181 virtual void TearDown() {
186 TEST_F(WidgetServiceTest, CheckDBIntegrity) {
190 ret = widget_service_check_db_integrity(false);
191 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
193 // insert default data
194 insert_default_data();
197 TEST_F(WidgetServiceTest, SetDisabled) {
200 ret = widget_service_set_widget_disabled("org.tizen.test_widget", true);
201 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
204 TEST_F(WidgetServiceTest, GetDisabled) {
205 int ret = WIDGET_ERROR_NONE;
208 ret = widget_service_get_widget_disabled("org.tizen.test_widget",
210 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
213 void _widget_disable_event_cb(const char *widget_id, bool is_disabled, void *user_data) {
216 TEST_F(WidgetServiceTest, SetDisabledEventCallback) {
219 ret = widget_service_set_disable_event_cb(_widget_disable_event_cb, NULL);
220 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
223 TEST_F(WidgetServiceTest, UnsetDisabledEventCallback) {
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);
231 TEST_F(WidgetServiceTest, GetSize) {
235 ret = widget_service_get_size(WIDGET_SIZE_TYPE_4x4, &width, &height);
236 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
239 TEST_F(WidgetServiceTest, GetSizeType) {
241 widget_size_type_e type;
243 ret = widget_service_get_size_type(175, 175, &type);
244 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
247 TEST_F(WidgetServiceTest, GetNeedOfMouseEvent) {
251 ret = widget_service_get_need_of_mouse_event("org.tizen.test_widget", WIDGET_SIZE_TYPE_4x2,
253 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
256 TEST_F(WidgetServiceTest, GetNeedOfTouchEffect) {
260 ret = widget_service_get_need_of_touch_effect("org.tizen.test_widget", WIDGET_SIZE_TYPE_4x2,
262 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
265 TEST_F(WidgetServiceTest, GetNeedOfFrame) {
269 ret = widget_service_get_need_of_frame("org.tizen.test_widget", WIDGET_SIZE_TYPE_4x2,
271 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
274 TEST_F(WidgetServiceTest, TriggerUpdate) {
277 ret = widget_service_trigger_update("org.tizen.test_widget",
278 "org.tizen.test_widget", NULL, 1);
279 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
282 TEST_F(WidgetServiceTest, ChangePeriod) {
285 ret = widget_service_change_period("org.tizen.test_widget",
286 "org.tizen.test_widget", 1.0);
287 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
290 int _widget_list_cb(const char *pkgid, const char *widget_id, int is_prime, void *user_data) {
294 TEST_F(WidgetServiceTest, GetWidgetList) {
297 ret = widget_service_get_widget_list(_widget_list_cb, NULL);
301 TEST_F(WidgetServiceTest, GetMainAppId) {
304 app_id = widget_service_get_main_app_id("org.tizen.test_widget");
305 ASSERT_STREQ(app_id, "org.tizen.mainappid");
308 int _widget_list_by_pkgid_cb(const char *widget_id, int is_prime, void *user_data) {
312 TEST_F(WidgetServiceTest, GetWidgetListByPkgId) {
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);
319 TEST_F(WidgetServiceTest, GetWidgetId) {
320 char *widget_id = NULL;
322 widget_id = widget_service_get_widget_id("org.tizen.test_appid");
323 ASSERT_STREQ(widget_id, "org.tizen.test_widget");
326 TEST_F(WidgetServiceTest, GetAppIdOfSetupApp) {
329 app_id = widget_service_get_app_id_of_setup_app("org.tizen.test_widget");
330 ASSERT_STREQ(app_id, "org.tizen.setup_appid");
333 TEST_F(WidgetServiceTest, GetPackageId) {
334 char *package_id = NULL;
336 package_id = widget_service_get_package_id("org.tizen.test_widget");
337 ASSERT_STREQ(package_id, "org.tizen.test_pkgid");
340 TEST_F(WidgetServiceTest, GetName) {
343 name = widget_service_get_name("org.tizen.test_widget", "en-us");
344 ASSERT_STREQ(name, "TestWidget");
347 TEST_F(WidgetServiceTest, GetPreviewImagePath) {
350 path = widget_service_get_preview_image_path("org.tizen.test_widget", WIDGET_SIZE_TYPE_4x2);
351 ASSERT_STREQ(path, "/unittest/preview.png");
354 TEST_F(WidgetServiceTest, GetIcon) {
357 path = widget_service_get_icon("org.tizen.test_pkgid", "en-us");
358 ASSERT_STREQ(path, "/unittest/icon.png");
361 TEST_F(WidgetServiceTest, GetNodisplay) {
362 int ret = WIDGET_ERROR_NONE;
364 ret = widget_service_get_nodisplay("org.tizen.test_widget");
368 TEST_F(WidgetServiceTest, GetSupportedSizes) {
374 ret = widget_service_get_supported_sizes("org.tizen.test_widget", &cnt, &w, &h);
375 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
378 TEST_F(WidgetServiceTest, GetSupportedSizeTypes) {
383 ret = widget_service_get_supported_size_types("org.tizen.test_widget", &cnt, &types);
384 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
387 int _widget_instance_list_cb(const char *widget_id, const char *instance_id, void *user_data) {
391 TEST_F(WidgetServiceTest, GetInstanceList) {
393 struct instance_cb cb_data;
395 cb_data.widget_id = "widget_id";
400 ret = widget_service_get_widget_instance_list("org.tizen.test_widget",
401 _widget_instance_list_cb, &cb_data);
402 ASSERT_TRUE(ret > 0);
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) {
410 TEST_F(WidgetServiceTest, SetLifecycleEventCb) {
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);
419 TEST_F(WidgetServiceTest, UnsetLifecycleEventCb) {
422 ret = widget_service_unset_lifecycle_event_cb("org.tizen.test_widget", NULL);
423 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
426 TEST_F(WidgetServiceTest, GetContent) {
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);
435 TEST_F(WidgetServiceTest, GetWidgetMaxCount) {
438 ret = widget_service_get_widget_max_count("org.tizen.test_widget");
443 TEST_F(WidgetServiceTest, GetInstanceCount) {
446 ret = widget_service_get_instance_count("org.tizen.test_widget", NULL, NULL);
447 ASSERT_EQ(ret, WIDGET_ERROR_NONE);