Fix wrong size issue
[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   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
274 }
275
276 TEST_F(WidgetAppTest, widget_app_exit) {
277   int ret = widget_app_exit();
278   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
279 }
280
281 TEST_F(WidgetAppTest, widget_app_terminate_context) {
282   widget_context_h context =
283       (widget_context_h)calloc(1, sizeof(struct _widget_context));
284   int ret = widget_app_terminate_context(context);
285   free(context);
286   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
287 }
288
289 bool __widget_context_cb(widget_context_h context, void *user_data) {
290   return true;
291 }
292
293 TEST_F(WidgetAppTest, widget_app_foreach_context) {
294   int ret = widget_app_foreach_context(__widget_context_cb, nullptr);
295   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
296 }
297
298 void __app_event_cb(app_event_info_h event_info, void *user_data) {
299 }
300
301 TEST_F(WidgetAppTest, widget_app_add_event_handler) {
302   app_event_handler_h handle;
303   EXPECT_CALL(GetMock<SystemInfoMock>(),
304       system_info_get_platform_bool(_, _)).
305           WillOnce(DoAll(
306                   SetArgPointee<1>(true),
307                   Return(0)));
308   int ret = widget_app_add_event_handler(&handle, APP_EVENT_LOW_MEMORY,
309       __app_event_cb, nullptr);
310   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
311 }
312
313 TEST_F(WidgetAppTest, widget_app_remove_event_handler) {
314   app_event_handler_h handle;
315   EXPECT_CALL(GetMock<SystemInfoMock>(),
316       system_info_get_platform_bool(_, _)).
317           WillRepeatedly(DoAll(
318                   SetArgPointee<1>(true),
319                   Return(0)));
320   int ret = widget_app_add_event_handler(&handle, APP_EVENT_LOW_MEMORY,
321       __app_event_cb, nullptr);
322   ret = widget_app_remove_event_handler(handle);
323   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
324 }
325
326 TEST_F(WidgetAppTest, widget_app_get_id) {
327   widget_context_h context =
328       (widget_context_h)calloc(1, sizeof(struct _widget_context));
329
330   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
331       appcore_multiwindow_base_instance_get_id(_)).
332           WillOnce(Return("test"));
333
334   const char *id = widget_app_get_id(context);
335   free(context);
336   EXPECT_STREQ(id, "test");
337 }
338
339 TEST_F(WidgetAppTest, widget_app_class_create) {
340   widget_instance_lifecycle_callback_s lifecycle;
341
342   lifecycle.create = __instance_create_cb;
343   lifecycle.destroy = __instance_destroy_cb;
344   lifecycle.pause = __instance_pause_cb;
345   lifecycle.resume = __instance_resume_cb;
346   lifecycle.resize = __instance_resize_cb;
347   lifecycle.update = __instance_update_cb;
348
349   EXPECT_CALL(GetMock<AppCommonMock>(), app_get_id(_)).
350           WillOnce(Invoke(__app_get_id_fake));
351
352   widget_class_h cls = widget_app_class_create(lifecycle, nullptr);
353   EXPECT_NE(nullptr, cls);
354 }
355
356 TEST_F(WidgetAppTest, widget_app_context_set_tag) {
357   widget_context_h context =
358       (widget_context_h)calloc(1, sizeof(struct _widget_context));
359
360   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
361       appcore_multiwindow_base_instance_get_extra(_)).
362       WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
363
364   int ret = widget_app_context_set_tag(context, (void*)"test");
365   free(context);
366   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
367 }
368
369 TEST_F(WidgetAppTest, widget_app_context_get_tag) {
370   widget_context_h context =
371       (widget_context_h)calloc(1, sizeof(struct _widget_context));
372
373   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
374       appcore_multiwindow_base_instance_get_extra(_)).
375       WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
376
377   void* tag;
378   int ret = widget_app_context_get_tag(context, &tag);
379   free(context);
380   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
381 }
382
383 TEST_F(WidgetAppTest, widget_app_context_set_content_info) {
384   widget_context_h context =
385       (widget_context_h)calloc(1, sizeof(struct _widget_context));
386
387   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
388       appcore_multiwindow_base_instance_get_extra(_)).
389       WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
390
391   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
392       appcore_multiwindow_base_instance_get_id(_)).
393       WillOnce(Invoke(__appcore_multiwindow_base_instance_get_id_fake));
394
395   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
396       appcore_multiwindow_base_instance_get_class_id(_)).
397       WillOnce(Invoke(__appcore_multiwindow_base_instance_get_class_id_fake));
398
399   bundle* content_info = bundle_create();
400   int ret = widget_app_context_set_content_info(context, content_info);
401   free(context);
402   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
403 }
404
405 TEST_F(WidgetAppTest, widget_app_context_set_title) {
406   widget_context_h context =
407       (widget_context_h)calloc(1, sizeof(struct _widget_context));
408
409   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
410       appcore_multiwindow_base_instance_get_extra(_)).
411       WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
412
413   int ret = widget_app_context_set_title(context, "title");
414   free(context);
415   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
416 }
417
418 TEST_F(WidgetAppTest, widget_app_class_add) {
419   widget_instance_lifecycle_callback_s lifecycle;
420   lifecycle.create = __instance_create_cb;
421   lifecycle.destroy = __instance_destroy_cb;
422   lifecycle.pause = __instance_pause_cb;
423   lifecycle.resume = __instance_resume_cb;
424   lifecycle.resize = __instance_resize_cb;
425   lifecycle.update = __instance_update_cb;
426
427   widget_class_h cls = widget_app_class_add(
428       nullptr, "test", lifecycle, nullptr);
429
430   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
431       appcore_multiwindow_base_class_add(_)).
432       WillOnce(Invoke(__appcore_multiwindow_base_class_add_fake));
433
434   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
435       appcore_multiwindow_base_instance_get_extra(_)).
436       WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
437
438   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
439       appcore_multiwindow_base_instance_get_class_id(_)).
440       WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_class_id_fake));
441
442   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
443       appcore_multiwindow_base_instance_drop(_)).
444       Times(1);
445
446   cls = widget_app_class_add(
447       nullptr, "test", lifecycle, nullptr);
448   EXPECT_NE(nullptr, cls);
449
450   tizen_base::Bundle b;
451   b.Add("__AUL_WIDGET_VIEWER__", "test");
452   int ret = bundle_add_str(b.GetHandle(), "KEY", "VALUE");
453   ASSERT_EQ(ret, BUNDLE_ERROR_NONE);
454
455   char** argv = nullptr;
456   int argc = bundle_export_to_argv(b.GetHandle(), &argv);
457   ASSERT_EQ(get_last_result(), BUNDLE_ERROR_NONE);
458   ASSERT_NE(argv, nullptr);
459   ASSERT_NE(argc, 0);
460
461   widget_app_lifecycle_callback_s callback;
462   callback.create = __app_create_cb;
463   callback.terminate = __app_terminate_cb;
464
465   ret = widget_app_main(argc, argv, &callback, nullptr);
466   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
467 }
468
469 TEST_F(WidgetAppTest, widget_app_get_elm_win_negative) {
470   Evas_Object* win;
471   widget_context_h context =
472       (widget_context_h)calloc(1, sizeof(struct _widget_context));
473   int ret = widget_app_get_elm_win(context, &win);
474   free(context);
475   EXPECT_EQ(WIDGET_ERROR_FAULT, ret);
476 }
477
478 TEST_F(WidgetAppTest, widget_app_get_elm_win) {
479   EXPECT_CALL(GetMock<ElmMock>(),
480       elm_win_add(_, _, _)).
481       WillOnce(Return((Evas_Object*)calloc(1, sizeof(char))));
482
483   EXPECT_CALL(GetMock<EcoreWl2Mock>(),
484       ecore_evas_wayland2_window_get(_)).
485       WillOnce(Return((Ecore_Wl2_Window*)calloc(1, sizeof(char))));
486
487   EXPECT_CALL(GetMock<ElmMock>(),
488       elm_win_aux_hint_add(_, _, _)).
489       WillOnce(Return(0));
490
491   EXPECT_CALL(GetMock<EcoreWl2Mock>(),
492       ecore_wl2_window_surface_get(_)).
493       WillOnce(Return(nullptr));
494
495   EXPECT_CALL(GetMock<EcoreWl2Mock>(),
496       evas_object_event_callback_add(_, _, _, _)).
497       Times(1);
498
499   EXPECT_CALL(GetMock<EcoreWl2Mock>(),
500       evas_object_data_set(_, _, _)).
501       Times(1);
502
503   EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
504       appcore_multiwindow_base_instance_get_extra(_)).
505       WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
506
507   Evas_Object* win;
508   widget_context_h context =
509       (widget_context_h)calloc(1, sizeof(struct _widget_context));
510   int ret = widget_app_get_elm_win(context, &win);
511   free(context);
512   EXPECT_EQ(WIDGET_ERROR_NONE, ret);
513 }