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"
35 int __fake_system_info_get_platform_bool(const char* key, bool* value) {
40 uid_t __fake_getuid(void) {
44 int __fake_aul_widget_service_set_disable(const char* widget_id, bool disabled) {
48 int __fake_cynara_initialize(cynara** cyn, const cynara_configuration* conf) {
49 return CYNARA_API_SUCCESS;
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;
57 int __fake_cynara_finish(cynara* cyn) {
61 int __fake_open(const char* path, int flag) {
65 ssize_t __fake_read(int fd, void* buf, size_t size) {
69 int __fake_chown(const char *pathname, uid_t owner, gid_t group) {
72 int __fake_chmod(const char *pathname, mode_t mode) {
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;
83 int __fake_aul_app_com_leave(aul_app_com_connection_h connection) {
87 int __fake_aul_widget_instance_update(const char *widget_id, const char *instance_id, bundle *b) {
91 int __fake_aul_widget_instance_get_content(const char *widget_id,
92 const char *instance_id, char **content) {
98 bundle_add_str(b, "testkey", "testvalue");
99 bundle_encode(b, &raw, &len);
101 *content = strdup((const char *)raw);
108 int __fake_aul_widget_instance_count(const char *widget_id) {
113 const char *widget_id;
114 widget_instance_list_cb cb;
119 struct widget_instance_info_s {
122 const char *instance_id;
125 int __fake_aul_widget_instance_foreach(const char *widget_id, aul_widget_instance_foreach_cb cb,
127 struct instance_cb *cb_data = (struct instance_cb *)data;
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;
138 const char *__fake_tzplatform_mkpath(enum tzplatform_variable id, const char *path) {
139 return get_db_path();
142 int __fake_pkgmgrinfo_pkginfo_get_usr_pkginfo(const char *pkgid,
143 uid_t uid, pkgmgrinfo_pkginfo_h *handle) {
147 int __fake_pkgmgrinfo_pkginfo_get_mainappid(pkgmgrinfo_pkginfo_h handle, char **mainappid) {
148 *mainappid = (char *)"org.tizen.mainappid";
152 int __fake_pkgmgrinfo_pkginfo_destroy_pkginfo(pkgmgrinfo_pkginfo_h handle) {
156 class WidgetServiceTest : public ::testing::Test {
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;
180 virtual void TearDown() {
185 TEST_F(WidgetServiceTest, CheckDBIntegrity) {
189 ret = widget_service_check_db_integrity(false);
190 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
192 // insert default data
193 insert_default_data();
196 TEST_F(WidgetServiceTest, SetDisabled) {
199 ret = widget_service_set_widget_disabled("org.tizen.test_widget", true);
200 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
203 TEST_F(WidgetServiceTest, GetDisabled) {
204 int ret = WIDGET_ERROR_NONE;
207 ret = widget_service_get_widget_disabled("org.tizen.test_widget",
209 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
212 void _widget_disable_event_cb(const char *widget_id, bool is_disabled, void *user_data) {
215 TEST_F(WidgetServiceTest, SetDisabledEventCallback) {
218 ret = widget_service_set_disable_event_cb(_widget_disable_event_cb, NULL);
219 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
222 TEST_F(WidgetServiceTest, UnsetDisabledEventCallback) {
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);
230 TEST_F(WidgetServiceTest, GetSize) {
234 ret = widget_service_get_size(WIDGET_SIZE_TYPE_4x4, &width, &height);
235 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
238 TEST_F(WidgetServiceTest, GetSizeType) {
240 widget_size_type_e type;
242 ret = widget_service_get_size_type(175, 175, &type);
243 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
246 TEST_F(WidgetServiceTest, GetNeedOfMouseEvent) {
250 ret = widget_service_get_need_of_mouse_event("org.tizen.test_widget", WIDGET_SIZE_TYPE_4x2,
252 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
255 TEST_F(WidgetServiceTest, GetNeedOfTouchEffect) {
259 ret = widget_service_get_need_of_touch_effect("org.tizen.test_widget", WIDGET_SIZE_TYPE_4x2,
261 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
264 TEST_F(WidgetServiceTest, GetNeedOfFrame) {
268 ret = widget_service_get_need_of_frame("org.tizen.test_widget", WIDGET_SIZE_TYPE_4x2,
270 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
273 TEST_F(WidgetServiceTest, TriggerUpdate) {
276 ret = widget_service_trigger_update("org.tizen.test_widget",
277 "org.tizen.test_widget", NULL, 1);
278 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
281 TEST_F(WidgetServiceTest, ChangePeriod) {
284 ret = widget_service_change_period("org.tizen.test_widget",
285 "org.tizen.test_widget", 1.0);
286 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
289 int _widget_list_cb(const char *pkgid, const char *widget_id, int is_prime, void *user_data) {
293 TEST_F(WidgetServiceTest, GetWidgetList) {
296 ret = widget_service_get_widget_list(_widget_list_cb, NULL);
300 TEST_F(WidgetServiceTest, GetMainAppId) {
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");
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 auto p = std::unique_ptr<char, decltype(std::free)*>(widget_id, std::free);
324 ASSERT_STREQ(p.get(), "org.tizen.test_widget");
327 TEST_F(WidgetServiceTest, GetAppIdOfSetupApp) {
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");
335 TEST_F(WidgetServiceTest, GetPackageId) {
336 char *package_id = NULL;
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");
343 TEST_F(WidgetServiceTest, GetName) {
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");
351 TEST_F(WidgetServiceTest, GetPreviewImagePath) {
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");
360 TEST_F(WidgetServiceTest, GetIcon) {
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");
368 TEST_F(WidgetServiceTest, GetNodisplay) {
369 int ret = WIDGET_ERROR_NONE;
371 ret = widget_service_get_nodisplay("org.tizen.test_widget");
375 TEST_F(WidgetServiceTest, GetSupportedSizes) {
381 ret = widget_service_get_supported_sizes("org.tizen.test_widget", &cnt, &w, &h);
382 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
385 TEST_F(WidgetServiceTest, GetSupportedSizeTypes) {
390 ret = widget_service_get_supported_size_types("org.tizen.test_widget", &cnt, &types);
391 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
394 int _widget_instance_list_cb(const char *widget_id, const char *instance_id, void *user_data) {
398 TEST_F(WidgetServiceTest, GetInstanceList) {
400 struct instance_cb cb_data;
402 cb_data.widget_id = "widget_id";
407 ret = widget_service_get_widget_instance_list("org.tizen.test_widget",
408 _widget_instance_list_cb, &cb_data);
409 ASSERT_TRUE(ret > 0);
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) {
417 TEST_F(WidgetServiceTest, SetLifecycleEventCb) {
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);
426 TEST_F(WidgetServiceTest, UnsetLifecycleEventCb) {
429 ret = widget_service_unset_lifecycle_event_cb("org.tizen.test_widget", NULL);
430 ASSERT_EQ(ret, WIDGET_ERROR_NONE);
433 TEST_F(WidgetServiceTest, GetContent) {
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);
444 TEST_F(WidgetServiceTest, GetWidgetMaxCount) {
447 ret = widget_service_get_widget_max_count("org.tizen.test_widget");
452 TEST_F(WidgetServiceTest, GetInstanceCount) {
455 ret = widget_service_get_instance_count("org.tizen.test_widget", NULL, NULL);
456 ASSERT_EQ(ret, WIDGET_ERROR_NONE);