Change unittest package name and improves code coverage
[platform/core/appfw/appcore-widget.git] / test / unit_tests / test_widget_app.cc
index 3966a4f..dce49a5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 - 2021 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * limitations under the License.
  */
 
-#include <stdlib.h>
-#include <gtest/gtest.h>
-#include <gmock/gmock.h>
 #include <bundle_cpp.h>
 #include <bundle_internal.h>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include <stdlib.h>
 
 #include <iostream>
 #include <memory>
 
-#include "widget_app.h"
-#include "widget_app_efl.h"
-#include "unit_tests/mock/gio_mock.h"
-#include "unit_tests/mock/ecore_wl2_mock.h"
-#include "unit_tests/mock/system_info_mock.h"
-#include "unit_tests/mock/appcore_multiwindow_base_mock.h"
+#include "include/widget_app.h"
+#include "include/widget_app_internal.h"
+#include "include/widget_app_efl.h"
 #include "unit_tests/mock/app_common_mock.h"
-#include "unit_tests/mock/elm_mock.h"
+#include "unit_tests/mock/appcore_multiwindow_base_mock.h"
 #include "unit_tests/mock/aul_mock.h"
+#include "unit_tests/mock/ecore_wl2_mock.h"
+#include "unit_tests/mock/elm_mock.h"
+#include "unit_tests/mock/system_info_mock.h"
 #include "unit_tests/mock/test_fixture.h"
+#include "unit_tests/mock/widget_service_mock.h"
 
 using ::testing::_;
 using ::testing::DoAll;
@@ -40,41 +41,7 @@ using ::testing::Return;
 using ::testing::SetArgPointee;
 using ::testing::Invoke;
 
