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 bundle_free_exported_argv(argc, &argv);
274 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
277 TEST_F(WidgetAppTest, widget_app_exit) {
278 int ret = widget_app_exit();
279 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
282 TEST_F(WidgetAppTest, widget_app_terminate_context) {
283 widget_context_h context =
284 (widget_context_h)calloc(1, sizeof(struct _widget_context));
285 int ret = widget_app_terminate_context(context);
287 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
290 bool __widget_context_cb(widget_context_h context, void *user_data) {
294 TEST_F(WidgetAppTest, widget_app_foreach_context) {
295 int ret = widget_app_foreach_context(__widget_context_cb, nullptr);
296 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
299 void __app_event_cb(app_event_info_h event_info, void *user_data) {
302 TEST_F(WidgetAppTest, widget_app_add_event_handler) {
303 app_event_handler_h handle;
304 EXPECT_CALL(GetMock<SystemInfoMock>(),
305 system_info_get_platform_bool(_, _)).
307 SetArgPointee<1>(true),
309 int ret = widget_app_add_event_handler(&handle, APP_EVENT_LOW_MEMORY,
310 __app_event_cb, nullptr);
311 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
314 TEST_F(WidgetAppTest, widget_app_remove_event_handler) {
315 app_event_handler_h handle;
316 EXPECT_CALL(GetMock<SystemInfoMock>(),
317 system_info_get_platform_bool(_, _)).
318 WillRepeatedly(DoAll(
319 SetArgPointee<1>(true),
321 int ret = widget_app_add_event_handler(&handle, APP_EVENT_LOW_MEMORY,
322 __app_event_cb, nullptr);
323 ret = widget_app_remove_event_handler(handle);
324 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
327 TEST_F(WidgetAppTest, widget_app_get_id) {
328 widget_context_h context =
329 (widget_context_h)calloc(1, sizeof(struct _widget_context));
331 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
332 appcore_multiwindow_base_instance_get_id(_)).
333 WillOnce(Return("test"));
335 const char *id = widget_app_get_id(context);
337 EXPECT_STREQ(id, "test");
340 TEST_F(WidgetAppTest, widget_app_class_create) {
341 widget_instance_lifecycle_callback_s lifecycle;
343 lifecycle.create = __instance_create_cb;
344 lifecycle.destroy = __instance_destroy_cb;
345 lifecycle.pause = __instance_pause_cb;
346 lifecycle.resume = __instance_resume_cb;
347 lifecycle.resize = __instance_resize_cb;
348 lifecycle.update = __instance_update_cb;
350 EXPECT_CALL(GetMock<AppCommonMock>(), app_get_id(_)).
351 WillOnce(Invoke(__app_get_id_fake));
353 widget_class_h cls = widget_app_class_create(lifecycle, nullptr);
354 EXPECT_NE(nullptr, cls);
357 TEST_F(WidgetAppTest, widget_app_context_set_tag) {
358 widget_context_h context =
359 (widget_context_h)calloc(1, sizeof(struct _widget_context));
361 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
362 appcore_multiwindow_base_instance_get_extra(_)).
363 WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
365 int ret = widget_app_context_set_tag(context, (void*)"test");
367 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
370 TEST_F(WidgetAppTest, widget_app_context_get_tag) {
371 widget_context_h context =
372 (widget_context_h)calloc(1, sizeof(struct _widget_context));
374 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
375 appcore_multiwindow_base_instance_get_extra(_)).
376 WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
379 int ret = widget_app_context_get_tag(context, &tag);
381 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
384 TEST_F(WidgetAppTest, widget_app_context_set_content_info) {
385 widget_context_h context =
386 (widget_context_h)calloc(1, sizeof(struct _widget_context));
388 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
389 appcore_multiwindow_base_instance_get_extra(_)).
390 WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
392 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
393 appcore_multiwindow_base_instance_get_id(_)).
394 WillOnce(Invoke(__appcore_multiwindow_base_instance_get_id_fake));
396 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
397 appcore_multiwindow_base_instance_get_class_id(_)).
398 WillOnce(Invoke(__appcore_multiwindow_base_instance_get_class_id_fake));
400 bundle* content_info = bundle_create();
401 int ret = widget_app_context_set_content_info(context, content_info);
403 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
406 TEST_F(WidgetAppTest, widget_app_context_set_title) {
407 widget_context_h context =
408 (widget_context_h)calloc(1, sizeof(struct _widget_context));
410 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
411 appcore_multiwindow_base_instance_get_extra(_)).
412 WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
414 int ret = widget_app_context_set_title(context, "title");
416 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
419 TEST_F(WidgetAppTest, widget_app_class_add) {
420 widget_instance_lifecycle_callback_s lifecycle;
421 lifecycle.create = __instance_create_cb;
422 lifecycle.destroy = __instance_destroy_cb;
423 lifecycle.pause = __instance_pause_cb;
424 lifecycle.resume = __instance_resume_cb;
425 lifecycle.resize = __instance_resize_cb;
426 lifecycle.update = __instance_update_cb;
428 widget_class_h cls = widget_app_class_add(
429 nullptr, "test", lifecycle, nullptr);
431 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
432 appcore_multiwindow_base_class_add(_)).
433 WillOnce(Invoke(__appcore_multiwindow_base_class_add_fake));
435 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
436 appcore_multiwindow_base_instance_get_extra(_)).
437 WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
439 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
440 appcore_multiwindow_base_instance_get_class_id(_)).
441 WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_class_id_fake));
443 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
444 appcore_multiwindow_base_instance_drop(_)).
447 cls = widget_app_class_add(
448 nullptr, "test", lifecycle, nullptr);
449 EXPECT_NE(nullptr, cls);
451 tizen_base::Bundle b;
452 b.Add("__AUL_WIDGET_VIEWER__", "test");
453 int ret = bundle_add_str(b.GetHandle(), "KEY", "VALUE");
454 ASSERT_EQ(ret, BUNDLE_ERROR_NONE);
456 char** argv = nullptr;
457 int argc = bundle_export_to_argv(b.GetHandle(), &argv);
458 ASSERT_EQ(get_last_result(), BUNDLE_ERROR_NONE);
459 ASSERT_NE(argv, nullptr);
462 widget_app_lifecycle_callback_s callback;
463 callback.create = __app_create_cb;
464 callback.terminate = __app_terminate_cb;
466 ret = widget_app_main(argc, argv, &callback, nullptr);
467 bundle_free_exported_argv(argc, &argv);
468 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
471 TEST_F(WidgetAppTest, widget_app_get_elm_win_negative) {
473 widget_context_h context =
474 (widget_context_h)calloc(1, sizeof(struct _widget_context));
475 int ret = widget_app_get_elm_win(context, &win);
477 EXPECT_EQ(WIDGET_ERROR_FAULT, ret);
480 TEST_F(WidgetAppTest, widget_app_get_elm_win) {
481 EXPECT_CALL(GetMock<ElmMock>(),
482 elm_win_add(_, _, _)).
483 WillOnce(Return((Evas_Object*)calloc(1, sizeof(char))));
485 EXPECT_CALL(GetMock<EcoreWl2Mock>(),
486 ecore_evas_wayland2_window_get(_)).
487 WillOnce(Return((Ecore_Wl2_Window*)calloc(1, sizeof(char))));
489 EXPECT_CALL(GetMock<ElmMock>(),
490 elm_win_aux_hint_add(_, _, _)).
493 EXPECT_CALL(GetMock<EcoreWl2Mock>(),
494 ecore_wl2_window_surface_get(_)).
495 WillOnce(Return(nullptr));
497 EXPECT_CALL(GetMock<EcoreWl2Mock>(),
498 evas_object_event_callback_add(_, _, _, _)).
501 EXPECT_CALL(GetMock<EcoreWl2Mock>(),
502 evas_object_data_set(_, _, _)).
505 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
506 appcore_multiwindow_base_instance_get_extra(_)).
507 WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
510 widget_context_h context =
511 (widget_context_h)calloc(1, sizeof(struct _widget_context));
512 int ret = widget_app_get_elm_win(context, &win);
514 EXPECT_EQ(WIDGET_ERROR_NONE, ret);