Fix notify event sequence
[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 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> {};
62
63 class WidgetAppTest : public TestFixture {
64  public:
65   WidgetAppTest() : TestFixture(std::make_unique<Mocks>()) {}
66   virtual ~WidgetAppTest() {}
67
68   virtual void SetUp() {
69   }
70
71   virtual void TearDown() {
72   }
73 };
74
75 int __instance_create_cb(widget_context_h context, bundle* content,
76     int w, int h, void* user_data) {
77   return 0;
78 }
79
80 int __instance_destroy_cb(widget_context_h context,
81     widget_app_destroy_type_e reason, bundle* content, void* user_data) {
82   return 0;
83 }
84
85 int __instance_pause_cb(widget_context_h context, void* user_data) {
86   return 0;
87 }
88
89 int __instance_resume_cb(widget_context_h context, void* user_data) {
90   return 0;
91 }
92
93 int __instance_resize_cb(widget_context_h context,
94     int w, int h, void* user_data) {
95   return 0;
96 }
97
98 int __instance_update_cb(widget_context_h context,
99     bundle* content, int force, void* user_data) {
100   return 0;
101 }
102
103 widget_class_h __app_create_cb(void* user_data) {
104   return nullptr;
105 }
106
107 void __app_terminate_cb(void* user_data) {
108 }
109
110 int __app_get_id_fake(char** id) {
111   *id = strdup("test");
112   return 0;
113 }
114
115 int __aul_app_get_pkgid_bypid_fake(int pid, char* pkgid, int len) {
116   snprintf(pkgid, 10, "%s", "test");
117   return 0;
118 }
119
120 struct instance_data {
121   Evas_Object* win;
122   guint iconify_timer;
123   bool is_iconified;
124 };
125
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));
139
140   return data;
141 }
142
143 const char* __appcore_multiwindow_base_instance_get_id_fake(
144     appcore_multiwindow_base_instance_h handle) {
145   return strdup("test");
146 }
147
148 const char* __appcore_multiwindow_base_instance_get_class_id_fake(
149     appcore_multiwindow_base_instance_h context) {
150   return strdup("test");
151 }
152
153 appcore_multiwindow_base_class __cls;
154 void __appcore_multiwindow_base_class_add_fake(
155     appcore_multiwindow_base_class cls) {
156   __cls = 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);
163 }
164
165 int __appcore_multiwindow_base_init_fake(appcore_multiwindow_base_ops ops,
166     int argc, char** argv, void* data) {
167   tizen_base::Bundle b;
168
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);
175
176   b.Delete("__WIDGET_OP__");
177   b.Add("__WIDGET_OP__", "resize");
178   ops.base.control(b.GetHandle(), nullptr);
179
180   b.Delete("__WIDGET_OP__");
181   b.Add("__WIDGET_OP__", "update");
182   ops.base.control(b.GetHandle(), nullptr);
183
184   b.Delete("__WIDGET_OP__");
185   b.Add("__WIDGET_OP__", "destroy");
186   ops.base.control(b.GetHandle(), nullptr);
187
188   b.Delete("__WIDGET_OP__");
189   b.Add("__WIDGET_OP__", "resume");
190   ops.base.control(b.GetHandle(), nullptr);
191
192   b.Delete("__WIDGET_OP__");
193   b.Add("__WIDGET_OP__", "pause");
194   ops.base.control(b.GetHandle(), nullptr);
195
196   b.Delete("__WIDGET_OP__");
197   b.Add("__WIDGET_OP__", "terminate");
198   ops.base.control(b.GetHandle(), nullptr);
199
200   b.Delete("__WIDGET_OP__");
201   b.Add("__WIDGET_OP__", "period");
202   ops.base.control(b.GetHandle(), nullptr);
203
204   ops.base.create(nullptr);
205   ops.base.terminate(nullptr);
206
207   return 0;
208 }
209
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);
215
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);
220   ASSERT_NE(argc, 0);
221
222   widget_app_lifecycle_callback_s callback;
223   callback.create = __app_create_cb;
224   callback.terminate = __app_terminate_cb;
225
226   EXPECT_CALL(GetMock<EcoreWl2Mock>(), ecore_wl2_display_disconnect(_)).
227        Times(1);
228
229   EXPECT_CALL(GetMock<EcoreWl2Mock>(), ecore_wl2_shutdown()).
230        Times(1);
231
232   EXPECT_CALL(GetMock<SystemInfoMock>(),
233       system_info_get_platform_bool(_, _)).
234           WillOnce(DoAll(
235                   SetArgPointee<1>(true),
236                   Return(0)));
237
238   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
239       appcore_multiwindow_base_init(_, _, _, _)).
240           WillOnce(Invoke(__appcore_multiwindow_base_init_fake));
241
242   EXPECT_CALL(GetMock<AulMock>(),
243       aul_app_get_pkgid_bypid(_, _, _)).
244           WillOnce(Invoke(__aul_app_get_pkgid_bypid_fake));
245
246   EXPECT_CALL(GetMock<AppCommonMock>(), app_get_id(_)).
247           WillOnce(Invoke(__app_get_id_fake));
248
249   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
250       appcore_multiwindow_base_instance_find(_)).
251           WillRepeatedly(
252             Return(
253               (appcore_multiwindow_base_instance_h)calloc(1, sizeof(char))));
254
255   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
256       appcore_multiwindow_base_instance_get_class(_)).
257           WillRepeatedly(
258             Return(&__cls));
259
260   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
261       appcore_multiwindow_base_instance_get_extra(_)).
262       WillRepeatedly(
263         Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
264
265   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
266       appcore_multiwindow_base_instance_exit(_)).Times(2);
267
268   ret = widget_app_main(argc, argv, &callback, nullptr);
269   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
270 }
271
272 TEST_F(WidgetAppTest, widget_app_exit) {
273   int ret = widget_app_exit();
274   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
275 }
276
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);
282 }
283
284 bool __widget_context_cb(widget_context_h context, void *user_data) {
285   return true;
286 }
287
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);
291 }
292
293 void __app_event_cb(app_event_info_h event_info, void *user_data) {
294 }
295
296 TEST_F(WidgetAppTest, widget_app_add_event_handler) {
297   app_event_handler_h handle;
298   EXPECT_CALL(GetMock<SystemInfoMock>(),
299       system_info_get_platform_bool(_, _)).
300           WillOnce(DoAll(
301                   SetArgPointee<1>(true),
302                   Return(0)));
303   int ret = widget_app_add_event_handler(&handle, APP_EVENT_LOW_MEMORY,
304       __app_event_cb, nullptr);
305   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
306 }
307
308 TEST_F(WidgetAppTest, widget_app_remove_event_handler) {
309   app_event_handler_h handle;
310   EXPECT_CALL(GetMock<SystemInfoMock>(),
311       system_info_get_platform_bool(_, _)).
312           WillRepeatedly(DoAll(
313                   SetArgPointee<1>(true),
314                   Return(0)));
315   int ret = widget_app_add_event_handler(&handle, APP_EVENT_LOW_MEMORY,
316       __app_event_cb, nullptr);
317   ret = widget_app_remove_event_handler(handle);
318   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
319 }
320
321 TEST_F(WidgetAppTest, widget_app_get_id) {
322   widget_context_h context =
323       (widget_context_h)calloc(1, sizeof(widget_context_h));
324
325   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
326       appcore_multiwindow_base_instance_get_id(_)).
327           WillOnce(Return("test"));
328
329   const char *id = widget_app_get_id(context);
330   EXPECT_STREQ(id, "test");
331 }
332
333 TEST_F(WidgetAppTest, widget_app_class_create) {
334   widget_instance_lifecycle_callback_s lifecycle;
335
336   lifecycle.create = __instance_create_cb;
337   lifecycle.destroy = __instance_destroy_cb;
338   lifecycle.pause = __instance_pause_cb;
339   lifecycle.resume = __instance_resume_cb;
340   lifecycle.resize = __instance_resize_cb;
341   lifecycle.update = __instance_update_cb;
342
343   EXPECT_CALL(GetMock<AppCommonMock>(), app_get_id(_)).
344           WillOnce(Invoke(__app_get_id_fake));
345
346   widget_class_h cls = widget_app_class_create(lifecycle, nullptr);
347   EXPECT_NE(nullptr, cls);
348 }
349
350 TEST_F(WidgetAppTest, widget_app_context_set_tag) {
351   widget_context_h context =
352       (widget_context_h)calloc(1, sizeof(widget_context_h));
353
354   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
355       appcore_multiwindow_base_instance_get_extra(_)).
356       WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
357
358   int ret = widget_app_context_set_tag(context, (void*)"test");
359   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
360 }
361
362 TEST_F(WidgetAppTest, widget_app_context_get_tag) {
363   widget_context_h context =
364       (widget_context_h)calloc(1, sizeof(widget_context_h));
365
366   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
367       appcore_multiwindow_base_instance_get_extra(_)).
368       WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
369
370   void* tag;
371   int ret = widget_app_context_get_tag(context, &tag);
372   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
373 }
374
375 TEST_F(WidgetAppTest, widget_app_context_set_content_info) {
376   widget_context_h context =
377       (widget_context_h)calloc(1, sizeof(widget_context_h));
378
379   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
380       appcore_multiwindow_base_instance_get_extra(_)).
381       WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
382
383   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
384       appcore_multiwindow_base_instance_get_id(_)).
385       WillOnce(Invoke(__appcore_multiwindow_base_instance_get_id_fake));
386
387   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
388       appcore_multiwindow_base_instance_get_class_id(_)).
389       WillOnce(Invoke(__appcore_multiwindow_base_instance_get_class_id_fake));
390
391   bundle* content_info = bundle_create();
392   int ret = widget_app_context_set_content_info(context, content_info);
393   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
394 }
395
396 TEST_F(WidgetAppTest, widget_app_context_set_title) {
397   widget_context_h context =
398       (widget_context_h)calloc(1, sizeof(widget_context_h));
399
400   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
401       appcore_multiwindow_base_instance_get_extra(_)).
402       WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
403
404   int ret = widget_app_context_set_title(context, "title");
405   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
406 }
407
408 TEST_F(WidgetAppTest, widget_app_class_add) {
409   widget_instance_lifecycle_callback_s lifecycle;
410   widget_class_h cls = widget_app_class_add(
411       nullptr, "test", lifecycle, nullptr);
412
413   lifecycle.create = __instance_create_cb;
414   lifecycle.destroy = __instance_destroy_cb;
415   lifecycle.pause = __instance_pause_cb;
416   lifecycle.resume = __instance_resume_cb;
417   lifecycle.resize = __instance_resize_cb;
418   lifecycle.update = __instance_update_cb;
419
420   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
421       appcore_multiwindow_base_class_add(_)).
422       WillOnce(Invoke(__appcore_multiwindow_base_class_add_fake));
423
424   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
425       appcore_multiwindow_base_instance_get_extra(_)).
426       WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
427
428   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
429       appcore_multiwindow_base_instance_get_class_id(_)).
430       WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_class_id_fake));
431
432   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
433       appcore_multiwindow_base_instance_drop(_)).
434       Times(1);
435
436   cls = widget_app_class_add(
437       nullptr, "test", lifecycle, nullptr);
438   EXPECT_NE(nullptr, cls);
439
440   tizen_base::Bundle b;
441   b.Add("__AUL_WIDGET_VIEWER__", "test");
442   int ret = bundle_add_str(b.GetHandle(), "KEY", "VALUE");
443   ASSERT_EQ(ret, BUNDLE_ERROR_NONE);
444
445   char** argv = nullptr;
446   int argc = bundle_export_to_argv(b.GetHandle(), &argv);
447   ASSERT_EQ(get_last_result(), BUNDLE_ERROR_NONE);
448   ASSERT_NE(argv, nullptr);
449   ASSERT_NE(argc, 0);
450
451   widget_app_lifecycle_callback_s callback;
452   callback.create = __app_create_cb;
453   callback.terminate = __app_terminate_cb;
454
455   ret = widget_app_main(argc, argv, &callback, nullptr);
456   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
457 }
458
459 TEST_F(WidgetAppTest, widget_app_get_elm_win_negative) {
460   Evas_Object* win;
461   widget_context_h context =
462       (widget_context_h)calloc(1, sizeof(widget_context_h));
463   int ret = widget_app_get_elm_win(context, &win);
464   EXPECT_EQ(WIDGET_ERROR_FAULT, ret);
465 }
466
467 TEST_F(WidgetAppTest, widget_app_get_elm_win) {
468   EXPECT_CALL(GetMock<ElmMock>(),
469       elm_win_add(_, _, _)).
470       WillOnce(Return((Evas_Object*)calloc(1, sizeof(char))));
471
472   EXPECT_CALL(GetMock<EcoreWl2Mock>(),
473       ecore_evas_wayland2_window_get(_)).
474       WillOnce(Return((Ecore_Wl2_Window*)calloc(1, sizeof(char))));
475
476   EXPECT_CALL(GetMock<ElmMock>(),
477       elm_win_aux_hint_add(_, _, _)).
478       WillOnce(Return(0));
479
480   EXPECT_CALL(GetMock<EcoreWl2Mock>(),
481       ecore_wl2_window_surface_get(_)).
482       WillOnce(Return(nullptr));
483
484   EXPECT_CALL(GetMock<EcoreWl2Mock>(),
485       evas_object_event_callback_add(_, _, _, _)).
486       Times(1);
487
488   EXPECT_CALL(GetMock<EcoreWl2Mock>(),
489       evas_object_data_set(_, _, _)).
490       Times(1);
491
492   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
493       appcore_multiwindow_base_instance_get_extra(_)).
494       WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
495
496   Evas_Object* win;
497   widget_context_h context =
498       (widget_context_h)calloc(1, sizeof(widget_context_h));
499   int ret = widget_app_get_elm_win(context, &win);
500   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
501 }