-typedef struct _widget_base_instance_data {
-  bundle* args;
-  char* id;
-  char* content;
-  void* tag;
-  double period;
-  guint periodic_timer;
-  bool pending_update;
-  char* pending_content;
-  void* user_data;
-} widget_base_instance_data;
-
-struct _widget_context {
-  int context;
-};
-
-class Mocks : public ::testing::NiceMock<GioMock>,
-              public ::testing::NiceMock<MultiWindowBaseMock>,
-              public ::testing::NiceMock<AppCommonMock>,
-              public ::testing::NiceMock<ElmMock>,
-              public ::testing::NiceMock<AulMock>,
-              public ::testing::NiceMock<SystemInfoMock>,
-              public ::testing::NiceMock<EcoreWl2Mock> {};
-
-class WidgetAppTest : public TestFixture {
- public:
-  WidgetAppTest() : TestFixture(std::make_unique<Mocks>()) {}
-  virtual ~WidgetAppTest() {}
-
-  virtual void SetUp() {
-  }
-
-  virtual void TearDown() {
-  }
-};
+namespace {
 
 int __instance_create_cb(widget_context_h context, bundle* content,
     int w, int h, void* user_data) {
@@ -86,6 +53,7 @@ int __instance_destroy_cb(widget_context_h context,
   return 0;
 }
 
+/* LCOV_EXCL_START */
 int __instance_pause_cb(widget_context_h context, void* user_data) {
   return 0;
 }
@@ -94,18 +62,15 @@ int __instance_resume_cb(widget_context_h context, void* user_data) {
   return 0;
 }
 
-int __instance_resize_cb(widget_context_h context,
-    int w, int h, void* user_data) {
-  return 0;
-}
-
 int __instance_update_cb(widget_context_h context,
     bundle* content, int force, void* user_data) {
   return 0;
 }
+/* LCOV_EXCL_STOP */
 
-widget_class_h __app_create_cb(void* user_data) {
-  return nullptr;
+int __instance_resize_cb(widget_context_h context,
+    int w, int h, void* user_data) {
+  return 0;
 }
 
 void __app_terminate_cb(void* user_data) {
@@ -116,186 +81,191 @@ int __app_get_id_fake(char** id) {
   return 0;
 }
 
-int __aul_app_get_pkgid_bypid_fake(int pid, char* pkgid, int len) {
-  snprintf(pkgid, 10, "%s", "test");
+int __aul_app_get_appid_bypid_fake(int pid, char* appid, int len) {
+  snprintf(appid, len, "%s", "test");
   return 0;
 }
 
-struct instance_data {
-  Evas_Object* win;
-  guint iconify_timer;
-  bool is_iconified;
-};
-
-void* __appcore_multiwindow_base_instance_get_extra_fake(
-    appcore_multiwindow_base_instance_h handle) {
-  widget_base_instance_data* data = (widget_base_instance_data*)
-      calloc(1, sizeof(widget_base_instance_data));
-  data->args = bundle_create();
-  bundle_add_str(data->args, "__AUL_WIDGET_ID__", "test");
-  bundle_add_str(data->args, "__WIDGET_OP__", "optest");
-  bundle_add_str(data->args, "__WIDGET_WIDTH__", "320");
-  bundle_add_str(data->args, "__WIDGET_HEIGHT__", "320");
-  data->pending_content = strdup("test");
-  data->content = strdup("test");
-  data->id = strdup("id");
-  data->user_data = calloc(1, sizeof (struct instance_data));
-
-  return data;
-}
-
-const char* __appcore_multiwindow_base_instance_get_id_fake(
-    appcore_multiwindow_base_instance_h handle) {
-  return strdup("test");
+int __aul_app_get_pkgid_bypid_fake(int pid, char* pkgid, int len) {
+  snprintf(pkgid, len, "%s", "test");
+  return 0;
 }
-
-const char* __appcore_multiwindow_base_instance_get_class_id_fake(
-    appcore_multiwindow_base_instance_h context) {
-  return strdup("test");
+/* LCOV_EXCL_START */
+void __app_event_cb(app_event_info_h event_info, void *user_data) {
 }
 
-appcore_multiwindow_base_class __cls;
-void __appcore_multiwindow_base_class_add_fake(
-    appcore_multiwindow_base_class cls) {
-  __cls = cls;
-  cls.create(nullptr, nullptr);
-  cls.terminate(nullptr, nullptr);
-  cls.pause((appcore_multiwindow_base_instance_h)
-      calloc(1, sizeof(char)), nullptr);
-  cls.resume((appcore_multiwindow_base_instance_h)
-      calloc(1, sizeof(char)), nullptr);
+bool __widget_context_cb(widget_context_h context, void *user_data) {
+  return true;
 }
+/* LCOV_EXCL_STOP */
+}  // namespace
 
-int __appcore_multiwindow_base_init_fake(appcore_multiwindow_base_ops ops,
-    int argc, char** argv, void* data) {
-  tizen_base::Bundle b;
-
-  b.Add("__WIDGET_WIDTH__", "320");
-  b.Add("__WIDGET_HEIGHT__", "320");
-  b.Add("__AUL_WIDGET_INSTANCE_ID__", "instance_id");
-  b.Add("__AUL_WIDGET_ID__", "test");
-  b.Add("__WIDGET_OP__", "create");
-  ops.base.control(b.GetHandle(), nullptr);
-
-  b.Delete("__WIDGET_OP__");
-  b.Add("__WIDGET_OP__", "resize");
-  ops.base.control(b.GetHandle(), nullptr);
-
-  b.Delete("__WIDGET_OP__");
-  b.Add("__WIDGET_OP__", "update");
-  ops.base.control(b.GetHandle(), nullptr);
-
-  b.Delete("__WIDGET_OP__");
-  b.Add("__WIDGET_OP__", "destroy");
-  ops.base.control(b.GetHandle(), nullptr);
-
-  b.Delete("__WIDGET_OP__");
-  b.Add("__WIDGET_OP__", "resume");
-  ops.base.control(b.GetHandle(), nullptr);
-
-  b.Delete("__WIDGET_OP__");
-  b.Add("__WIDGET_OP__", "pause");
-  ops.base.control(b.GetHandle(), nullptr);
-
-  b.Delete("__WIDGET_OP__");
-  b.Add("__WIDGET_OP__", "terminate");
-  ops.base.control(b.GetHandle(), nullptr);
+class Mocks : public ::testing::NiceMock<MultiWindowBaseMock>,
+              public ::testing::NiceMock<AppCommonMock>,
+              public ::testing::NiceMock<WidgetServiceMock>,
+              public ::testing::NiceMock<ElmMock>,
+              public ::testing::NiceMock<AulMock>,
+              public ::testing::NiceMock<SystemInfoMock>,
+              public ::testing::NiceMock<EcoreWl2Mock> {};
 
-  b.Delete("__WIDGET_OP__");
-  b.Add("__WIDGET_OP__", "period");
-  ops.base.control(b.GetHandle(), nullptr);
+class WidgetAppTest : public TestFixture {
+ public:
+  WidgetAppTest() : TestFixture(std::make_unique<Mocks>()) {}
+  virtual ~WidgetAppTest() {}
 
-  ops.base.create(nullptr);
-  ops.base.terminate(nullptr);
+  virtual void SetUp() {
+  }
 
-  return 0;
-}
+  virtual void TearDown() {
+  }
 
-TEST_F(WidgetAppTest, widget_app_main) {
-  tizen_base::Bundle b;
-  b.Add("__AUL_WIDGET_VIEWER__", "test");
-  int ret = bundle_add_str(b.GetHandle(), "KEY", "VALUE");
-  ASSERT_EQ(ret, BUNDLE_ERROR_NONE);
+  void PrepareTest(widget_app_create_cb created_cb, void* data) {
+    tizen_base::Bundle b;
+    b.Add("__AUL_WIDGET_VIEWER__", "test");
+    int ret = bundle_add_str(b.GetHandle(), "KEY", "VALUE");
+    ASSERT_EQ(ret, BUNDLE_ERROR_NONE);
+
+    char** argv = nullptr;
+    int argc = bundle_export_to_argv(b.GetHandle(), &argv);
+    ASSERT_EQ(get_last_result(), BUNDLE_ERROR_NONE);
+    ASSERT_NE(argv, nullptr);
+    ASSERT_NE(argc, 0);
+
+    widget_app_lifecycle_callback_s callback;
+    callback.create = created_cb;
+    callback.terminate = __app_terminate_cb;
+
+    EXPECT_CALL(GetMock<SystemInfoMock>(),
+        system_info_get_platform_bool(_, _)).
+            WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(0)));
+
+    EXPECT_CALL(GetMock<AulMock>(),
+        aul_app_get_appid_bypid(_, _, _)).
+            WillOnce(Invoke(__aul_app_get_appid_bypid_fake));
+    EXPECT_CALL(GetMock<AulMock>(),
+        aul_app_get_pkgid_bypid(_, _, _)).
+            WillOnce(Invoke(__aul_app_get_pkgid_bypid_fake));
+    EXPECT_CALL(GetMock<AulMock>(),
+        aul_widget_send_status_to_service(_, _, _, _)).
+            WillRepeatedly(Return(0));
+    EXPECT_CALL(GetMock<AulMock>(),
+        aul_widget_send_status_to_viewer(_, _, _, _, _, _)).
+            WillRepeatedly(Return(0));
+    EXPECT_CALL(GetMock<WidgetServiceMock>(),
+        widget_instance_convert_event_to_lifecycle_status(_)).
+            WillRepeatedly(Return(0));
+
+    EXPECT_CALL(GetMock<AppCommonMock>(),
+        app_get_id(_)).WillRepeatedly(Invoke(__app_get_id_fake));
+
+    EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
+        Run(_, _)).WillOnce(Invoke(this, &WidgetAppTest::__run_fake));
+
+    ret = widget_app_main(argc, argv, &callback, data);
+    bundle_free_exported_argv(argc, &argv);
+    EXPECT_EQ(ret, WIDGET_ERROR_NONE);
+  }
 
-  char** argv = nullptr;
-  int argc = bundle_export_to_argv(b.GetHandle(), &argv);
-  ASSERT_EQ(get_last_result(), BUNDLE_ERROR_NONE);
-  ASSERT_NE(argv, nullptr);
-  ASSERT_NE(argc, 0);
+  void PrepareContext(widget_instance_create_cb created_cb) {
+    PrepareTest([](void* user_data) -> widget_class_h {
+      widget_instance_lifecycle_callback_s lifecycle;
+      lifecycle.create = reinterpret_cast<widget_instance_create_cb>(user_data);
+      lifecycle.destroy = __instance_destroy_cb;
+      lifecycle.pause = __instance_pause_cb;
+      lifecycle.resume = __instance_resume_cb;
+      lifecycle.resize = __instance_resize_cb;
+      lifecycle.update = __instance_update_cb;
+      widget_class_h cls = widget_app_class_create(lifecycle, nullptr);
+      EXPECT_NE(cls, nullptr);
+      return cls;
+    }, reinterpret_cast<void*>(created_cb));
+  }
 
-  widget_app_lifecycle_callback_s callback;
-  callback.create = __app_create_cb;
-  callback.terminate = __app_terminate_cb;
+  void __run_fake(int argc, char** argv) {
+    tizen_base::Bundle b;
+    auto* p = GetMock<MultiWindowBaseMock>().real_;
 
-  EXPECT_CALL(GetMock<EcoreWl2Mock>(), ecore_wl2_display_disconnect(_)).
-       Times(1);
+    p->OnCreate();
 
-  EXPECT_CALL(GetMock<EcoreWl2Mock>(), ecore_wl2_shutdown()).
-       Times(1);
+    b.Add("__WIDGET_WIDTH__", "320");
+    b.Add("__WIDGET_HEIGHT__", "320");
+    b.Add("__AUL_WIDGET_INSTANCE_ID__", "instance_id");
+    b.Add("__AUL_WIDGET_ID__", "test");
+    b.Add("__WIDGET_OP__", "create");
+    p->OnControl(b);
 
-  EXPECT_CALL(GetMock<SystemInfoMock>(),
-      system_info_get_platform_bool(_, _)).
-          WillOnce(DoAll(
-                  SetArgPointee<1>(true),
-                  Return(0)));
+    b.Delete("__WIDGET_OP__");
+    b.Add("__WIDGET_OP__", "resize");
+    p->OnControl(b);
 
-  EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
-      appcore_multiwindow_base_init(_, _, _, _)).
-          WillOnce(Invoke(__appcore_multiwindow_base_init_fake));
+    b.Delete("__WIDGET_OP__");
+    b.Add("__WIDGET_OP__", "update");
+    p->OnControl(b);
 
-  EXPECT_CALL(GetMock<AulMock>(),
-      aul_app_get_pkgid_bypid(_, _, _)).
-          WillOnce(Invoke(__aul_app_get_pkgid_bypid_fake));
+    b.Delete("__WIDGET_OP__");
+    b.Add("__WIDGET_OP__", "destroy");
+    p->OnControl(b);
 
-  EXPECT_CALL(GetMock<AppCommonMock>(), app_get_id(_)).
-          WillOnce(Invoke(__app_get_id_fake));
+    b.Delete("__WIDGET_OP__");
+    b.Add("__WIDGET_OP__", "resume");
+    p->OnControl(b);
 
-  EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
-      appcore_multiwindow_base_instance_find(_)).
-          WillRepeatedly(
-            Return(
-              (appcore_multiwindow_base_instance_h)calloc(1, sizeof(char))));
+    b.Delete("__WIDGET_OP__");
+    b.Add("__WIDGET_OP__", "pause");
+    p->OnControl(b);
 
-  EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
-      appcore_multiwindow_base_instance_get_class(_)).
-          WillRepeatedly(
-            Return(&__cls));
+    b.Delete("__WIDGET_OP__");
+    b.Add("__WIDGET_OP__", "terminate");
+    p->OnControl(b);
 
-  EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
-      appcore_multiwindow_base_instance_get_extra(_)).
-      WillRepeatedly(
-        Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
+    b.Delete("__WIDGET_OP__");
+    b.Add("__WIDGET_OP__", "period");
+    p->OnControl(b);
 
-  EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
-      appcore_multiwindow_base_instance_exit(_)).Times(2);
+    p->OnReceive(AUL_WIDGET_CONTENT, b);
 
-  ret = widget_app_main(argc, argv, &callback, nullptr);
-  EXPECT_EQ(WIDGET_ERROR_NONE, ret);
-}
+    p->OnTerminate();
+  }
+};
 
