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