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.
18 #include <gtest/gtest.h>
19 #include <gmock/gmock.h>
20 #include <bundle_cpp.h>
21 #include <bundle_internal.h>
26 #include "widget_app.h"
27 #include "widget_app_efl.h"
28 #include "unit_tests/mock/gio_mock.h"
29 #include "unit_tests/mock/ecore_wl2_mock.h"
30 #include "unit_tests/mock/system_info_mock.h"
31 #include "unit_tests/mock/appcore_multiwindow_base_mock.h"
32 #include "unit_tests/mock/app_common_mock.h"
33 #include "unit_tests/mock/elm_mock.h"
34 #include "unit_tests/mock/aul_mock.h"
35 #include "unit_tests/mock/test_fixture.h"
38 using ::testing::DoAll;
39 using ::testing::Return;
40 using ::testing::SetArgPointee;
41 using ::testing::Invoke;
43 typedef struct _widget_base_instance_data {
51 char* pending_content;
53 } widget_base_instance_data;
55 struct _widget_context {
59 class Mocks : public ::testing::NiceMock<GioMock>,
60 public ::testing::NiceMock<MultiWindowBaseMock>,
61 public ::testing::NiceMock<AppCommonMock>,
62 public ::testing::NiceMock<ElmMock>,
63 public ::testing::NiceMock<AulMock>,
64 public ::testing::NiceMock<SystemInfoMock>,
65 public ::testing::NiceMock<EcoreWl2Mock> {};
67 class WidgetAppTest : public TestFixture {
69 WidgetAppTest() : TestFixture(std::make_unique<Mocks>()) {}
70 virtual ~WidgetAppTest() {}
72 virtual void SetUp() {
75 virtual void TearDown() {
79 int __instance_create_cb(widget_context_h context, bundle* content,
80 int w, int h, void* user_data) {
84 int __instance_destroy_cb(widget_context_h context,
85 widget_app_destroy_type_e reason, bundle* content, void* user_data) {
89 int __instance_pause_cb(widget_context_h context, void* user_data) {
93 int __instance_resume_cb(widget_context_h context, void* user_data) {
97 int __instance_resize_cb(widget_context_h context,
98 int w, int h, void* user_data) {
102 int __instance_update_cb(widget_context_h context,
103 bundle* content, int force, void* user_data) {
107 widget_class_h __app_create_cb(void* user_data) {
111 void __app_terminate_cb(void* user_data) {
114 int __app_get_id_fake(char** id) {
115 *id = strdup("test");
119 int __aul_app_get_pkgid_bypid_fake(int pid, char* pkgid, int len) {
120 snprintf(pkgid, 10, "%s", "test");
124 struct instance_data {
130 void* __appcore_multiwindow_base_instance_get_extra_fake(
131 appcore_multiwindow_base_instance_h handle) {
132 widget_base_instance_data* data = (widget_base_instance_data*)
133 calloc(1, sizeof(widget_base_instance_data));
134 data->args = bundle_create();
135 bundle_add_str(data->args, "__AUL_WIDGET_ID__", "test");
136 bundle_add_str(data->args, "__WIDGET_OP__", "optest");
137 bundle_add_str(data->args, "__WIDGET_WIDTH__", "320");
138 bundle_add_str(data->args, "__WIDGET_HEIGHT__", "320");
139 data->pending_content = strdup("test");
140 data->content = strdup("test");
141 data->id = strdup("id");
142 data->user_data = calloc(1, sizeof (struct instance_data));
147 const char* __appcore_multiwindow_base_instance_get_id_fake(
148 appcore_multiwindow_base_instance_h handle) {
149 return strdup("test");
152 const char* __appcore_multiwindow_base_instance_get_class_id_fake(
153 appcore_multiwindow_base_instance_h context) {
154 return strdup("test");
157 appcore_multiwindow_base_class __cls;
158 void __appcore_multiwindow_base_class_add_fake(
159 appcore_multiwindow_base_class cls) {
161 cls.create(nullptr, nullptr);
162 cls.terminate(nullptr, nullptr);
163 cls.pause((appcore_multiwindow_base_instance_h)
164 calloc(1, sizeof(char)), nullptr);
165 cls.resume((appcore_multiwindow_base_instance_h)
166 calloc(1, sizeof(char)), nullptr);
169 int __appcore_multiwindow_base_init_fake(appcore_multiwindow_base_ops ops,
170 int argc, char** argv, void* data) {
171 tizen_base::Bundle b;
173 b.Add("__WIDGET_WIDTH__", "320");
174 b.Add("__WIDGET_HEIGHT__", "320");
175 b.Add("__AUL_WIDGET_INSTANCE_ID__", "instance_id");
176 b.Add("__AUL_WIDGET_ID__", "test");
177 b.Add("__WIDGET_OP__", "create");
178 ops.base.control(b.GetHandle(), nullptr);
180 b.Delete("__WIDGET_OP__");
181 b.Add("__WIDGET_OP__", "resize");
182 ops.base.control(b.GetHandle(), nullptr);
184 b.Delete("__WIDGET_OP__");
185 b.Add("__WIDGET_OP__", "update");
186 ops.base.control(b.GetHandle(), nullptr);
188 b.Delete("__WIDGET_OP__");
189 b.Add("__WIDGET_OP__", "destroy");
190 ops.base.control(b.GetHandle(), nullptr);
192 b.Delete("__WIDGET_OP__");
193 b.Add("__WIDGET_OP__", "resume");
194 ops.base.control(b.GetHandle(), nullptr);
196 b.Delete("__WIDGET_OP__");
197 b.Add("__WIDGET_OP__", "pause");
198 ops.base.control(b.GetHandle(), nullptr);
200 b.Delete("__WIDGET_OP__");
201 b.Add("__WIDGET_OP__", "terminate");
202 ops.base.control(b.GetHandle(), nullptr);
204 b.Delete("__WIDGET_OP__");
205 b.Add("__WIDGET_OP__", "period");
206 ops.base.control(b.GetHandle(), nullptr);
208 ops.base.create(nullptr);
209 ops.base.terminate(nullptr);
214 TEST_F(WidgetAppTest, widget_app_main) {
215 tizen_base::Bundle b;
216 b.Add("__AUL_WIDGET_VIEWER__", "test");
217 int ret = bundle_add_str(b.GetHandle(), "KEY", "VALUE");
218 ASSERT_EQ(ret, BUNDLE_ERROR_NONE);
220 char** argv = nullptr;
221 int argc = bundle_export_to_argv(b.GetHandle(), &argv);
222 ASSERT_EQ(get_last_result(), BUNDLE_ERROR_NONE);
223 ASSERT_NE(argv, nullptr);
226 widget_app_lifecycle_callback_s callback;
227 callback.create = __app_create_cb;
228 callback.terminate = __app_terminate_cb;
230 EXPECT_CALL(GetMock<EcoreWl2Mock>(), ecore_wl2_display_disconnect(_)).
233 EXPECT_CALL(GetMock<EcoreWl2Mock>(), ecore_wl2_shutdown()).
236 EXPECT_CALL(GetMock<SystemInfoMock>(),
237 system_info_get_platform_bool(_, _)).
239 SetArgPointee<1>(true),
242 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
243 appcore_multiwindow_base_init(_, _, _, _)).
244 WillOnce(Invoke(__appcore_multiwindow_base_init_fake));
246 EXPECT_CALL(GetMock<AulMock>(),
247 aul_app_get_pkgid_bypid(_, _, _)).
248 WillOnce(Invoke(__aul_app_get_pkgid_bypid_fake));
250 EXPECT_CALL(GetMock<AppCommonMock>(), app_get_id(_)).
251 WillOnce(Invoke(__app_get_id_fake));
253 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
254 appcore_multiwindow_base_instance_find(_)).
257 (appcore_multiwindow_base_instance_h)calloc(1, sizeof(char))));
259 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
260 appcore_multiwindow_base_instance_get_class(_)).
264 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
265 appcore_multiwindow_base_instance_get_extra(_)).
267 Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
269 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
270 appcore_multiwindow_base_instance_exit(_)).Times(2);
272 ret = widget_app_main(argc, argv, &callback, nullptr);
273 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
276 TEST_F(WidgetAppTest, widget_app_exit) {
277 int ret = widget_app_exit();
278 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
281 TEST_F(WidgetAppTest, widget_app_terminate_context) {
282 widget_context_h context =
283 (widget_context_h)calloc(1, sizeof(struct _widget_context));
284 int ret = widget_app_terminate_context(context);
286 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
289 bool __widget_context_cb(widget_context_h context, void *user_data) {
293 TEST_F(WidgetAppTest, widget_app_foreach_context) {
294 int ret = widget_app_foreach_context(__widget_context_cb, nullptr);
295 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
298 void __app_event_cb(app_event_info_h event_info, void *user_data) {
301 TEST_F(WidgetAppTest, widget_app_add_event_handler) {
302 app_event_handler_h handle;
303 EXPECT_CALL(GetMock<SystemInfoMock>(),
304 system_info_get_platform_bool(_, _)).
306 SetArgPointee<1>(true),
308 int ret = widget_app_add_event_handler(&handle, APP_EVENT_LOW_MEMORY,
309 __app_event_cb, nullptr);
310 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
313 TEST_F(WidgetAppTest, widget_app_remove_event_handler) {
314 app_event_handler_h handle;
315 EXPECT_CALL(GetMock<SystemInfoMock>(),
316 system_info_get_platform_bool(_, _)).
317 WillRepeatedly(DoAll(
318 SetArgPointee<1>(true),
320 int ret = widget_app_add_event_handler(&handle, APP_EVENT_LOW_MEMORY,
321 __app_event_cb, nullptr);
322 ret = widget_app_remove_event_handler(handle);
323 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
326 TEST_F(WidgetAppTest, widget_app_get_id) {
327 widget_context_h context =
328 (widget_context_h)calloc(1, sizeof(struct _widget_context));
330 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
331 appcore_multiwindow_base_instance_get_id(_)).
332 WillOnce(Return("test"));
334 const char *id = widget_app_get_id(context);
336 EXPECT_STREQ(id, "test");
339 TEST_F(WidgetAppTest, widget_app_class_create) {
340 widget_instance_lifecycle_callback_s lifecycle;
342 lifecycle.create = __instance_create_cb;
343 lifecycle.destroy = __instance_destroy_cb;
344 lifecycle.pause = __instance_pause_cb;
345 lifecycle.resume = __instance_resume_cb;
346 lifecycle.resize = __instance_resize_cb;
347 lifecycle.update = __instance_update_cb;
349 EXPECT_CALL(GetMock<AppCommonMock>(), app_get_id(_)).
350 WillOnce(Invoke(__app_get_id_fake));
352 widget_class_h cls = widget_app_class_create(lifecycle, nullptr);
353 EXPECT_NE(nullptr, cls);
356 TEST_F(WidgetAppTest, widget_app_context_set_tag) {
357 widget_context_h context =
358 (widget_context_h)calloc(1, sizeof(struct _widget_context));
360 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
361 appcore_multiwindow_base_instance_get_extra(_)).
362 WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
364 int ret = widget_app_context_set_tag(context, (void*)"test");
366 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
369 TEST_F(WidgetAppTest, widget_app_context_get_tag) {
370 widget_context_h context =
371 (widget_context_h)calloc(1, sizeof(struct _widget_context));
373 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
374 appcore_multiwindow_base_instance_get_extra(_)).
375 WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
378 int ret = widget_app_context_get_tag(context, &tag);
380 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
383 TEST_F(WidgetAppTest, widget_app_context_set_content_info) {
384 widget_context_h context =
385 (widget_context_h)calloc(1, sizeof(struct _widget_context));
387 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
388 appcore_multiwindow_base_instance_get_extra(_)).
389 WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
391 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
392 appcore_multiwindow_base_instance_get_id(_)).
393 WillOnce(Invoke(__appcore_multiwindow_base_instance_get_id_fake));
395 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
396 appcore_multiwindow_base_instance_get_class_id(_)).
397 WillOnce(Invoke(__appcore_multiwindow_base_instance_get_class_id_fake));
399 bundle* content_info = bundle_create();
400 int ret = widget_app_context_set_content_info(context, content_info);
402 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
405 TEST_F(WidgetAppTest, widget_app_context_set_title) {
406 widget_context_h context =
407 (widget_context_h)calloc(1, sizeof(struct _widget_context));
409 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
410 appcore_multiwindow_base_instance_get_extra(_)).
411 WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
413 int ret = widget_app_context_set_title(context, "title");
415 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
418 TEST_F(WidgetAppTest, widget_app_class_add) {
419 widget_instance_lifecycle_callback_s lifecycle;
420 lifecycle.create = __instance_create_cb;
421 lifecycle.destroy = __instance_destroy_cb;
422 lifecycle.pause = __instance_pause_cb;
423 lifecycle.resume = __instance_resume_cb;
424 lifecycle.resize = __instance_resize_cb;
425 lifecycle.update = __instance_update_cb;
427 widget_class_h cls = widget_app_class_add(
428 nullptr, "test", lifecycle, nullptr);
430 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
431 appcore_multiwindow_base_class_add(_)).
432 WillOnce(Invoke(__appcore_multiwindow_base_class_add_fake));
434 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
435 appcore_multiwindow_base_instance_get_extra(_)).
436 WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
438 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
439 appcore_multiwindow_base_instance_get_class_id(_)).
440 WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_class_id_fake));
442 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
443 appcore_multiwindow_base_instance_drop(_)).
446 cls = widget_app_class_add(
447 nullptr, "test", lifecycle, nullptr);
448 EXPECT_NE(nullptr, cls);
450 tizen_base::Bundle b;
451 b.Add("__AUL_WIDGET_VIEWER__", "test");
452 int ret = bundle_add_str(b.GetHandle(), "KEY", "VALUE");
453 ASSERT_EQ(ret, BUNDLE_ERROR_NONE);
455 char** argv = nullptr;
456 int argc = bundle_export_to_argv(b.GetHandle(), &argv);
457 ASSERT_EQ(get_last_result(), BUNDLE_ERROR_NONE);
458 ASSERT_NE(argv, nullptr);
461 widget_app_lifecycle_callback_s callback;
462 callback.create = __app_create_cb;
463 callback.terminate = __app_terminate_cb;
465 ret = widget_app_main(argc, argv, &callback, nullptr);
466 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
469 TEST_F(WidgetAppTest, widget_app_get_elm_win_negative) {
471 widget_context_h context =
472 (widget_context_h)calloc(1, sizeof(struct _widget_context));
473 int ret = widget_app_get_elm_win(context, &win);
475 EXPECT_EQ(WIDGET_ERROR_FAULT, ret);
478 TEST_F(WidgetAppTest, widget_app_get_elm_win) {
479 EXPECT_CALL(GetMock<ElmMock>(),
480 elm_win_add(_, _, _)).
481 WillOnce(Return((Evas_Object*)calloc(1, sizeof(char))));
483 EXPECT_CALL(GetMock<EcoreWl2Mock>(),
484 ecore_evas_wayland2_window_get(_)).
485 WillOnce(Return((Ecore_Wl2_Window*)calloc(1, sizeof(char))));
487 EXPECT_CALL(GetMock<ElmMock>(),
488 elm_win_aux_hint_add(_, _, _)).
491 EXPECT_CALL(GetMock<EcoreWl2Mock>(),
492 ecore_wl2_window_surface_get(_)).
493 WillOnce(Return(nullptr));
495 EXPECT_CALL(GetMock<EcoreWl2Mock>(),
496 evas_object_event_callback_add(_, _, _, _)).
499 EXPECT_CALL(GetMock<EcoreWl2Mock>(),
500 evas_object_data_set(_, _, _)).
503 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
504 appcore_multiwindow_base_instance_get_extra(_)).
505 WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
508 widget_context_h context =
509 (widget_context_h)calloc(1, sizeof(struct _widget_context));
510 int ret = widget_app_get_elm_win(context, &win);
512 EXPECT_EQ(WIDGET_ERROR_NONE, ret);