7c73261d060438d94fa0f19f13a8c92b3c56824e
[platform/core/appfw/appcore-widget.git] / test / unit_tests / test_widget_app.cc
1 /*
2  * Copyright (c) 2020 - 2021 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 <bundle_cpp.h>
18 #include <bundle_internal.h>
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 #include <stdlib.h>
22
23 #include <iostream>
24 #include <memory>
25
26 #include "include/widget_app.h"
27 #include "include/widget_app_efl.h"
28 #include "unit_tests/mock/app_common_mock.h"
29 #include "unit_tests/mock/appcore_multiwindow_base_mock.h"
30 #include "unit_tests/mock/aul_mock.h"
31 #include "unit_tests/mock/ecore_wl2_mock.h"
32 #include "unit_tests/mock/elm_mock.h"
33 #include "unit_tests/mock/gio_mock.h"
34 #include "unit_tests/mock/system_info_mock.h"
35 #include "unit_tests/mock/test_fixture.h"
36 #include "unit_tests/mock/widget_service_mock.h"
37
38 using ::testing::_;
39 using ::testing::DoAll;
40 using ::testing::Return;
41 using ::testing::SetArgPointee;
42 using ::testing::Invoke;
43
44 namespace {
45
46 int __instance_create_cb(widget_context_h context, bundle* content,
47     int w, int h, void* user_data) {
48   return 0;
49 }
50
51 int __instance_destroy_cb(widget_context_h context,
52     widget_app_destroy_type_e reason, bundle* content, void* user_data) {
53   return 0;
54 }
55
56 int __instance_pause_cb(widget_context_h context, void* user_data) {
57   return 0;
58 }
59
60 int __instance_resume_cb(widget_context_h context, void* user_data) {
61   return 0;
62 }
63
64 int __instance_resize_cb(widget_context_h context,
65     int w, int h, void* user_data) {
66   return 0;
67 }
68
69 int __instance_update_cb(widget_context_h context,
70     bundle* content, int force, void* user_data) {
71   return 0;
72 }
73
74 void __app_terminate_cb(void* user_data) {
75 }
76
77 int __app_get_id_fake(char** id) {
78   *id = strdup("test");
79   return 0;
80 }
81
82 int __aul_app_get_appid_bypid_fake(int pid, char* appid, int len) {
83   snprintf(appid, len, "%s", "test");
84   return 0;
85 }
86
87 int __aul_app_get_pkgid_bypid_fake(int pid, char* pkgid, int len) {
88   snprintf(pkgid, len, "%s", "test");
89   return 0;
90 }
91
92 void __app_event_cb(app_event_info_h event_info, void *user_data) {
93 }
94
95 bool __widget_context_cb(widget_context_h context, void *user_data) {
96   return true;
97 }
98
99 }  // namespace
100
101 class Mocks : public ::testing::NiceMock<GioMock>,
102               public ::testing::NiceMock<MultiWindowBaseMock>,
103               public ::testing::NiceMock<AppCommonMock>,
104               public ::testing::NiceMock<WidgetServiceMock>,
105               public ::testing::NiceMock<ElmMock>,
106               public ::testing::NiceMock<AulMock>,
107               public ::testing::NiceMock<SystemInfoMock>,
108               public ::testing::NiceMock<EcoreWl2Mock> {};
109
110 class WidgetAppTest : public TestFixture {
111  public:
112   WidgetAppTest() : TestFixture(std::make_unique<Mocks>()) {}
113   virtual ~WidgetAppTest() {}
114
115   virtual void SetUp() {
116   }
117
118   virtual void TearDown() {
119   }
120
121   void PrepareTest(widget_app_create_cb created_cb, void* data) {
122     tizen_base::Bundle b;
123     b.Add("__AUL_WIDGET_VIEWER__", "test");
124     int ret = bundle_add_str(b.GetHandle(), "KEY", "VALUE");
125     ASSERT_EQ(ret, BUNDLE_ERROR_NONE);
126
127     char** argv = nullptr;
128     int argc = bundle_export_to_argv(b.GetHandle(), &argv);
129     ASSERT_EQ(get_last_result(), BUNDLE_ERROR_NONE);
130     ASSERT_NE(argv, nullptr);
131     ASSERT_NE(argc, 0);
132
133     widget_app_lifecycle_callback_s callback;
134     callback.create = created_cb;
135     callback.terminate = __app_terminate_cb;
136
137     EXPECT_CALL(GetMock<SystemInfoMock>(),
138         system_info_get_platform_bool(_, _)).
139             WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(0)));
140
141     EXPECT_CALL(GetMock<AulMock>(),
142         aul_app_get_appid_bypid(_, _, _)).
143             WillOnce(Invoke(__aul_app_get_appid_bypid_fake));
144     EXPECT_CALL(GetMock<AulMock>(),
145         aul_app_get_pkgid_bypid(_, _, _)).
146             WillOnce(Invoke(__aul_app_get_pkgid_bypid_fake));
147     EXPECT_CALL(GetMock<AulMock>(),
148         aul_widget_send_status_to_service(_, _, _, _)).
149             WillRepeatedly(Return(0));
150     EXPECT_CALL(GetMock<AulMock>(),
151         aul_widget_send_status_to_viewer(_, _, _, _, _, _)).
152             WillRepeatedly(Return(0));
153     EXPECT_CALL(GetMock<WidgetServiceMock>(),
154         widget_instance_convert_event_to_lifecycle_status(_)).
155             WillRepeatedly(Return(0));
156
157     EXPECT_CALL(GetMock<AppCommonMock>(),
158         app_get_id(_)).WillRepeatedly(Invoke(__app_get_id_fake));
159
160     EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
161         Run(_, _)).WillOnce(Invoke(this, &WidgetAppTest::__run_fake));
162
163     ret = widget_app_main(argc, argv, &callback, data);
164     bundle_free_exported_argv(argc, &argv);
165     EXPECT_EQ(ret, WIDGET_ERROR_NONE);
166   }
167
168   void PrepareContext(widget_instance_create_cb created_cb) {
169     PrepareTest([](void* user_data) -> widget_class_h {
170       widget_instance_lifecycle_callback_s lifecycle;
171       lifecycle.create = reinterpret_cast<widget_instance_create_cb>(user_data);
172       lifecycle.destroy = __instance_destroy_cb;
173       lifecycle.pause = __instance_pause_cb;
174       lifecycle.resume = __instance_resume_cb;
175       lifecycle.resize = __instance_resize_cb;
176       lifecycle.update = __instance_update_cb;
177       widget_class_h cls = widget_app_class_create(lifecycle, nullptr);
178       EXPECT_NE(cls, nullptr);
179       return cls;
180     }, reinterpret_cast<void*>(created_cb));
181   }
182
183   void __run_fake(int argc, char** argv) {
184     tizen_base::Bundle b;
185     auto* p = GetMock<MultiWindowBaseMock>().real_;
186
187     p->OnCreate();
188
189     b.Add("__WIDGET_WIDTH__", "320");
190     b.Add("__WIDGET_HEIGHT__", "320");
191     b.Add("__AUL_WIDGET_INSTANCE_ID__", "instance_id");
192     b.Add("__AUL_WIDGET_ID__", "test");
193     b.Add("__WIDGET_OP__", "create");
194     p->OnControl(b);
195
196     b.Delete("__WIDGET_OP__");
197     b.Add("__WIDGET_OP__", "resize");
198     p->OnControl(b);
199
200     b.Delete("__WIDGET_OP__");
201     b.Add("__WIDGET_OP__", "update");
202     p->OnControl(b);
203
204     b.Delete("__WIDGET_OP__");
205     b.Add("__WIDGET_OP__", "destroy");
206     p->OnControl(b);
207
208     b.Delete("__WIDGET_OP__");
209     b.Add("__WIDGET_OP__", "resume");
210     p->OnControl(b);
211
212     b.Delete("__WIDGET_OP__");
213     b.Add("__WIDGET_OP__", "pause");
214     p->OnControl(b);
215
216     b.Delete("__WIDGET_OP__");
217     b.Add("__WIDGET_OP__", "terminate");
218     p->OnControl(b);
219
220     b.Delete("__WIDGET_OP__");
221     b.Add("__WIDGET_OP__", "period");
222     p->OnControl(b);
223
224     p->OnTerminate();
225   }
226 };
227
228 TEST_F(WidgetAppTest, widget_app_exit_n) {
229   int ret = widget_app_exit();
230   EXPECT_NE(ret, WIDGET_ERROR_NONE);
231 }
232
233 TEST_F(WidgetAppTest, widget_app_exit) {
234   PrepareContext(
235       [](widget_context_h context, bundle* content,
236           int w, int h, void* user_data) -> int {
237         EXPECT_CALL(GetMock<AulMock>(),
238             aul_notify_exit()).WillRepeatedly(Return(0));
239         int ret = widget_app_exit();
240         EXPECT_EQ(ret, WIDGET_ERROR_NONE);
241         return 0;
242       });
243 }
244
245 TEST_F(WidgetAppTest, widget_app_main) {
246   PrepareContext(
247       [](widget_context_h context, bundle* content,
248           int w, int h, void* user_data) -> int {
249         EXPECT_CALL(GetMock<AulMock>(),
250             aul_notify_exit()).WillRepeatedly(Return(0));
251         widget_app_exit();
252         return 0;
253       });
254 }
255
256 TEST_F(WidgetAppTest, widget_app_add_event_handler) {
257   app_event_handler_h handle;
258   EXPECT_CALL(GetMock<SystemInfoMock>(),
259       system_info_get_platform_bool(_, _)).
260           WillOnce(DoAll(
261                   SetArgPointee<1>(true),
262                   Return(0)));
263   int ret = widget_app_add_event_handler(&handle, APP_EVENT_LOW_MEMORY,
264       __app_event_cb, nullptr);
265   EXPECT_EQ(ret, WIDGET_ERROR_NONE);
266 }
267
268 TEST_F(WidgetAppTest, widget_app_remove_event_handler) {
269   app_event_handler_h handle;
270   EXPECT_CALL(GetMock<SystemInfoMock>(),
271       system_info_get_platform_bool(_, _)).
272           WillRepeatedly(DoAll(
273                   SetArgPointee<1>(true),
274                   Return(0)));
275   int ret = widget_app_add_event_handler(&handle, APP_EVENT_LOW_MEMORY,
276       __app_event_cb, nullptr);
277   ret = widget_app_remove_event_handler(handle);
278   EXPECT_EQ(ret, WIDGET_ERROR_NONE);
279 }
280
281 TEST_F(WidgetAppTest, widget_app_terminate_context) {
282   PrepareContext([](widget_context_h context, bundle* content,
283       int w, int h, void* user_data) -> int {
284         int ret = widget_app_terminate_context(context);
285         EXPECT_EQ(ret, WIDGET_ERROR_NONE);
286         return 0;
287       });
288 }
289
290 TEST_F(WidgetAppTest, widget_app_foreach_context) {
291   PrepareTest([](void* user_data) -> widget_class_h {
292     widget_instance_lifecycle_callback_s lifecycle;
293     lifecycle.create = __instance_create_cb;
294     lifecycle.destroy = __instance_destroy_cb;
295     lifecycle.pause = __instance_pause_cb;
296     lifecycle.resume = __instance_resume_cb;
297     lifecycle.resize = __instance_resize_cb;
298     lifecycle.update = __instance_update_cb;
299
300     widget_class_h cls = widget_app_class_create(lifecycle, nullptr);
301     EXPECT_NE(cls, nullptr);
302
303     int ret = widget_app_foreach_context(__widget_context_cb, nullptr);
304     EXPECT_EQ(ret, WIDGET_ERROR_NONE);
305
306     return cls;
307   }, nullptr);
308 }
309
310 TEST_F(WidgetAppTest, widget_app_get_id) {
311   PrepareContext(
312       [](widget_context_h context, bundle* content,
313           int w, int h, void* user_data) -> int {
314         const char *id = widget_app_get_id(context);
315         EXPECT_STREQ(id, "instance_id");
316         return 0;
317       });
318 }
319
320 TEST_F(WidgetAppTest, widget_app_class_create) {
321   PrepareContext(__instance_create_cb);
322 }
323
324 TEST_F(WidgetAppTest, widget_app_context_set_tag) {
325   PrepareContext(
326       [](widget_context_h context, bundle* content,
327           int w, int h, void* user_data) -> int {
328         int ret = widget_app_context_set_tag(context,
329             reinterpret_cast<void*>(const_cast<char*>("test")));
330         EXPECT_EQ(ret, WIDGET_ERROR_NONE);
331         return 0;
332       });
333 }
334
335 TEST_F(WidgetAppTest, widget_app_context_get_tag) {
336   PrepareContext(
337       [](widget_context_h context, bundle* content,
338           int w, int h, void* user_data) -> int {
339         int ret = widget_app_context_set_tag(context,
340             reinterpret_cast<void*>(const_cast<char*>("test")));
341         EXPECT_EQ(ret, WIDGET_ERROR_NONE);
342         void* tag = nullptr;
343         ret = widget_app_context_get_tag(context, &tag);
344         EXPECT_EQ(ret, WIDGET_ERROR_NONE);
345         return 0;
346       });
347 }
348
349 TEST_F(WidgetAppTest, widget_app_context_set_content_info) {
350   PrepareContext(
351       [](widget_context_h context, bundle* content,
352           int w, int h, void* user_data) -> int {
353         bundle* content_info = bundle_create();
354         EXPECT_NE(content_info, nullptr);
355         int ret = widget_app_context_set_content_info(context, content_info);
356         bundle_free(content_info);
357         EXPECT_EQ(ret, WIDGET_ERROR_NONE);
358         return 0;
359       });
360 }
361
362 TEST_F(WidgetAppTest, widget_app_context_set_title) {
363   PrepareContext(
364       [](widget_context_h context, bundle* content,
365           int w, int h, void* user_data) -> int {
366         int ret = widget_app_context_set_title(context, "title");
367         EXPECT_EQ(ret, WIDGET_ERROR_NONE);
368         return 0;
369       });
370 }
371
372 TEST_F(WidgetAppTest, widget_app_class_add) {
373   PrepareTest(
374       [](void* user_data) -> widget_class_h {
375         widget_instance_lifecycle_callback_s lifecycle;
376         lifecycle.create = __instance_create_cb;
377         lifecycle.destroy = __instance_destroy_cb;
378         lifecycle.pause = __instance_pause_cb;
379         lifecycle.resume = __instance_resume_cb;
380         lifecycle.resize = __instance_resize_cb;
381         lifecycle.update = __instance_update_cb;
382
383         widget_class_h cls = widget_app_class_add(
384             nullptr, "test", lifecycle, nullptr);
385         EXPECT_NE(cls, nullptr);
386         return cls;
387     }, nullptr);
388 }
389
390 TEST_F(WidgetAppTest, widget_app_get_elm_win_negative) {
391   PrepareContext(
392       [](widget_context_h context, bundle* content,
393           int w, int h, void* user_data) -> int {
394         Evas_Object* win = nullptr;
395         int ret = widget_app_get_elm_win(context, &win);
396         EXPECT_EQ(ret, WIDGET_ERROR_FAULT);
397         return 0;
398       });
399 }
400
401 TEST_F(WidgetAppTest, widget_app_get_elm_win) {
402   PrepareContext(
403       [](widget_context_h context, bundle* content,
404           int w, int h, void* user_data) -> int {
405         EXPECT_CALL(GetMock<ElmMock>(),
406             elm_win_add(_, _, _)).
407                 WillOnce(Return(reinterpret_cast<Evas_Object*>(
408                     calloc(1, sizeof(char)))));
409
410         EXPECT_CALL(GetMock<EcoreWl2Mock>(),
411             ecore_evas_wayland2_window_get(_)).
412                 WillOnce(Return(reinterpret_cast<Ecore_Wl2_Window*>(
413                     calloc(1, sizeof(char)))));
414
415         EXPECT_CALL(GetMock<ElmMock>(),
416             elm_win_aux_hint_add(_, _, _)).WillOnce(Return(0));
417
418         EXPECT_CALL(GetMock<EcoreWl2Mock>(),
419             ecore_wl2_window_surface_get(_)).WillOnce(Return(nullptr));
420
421         EXPECT_CALL(GetMock<EcoreWl2Mock>(),
422             evas_object_event_callback_add(_, _, _, _)).Times(1);
423
424         EXPECT_CALL(GetMock<EcoreWl2Mock>(),
425             evas_object_data_set(_, _, _)).Times(1);
426
427         Evas_Object* win = nullptr;
428         int ret = widget_app_get_elm_win(context, &win);
429         EXPECT_EQ(ret, WIDGET_ERROR_NONE);
430         return -1;
431       });
432 }