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 class Mocks : public ::testing::NiceMock<GioMock>,
56 public ::testing::NiceMock<MultiWindowBaseMock>,
57 public ::testing::NiceMock<AppCommonMock>,
58 public ::testing::NiceMock<ElmMock>,
59 public ::testing::NiceMock<AulMock>,
60 public ::testing::NiceMock<SystemInfoMock>,
61 public ::testing::NiceMock<EcoreWl2Mock> {};
63 class WidgetAppTest : public TestFixture {
65 WidgetAppTest() : TestFixture(std::make_unique<Mocks>()) {}
66 virtual ~WidgetAppTest() {}
68 virtual void SetUp() {
71 virtual void TearDown() {
75 int __instance_create_cb(widget_context_h context, bundle* content,
76 int w, int h, void* user_data) {
80 int __instance_destroy_cb(widget_context_h context,
81 widget_app_destroy_type_e reason, bundle* content, void* user_data) {
85 int __instance_pause_cb(widget_context_h context, void* user_data) {
89 int __instance_resume_cb(widget_context_h context, void* user_data) {
93 int __instance_resize_cb(widget_context_h context,
94 int w, int h, void* user_data) {
98 int __instance_update_cb(widget_context_h context,
99 bundle* content, int force, void* user_data) {
103 widget_class_h __app_create_cb(void* user_data) {
107 void __app_terminate_cb(void* user_data) {
110 int __app_get_id_fake(char** id) {
111 *id = strdup("test");
115 int __aul_app_get_pkgid_bypid_fake(int pid, char* pkgid, int len) {
116 snprintf(pkgid, 10, "%s", "test");
120 struct instance_data {
126 void* __appcore_multiwindow_base_instance_get_extra_fake(
127 appcore_multiwindow_base_instance_h handle) {
128 widget_base_instance_data* data = (widget_base_instance_data*)
129 calloc(1, sizeof(widget_base_instance_data));
130 data->args = bundle_create();
131 bundle_add_str(data->args, "__AUL_WIDGET_ID__", "test");
132 bundle_add_str(data->args, "__WIDGET_OP__", "optest");
133 bundle_add_str(data->args, "__WIDGET_WIDTH__", "320");
134 bundle_add_str(data->args, "__WIDGET_HEIGHT__", "320");
135 data->pending_content = strdup("test");
136 data->content = strdup("test");
137 data->id = strdup("id");
138 data->user_data = calloc(1, sizeof (struct instance_data));
143 const char* __appcore_multiwindow_base_instance_get_id_fake(
144 appcore_multiwindow_base_instance_h handle) {
145 return strdup("test");
148 const char* __appcore_multiwindow_base_instance_get_class_id_fake(
149 appcore_multiwindow_base_instance_h context) {
150 return strdup("test");
153 appcore_multiwindow_base_class __cls;
154 void __appcore_multiwindow_base_class_add_fake(
155 appcore_multiwindow_base_class cls) {
157 cls.create(nullptr, nullptr);
158 cls.terminate(nullptr, nullptr);
159 cls.pause((appcore_multiwindow_base_instance_h)
160 calloc(1, sizeof(char)), nullptr);
161 cls.resume((appcore_multiwindow_base_instance_h)
162 calloc(1, sizeof(char)), nullptr);
165 int __appcore_multiwindow_base_init_fake(appcore_multiwindow_base_ops ops,
166 int argc, char** argv, void* data) {
167 tizen_base::Bundle b;
169 b.Add("__WIDGET_WIDTH__", "320");
170 b.Add("__WIDGET_HEIGHT__", "320");
171 b.Add("__AUL_WIDGET_INSTANCE_ID__", "instance_id");
172 b.Add("__AUL_WIDGET_ID__", "test");
173 b.Add("__WIDGET_OP__", "create");
174 ops.base.control(b.GetHandle(), nullptr);
176 b.Delete("__WIDGET_OP__");
177 b.Add("__WIDGET_OP__", "resize");
178 ops.base.control(b.GetHandle(), nullptr);
180 b.Delete("__WIDGET_OP__");
181 b.Add("__WIDGET_OP__", "update");
182 ops.base.control(b.GetHandle(), nullptr);
184 b.Delete("__WIDGET_OP__");
185 b.Add("__WIDGET_OP__", "destroy");
186 ops.base.control(b.GetHandle(), nullptr);
188 b.Delete("__WIDGET_OP__");
189 b.Add("__WIDGET_OP__", "resume");
190 ops.base.control(b.GetHandle(), nullptr);
192 b.Delete("__WIDGET_OP__");
193 b.Add("__WIDGET_OP__", "pause");
194 ops.base.control(b.GetHandle(), nullptr);
196 b.Delete("__WIDGET_OP__");
197 b.Add("__WIDGET_OP__", "terminate");
198 ops.base.control(b.GetHandle(), nullptr);
200 b.Delete("__WIDGET_OP__");
201 b.Add("__WIDGET_OP__", "period");
202 ops.base.control(b.GetHandle(), nullptr);
204 ops.base.create(nullptr);
205 ops.base.terminate(nullptr);
210 TEST_F(WidgetAppTest, widget_app_main) {
211 tizen_base::Bundle b;
212 b.Add("__AUL_WIDGET_VIEWER__", "test");
213 int ret = bundle_add_str(b.GetHandle(), "KEY", "VALUE");
214 ASSERT_EQ(ret, BUNDLE_ERROR_NONE);
216 char** argv = nullptr;
217 int argc = bundle_export_to_argv(b.GetHandle(), &argv);
218 ASSERT_EQ(get_last_result(), BUNDLE_ERROR_NONE);
219 ASSERT_NE(argv, nullptr);
222 widget_app_lifecycle_callback_s callback;
223 callback.create = __app_create_cb;
224 callback.terminate = __app_terminate_cb;
226 EXPECT_CALL(GetMock<EcoreWl2Mock>(), ecore_wl2_display_disconnect(_)).
229 EXPECT_CALL(GetMock<EcoreWl2Mock>(), ecore_wl2_shutdown()).
232 EXPECT_CALL(GetMock<SystemInfoMock>(),
233 system_info_get_platform_bool(_, _)).
235 SetArgPointee<1>(true),
238 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
239 appcore_multiwindow_base_init(_, _, _, _)).
240 WillOnce(Invoke(__appcore_multiwindow_base_init_fake));
242 EXPECT_CALL(GetMock<AulMock>(),
243 aul_app_get_pkgid_bypid(_, _, _)).
244 WillOnce(Invoke(__aul_app_get_pkgid_bypid_fake));
246 EXPECT_CALL(GetMock<AppCommonMock>(), app_get_id(_)).
247 WillOnce(Invoke(__app_get_id_fake));
249 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
250 appcore_multiwindow_base_instance_find(_)).
253 (appcore_multiwindow_base_instance_h)calloc(1, sizeof(char))));
255 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
256 appcore_multiwindow_base_instance_get_class(_)).
260 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
261 appcore_multiwindow_base_instance_get_extra(_)).
263 Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
265 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
266 appcore_multiwindow_base_instance_exit(_)).Times(2);
268 ret = widget_app_main(argc, argv, &callback, nullptr);
269 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
272 TEST_F(WidgetAppTest, widget_app_exit) {
273 int ret = widget_app_exit();
274 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
277 TEST_F(WidgetAppTest, widget_app_terminate_context) {
278 widget_context_h context =
279 (widget_context_h)calloc(1, sizeof(widget_context_h));
280 int ret = widget_app_terminate_context(context);
281 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
284 bool __widget_context_cb(widget_context_h context, void *user_data) {
288 TEST_F(WidgetAppTest, widget_app_foreach_context) {
289 int ret = widget_app_foreach_context(__widget_context_cb, nullptr);
290 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
293 void __app_event_cb(app_event_info_h event_info, void *user_data) {
296 TEST_F(WidgetAppTest, widget_app_add_event_handler) {
297 app_event_handler_h handle;
298 int ret = widget_app_add_event_handler(&handle, APP_EVENT_LOW_MEMORY,
299 __app_event_cb, nullptr);
300 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
303 TEST_F(WidgetAppTest, widget_app_remove_event_handler) {
304 app_event_handler_h handle;
305 int ret = widget_app_add_event_handler(&handle, APP_EVENT_LOW_MEMORY,
306 __app_event_cb, nullptr);
307 ret = widget_app_remove_event_handler(handle);
308 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
311 TEST_F(WidgetAppTest, widget_app_get_id) {
312 widget_context_h context =
313 (widget_context_h)calloc(1, sizeof(widget_context_h));
315 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
316 appcore_multiwindow_base_instance_get_id(_)).
317 WillOnce(Return("test"));
319 const char *id = widget_app_get_id(context);
320 EXPECT_STREQ(id, "test");
323 TEST_F(WidgetAppTest, widget_app_class_create) {
324 widget_instance_lifecycle_callback_s lifecycle;
326 lifecycle.create = __instance_create_cb;
327 lifecycle.destroy = __instance_destroy_cb;
328 lifecycle.pause = __instance_pause_cb;
329 lifecycle.resume = __instance_resume_cb;
330 lifecycle.resize = __instance_resize_cb;
331 lifecycle.update = __instance_update_cb;
333 EXPECT_CALL(GetMock<AppCommonMock>(), app_get_id(_)).
334 WillOnce(Invoke(__app_get_id_fake));
336 widget_class_h cls = widget_app_class_create(lifecycle, nullptr);
337 EXPECT_NE(nullptr, cls);
340 TEST_F(WidgetAppTest, widget_app_context_set_tag) {
341 widget_context_h context =
342 (widget_context_h)calloc(1, sizeof(widget_context_h));
344 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
345 appcore_multiwindow_base_instance_get_extra(_)).
346 WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
348 int ret = widget_app_context_set_tag(context, (void*)"test");
349 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
352 TEST_F(WidgetAppTest, widget_app_context_get_tag) {
353 widget_context_h context =
354 (widget_context_h)calloc(1, sizeof(widget_context_h));
356 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
357 appcore_multiwindow_base_instance_get_extra(_)).
358 WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
361 int ret = widget_app_context_get_tag(context, &tag);
362 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
365 TEST_F(WidgetAppTest, widget_app_context_set_content_info) {
366 widget_context_h context =
367 (widget_context_h)calloc(1, sizeof(widget_context_h));
369 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
370 appcore_multiwindow_base_instance_get_extra(_)).
371 WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
373 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
374 appcore_multiwindow_base_instance_get_id(_)).
375 WillOnce(Invoke(__appcore_multiwindow_base_instance_get_id_fake));
377 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
378 appcore_multiwindow_base_instance_get_class_id(_)).
379 WillOnce(Invoke(__appcore_multiwindow_base_instance_get_class_id_fake));
381 bundle* content_info = bundle_create();
382 int ret = widget_app_context_set_content_info(context, content_info);
383 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
386 TEST_F(WidgetAppTest, widget_app_context_set_title) {
387 widget_context_h context =
388 (widget_context_h)calloc(1, sizeof(widget_context_h));
390 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
391 appcore_multiwindow_base_instance_get_extra(_)).
392 WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
394 int ret = widget_app_context_set_title(context, "title");
395 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
398 TEST_F(WidgetAppTest, widget_app_class_add) {
399 widget_instance_lifecycle_callback_s lifecycle;
400 widget_class_h cls = widget_app_class_add(
401 nullptr, "test", lifecycle, nullptr);
403 lifecycle.create = __instance_create_cb;
404 lifecycle.destroy = __instance_destroy_cb;
405 lifecycle.pause = __instance_pause_cb;
406 lifecycle.resume = __instance_resume_cb;
407 lifecycle.resize = __instance_resize_cb;
408 lifecycle.update = __instance_update_cb;
410 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
411 appcore_multiwindow_base_class_add(_)).
412 WillOnce(Invoke(__appcore_multiwindow_base_class_add_fake));
414 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
415 appcore_multiwindow_base_instance_get_extra(_)).
416 WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
418 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
419 appcore_multiwindow_base_instance_get_class_id(_)).
420 WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_class_id_fake));
422 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
423 appcore_multiwindow_base_instance_drop(_)).
426 cls = widget_app_class_add(
427 nullptr, "test", lifecycle, nullptr);
428 EXPECT_NE(nullptr, cls);
430 tizen_base::Bundle b;
431 b.Add("__AUL_WIDGET_VIEWER__", "test");
432 int ret = bundle_add_str(b.GetHandle(), "KEY", "VALUE");
433 ASSERT_EQ(ret, BUNDLE_ERROR_NONE);
435 char** argv = nullptr;
436 int argc = bundle_export_to_argv(b.GetHandle(), &argv);
437 ASSERT_EQ(get_last_result(), BUNDLE_ERROR_NONE);
438 ASSERT_NE(argv, nullptr);
441 widget_app_lifecycle_callback_s callback;
442 callback.create = __app_create_cb;
443 callback.terminate = __app_terminate_cb;
445 ret = widget_app_main(argc, argv, &callback, nullptr);
446 EXPECT_EQ(WIDGET_ERROR_NONE, ret);
449 TEST_F(WidgetAppTest, widget_app_get_elm_win_negative) {
451 widget_context_h context =
452 (widget_context_h)calloc(1, sizeof(widget_context_h));
453 int ret = widget_app_get_elm_win(context, &win);
454 EXPECT_EQ(WIDGET_ERROR_FAULT, ret);
457 TEST_F(WidgetAppTest, widget_app_get_elm_win) {
458 EXPECT_CALL(GetMock<ElmMock>(),
459 elm_win_add(_, _, _)).
460 WillOnce(Return((Evas_Object*)calloc(1, sizeof(char))));
462 EXPECT_CALL(GetMock<EcoreWl2Mock>(),
463 ecore_evas_wayland2_window_get(_)).
464 WillOnce(Return((Ecore_Wl2_Window*)calloc(1, sizeof(char))));
466 EXPECT_CALL(GetMock<ElmMock>(),
467 elm_win_aux_hint_add(_, _, _)).
470 EXPECT_CALL(GetMock<EcoreWl2Mock>(),
471 ecore_wl2_window_surface_get(_)).
472 WillOnce(Return(nullptr));
474 EXPECT_CALL(GetMock<EcoreWl2Mock>(),
475 evas_object_event_callback_add(_, _, _, _)).
478 EXPECT_CALL(GetMock<EcoreWl2Mock>(),
479 evas_object_data_set(_, _, _)).
482 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
483 appcore_multiwindow_base_instance_get_extra(_)).
484 WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
487 widget_context_h context =
488 (widget_context_h)calloc(1, sizeof(widget_context_h));
489 int ret = widget_app_get_elm_win(context, &win);
490 EXPECT_EQ(WIDGET_ERROR_NONE, ret);