-TEST_F(WidgetAppTest, widget_app_exit) {
+TEST_F(WidgetAppTest, widget_app_exit_n) {
   int ret = widget_app_exit();
-  EXPECT_EQ(WIDGET_ERROR_NONE, ret);
-}
-
-TEST_F(WidgetAppTest, widget_app_terminate_context) {
-  widget_context_h context =
-      (widget_context_h)calloc(1, sizeof(struct _widget_context));
-  int ret = widget_app_terminate_context(context);
-  free(context);
-  EXPECT_EQ(WIDGET_ERROR_NONE, ret);
-}
-
-bool __widget_context_cb(widget_context_h context, void *user_data) {
-  return true;
+  EXPECT_NE(ret, WIDGET_ERROR_NONE);
 }
 
-TEST_F(WidgetAppTest, widget_app_foreach_context) {
-  int ret = widget_app_foreach_context(__widget_context_cb, nullptr);
-  EXPECT_EQ(WIDGET_ERROR_NONE, ret);
+TEST_F(WidgetAppTest, widget_app_exit) {
+  PrepareContext(
+      [](widget_context_h context, bundle* content,
+          int w, int h, void* user_data) -> int {
+        EXPECT_CALL(GetMock<AulMock>(),
+            aul_notify_exit()).WillRepeatedly(Return(0));
+        int ret = widget_app_exit();
+        EXPECT_EQ(ret, WIDGET_ERROR_NONE);
+        return 0;
+      });
 }
 
-void __app_event_cb(app_event_info_h event_info, void *user_data) {
+TEST_F(WidgetAppTest, widget_app_main) {
+  PrepareContext(
+      [](widget_context_h context, bundle* content,
+          int w, int h, void* user_data) -> int {
+        EXPECT_CALL(GetMock<AulMock>(),
+            aul_notify_exit()).WillRepeatedly(Return(0));
+        widget_app_exit();
+        return 0;
+      });
+}
+
+TEST_F(WidgetAppTest, widget_app_restart) {
+  PrepareContext(
+      [](widget_context_h context, bundle* content,
+          int w, int h, void* user_data) -> int {
+        EXPECT_CALL(GetMock<AulMock>(),
+            aul_notify_exit()).WillRepeatedly(Return(0));
+        widget_app_exit();
+        widget_app_restart();
+        return 0;
+      });
 }
 
 TEST_F(WidgetAppTest, widget_app_add_event_handler) {
@@ -307,7 +277,7 @@ TEST_F(WidgetAppTest, widget_app_add_event_handler) {
                   Return(0)));
   int ret = widget_app_add_event_handler(&handle, APP_EVENT_LOW_MEMORY,
       __app_event_cb, nullptr);
-  EXPECT_EQ(WIDGET_ERROR_NONE, ret);
+  EXPECT_EQ(ret, WIDGET_ERROR_NONE);
 }
 
 TEST_F(WidgetAppTest, widget_app_remove_event_handler) {
@@ -320,194 +290,158 @@ TEST_F(WidgetAppTest, widget_app_remove_event_handler) {
   int ret = widget_app_add_event_handler(&handle, APP_EVENT_LOW_MEMORY,
       __app_event_cb, nullptr);
   ret = widget_app_remove_event_handler(handle);
-  EXPECT_EQ(WIDGET_ERROR_NONE, ret);
+  EXPECT_EQ(ret, WIDGET_ERROR_NONE);
 }
 
-TEST_F(WidgetAppTest, widget_app_get_id) {
-  widget_context_h context =
-      (widget_context_h)calloc(1, sizeof(struct _widget_context));
-
-  EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
-      appcore_multiwindow_base_instance_get_id(_)).
-          WillOnce(Return("test"));
-
-  const char *id = widget_app_get_id(context);
-  free(context);
-  EXPECT_STREQ(id, "test");
+TEST_F(WidgetAppTest, widget_app_terminate_context) {
+  PrepareContext([](widget_context_h context, bundle* content,
+      int w, int h, void* user_data) -> int {
+        int ret = widget_app_terminate_context(context);
+        EXPECT_EQ(ret, WIDGET_ERROR_NONE);
+        return 0;
+      });
 }
 
-TEST_F(WidgetAppTest, widget_app_class_create) {
-  widget_instance_lifecycle_callback_s lifecycle;
+TEST_F(WidgetAppTest, widget_app_foreach_context) {
+  PrepareTest([](void* user_data) -> widget_class_h {
+    widget_instance_lifecycle_callback_s lifecycle;
+    lifecycle.create = __instance_create_cb;
+    lifecycle.destroy = __instance_destroy_cb;
+    lifecycle.pause = __instance_pause_cb;
+    lifecycle.resume = __instance_resume_cb;
+    lifecycle.resize = __instance_resize_cb;
+    lifecycle.update = __instance_update_cb;
 
-  lifecycle.create = __instance_create_cb;
-  lifecycle.destroy = __instance_destroy_cb;
-  lifecycle.pause = __instance_pause_cb;
-  lifecycle.resume = __instance_resume_cb;
-  lifecycle.resize = __instance_resize_cb;
-  lifecycle.update = __instance_update_cb;
+    widget_class_h cls = widget_app_class_create(lifecycle, nullptr);
+    EXPECT_NE(cls, nullptr);
 
-  EXPECT_CALL(GetMock<AppCommonMock>(), app_get_id(_)).
-          WillOnce(Invoke(__app_get_id_fake));
+    int ret = widget_app_foreach_context(__widget_context_cb, nullptr);
+    EXPECT_EQ(ret, WIDGET_ERROR_NONE);
 
-  widget_class_h cls = widget_app_class_create(lifecycle, nullptr);
-  EXPECT_NE(nullptr, cls);
+    return cls;
+  }, nullptr);
 }
 
-TEST_F(WidgetAppTest, widget_app_context_set_tag) {
-  widget_context_h context =
-      (widget_context_h)calloc(1, sizeof(struct _widget_context));
+TEST_F(WidgetAppTest, widget_app_get_id) {
+  PrepareContext(
+      [](widget_context_h context, bundle* content,
+          int w, int h, void* user_data) -> int {
+        const char *id = widget_app_get_id(context);
+        EXPECT_STREQ(id, "instance_id");
+        return 0;
+      });
+}
 
-  EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
-      appcore_multiwindow_base_instance_get_extra(_)).
-      WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
+TEST_F(WidgetAppTest, widget_app_class_create) {
+  PrepareContext(__instance_create_cb);
+}
 
-  int ret = widget_app_context_set_tag(context, (void*)"test");
-  free(context);
-  EXPECT_EQ(WIDGET_ERROR_NONE, ret);
+TEST_F(WidgetAppTest, widget_app_context_set_tag) {
+  PrepareContext(
+      [](widget_context_h context, bundle* content,
+          int w, int h, void* user_data) -> int {
+        int ret = widget_app_context_set_tag(context,
+            reinterpret_cast<void*>(const_cast<char*>("test")));
+        EXPECT_EQ(ret, WIDGET_ERROR_NONE);
+        return 0;
+      });
 }
 
 TEST_F(WidgetAppTest, widget_app_context_get_tag) {
-  widget_context_h context =
-      (widget_context_h)calloc(1, sizeof(struct _widget_context));
-
-  EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
-      appcore_multiwindow_base_instance_get_extra(_)).
-      WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
-
-  void* tag;
-  int ret = widget_app_context_get_tag(context, &tag);
-  free(context);
-  EXPECT_EQ(WIDGET_ERROR_NONE, ret);
+  PrepareContext(
+      [](widget_context_h context, bundle* content,
+          int w, int h, void* user_data) -> int {
+        int ret = widget_app_context_set_tag(context,
+            reinterpret_cast<void*>(const_cast<char*>("test")));
+        EXPECT_EQ(ret, WIDGET_ERROR_NONE);
+        void* tag = nullptr;
+        ret = widget_app_context_get_tag(context, &tag);
+        EXPECT_EQ(ret, WIDGET_ERROR_NONE);
+        return 0;
+      });
 }
 
 TEST_F(WidgetAppTest, widget_app_context_set_content_info) {
-  widget_context_h context =
-      (widget_context_h)calloc(1, sizeof(struct _widget_context));
-
-  EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
-      appcore_multiwindow_base_instance_get_extra(_)).
-      WillOnce(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
-
-  EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
-      appcore_multiwindow_base_instance_get_id(_)).
-      WillOnce(Invoke(__appcore_multiwindow_base_instance_get_id_fake));
-
-  EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
-      appcore_multiwindow_base_instance_get_class_id(_)).
-      WillOnce(Invoke(__appcore_multiwindow_base_instance_get_class_id_fake));
-
-  bundle* content_info = bundle_create();
-  int ret = widget_app_context_set_content_info(context, content_info);
-  free(context);
-  EXPECT_EQ(WIDGET_ERROR_NONE, ret);
+  PrepareContext(
+      [](widget_context_h context, bundle* content,
+          int w, int h, void* user_data) -> int {
+        bundle* content_info = bundle_create();
+        EXPECT_NE(content_info, nullptr);
+        int ret = widget_app_context_set_content_info(context, content_info);
+        bundle_free(content_info);
+        EXPECT_EQ(ret, WIDGET_ERROR_NONE);
+        return 0;
+      });
 }
 
 TEST_F(WidgetAppTest, widget_app_context_set_title) {
-  widget_context_h context =
-      (widget_context_h)calloc(1, sizeof(struct _widget_context));
-
-  EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
-      appcore_multiwindow_base_instance_get_extra(_)).
-      WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
-
-  int ret = widget_app_context_set_title(context, "title");
-  free(context);
-  EXPECT_EQ(WIDGET_ERROR_NONE, ret);
+  PrepareContext(
+      [](widget_context_h context, bundle* content,
+          int w, int h, void* user_data) -> int {
+        int ret = widget_app_context_set_title(context, "title");
+        EXPECT_EQ(ret, WIDGET_ERROR_NONE);
+        return 0;
+      });
 }
 
 TEST_F(WidgetAppTest, widget_app_class_add) {
-  widget_instance_lifecycle_callback_s lifecycle;
-  lifecycle.create = __instance_create_cb;
-  lifecycle.destroy = __instance_destroy_cb;
-  lifecycle.pause = __instance_pause_cb;
-  lifecycle.resume = __instance_resume_cb;
-  lifecycle.resize = __instance_resize_cb;
-  lifecycle.update = __instance_update_cb;
-
-  widget_class_h cls = widget_app_class_add(
-      nullptr, "test", lifecycle, nullptr);
-
-  EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
-      appcore_multiwindow_base_class_add(_)).
-      WillOnce(Invoke(__appcore_multiwindow_base_class_add_fake));
-
-  EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
-      appcore_multiwindow_base_instance_get_extra(_)).
-      WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
-
-  EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
-      appcore_multiwindow_base_instance_get_class_id(_)).
-      WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_class_id_fake));
-
-  EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
-      appcore_multiwindow_base_instance_drop(_)).
-      Times(1);
-
-  cls = widget_app_class_add(
-      nullptr, "test", lifecycle, nullptr);
-  EXPECT_NE(nullptr, cls);
-
-  tizen_base::Bundle b;
-  b.Add("__AUL_WIDGET_VIEWER__", "test");
-  int ret = bundle_add_str(b.GetHandle(), "KEY", "VALUE");
-  ASSERT_EQ(ret, BUNDLE_ERROR_NONE);
-
-  char** argv = nullptr;
-  int argc = bundle_export_to_argv(b.GetHandle(), &argv);
-  ASSERT_EQ(get_last_result(), BUNDLE_ERROR_NONE);
-  ASSERT_NE(argv, nullptr);
-  ASSERT_NE(argc, 0);
-
-  widget_app_lifecycle_callback_s callback;
-  callback.create = __app_create_cb;
-  callback.terminate = __app_terminate_cb;
-
-  ret = widget_app_main(argc, argv, &callback, nullptr);
-  EXPECT_EQ(WIDGET_ERROR_NONE, ret);
+  PrepareTest(
+      [](void* user_data) -> widget_class_h {
+        widget_instance_lifecycle_callback_s lifecycle;
+        lifecycle.create = __instance_create_cb;
+        lifecycle.destroy = __instance_destroy_cb;
+        lifecycle.pause = __instance_pause_cb;
+        lifecycle.resume = __instance_resume_cb;
+        lifecycle.resize = __instance_resize_cb;
+        lifecycle.update = __instance_update_cb;
+
+        widget_class_h cls = widget_app_class_add(
+            nullptr, "test", lifecycle, nullptr);
+        EXPECT_NE(cls, nullptr);
+        return cls;
+    }, nullptr);
 }
 
 TEST_F(WidgetAppTest, widget_app_get_elm_win_negative) {
-  Evas_Object* win;
-  widget_context_h context =
-      (widget_context_h)calloc(1, sizeof(struct _widget_context));
-  int ret = widget_app_get_elm_win(context, &win);
-  free(context);
-  EXPECT_EQ(WIDGET_ERROR_FAULT, ret);
+  PrepareContext(
+      [](widget_context_h context, bundle* content,
+          int w, int h, void* user_data) -> int {
+        Evas_Object* win = nullptr;
+        int ret = widget_app_get_elm_win(context, &win);
+        EXPECT_EQ(ret, WIDGET_ERROR_FAULT);
+        return 0;
+      });
 }
 
 TEST_F(WidgetAppTest, widget_app_get_elm_win) {
-  EXPECT_CALL(GetMock<ElmMock>(),
-      elm_win_add(_, _, _)).
-      WillOnce(Return((Evas_Object*)calloc(1, sizeof(char))));
-
-  EXPECT_CALL(GetMock<EcoreWl2Mock>(),
-      ecore_evas_wayland2_window_get(_)).
-      WillOnce(Return((Ecore_Wl2_Window*)calloc(1, sizeof(char))));
-
-  EXPECT_CALL(GetMock<ElmMock>(),
-      elm_win_aux_hint_add(_, _, _)).
-      WillOnce(Return(0));
-
-  EXPECT_CALL(GetMock<EcoreWl2Mock>(),
-      ecore_wl2_window_surface_get(_)).
-      WillOnce(Return(nullptr));
-
-  EXPECT_CALL(GetMock<EcoreWl2Mock>(),
-      evas_object_event_callback_add(_, _, _, _)).
-      Times(1);
-
-  EXPECT_CALL(GetMock<EcoreWl2Mock>(),
-      evas_object_data_set(_, _, _)).
-      Times(1);
-
-  EXPECT_CALL(GetMock<MultiWindowBaseMock>(),
-      appcore_multiwindow_base_instance_get_extra(_)).
-      WillRepeatedly(Invoke(__appcore_multiwindow_base_instance_get_extra_fake));
-
-  Evas_Object* win;
-  widget_context_h context =
-      (widget_context_h)calloc(1, sizeof(struct _widget_context));
-  int ret = widget_app_get_elm_win(context, &win);
-  free(context);
-  EXPECT_EQ(WIDGET_ERROR_NONE, ret);
+  PrepareContext(
+      [](widget_context_h context, bundle* content,
+          int w, int h, void* user_data) -> int {
+        EXPECT_CALL(GetMock<ElmMock>(),
+            elm_win_add(_, _, _)).
+                WillOnce(Return(reinterpret_cast<Evas_Object*>(
+                    calloc(1, sizeof(char)))));
+
+        EXPECT_CALL(GetMock<EcoreWl2Mock>(),
+            ecore_evas_wayland2_window_get(_)).
+                WillOnce(Return(reinterpret_cast<Ecore_Wl2_Window*>(
+                    calloc(1, sizeof(char)))));
+
+        EXPECT_CALL(GetMock<ElmMock>(),
+            elm_win_aux_hint_add(_, _, _)).WillOnce(Return(0));
+
+        EXPECT_CALL(GetMock<EcoreWl2Mock>(),
+            ecore_wl2_window_surface_get(_)).WillOnce(Return(nullptr));
+
+        EXPECT_CALL(GetMock<EcoreWl2Mock>(),
+            evas_object_event_callback_add(_, _, _, _)).Times(1);
+
+        EXPECT_CALL(GetMock<EcoreWl2Mock>(),
+            evas_object_data_set(_, _, _)).Times(1);
+
+        Evas_Object* win = nullptr;
+        int ret = widget_app_get_elm_win(context, &win);
+        EXPECT_EQ(ret, WIDGET_ERROR_NONE);
+        return -1;
+      });
 }