2 * Copyright (c) 2020 - 2021 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 <bundle_cpp.h>
18 #include <bundle_internal.h>
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
26 #include "include/widget_app.h"
27 #include "include/widget_app_efl.h"
28 #include "unit_tests/mock/app_common_mock.h"
29 #include "unit_tests/mock/appcore_multiwindow_base_mock.h"
30 #include "unit_tests/mock/aul_mock.h"
31 #include "unit_tests/mock/ecore_wl2_mock.h"
32 #include "unit_tests/mock/elm_mock.h"
33 #include "unit_tests/mock/gio_mock.h"
34 #include "unit_tests/mock/system_info_mock.h"
35 #include "unit_tests/mock/test_fixture.h"
36 #include "unit_tests/mock/widget_service_mock.h"
39 using ::testing::DoAll;
40 using ::testing::Return;
41 using ::testing::SetArgPointee;
42 using ::testing::Invoke;
46 int __instance_create_cb(widget_context_h context, bundle* content,
47 int w, int h, void* user_data) {
51 int __instance_destroy_cb(widget_context_h context,
52 widget_app_destroy_type_e reason, bundle* content, void* user_data) {
56 int __instance_pause_cb(widget_context_h context, void* user_data) {
60 int __instance_resume_cb(widget_context_h context, void* user_data) {
64 int __instance_resize_cb(widget_context_h context,
65 int w, int h, void* user_data) {
69 int __instance_update_cb(widget_context_h context,
70 bundle* content, int force, void* user_data) {
74 void __app_terminate_cb(void* user_data) {
77 int __app_get_id_fake(char** id) {
82 int __aul_app_get_appid_bypid_fake(int pid, char* appid, int len) {
83 snprintf(appid, len, "%s", "test");
87 int __aul_app_get_pkgid_bypid_fake(int pid, char* pkgid, int len) {
88 snprintf(pkgid, len, "%s", "test");
92 void __app_event_cb(app_event_info_h event_info, void *user_data) {
95 bool __widget_context_cb(widget_context_h context, void *user_data) {
101 class Mocks : public ::testing::NiceMock<GioMock>,
102 public ::testing::NiceMock<MultiWindowBaseMock>,
103 public ::testing::NiceMock<AppCommonMock>,
104 public ::testing::NiceMock<WidgetServiceMock>,
105 public ::testing::NiceMock<ElmMock>,
106 public ::testing::NiceMock<AulMock>,
107 public ::testing::NiceMock<SystemInfoMock>,
108 public ::testing::NiceMock<EcoreWl2Mock> {};
110 class WidgetAppTest : public TestFixture {
112 WidgetAppTest() : TestFixture(std::make_unique<Mocks>()) {}
113 virtual ~WidgetAppTest() {}
115 virtual void SetUp() {
118 virtual void TearDown() {
121 void PrepareTest(widget_app_create_cb created_cb, void* data) {
122 tizen_base::Bundle b;
123 b.Add("__AUL_WIDGET_VIEWER__", "test");
124 int ret = bundle_add_str(b.GetHandle(), "KEY", "VALUE");
125 ASSERT_EQ(ret, BUNDLE_ERROR_NONE);
127 char** argv = nullptr;
128 int argc = bundle_export_to_argv(b.GetHandle(), &argv);
129 ASSERT_EQ(get_last_result(), BUNDLE_ERROR_NONE);
130 ASSERT_NE(argv, nullptr);
133 widget_app_lifecycle_callback_s callback;
134 callback.create = created_cb;
135 callback.terminate = __app_terminate_cb;
137 EXPECT_CALL(GetMock<SystemInfoMock>(),
138 system_info_get_platform_bool(_, _)).
139 WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(0)));
141 EXPECT_CALL(GetMock<AulMock>(),
142 aul_app_get_appid_bypid(_, _, _)).
143 WillOnce(Invoke(__aul_app_get_appid_bypid_fake));
144 EXPECT_CALL(GetMock<AulMock>(),
145 aul_app_get_pkgid_bypid(_, _, _)).
146 WillOnce(Invoke(__aul_app_get_pkgid_bypid_fake));
147 EXPECT_CALL(GetMock<AulMock>(),
148 aul_widget_send_status_to_service(_, _, _, _)).
149 WillRepeatedly(Return(0));
150 EXPECT_CALL(GetMock<AulMock>(),
151 aul_widget_send_status_to_viewer(_, _, _, _, _, _)).
152 WillRepeatedly(Return(0));
153 EXPECT_CALL(GetMock<WidgetServiceMock>(),
154 widget_instance_convert_event_to_lifecycle_status(_)).
155 WillRepeatedly(Return(0));
157 EXPECT_CALL(GetMock<AppCommonMock>(),
158 app_get_id(_)).WillRepeatedly(Invoke(__app_get_id_fake));
160 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
161 Run(_, _)).WillOnce(Invoke(this, &WidgetAppTest::__run_fake));
163 ret = widget_app_main(argc, argv, &callback, data);
164 bundle_free_exported_argv(argc, &argv);
165 EXPECT_EQ(ret, WIDGET_ERROR_NONE);
168 void PrepareContext(widget_instance_create_cb created_cb) {
169 PrepareTest([](void* user_data) -> widget_class_h {
170 widget_instance_lifecycle_callback_s lifecycle;
171 lifecycle.create = reinterpret_cast<widget_instance_create_cb>(user_data);
172 lifecycle.destroy = __instance_destroy_cb;
173 lifecycle.pause = __instance_pause_cb;
174 lifecycle.resume = __instance_resume_cb;
175 lifecycle.resize = __instance_resize_cb;
176 lifecycle.update = __instance_update_cb;
177 widget_class_h cls = widget_app_class_create(lifecycle, nullptr);
178 EXPECT_NE(cls, nullptr);
180 }, reinterpret_cast<void*>(created_cb));
183 void __run_fake(int argc, char** argv) {
184 tizen_base::Bundle b;
185 auto* p = GetMock<MultiWindowBaseMock>().real_;
189 b.Add("__WIDGET_WIDTH__", "320");
190 b.Add("__WIDGET_HEIGHT__", "320");
191 b.Add("__AUL_WIDGET_INSTANCE_ID__", "instance_id");
192 b.Add("__AUL_WIDGET_ID__", "test");
193 b.Add("__WIDGET_OP__", "create");
196 b.Delete("__WIDGET_OP__");
197 b.Add("__WIDGET_OP__", "resize");
200 b.Delete("__WIDGET_OP__");
201 b.Add("__WIDGET_OP__", "update");
204 b.Delete("__WIDGET_OP__");
205 b.Add("__WIDGET_OP__", "destroy");
208 b.Delete("__WIDGET_OP__");
209 b.Add("__WIDGET_OP__", "resume");
212 b.Delete("__WIDGET_OP__");
213 b.Add("__WIDGET_OP__", "pause");
216 b.Delete("__WIDGET_OP__");
217 b.Add("__WIDGET_OP__", "terminate");
220 b.Delete("__WIDGET_OP__");
221 b.Add("__WIDGET_OP__", "period");
228 TEST_F(WidgetAppTest, widget_app_exit_n) {
229 int ret = widget_app_exit();
230 EXPECT_NE(ret, WIDGET_ERROR_NONE);
233 TEST_F(WidgetAppTest, widget_app_exit) {
235 [](widget_context_h context, bundle* content,
236 int w, int h, void* user_data) -> int {
237 EXPECT_CALL(GetMock<AulMock>(),
238 aul_notify_exit()).WillRepeatedly(Return(0));
239 int ret = widget_app_exit();
240 EXPECT_EQ(ret, WIDGET_ERROR_NONE);
245 TEST_F(WidgetAppTest, widget_app_main) {
247 [](widget_context_h context, bundle* content,
248 int w, int h, void* user_data) -> int {
249 EXPECT_CALL(GetMock<AulMock>(),
250 aul_notify_exit()).WillRepeatedly(Return(0));
256 TEST_F(WidgetAppTest, widget_app_add_event_handler) {
257 app_event_handler_h handle;
258 EXPECT_CALL(GetMock<SystemInfoMock>(),
259 system_info_get_platform_bool(_, _)).
261 SetArgPointee<1>(true),
263 int ret = widget_app_add_event_handler(&handle, APP_EVENT_LOW_MEMORY,
264 __app_event_cb, nullptr);
265 EXPECT_EQ(ret, WIDGET_ERROR_NONE);
268 TEST_F(WidgetAppTest, widget_app_remove_event_handler) {
269 app_event_handler_h handle;
270 EXPECT_CALL(GetMock<SystemInfoMock>(),
271 system_info_get_platform_bool(_, _)).
272 WillRepeatedly(DoAll(
273 SetArgPointee<1>(true),
275 int ret = widget_app_add_event_handler(&handle, APP_EVENT_LOW_MEMORY,
276 __app_event_cb, nullptr);
277 ret = widget_app_remove_event_handler(handle);
278 EXPECT_EQ(ret, WIDGET_ERROR_NONE);
281 TEST_F(WidgetAppTest, widget_app_terminate_context) {
282 PrepareContext([](widget_context_h context, bundle* content,
283 int w, int h, void* user_data) -> int {
284 int ret = widget_app_terminate_context(context);
285 EXPECT_EQ(ret, WIDGET_ERROR_NONE);
290 TEST_F(WidgetAppTest, widget_app_foreach_context) {
291 PrepareTest([](void* user_data) -> widget_class_h {
292 widget_instance_lifecycle_callback_s lifecycle;
293 lifecycle.create = __instance_create_cb;
294 lifecycle.destroy = __instance_destroy_cb;
295 lifecycle.pause = __instance_pause_cb;
296 lifecycle.resume = __instance_resume_cb;
297 lifecycle.resize = __instance_resize_cb;
298 lifecycle.update = __instance_update_cb;
300 widget_class_h cls = widget_app_class_create(lifecycle, nullptr);
301 EXPECT_NE(cls, nullptr);
303 int ret = widget_app_foreach_context(__widget_context_cb, nullptr);
304 EXPECT_EQ(ret, WIDGET_ERROR_NONE);
310 TEST_F(WidgetAppTest, widget_app_get_id) {
312 [](widget_context_h context, bundle* content,
313 int w, int h, void* user_data) -> int {
314 const char *id = widget_app_get_id(context);
315 EXPECT_STREQ(id, "instance_id");
320 TEST_F(WidgetAppTest, widget_app_class_create) {
321 PrepareContext(__instance_create_cb);
324 TEST_F(WidgetAppTest, widget_app_context_set_tag) {
326 [](widget_context_h context, bundle* content,
327 int w, int h, void* user_data) -> int {
328 int ret = widget_app_context_set_tag(context,
329 reinterpret_cast<void*>(const_cast<char*>("test")));
330 EXPECT_EQ(ret, WIDGET_ERROR_NONE);
335 TEST_F(WidgetAppTest, widget_app_context_get_tag) {
337 [](widget_context_h context, bundle* content,
338 int w, int h, void* user_data) -> int {
339 int ret = widget_app_context_set_tag(context,
340 reinterpret_cast<void*>(const_cast<char*>("test")));
341 EXPECT_EQ(ret, WIDGET_ERROR_NONE);
343 ret = widget_app_context_get_tag(context, &tag);
344 EXPECT_EQ(ret, WIDGET_ERROR_NONE);
349 TEST_F(WidgetAppTest, widget_app_context_set_content_info) {
351 [](widget_context_h context, bundle* content,
352 int w, int h, void* user_data) -> int {
353 bundle* content_info = bundle_create();
354 EXPECT_NE(content_info, nullptr);
355 int ret = widget_app_context_set_content_info(context, content_info);
356 bundle_free(content_info);
357 EXPECT_EQ(ret, WIDGET_ERROR_NONE);
362 TEST_F(WidgetAppTest, widget_app_context_set_title) {
364 [](widget_context_h context, bundle* content,
365 int w, int h, void* user_data) -> int {
366 int ret = widget_app_context_set_title(context, "title");
367 EXPECT_EQ(ret, WIDGET_ERROR_NONE);
372 TEST_F(WidgetAppTest, widget_app_class_add) {
374 [](void* user_data) -> widget_class_h {
375 widget_instance_lifecycle_callback_s lifecycle;
376 lifecycle.create = __instance_create_cb;
377 lifecycle.destroy = __instance_destroy_cb;
378 lifecycle.pause = __instance_pause_cb;
379 lifecycle.resume = __instance_resume_cb;
380 lifecycle.resize = __instance_resize_cb;
381 lifecycle.update = __instance_update_cb;
383 widget_class_h cls = widget_app_class_add(
384 nullptr, "test", lifecycle, nullptr);
385 EXPECT_NE(cls, nullptr);
390 TEST_F(WidgetAppTest, widget_app_get_elm_win_negative) {
392 [](widget_context_h context, bundle* content,
393 int w, int h, void* user_data) -> int {
394 Evas_Object* win = nullptr;
395 int ret = widget_app_get_elm_win(context, &win);
396 EXPECT_EQ(ret, WIDGET_ERROR_FAULT);
401 TEST_F(WidgetAppTest, widget_app_get_elm_win) {
403 [](widget_context_h context, bundle* content,
404 int w, int h, void* user_data) -> int {
405 EXPECT_CALL(GetMock<ElmMock>(),
406 elm_win_add(_, _, _)).
407 WillOnce(Return(reinterpret_cast<Evas_Object*>(
408 calloc(1, sizeof(char)))));
410 EXPECT_CALL(GetMock<EcoreWl2Mock>(),
411 ecore_evas_wayland2_window_get(_)).
412 WillOnce(Return(reinterpret_cast<Ecore_Wl2_Window*>(
413 calloc(1, sizeof(char)))));
415 EXPECT_CALL(GetMock<ElmMock>(),
416 elm_win_aux_hint_add(_, _, _)).WillOnce(Return(0));
418 EXPECT_CALL(GetMock<EcoreWl2Mock>(),
419 ecore_wl2_window_surface_get(_)).WillOnce(Return(nullptr));
421 EXPECT_CALL(GetMock<EcoreWl2Mock>(),
422 evas_object_event_callback_add(_, _, _, _)).Times(1);
424 EXPECT_CALL(GetMock<EcoreWl2Mock>(),
425 evas_object_data_set(_, _, _)).Times(1);
427 Evas_Object* win = nullptr;
428 int ret = widget_app_get_elm_win(context, &win);
429 EXPECT_EQ(ret, WIDGET_ERROR_NONE);