From c61dd86e0f895f637875149dfcc67cd14b7c34bc Mon Sep 17 00:00:00 2001 From: Daehyeon Jung Date: Wed, 9 Sep 2020 15:23:42 +0900 Subject: [PATCH] Add internal, extension unit tests Change-Id: I9761a7ca5667361f91246045915fc2a7aacaff9c Signed-off-by: Daehyeon Jung --- unittests/CMakeLists.txt | 3 +- unittests/mock/appcore_mock.cc | 4 ++ unittests/mock/appcore_mock.h | 1 + unittests/service_app_test.cc | 129 ++++++++++++++++++++++++++++++++++++----- 4 files changed, 121 insertions(+), 16 deletions(-) diff --git a/unittests/CMakeLists.txt b/unittests/CMakeLists.txt index 6c7fd2d..7397d04 100644 --- a/unittests/CMakeLists.txt +++ b/unittests/CMakeLists.txt @@ -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) diff --git a/unittests/mock/appcore_mock.cc b/unittests/mock/appcore_mock.cc index 4829379..18cc1a7 100644 --- a/unittests/mock/appcore_mock.cc +++ b/unittests/mock/appcore_mock.cc @@ -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 diff --git a/unittests/mock/appcore_mock.h b/unittests/mock/appcore_mock.h index 8b1433b..e130ece 100644 --- a/unittests/mock/appcore_mock.h +++ b/unittests/mock/appcore_mock.h @@ -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 diff --git a/unittests/service_app_test.cc b/unittests/service_app_test.cc index 6b16ba2..9ad6fb9 100644 --- a/unittests/service_app_test.cc +++ b/unittests/service_app_test.cc @@ -19,13 +19,18 @@ #include #include +#include #include +#include #include #include #include #include #include +#include +#include +#include #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(user_data); + bool* loop = reinterpret_cast(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> 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(), 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(&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(), 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(user_data); + *calledCnt = *calledCnt + 1; + return 0; + }, reinterpret_cast(&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(user_data); + *calledCnt = *calledCnt + 1; + }, reinterpret_cast(&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 -- 2.7.4