2 * Copyright (c) 2020 - 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <bundle_cpp.h>
18 #include <bundle_internal.h>
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
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"
39 using ::testing::DoAll;
40 using ::testing::Return;
41 using ::testing::SetArgPointee;
42 using ::testing::Invoke;
46 int __instance_create_cb(widget_context_h context, bundle* content,
47 int w, int h, void* user_data) {
51 int __instance_destroy_cb(widget_context_h context,
52 widget_app_destroy_type_e reason, bundle* content, void* user_data) {
57 int __instance_pause_cb(widget_context_h context, void* user_data) {
61 int __instance_resume_cb(widget_context_h context, void* user_data) {
65 int __instance_update_cb(widget_context_h context,
66 bundle* content, int force, void* user_data) {
71 int __instance_resize_cb(widget_context_h context,
72 int w, int h, void* user_data) {
76 void __app_terminate_cb(void* user_data) {
79 int __app_get_id_fake(char** id) {
84 int __aul_app_get_appid_bypid_fake(int pid, char* appid, int len) {
85 snprintf(appid, len, "%s", "test");
89 int __aul_app_get_pkgid_bypid_fake(int pid, char* pkgid, int len) {
90 snprintf(pkgid, len, "%s", "test");
94 void __app_event_cb(app_event_info_h event_info, void *user_data) {
97 bool __widget_context_cb(widget_context_h context, void *user_data) {
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> {};
111 class WidgetAppTest : public TestFixture {
113 WidgetAppTest() : TestFixture(std::make_unique<Mocks>()) {}
114 virtual ~WidgetAppTest() {}
116 virtual void SetUp() {
119 virtual void TearDown() {
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);
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);
134 widget_app_lifecycle_callback_s callback;
135 callback.create = created_cb;
136 callback.terminate = __app_terminate_cb;
138 EXPECT_CALL(GetMock<SystemInfoMock>(),
139 system_info_get_platform_bool(_, _)).
140 WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(0)));
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));
158 EXPECT_CALL(GetMock<AppCommonMock>(),
159 app_get_id(_)).WillRepeatedly(Invoke(__app_get_id_fake));
161 EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
162 Run(_, _)).WillOnce(Invoke(this, &WidgetAppTest::__run_fake));
164 ret = widget_app_main(argc, argv, &callback, data);
165 bundle_free_exported_argv(argc, &argv);
166 EXPECT_EQ(ret, WIDGET_ERROR_NONE);
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);
181 }, reinterpret_cast<void*>(created_cb));
184 void __run_fake(int argc, char** argv) {
185 tizen_base::Bundle b;
186 auto* p = GetMock<MultiWindowBaseMock>().real_;
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");
197 b.Delete("__WIDGET_OP__");
198 b.Add("__WIDGET_OP__", "resize");
201 b.Delete("__WIDGET_OP__");
202 b.Add("__WIDGET_OP__", "update");
205 b.Delete("__WIDGET_OP__");
206 b.Add("__WIDGET_OP__", "destroy");
209 b.Delete("__WIDGET_OP__");
210 b.Add("__WIDGET_OP__", "resume");
213 b.Delete("__WIDGET_OP__");
214 b.Add("__WIDGET_OP__", "pause");
217 b.Delete("__WIDGET_OP__");
218 b.Add("__WIDGET_OP__", "terminate");
221 b.Delete("__WIDGET_OP__");
222 b.Add("__WIDGET_OP__", "period");
225 p->OnReceive(AUL_WIDGET_CONTENT, b);
231 TEST_F(WidgetAppTest, widget_app_exit_n) {
232 int ret = widget_app_exit();
233 EXPECT_NE(ret, WIDGET_ERROR_NONE);
236 TEST_F(WidgetAppTest, widget_app_exit) {
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);
248 TEST_F(WidgetAppTest, widget_app_main) {
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));
259 TEST_F(WidgetAppTest, widget_app_restart) {
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));
266 widget_app_restart();
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(_, _)).
276 SetArgPointee<1>(true),
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);
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),
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);
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);
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;
315 widget_class_h cls = widget_app_class_create(lifecycle, nullptr);
316 EXPECT_NE(cls, nullptr);
318 int ret = widget_app_foreach_context(__widget_context_cb, nullptr);
319 EXPECT_EQ(ret, WIDGET_ERROR_NONE);
325 TEST_F(WidgetAppTest, widget_app_get_id) {
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");
335 TEST_F(WidgetAppTest, widget_app_class_create) {
336 PrepareContext(__instance_create_cb);
339 TEST_F(WidgetAppTest, widget_app_context_set_tag) {
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);
350 TEST_F(WidgetAppTest, widget_app_context_get_tag) {
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);
358 ret = widget_app_context_get_tag(context, &tag);
359 EXPECT_EQ(ret, WIDGET_ERROR_NONE);
364 TEST_F(WidgetAppTest, widget_app_context_set_content_info) {
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);
377 TEST_F(WidgetAppTest, widget_app_context_set_title) {
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);
387 TEST_F(WidgetAppTest, widget_app_class_add) {
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;
398 widget_class_h cls = widget_app_class_add(
399 nullptr, "test", lifecycle, nullptr);
400 EXPECT_NE(cls, nullptr);
405 TEST_F(WidgetAppTest, widget_app_get_elm_win_negative) {
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);
416 TEST_F(WidgetAppTest, widget_app_get_elm_win) {
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)))));
425 EXPECT_CALL(GetMock<EcoreWl2Mock>(),
426 ecore_evas_wayland2_window_get(_)).
427 WillOnce(Return(reinterpret_cast<Ecore_Wl2_Window*>(
428 calloc(1, sizeof(char)))));
430 EXPECT_CALL(GetMock<ElmMock>(),
431 elm_win_aux_hint_add(_, _, _)).WillOnce(Return(0));
433 EXPECT_CALL(GetMock<EcoreWl2Mock>(),
434 ecore_wl2_window_surface_get(_)).WillOnce(Return(nullptr));
436 EXPECT_CALL(GetMock<EcoreWl2Mock>(),
437 evas_object_event_callback_add(_, _, _, _)).Times(1);
439 EXPECT_CALL(GetMock<EcoreWl2Mock>(),
440 evas_object_data_set(_, _, _)).Times(1);
442 Evas_Object* win = nullptr;
443 int ret = widget_app_get_elm_win(context, &win);
444 EXPECT_EQ(ret, WIDGET_ERROR_NONE);