3b3d2dcf45c685ae6ddecc620b8a15b1bbae89de
[platform/core/appfw/appcore-widget.git] / test / unit_tests / test_widget_app.cc
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdlib.h>
18 #include <gtest/gtest.h>
19 #include <gmock/gmock.h>
20 #include <bundle_cpp.h>
21 #include <bundle_internal.h>
22
23 #include <iostream>
24 #include <memory>
25
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"
36
37 using ::testing::_;
38 using ::testing::DoAll;
39 using ::testing::Return;
40 using ::testing::SetArgPointee;
41 using ::testing::Invoke;
42
43 typedef struct _widget_base_instance_data {
44   bundle* args;
45   char* id;
46   char* content;
47   void* tag;
48   double period;
49   guint periodic_timer;
50   bool pending_update;
51   char* pending_content;
52   void* user_data;
53 } widget_base_instance_data;
54
55 struct _widget_context {
56   int context;
57 };
58
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> {};
66
67 class WidgetAppTest : public TestFixture {
68  public:
69   WidgetAppTest() : TestFixture(std::make_unique<Mocks>()) {}
70   virtual ~WidgetAppTest() {}
71
72   virtual void SetUp() {
73   }
74
75   virtual void TearDown() {
76   }
77 };
78
79 int __instance_create_cb(widget_context_h context, bundle* content,
80     int w, int h, void* user_data) {
81   return 0;
82 }
83
84 int __instance_destroy_cb(widget_context_h context,
85     widget_app_destroy_type_e reason, bundle* content, void* user_data) {
86   return 0;
87 }
88
89 int __instance_pause_cb(widget_context_h context, void* user_data) {
90   return 0;
91 }
92
93 int __instance_resume_cb(widget_context_h context, void* user_data) {
94   return 0;
95 }
96
97 int __instance_resize_cb(widget_context_h context,
98     int w, int h, void* user_data) {
99   return 0;
100 }
101
102 int __instance_update_cb(widget_context_h context,
103     bundle* content, int force, void* user_data) {
104   return 0;
105 }
106
107 widget_class_h __app_create_cb(void* user_data) {
108   return nullptr;
109 }
110
111 void __app_terminate_cb(void* user_data) {
112 }
113
114 int __app_get_id_fake(char** id) {
115   *id = strdup("test");
116   return 0;
117 }
118
119 int __aul_app_get_pkgid_bypid_fake(int pid, char* pkgid, int len) {
120   snprintf(pkgid, 10, "%s", "test");
121   return 0;
122 }
123
124 struct instance_data {
125   Evas_Object* win;
126   guint iconify_timer;
127   bool is_iconified;
128 };
129
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));
143
144   return data;
145 }
146
147 const char* __appcore_multiwindow_base_instance_get_id_fake(
148     appcore_multiwindow_base_instance_h handle) {
149   return strdup("test");
150 }
151
152 const char* __appcore_multiwindow_base_instance_get_class_id_fake(
153     appcore_multiwindow_base_instance_h context) {
154   return strdup("test");
155 }
156
157 appcore_multiwindow_base_class __cls;
158 void __appcore_multiwindow_base_class_add_fake(
159     appcore_multiwindow_base_class cls) {
160   __cls = 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);
167 }
168
169 int __appcore_multiwindow_base_init_fake(appcore_multiwindow_base_ops ops,
170     int argc, char** argv, void* data) {
171   tizen_base::Bundle b;
172
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);
179
180   b.Delete("__WIDGET_OP__");
181   b.Add("__WIDGET_OP__", "resize");
182   ops.base.control(b.GetHandle(), nullptr);
183
184   b.Delete("__WIDGET_OP__");
185   b.Add("__WIDGET_OP__", "update");
186   ops.base.control(b.GetHandle(), nullptr);
187
188   b.Delete("__WIDGET_OP__");
189   b.Add("__WIDGET_OP__", "destroy");
190   ops.base.control(b.GetHandle(), nullptr);
191
192   b.Delete("__WIDGET_OP__");
193   b.Add("__WIDGET_OP__", "resume");
194   ops.base.control(b.GetHandle(), nullptr);
195
196   b.Delete("__WIDGET_OP__");
197   b.Add("__WIDGET_OP__", "pause");
198   ops.base.control(b.GetHandle(), nullptr);
199
200   b.Delete("__WIDGET_OP__");
201   b.Add("__WIDGET_OP__", "terminate");
202   ops.base.control(b.GetHandle(), nullptr);
203
204   b.Delete("__WIDGET_OP__");
205   b.Add("__WIDGET_OP__", "period");
206   ops.base.control(b.GetHandle(), nullptr);
207
208   ops.base.create(nullptr);
209   ops.base.terminate(nullptr);
210
211   return 0;
212 }
213
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);
219
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);
224   ASSERT_NE(argc, 0);
225
226   widget_app_lifecycle_callback_s callback;
227   callback.create = __app_create_cb;
228   callback.terminate = __app_terminate_cb;
229
230   EXPECT_CALL(GetMock<EcoreWl2Mock>(), ecore_wl2_display_disconnect(_)).
231        Times(1);
232
233   EXPECT_CALL(GetMock<EcoreWl2Mock>(), ecore_wl2_shutdown()).
234        Times(1);
235
236   EXPECT_CALL(GetMock<SystemInfoMock>(),
237       system_info_get_platform_bool(_, _)).
238           WillOnce(DoAll(
239                   SetArgPointee<1>(true),
240                   Return(0)));
241
242   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
243       appcore_multiwindow_base_init(_, _, _, _)).
244           WillOnce(Invoke(__appcore_multiwindow_base_init_fake));
245
246   EXPECT_CALL(GetMock<AulMock>(),
247       aul_app_get_pkgid_bypid(_, _, _)).
248           WillOnce(Invoke(__aul_app_get_pkgid_bypid_fake));
249
250   EXPECT_CALL(GetMock<AppCommonMock>(), app_get_id(_)).
251           WillOnce(Invoke(__app_get_id_fake));
252
253   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
254       appcore_multiwindow_base_instance_find(_)).
255           WillRepeatedly(
256             Return(
257               (appcore_multiwindow_base_instance_h)calloc(1, sizeof(char))));
258
259   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
260       appcore_multiwindow_base_instance_get_class(_)).
261           WillRepeatedly(
262             Return(&__cls));
263
264   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
265       appcore_multiwindow_base_instance_get_extra(_)).
266       WillRepeatedly(
267         Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
268
269   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
270       appcore_multiwindow_base_instance_exit(_)).Times(2);
271
272   ret = widget_app_main(argc, argv, &callback, nullptr);
273   bundle_free_exported_argv(argc, &argv);
274   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
275 }
276
277 TEST_F(WidgetAppTest, widget_app_exit) {
278   int ret = widget_app_exit();
279   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
280 }
281
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);
286   free(context);
287   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
288 }
289
290 bool __widget_context_cb(widget_context_h context, void *user_data) {
291   return true;
292 }
293
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);
297 }
298
299 void __app_event_cb(app_event_info_h event_info, void *user_data) {
300 }
301
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(_, _)).
306           WillOnce(DoAll(
307                   SetArgPointee<1>(true),
308                   Return(0)));
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);
312 }
313
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),
320                   Return(0)));
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);
325 }
326
327 TEST_F(WidgetAppTest, widget_app_get_id) {
328   widget_context_h context =
329       (widget_context_h)calloc(1, sizeof(struct _widget_context));
330
331   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
332       appcore_multiwindow_base_instance_get_id(_)).
333           WillOnce(Return("test"));
334
335   const char *id = widget_app_get_id(context);
336   free(context);
337   EXPECT_STREQ(id, "test");
338 }
339
340 TEST_F(WidgetAppTest, widget_app_class_create) {
341   widget_instance_lifecycle_callback_s lifecycle;
342
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;
349
350   EXPECT_CALL(GetMock<AppCommonMock>(), app_get_id(_)).
351           WillOnce(Invoke(__app_get_id_fake));
352
353   widget_class_h cls = widget_app_class_create(lifecycle, nullptr);
354   EXPECT_NE(nullptr, cls);
355 }
356
357 TEST_F(WidgetAppTest, widget_app_context_set_tag) {
358   widget_context_h context =
359       (widget_context_h)calloc(1, sizeof(struct _widget_context));
360
361   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
362       appcore_multiwindow_base_instance_get_extra(_)).
363       WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
364
365   int ret = widget_app_context_set_tag(context, (void*)"test");
366   free(context);
367   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
368 }
369
370 TEST_F(WidgetAppTest, widget_app_context_get_tag) {
371   widget_context_h context =
372       (widget_context_h)calloc(1, sizeof(struct _widget_context));
373
374   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
375       appcore_multiwindow_base_instance_get_extra(_)).
376       WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
377
378   void* tag;
379   int ret = widget_app_context_get_tag(context, &tag);
380   free(context);
381   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
382 }
383
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));
387
388   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
389       appcore_multiwindow_base_instance_get_extra(_)).
390       WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
391
392   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
393       appcore_multiwindow_base_instance_get_id(_)).
394       WillOnce(Invoke(__appcore_multiwindow_base_instance_get_id_fake));
395
396   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
397       appcore_multiwindow_base_instance_get_class_id(_)).
398       WillOnce(Invoke(__appcore_multiwindow_base_instance_get_class_id_fake));
399
400   bundle* content_info = bundle_create();
401   int ret = widget_app_context_set_content_info(context, content_info);
402   free(context);
403   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
404 }
405
406 TEST_F(WidgetAppTest, widget_app_context_set_title) {
407   widget_context_h context =
408       (widget_context_h)calloc(1, sizeof(struct _widget_context));
409
410   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
411       appcore_multiwindow_base_instance_get_extra(_)).
412       WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
413
414   int ret = widget_app_context_set_title(context, "title");
415   free(context);
416   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
417 }
418
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;
427
428   widget_class_h cls = widget_app_class_add(
429       nullptr, "test", lifecycle, nullptr);
430
431   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
432       appcore_multiwindow_base_class_add(_)).
433       WillOnce(Invoke(__appcore_multiwindow_base_class_add_fake));
434
435   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
436       appcore_multiwindow_base_instance_get_extra(_)).
437       WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
438
439   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
440       appcore_multiwindow_base_instance_get_class_id(_)).
441       WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_class_id_fake));
442
443   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
444       appcore_multiwindow_base_instance_drop(_)).
445       Times(1);
446
447   cls = widget_app_class_add(
448       nullptr, "test", lifecycle, nullptr);
449   EXPECT_NE(nullptr, cls);
450
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);
455
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);
460   ASSERT_NE(argc, 0);
461
462   widget_app_lifecycle_callback_s callback;
463   callback.create = __app_create_cb;
464   callback.terminate = __app_terminate_cb;
465
466   ret = widget_app_main(argc, argv, &callback, nullptr);
467   bundle_free_exported_argv(argc, &argv);
468   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
469 }
470
471 TEST_F(WidgetAppTest, widget_app_get_elm_win_negative) {
472   Evas_Object* win;
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);
476   free(context);
477   EXPECT_EQ(WIDGET_ERROR_FAULT, ret);
478 }
479
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))));
484
485   EXPECT_CALL(GetMock<EcoreWl2Mock>(),
486       ecore_evas_wayland2_window_get(_)).
487       WillOnce(Return((Ecore_Wl2_Window*)calloc(1, sizeof(char))));
488
489   EXPECT_CALL(GetMock<ElmMock>(),
490       elm_win_aux_hint_add(_, _, _)).
491       WillOnce(Return(0));
492
493   EXPECT_CALL(GetMock<EcoreWl2Mock>(),
494       ecore_wl2_window_surface_get(_)).
495       WillOnce(Return(nullptr));
496
497   EXPECT_CALL(GetMock<EcoreWl2Mock>(),
498       evas_object_event_callback_add(_, _, _, _)).
499       Times(1);
500
501   EXPECT_CALL(GetMock<EcoreWl2Mock>(),
502       evas_object_data_set(_, _, _)).
503       Times(1);
504
505   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
506       appcore_multiwindow_base_instance_get_extra(_)).
507       WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
508
509   Evas_Object* win;
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);
513   free(context);
514   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
515 }