Add widget efl unittests
[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   int ret = widget_app_add_event_handler(&handle, APP_EVENT_LOW_MEMORY,
299       __app_event_cb, nullptr);
300   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
301 }
302
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);
309 }
310
311 TEST_F(WidgetAppTest, widget_app_get_id) {
312   widget_context_h context =
313       (widget_context_h)calloc(1, sizeof(widget_context_h));
314
315   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
316       appcore_multiwindow_base_instance_get_id(_)).
317           WillOnce(Return("test"));
318
319   const char *id = widget_app_get_id(context);
320   EXPECT_STREQ(id, "test");
321 }
322
323 TEST_F(WidgetAppTest, widget_app_class_create) {
324   widget_instance_lifecycle_callback_s lifecycle;
325
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;
332
333   EXPECT_CALL(GetMock<AppCommonMock>(), app_get_id(_)).
334           WillOnce(Invoke(__app_get_id_fake));
335
336   widget_class_h cls = widget_app_class_create(lifecycle, nullptr);
337   EXPECT_NE(nullptr, cls);
338 }
339
340 TEST_F(WidgetAppTest, widget_app_context_set_tag) {
341   widget_context_h context =
342       (widget_context_h)calloc(1, sizeof(widget_context_h));
343
344   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
345       appcore_multiwindow_base_instance_get_extra(_)).
346       WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
347
348   int ret = widget_app_context_set_tag(context, (void*)"test");
349   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
350 }
351
352 TEST_F(WidgetAppTest, widget_app_context_get_tag) {
353   widget_context_h context =
354       (widget_context_h)calloc(1, sizeof(widget_context_h));
355
356   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
357       appcore_multiwindow_base_instance_get_extra(_)).
358       WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
359
360   void* tag;
361   int ret = widget_app_context_get_tag(context, &tag);
362   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
363 }
364
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));
368
369   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
370       appcore_multiwindow_base_instance_get_extra(_)).
371       WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
372
373   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
374       appcore_multiwindow_base_instance_get_id(_)).
375       WillOnce(Invoke(__appcore_multiwindow_base_instance_get_id_fake));
376
377   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
378       appcore_multiwindow_base_instance_get_class_id(_)).
379       WillOnce(Invoke(__appcore_multiwindow_base_instance_get_class_id_fake));
380
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);
384 }
385
386 TEST_F(WidgetAppTest, widget_app_context_set_title) {
387   widget_context_h context =
388       (widget_context_h)calloc(1, sizeof(widget_context_h));
389
390   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
391       appcore_multiwindow_base_instance_get_extra(_)).
392       WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
393
394   int ret = widget_app_context_set_title(context, "title");
395   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
396 }
397
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);
402
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;
409
410   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
411       appcore_multiwindow_base_class_add(_)).
412       WillOnce(Invoke(__appcore_multiwindow_base_class_add_fake));
413
414   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
415       appcore_multiwindow_base_instance_get_extra(_)).
416       WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
417
418   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
419       appcore_multiwindow_base_instance_get_class_id(_)).
420       WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_class_id_fake));
421
422   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
423       appcore_multiwindow_base_instance_drop(_)).
424       Times(1);
425
426   cls = widget_app_class_add(
427       nullptr, "test", lifecycle, nullptr);
428   EXPECT_NE(nullptr, cls);
429
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);
434
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);
439   ASSERT_NE(argc, 0);
440
441   widget_app_lifecycle_callback_s callback;
442   callback.create = __app_create_cb;
443   callback.terminate = __app_terminate_cb;
444
445   ret = widget_app_main(argc, argv, &callback, nullptr);
446   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
447 }
448
449 TEST_F(WidgetAppTest, widget_app_get_elm_win_negative) {
450   Evas_Object* win;
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);
455 }
456
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))));
461
462   EXPECT_CALL(GetMock<EcoreWl2Mock>(),
463       ecore_evas_wayland2_window_get(_)).
464       WillOnce(Return((Ecore_Wl2_Window*)calloc(1, sizeof(char))));
465
466   EXPECT_CALL(GetMock<ElmMock>(),
467       elm_win_aux_hint_add(_, _, _)).
468       WillOnce(Return(0));
469
470   EXPECT_CALL(GetMock<EcoreWl2Mock>(),
471       ecore_wl2_window_surface_get(_)).
472       WillOnce(Return(nullptr));
473
474   EXPECT_CALL(GetMock<EcoreWl2Mock>(),
475       evas_object_event_callback_add(_, _, _, _)).
476       Times(1);
477
478   EXPECT_CALL(GetMock<EcoreWl2Mock>(),
479       evas_object_data_set(_, _, _)).
480       Times(1);
481
482   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
483       appcore_multiwindow_base_instance_get_extra(_)).
484       WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
485
486   Evas_Object* win;
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);
491 }