Add internal, extension unit tests 86/243686/4 accepted/tizen_6.0_unified_hotfix tizen_6.0_hotfix accepted/tizen/6.0/unified/20201030.122457 accepted/tizen/6.0/unified/hotfix/20201103.004756 accepted/tizen/6.0/unified/hotfix/20201103.052051 accepted/tizen/unified/20200916.121757 submit/tizen/20200914.061905 submit/tizen_6.0/20201029.205102 submit/tizen_6.0_hotfix/20201102.192502 submit/tizen_6.0_hotfix/20201103.114802 tizen_6.0.m2_release
authorDaehyeon Jung <darrenh.jung@samsung.com>
Wed, 9 Sep 2020 06:23:42 +0000 (15:23 +0900)
committerDaehyeon Jung <darrenh.jung@samsung.com>
Mon, 14 Sep 2020 05:38:58 +0000 (14:38 +0900)
Change-Id: I9761a7ca5667361f91246045915fc2a7aacaff9c
Signed-off-by: Daehyeon Jung <darrenh.jung@samsung.com>
unittests/CMakeLists.txt
unittests/mock/appcore_mock.cc
unittests/mock/appcore_mock.h
unittests/service_app_test.cc

index 6c7fd2d..7397d04 100644 (file)
@@ -13,7 +13,8 @@ pkg_check_modules(test REQUIRED aul dlog capi-appfw-app-control capi-appfw-app-c
 FOREACH(flag ${test_CFLAGS})
        SET(EXTRA_CXXFLAGS_test "${EXTRA_CXXFLAGS_test} ${flag}")
 ENDFOREACH(flag)
+
+SET(${EXTRA_CXXFLAGS_test} "${EXTRA_CXXFLAGS_test} --std=c++14")
 SET_TARGET_PROPERTIES(${TARGET_TEST} PROPERTIES COMPILE_FLAGS ${EXTRA_CXXFLAGS_test})
-message(${EXTRA_CXXFLAGS_test})
 
 TARGET_LINK_LIBRARIES(${TARGET_TEST} gmock appcore-agent)
index 4829379..18cc1a7 100644 (file)
@@ -25,3 +25,7 @@ extern "C" int appcore_base_init(appcore_base_ops ops, int argc, char **argv, vo
 extern "C" void appcore_base_fini() {
   return MOCK_HOOK_P0(AppCoreMock, appcore_base_fini);
 }
+
+extern "C" void appcore_base_exit() {
+  return MOCK_HOOK_P0(AppCoreMock, appcore_base_exit);
+}
\ No newline at end of file
index 8b1433b..e130ece 100644 (file)
@@ -27,6 +27,7 @@ class AppCoreMock : public virtual ModuleMock {
 
   MOCK_METHOD4(appcore_base_init, int(appcore_base_ops, int, char**, void*));
   MOCK_METHOD0(appcore_base_fini, void());
+  MOCK_METHOD0(appcore_base_exit, void());
 };
 
 #endif  // MOCK_APPCORE_HOOK_H_
\ No newline at end of file
index 6b16ba2..9ad6fb9 100644 (file)
 #include <stdio.h>
 
 #include <chrono>
+#include <functional>
 #include <thread>
 
+#include <bundle_cpp.h>
 #include <bundle_internal.h>
 #include <service_app.h>
 #include <service_app_extension.h>
 #include <service_app_internal.h>
 #include <appcore_base.h>
+#include <app_common.h>
+#include <aul_job_scheduler.h>
+#include <glib.h>
 
 #include "mock/test_fixture.h"
 #include "mock/appcore_mock.h"
@@ -55,19 +60,24 @@ extern "C" int __dlog_print(log_id_t log_id, int prio, const char *tag, const ch
   return 0;
 }
 
+extern "C" int aul_job_scheduler_update_job_status(const char* job_id,
+    aul_job_status_e job_status) {
+  return 0;
+}
+
 bool __service_app_create_cb(void* user_data) {
-    return true;
+  return false;
 }
 
 bool __service_app_create_cb_loop(void* user_data) {
-    bool *loop = reinterpret_cast<bool*>(user_data);
+  bool* loop = reinterpret_cast<bool*>(user_data);
 
-    while (*loop) {
-        std::cout << "wait..." << std::endl;
-        std::this_thread::sleep_for(std::chrono::milliseconds(100));
-    }
+  while (*loop) {
+    std::cout << "wait..." << std::endl;
+    std::this_thread::sleep_for(std::chrono::milliseconds(100));
+  }
 
-    return true;
+  return true;
 }
 
 void __service_app_terminate_cb(void* user_data) {
@@ -78,8 +88,8 @@ void __service_app_control_cb(app_control_h app_control, void* user_data) {
 
 }
 
-
 TEST_F(ServiceAppTest, Basic) {
+  // test service_app_main
   int ret = 0;
   service_app_lifecycle_callback_s callback = {
     .create = __service_app_create_cb,
@@ -87,11 +97,15 @@ TEST_F(ServiceAppTest, Basic) {
     .app_control = __service_app_control_cb
   };
 
-  bundle* b = bundle_create();
-  ASSERT_TRUE(b != nullptr);
-  bundle_add_str(b, "dummy", "dummy");
+  tizen_base::Bundle b;
+  b.Add("dummy", "dummy");
+
   char** argv;
-  int argc = bundle_export_to_argv(b, &argv);
+  int argc = bundle_export_to_argv(b.GetHandle(), &argv);
+  std::unique_ptr<char**, std::function<void(char***)>> ptr(&argv, [argc](char*** ptr){
+    bundle_free_exported_argv(argc, ptr);
+  });
+
   ASSERT_TRUE(argc > 0);
 
   ret = service_app_main(argc, argv, &callback, nullptr);
@@ -104,21 +118,106 @@ TEST_F(ServiceAppTest, Basic) {
   ret = service_app_main(argc, argv, &callback, nullptr);
   EXPECT_THAT(ret, testing::Eq(APP_ERROR_INVALID_PARAMETER));
 
+  using testing::_;
+
+  EXPECT_CALL(GetMock<AppCoreMock>(), appcore_base_init(_,_,_,_))
+    .WillOnce(testing::Invoke([&](appcore_base_ops ops, int argc, char** argv,
+        void* data) {
+      ops.create(data);
+      return 0;
+  }));
+
   bool loop = true;
   std::thread t([&]() {
       callback.create = __service_app_create_cb_loop;
       service_app_main(argc, argv, &callback, reinterpret_cast<void*>(&loop));
   });
 
-  std::this_thread::sleep_for(std::chrono::milliseconds(10));
+  std::this_thread::sleep_for(std::chrono::milliseconds(50));
   callback.create = __service_app_create_cb;
   ret = service_app_main(1, argv, &callback, &loop);
   EXPECT_THAT(ret, testing::Eq(APP_ERROR_ALREADY_RUNNING));
 
   loop = false;
   t.join();
-  bundle_free_exported_argv(argc, &argv);
-  bundle_free(b);
+
+  // test service_app_exit
+  EXPECT_CALL(GetMock<AppCoreMock>(), appcore_base_exit()).Times(testing::AtLeast(1));
+  service_app_exit();
+}
+
+TEST_F(ServiceAppTest, Internals) {
+  int ret;
+  int calledCnt = 0;
+  service_app_job_h handle;
+
+  handle = service_app_add_job_handler(nullptr, nullptr, nullptr);
+  EXPECT_TRUE(handle == nullptr);
+
+  handle = service_app_add_job_handler("temp", [] (int status, const char* job_id,
+      bundle* job_data, void* user_data) -> int {
+    int* calledCnt = reinterpret_cast<int*>(user_data);
+    *calledCnt = *calledCnt + 1;
+    return 0;
+  }, reinterpret_cast<void*>(&calledCnt));
+
+  EXPECT_TRUE(handle != nullptr);
+  EXPECT_THAT(calledCnt, testing::Eq(0));
+
+  tizen_base::Bundle b;
+
+  ret = service_app_job_raise(0, nullptr, nullptr);
+  EXPECT_THAT(ret, testing::Eq(APP_ERROR_INVALID_PARAMETER));
+
+  ret = service_app_job_raise(SERVICE_APP_JOB_STATUS_START, "temp", b.GetHandle());
+
+  g_main_context_iteration(g_main_context_default(), 0);
+  EXPECT_THAT(ret, testing::Eq(APP_ERROR_NONE));
+  EXPECT_THAT(calledCnt, testing::Eq(1));
+
+  ret = service_app_job_finished(nullptr);
+  EXPECT_THAT(ret, testing::Eq(APP_ERROR_INVALID_PARAMETER));
+
+  ret = service_app_job_finished("temp");
+  EXPECT_THAT(ret, testing::Eq(APP_ERROR_NONE));
+
+  ret = service_app_remove_job_handler(nullptr);
+  EXPECT_THAT(ret, testing::Eq(APP_ERROR_INVALID_PARAMETER));
+
+  ret = service_app_remove_job_handler(handle);
+  EXPECT_THAT(ret, testing::Eq(APP_ERROR_NONE));
+}
+
+TEST_F(ServiceAppTest, EventHandlers) {
+  app_event_handler_h handle;
+  int ret;
+  int calledCnt = 0;
+
+  ret = service_app_add_event_handler(nullptr, APP_EVENT_LOW_MEMORY, nullptr,
+          nullptr);
+  EXPECT_THAT(ret, testing::Eq(APP_ERROR_INVALID_PARAMETER));
+
+  ret = service_app_add_event_handler(&handle, APP_EVENT_LOW_MEMORY,
+            [] (app_event_info_h event_info, void* user_data) {
+    int* calledCnt = reinterpret_cast<int*>(user_data);
+    *calledCnt = *calledCnt + 1;
+  }, reinterpret_cast<void*>(&calledCnt));
+
+  EXPECT_THAT(ret, testing::Eq(APP_ERROR_NONE));
+  EXPECT_THAT(calledCnt, testing::Eq(0));
+
+  appcore_base_raise_event(&calledCnt, APPCORE_BASE_EVENT_LOW_MEMORY);
+
+  EXPECT_THAT(calledCnt, testing::Eq(1));
+
+  ret = service_app_remove_event_handler(nullptr);
+  EXPECT_THAT(ret, testing::Eq(APP_ERROR_INVALID_PARAMETER));
+
+  ret = service_app_remove_event_handler(handle);
+  EXPECT_THAT(ret, testing::Eq(APP_ERROR_NONE));
+
+  appcore_base_raise_event(&calledCnt, APPCORE_BASE_EVENT_LOW_MEMORY);
+  EXPECT_THAT(calledCnt, testing::Eq(1));
 }
 
 } // namespace appcore_agent