#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"
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) {
}
-
TEST_F(ServiceAppTest, Basic) {
+ // test service_app_main
int ret = 0;
service_app_lifecycle_callback_s callback = {
.create = __service_app_create_cb,
.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);
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