From: Mu-Woong Lee Date: Mon, 10 Jul 2017 11:39:00 +0000 (+0900) Subject: Add job-scheduler testcases X-Git-Tag: submit/tizen/20170802.043634~6 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F56%2F137956%2F7;p=platform%2Fcore%2Fapi%2Fcontext.git Add job-scheduler testcases Change-Id: Id8a66ff1b5337497daa8e2ad650325b5482b0b30 Signed-off-by: Mu-Woong Lee --- diff --git a/testsuite/CMakeLists.txt b/testsuite/CMakeLists.txt index e834f40..f8d0518 100644 --- a/testsuite/CMakeLists.txt +++ b/testsuite/CMakeLists.txt @@ -3,7 +3,7 @@ PROJECT(capi-context-test) SET(TEST_EXEC context-test) # Source List -FILE(GLOB TEST_SRCS src/*.cpp src/app-history/*.cpp) +FILE(GLOB TEST_SRCS src/*.cpp src/app-history/*.cpp src/job-scheduler/*.cpp) INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/src ) diff --git a/testsuite/src/app-history/history.cpp b/testsuite/src/app-history/history.cpp index 5d0168b..b4f33fb 100644 --- a/testsuite/src/app-history/history.cpp +++ b/testsuite/src/app-history/history.cpp @@ -30,11 +30,46 @@ using namespace test; +namespace value { + int int32 = 0; + double dbl = 0; + bool boolean = false; + std::string str = ""; +} + +static int lastError = E_NONE; + static context_history_h history_h = NULL; static context_history_list_h list_h = NULL; static context_history_record_h record_h = NULL; static context_history_filter_h filter_h = NULL; +static void __cleanup_vars() +{ + lastError = E_NONE; + value::int32 = 0; + value::dbl = 0; + value::boolean = false; + value::str = ""; +} + +static bool __get_input_int(const char *msg) +{ + std::cout << msg; + std::getline(std::cin, value::str); + IF_FAIL_RETURN(!value::str.empty(), false); + value::int32 = atoi(value::str.c_str()); + return true; +} + +static bool __get_input_str(const char *msg) +{ + std::cout << msg; + std::getline(std::cin, value::str); + IF_FAIL_RETURN(!value::str.empty(), false); + return true; +} + static void __release_resource() { context_history_list_destroy(list_h); @@ -47,7 +82,7 @@ static void __release_resource() filter_h = NULL; } -int testAppHistory(int *argc, char ***argv) +int test_app_history(int *argc, char ***argv) { lastError = context_history_create(&history_h); if (lastError != E_NONE) { @@ -55,9 +90,9 @@ int testAppHistory(int *argc, char ***argv) return 0; } - flag::newlineAfterTestCaseName = true; - while (true) { + __cleanup_vars(); + g_print("\nSelect the target item...\n"); g_print(" 1. Recently Used App 2. Frequently Used App\n"); g_print(" 3. Rarely Used App 4. Peak Time for App\n"); @@ -66,44 +101,44 @@ int testAppHistory(int *argc, char ***argv) g_print(" 9. Common Setting for Video 10. Frequently Communicated Address\n"); g_print(" 11. Battery Usage 12. Recent Battery Usage\n"); - while (!getInputInt("(to exit, ctrl-c)? ")) {;} + while (!__get_input_int("(to exit, ctrl-c)? ")) {;} switch (value::int32) { case 1: - runTestCase("/history/recently_used_app", _history_app_recently); + run_test_case("/history/recently_used_app", _history_app_recently); break; case 2: - runTestCase("/history/frequently_used_app", _history_app_frequently); + run_test_case("/history/frequently_used_app", _history_app_frequently); break; case 3: - runTestCase("/history/rarely_used_app", _history_app_rarely); + run_test_case("/history/rarely_used_app", _history_app_rarely); break; case 4: - runTestCase("/history/peak_time_for_app", _history_app_peak_time); + run_test_case("/history/peak_time_for_app", _history_app_peak_time); break; case 5: - runTestCase("/history/peak_time_for_music", _history_music_peak_time); + run_test_case("/history/peak_time_for_music", _history_music_peak_time); break; case 6: - runTestCase("/history/peak_time_for_video", _history_video_peak_time); + run_test_case("/history/peak_time_for_video", _history_video_peak_time); break; case 7: - runTestCase("/history/common_setting_for_app", _history_app_setting); + run_test_case("/history/common_setting_for_app", _history_app_setting); break; case 8: - runTestCase("/history/common_setting_for_music", _history_music_setting); + run_test_case("/history/common_setting_for_music", _history_music_setting); break; case 9: - runTestCase("/history/common_setting_for_video", _history_video_setting); + run_test_case("/history/common_setting_for_video", _history_video_setting); break; case 10: - runTestCase("/history/frequently_communicated_address", _history_comm_frequently); + run_test_case("/history/frequently_communicated_address", _history_comm_frequently); break; case 11: - runTestCase("/history/battery_usage", _history_battery); + run_test_case("/history/battery_usage", _history_battery); break; case 12: - runTestCase("/history/recent_battery_usage", _history_battery_recent); + run_test_case("/history/recent_battery_usage", _history_battery_recent); break; default: g_print("Invalid number\n"); @@ -125,7 +160,7 @@ static void __filter_set_int(context_history_filter_e type, const char *key) std::string msg = " "; msg = msg + key + "? "; - if (getInputInt(msg.c_str())) + if (__get_input_int(msg.c_str())) context_history_filter_set_int(filter_h, type, value::int32); } @@ -137,7 +172,7 @@ static void __filter_set_str(context_history_filter_e type, const char *key) std::string msg = " "; msg = msg + key + "? "; - if (getInputStr(msg.c_str())) + if (__get_input_str(msg.c_str())) context_history_filter_set_string(filter_h, type, value::str.c_str()); } diff --git a/testsuite/src/app-history/history.h b/testsuite/src/app-history/history.h index dc3c0d7..327b919 100644 --- a/testsuite/src/app-history/history.h +++ b/testsuite/src/app-history/history.h @@ -17,7 +17,7 @@ #ifndef __CONTEXT_TEST_HISTORY_H__ #define __CONTEXT_TEST_HISTORY_H__ -int testAppHistory(int *argc, char ***argv); +int test_app_history(int *argc, char ***argv); bool _history_app_recently(); bool _history_app_frequently(); diff --git a/testsuite/src/job-scheduler/JobSchedulerTest.cpp b/testsuite/src/job-scheduler/JobSchedulerTest.cpp new file mode 100644 index 0000000..7d8bc22 --- /dev/null +++ b/testsuite/src/job-scheduler/JobSchedulerTest.cpp @@ -0,0 +1,287 @@ +/* + * Copyright (c) 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "../shared.h" +#include "JobSchedulerTest.h" + +using namespace test; + +static bool __add_job(); +static bool __remove_job(); +static bool __start_job(); +static bool __stop_job(); +static bool __get_job(); +static bool __get_all_job(); +static bool __toggle_job_cb(); + +static void __start_job_cb(ctx_sched_h scheduler, ctx_sched_job_h job, bool timeout, void *cbData); +static void __start_job_cb_with_finish(ctx_sched_h scheduler, ctx_sched_job_h job, bool timeout, void *cbData); +static void __stop_job_cb(ctx_sched_h scheduler, ctx_sched_job_h job, void *cbData); +static void __stop_job_cb_with_finish(ctx_sched_h scheduler, ctx_sched_job_h job, void *cbData); + +static bool __print_job(ctx_sched_job_h job); + +static ctx_sched_h __scheduler = NULL; + +static gboolean __main(gpointer data) +{ + g_print("\n"); + g_print("-------------------------------------------\n"); + g_print(" (a) add a job (r) remove a job\n"); + g_print(" (s) start a job (t) stop a job\n"); + g_print(" (g) get a job (G) get all jobs\n"); + g_print(" (c) toggle callbacks (q) quit\n"); + g_print("-------------------------------------------\n"); + + std::string choice = read_str(" a/r/s/t/g/G/c/q ? "); + g_print("\n"); + + switch (choice.front()) { + case 'a': + run_test_case("/scheduler/add_job", __add_job); + break; + case 'r': + run_test_case("/scheduler/remove_job", __remove_job); + break; + case 's': + run_test_case("/scheduler/start_job", __start_job); + break; + case 't': + run_test_case("/scheduler/stop_job", __stop_job); + break; + case 'g': + run_test_case("/scheduler/get_job", __get_job); + break; + case 'G': + run_test_case("/scheduler/get_all_job", __get_all_job); + break; + case 'c': + run_test_case("/scheduler/toggle_job_cb", __toggle_job_cb); + break; + case 'q': + stop_mainloop(); + return G_SOURCE_REMOVE; + default: + break; + } + + return G_SOURCE_CONTINUE; +} + +int test_job_scheduler(int *argc, char ***argv) +{ + int error; + + error = ctx_sched_create(&__scheduler); + if (error != E_NONE) { + ctx_sched_destroy(__scheduler); + g_print(RED("ctx_sched_create() failed\n")); + return 0; + } + + error = ctx_sched_set_job_cb(__scheduler, __start_job_cb, __stop_job_cb, NULL); + if (error != E_NONE) { + ctx_sched_destroy(__scheduler); + g_print(RED("ctx_sched_set_job_cb() failed\n")); + return 0; + } + + g_idle_add(__main, NULL); + + start_mainloop(); + + ctx_sched_destroy(__scheduler); + return 0; +} + +bool __add_job() +{ + g_print(" 1. Periodic job, DBus method call, persistent\n"); + g_print(" 2. Periodic job, app-control\n"); + g_print(" 3. On-demand job, DBus method call, one-time\n"); + g_print(" 4. On-demand job, notification\n"); + + ctx_sched_job_h job = sched::create_job(__scheduler, read_int(" ? ")); + ASSERT(job != NULL); + + const char* jobStr = ctx_sched_job_serialize(job); + g_print("%s", jobStr); + + int jobId = 0; + int error = ctx_sched_add_job(__scheduler, job, &jobId); + ctx_sched_job_destroy(job); + + ASSERT_CMPINT(error, ==, E_NONE); + + g_print("> Assigned JobId: %d\n", jobId); + + return true; +} + +bool __remove_job() +{ + int jobId = read_int("JobId ? "); + int error = ctx_sched_remove_job(__scheduler, jobId); + g_print("> %s\n", get_error_message(error)); + ASSERT_CMPINT(error, ==, E_NONE); + return true; +} + +bool __start_job() +{ + int jobId = read_int("JobId ? "); + int error = ctx_sched_start_job(__scheduler, jobId); + g_print("> %s\n", get_error_message(error)); + ASSERT_CMPINT(error, ==, E_NONE); + return true; +} + +bool __stop_job() +{ + int jobId = read_int("JobId ? "); + int error = ctx_sched_stop_job(__scheduler, jobId); + g_print("> %s\n", get_error_message(error)); + ASSERT_CMPINT(error, ==, E_NONE); + return true; +} + +bool __get_job() +{ + int jobId = read_int("JobId ? "); + ctx_sched_job_h job = NULL; + + int error = ctx_sched_get_job(__scheduler, jobId, &job); + g_print("> %s\n", get_error_message(error)); + ASSERT_CMPINT(error, ==, E_NONE); + + const char* jobStr = ctx_sched_job_serialize(job); + ctx_sched_job_destroy(job); + ASSERT(jobStr != NULL); + + g_print("%s", jobStr); + + return true; +} + +static bool __foreach_job_cb(ctx_sched_h scheduler, ctx_sched_job_h job, void* cbData) +{ + ASSERT(__print_job(job)); + return true; +} + +bool __get_all_job() +{ + int error = ctx_sched_foreach_job(__scheduler, __foreach_job_cb, NULL); + ASSERT_CMPINT(error, ==, E_NONE); + return true; +} + +bool __toggle_job_cb() +{ + static bool withFinish = false; + + int error = E_NONE; + + withFinish = !withFinish; + + if (withFinish) { + error = ctx_sched_set_job_cb(__scheduler, __start_job_cb_with_finish, __stop_job_cb_with_finish, NULL); + ASSERT_CMPINT(error, ==, E_NONE); + g_print("> Job callback functions will immediately finish the job.\n"); + } else { + error = ctx_sched_set_job_cb(__scheduler, __start_job_cb, __stop_job_cb, NULL); + ASSERT_CMPINT(error, ==, E_NONE); + g_print("> Job callback functions will not finish the job.\n"); + } + + return true; +} + +void __start_job_cb(ctx_sched_h scheduler, ctx_sched_job_h job, bool timeout, void *cbData) +{ + g_print("> A job has been executed (timeout: %s).\n", timeout ? "yes" : "no"); + + if (!__print_job(job)) + g_print("> Invalid job handle\n"); +} + +void __start_job_cb_with_finish(ctx_sched_h scheduler, ctx_sched_job_h job, bool timeout, void *cbData) +{ + g_print("> A job has been executed (timeout: %s).\n", timeout ? "yes" : "no"); + + int jobId = -1; + ctx_sched_job_get_id(job, &jobId); + + if (!__print_job(job)) + g_print("> Invalid job handle\n"); + + int error = ctx_sched_job_finished(__scheduler, jobId); + if (error != E_NONE) + g_print("> Failed to finish Job-%d (%s)\n", jobId, CTX_ERROR_STR(error)); +} + +void __stop_job_cb(ctx_sched_h scheduler, ctx_sched_job_h job, void *cbData) +{ + g_print("> A job is recommended to be stopped.\n"); + + if (!__print_job(job)) + g_print("> Invalid job handle\n"); +} + +void __stop_job_cb_with_finish(ctx_sched_h scheduler, ctx_sched_job_h job, void *cbData) +{ + g_print("> A job is recommended to be stopped.\n"); + + int jobId = -1; + ctx_sched_job_get_id(job, &jobId); + + if (!__print_job(job)) + g_print("> Invalid job handle\n"); + + int error = ctx_sched_job_finished(__scheduler, jobId); + if (error != E_NONE) + g_print("> Failed to finish Job-%d (%s)\n", jobId, CTX_ERROR_STR(error)); +} + +bool __print_job(ctx_sched_job_h job) +{ + int error = 0; + int jobId = -1; + char* data = NULL; + size_t length = 0; + + error = ctx_sched_job_get_id(job, &jobId); + + if (error != E_NONE) { + ctx_sched_job_destroy(job); + return false; + } + + error = ctx_sched_job_get_user_data(job, &data, &length); + + if (error != E_NONE) { + ctx_sched_job_destroy(job); + return false; + } + + data[length - 1] = '\0'; + g_print("> Job-%d: %s\n", jobId, data); + g_free(data); + ctx_sched_job_destroy(job); + + return true; +} diff --git a/testsuite/src/job-scheduler/JobSchedulerTest.h b/testsuite/src/job-scheduler/JobSchedulerTest.h new file mode 100644 index 0000000..eb9f97f --- /dev/null +++ b/testsuite/src/job-scheduler/JobSchedulerTest.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __CONTEXT_CONTEXT_JOB_SCHEDULER_TEST_H__ +#define __CONTEXT_CONTEXT_JOB_SCHEDULER_TEST_H__ + +#include + +int test_job_scheduler(int *argc, char ***argv); + +namespace sched { + ctx_sched_job_h create_job(ctx_sched_h scheduler, int num); +} + +#endif /* __CONTEXT_CONTEXT_JOB_SCHEDULER_TEST_H__ */ diff --git a/testsuite/src/job-scheduler/JobSpecs.cpp b/testsuite/src/job-scheduler/JobSpecs.cpp new file mode 100644 index 0000000..bb24bcf --- /dev/null +++ b/testsuite/src/job-scheduler/JobSpecs.cpp @@ -0,0 +1,257 @@ +/* + * Copyright (c) 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "../shared.h" +#include "JobSchedulerTest.h" + +static bool __create_periodic_dbus_job(ctx_sched_h scheduler, ctx_sched_job_h* job); +static bool __create_periodic_app_control_job(ctx_sched_h scheduler, ctx_sched_job_h* job); +static bool __create_on_demand_dbus_job(ctx_sched_h scheduler, ctx_sched_job_h* job); +static bool __create_on_demand_notification_job(ctx_sched_h scheduler, ctx_sched_job_h* job); + +ctx_sched_job_h sched::create_job(ctx_sched_h scheduler, int num) +{ + bool success = true; + ctx_sched_job_h job = NULL; + + switch (num) { + case 1: + success = __create_periodic_dbus_job(scheduler, &job); + break; + case 2: + success = __create_periodic_app_control_job(scheduler, &job); + break; + case 3: + success = __create_on_demand_dbus_job(scheduler, &job); + break; + case 4: + success = __create_on_demand_notification_job(scheduler, &job); + break; + default: + g_print("> Bad choice\n"); + break; + } + + if (!success) { + ctx_sched_job_destroy(job); + job = NULL; + } + + return job; +} + +// Test if the charger is connected. +static ctx_sched_job_context_h __create_charger_requirement(ctx_sched_h scheduler, bool optional) +{ + int error = E_NONE; + bool supported = false; + ctx_sched_job_context_h jobContext = NULL; + + error = ctx_sched_job_requirement_is_supported(scheduler, CTX_SCHED_URI_CHARGER, &supported); + ASSERT_RETURN(error == E_NONE, NULL); + ASSERT_RETURN(supported, NULL); + + error = ctx_sched_job_requirement_create(CTX_SCHED_URI_CHARGER, optional, &jobContext); + ASSERT_RETURN(error == E_NONE, NULL); + ASSERT_RETURN(jobContext != NULL, NULL); + + ctx_sched_job_context_prepare_attribute_int(jobContext, CTX_SCHED_ATTR_NAME_IS_CONNECTED); + ctx_sched_job_context_attribute_add_eq_int(jobContext, CTX_SCHED_ATTR_NAME_IS_CONNECTED, CTX_SCHED_ATTR_VALUE_TRUE); + + return jobContext; +} + +// Test if the GPS is not disabled. +static ctx_sched_job_context_h __create_gps_requirement(ctx_sched_h scheduler, bool optional) +{ + int error = E_NONE; + bool supported = false; + ctx_sched_job_context_h jobContext = NULL; + + error = ctx_sched_job_requirement_is_supported(scheduler, CTX_SCHED_URI_GPS, &supported); + ASSERT_RETURN(error == E_NONE, NULL); + ASSERT_RETURN(supported, NULL); + + error = ctx_sched_job_requirement_create(CTX_SCHED_URI_GPS, optional, &jobContext); + ASSERT_RETURN(error == E_NONE, NULL); + ASSERT_RETURN(jobContext != NULL, NULL); + + ctx_sched_job_context_prepare_attribute_str(jobContext, CTX_SCHED_ATTR_NAME_STATE); + ctx_sched_job_context_attribute_add_ne_str(jobContext, CTX_SCHED_ATTR_NAME_STATE, CTX_SCHED_ATTR_VALUE_DISABLED); + + return jobContext; +} + +// Test if the GPS is connected. +static ctx_sched_job_context_h __create_gps_trigger(ctx_sched_h scheduler) +{ + int error = E_NONE; + bool supported = false; + ctx_sched_job_context_h jobContext = NULL; + + error = ctx_sched_job_trigger_is_supported(scheduler, CTX_SCHED_URI_GPS, &supported); + ASSERT_RETURN(error == E_NONE, NULL); + ASSERT_RETURN(supported, NULL); + + error = ctx_sched_job_trigger_create(CTX_SCHED_URI_GPS, &jobContext); + ASSERT_RETURN(error == E_NONE, NULL); + ASSERT_RETURN(jobContext != NULL, NULL); + + ctx_sched_job_context_prepare_attribute_str(jobContext, CTX_SCHED_ATTR_NAME_STATE); + ctx_sched_job_context_attribute_add_eq_str(jobContext, CTX_SCHED_ATTR_NAME_STATE, CTX_SCHED_ATTR_VALUE_CONNECTED); + + return jobContext; +} + +// Test if the earjack is connected +static ctx_sched_job_context_h __create_earjack_trigger(ctx_sched_h scheduler) +{ + int error = E_NONE; + bool supported = false; + ctx_sched_job_context_h jobContext = NULL; + + error = ctx_sched_job_trigger_is_supported(scheduler, CTX_SCHED_URI_EARJACK, &supported); + ASSERT_RETURN(error == E_NONE, NULL); + ASSERT_RETURN(supported, NULL); + + error = ctx_sched_job_trigger_create(CTX_SCHED_URI_EARJACK, &jobContext); + ASSERT_RETURN(error == E_NONE, NULL); + ASSERT_RETURN(jobContext != NULL, NULL); + + ctx_sched_job_context_prepare_attribute_int(jobContext, CTX_SCHED_ATTR_NAME_IS_CONNECTED); + ctx_sched_job_context_attribute_add_eq_int(jobContext, CTX_SCHED_ATTR_NAME_IS_CONNECTED, CTX_SCHED_ATTR_VALUE_TRUE); + + return jobContext; +} + +bool __create_periodic_dbus_job(ctx_sched_h scheduler, ctx_sched_job_h* job) +{ + int error = E_NONE; + ctx_sched_job_context_h jobContext = NULL; + + // Allocate a periodic job handle (10 minutes interval and zero anchor) + error = ctx_sched_job_create_periodic(10, 0, job); + ASSERT_CMPINT(error, ==, E_NONE); + + // Charger state requirement (not optional) + jobContext = __create_charger_requirement(scheduler, false); + ASSERT(jobContext != NULL); + + error = ctx_sched_job_add_requirement(*job, jobContext); + if (error != E_NONE) + ctx_sched_job_context_destroy(jobContext); + ASSERT_CMPINT(error, ==, E_NONE); + + // GPS state requirement (optional) + jobContext = __create_gps_requirement(scheduler, true); + ASSERT(jobContext != NULL); + + error = ctx_sched_job_add_requirement(*job, jobContext); + if (error != E_NONE) + ctx_sched_job_context_destroy(jobContext); + ASSERT_CMPINT(error, ==, E_NONE); + + // Set to a persistent job + error = ctx_sched_job_set_persistent(*job, true); + ASSERT_CMPINT(error, ==, E_NONE); + + // Set the requirement timeout (3000 ms) + error = ctx_sched_job_set_requirement_timeout(*job, 3000); + ASSERT_CMPINT(error, ==, E_NONE); + + // Set the action DBus + const char* data = "Preset #1: Periodic DBus Job"; + GVariant* param = g_variant_new("(s)", data); + error = ctx_sched_job_set_dbus(*job, + "org.tizen.context-test", "/org/tizen/context-test", + "org.tizen.context-test", "TestMethod", param); + g_variant_unref(param); + ASSERT_CMPINT(error, ==, E_NONE); + + // Set the user data (null-terminate string) + error = ctx_sched_job_set_user_data(*job, data, strlen(data) + 1); + ASSERT_CMPINT(error, ==, E_NONE); + + return true; +} + +bool __create_periodic_app_control_job(ctx_sched_h scheduler, ctx_sched_job_h* job) +{ + //TODO + return false; +} + +bool __create_on_demand_dbus_job(ctx_sched_h scheduler, ctx_sched_job_h* job) +{ + int error = E_NONE; + ctx_sched_job_context_h jobContext = NULL; + + // Allocate an on-demand job handle + error = ctx_sched_job_create_on_demand(job); + ASSERT_CMPINT(error, ==, E_NONE); + + // GPS state trigger + jobContext = __create_gps_trigger(scheduler); + ASSERT(jobContext != NULL); + + error = ctx_sched_job_add_trigger(*job, jobContext); + if (error != E_NONE) + ctx_sched_job_context_destroy(jobContext); + ASSERT_CMPINT(error, ==, E_NONE); + + // Earjack state trigger + jobContext = __create_earjack_trigger(scheduler); + ASSERT(jobContext != NULL); + + error = ctx_sched_job_add_trigger(*job, jobContext); + if (error != E_NONE) + ctx_sched_job_context_destroy(jobContext); + ASSERT_CMPINT(error, ==, E_NONE); + + // Charger state requirement (not optional) + jobContext = __create_charger_requirement(scheduler, false); + ASSERT(jobContext != NULL); + + error = ctx_sched_job_add_requirement(*job, jobContext); + if (error != E_NONE) + ctx_sched_job_context_destroy(jobContext); + ASSERT_CMPINT(error, ==, E_NONE); + + // Set to a one-time job + error = ctx_sched_job_set_one_time(*job, true); + ASSERT_CMPINT(error, ==, E_NONE); + + // Set the action DBus + const char* data = "Preset #3: On-demand DBus Job"; + GVariant* param = g_variant_new("(s)", data); + error = ctx_sched_job_set_dbus(*job, + "org.tizen.context-test", "/org/tizen/context-test", + "org.tizen.context-test", "TestMethod", param); + g_variant_unref(param); + ASSERT_CMPINT(error, ==, E_NONE); + + // Set the user data (null-terminate string) + error = ctx_sched_job_set_user_data(*job, data, strlen(data) + 1); + ASSERT_CMPINT(error, ==, E_NONE); + + return true; +} + +bool __create_on_demand_notification_job(ctx_sched_h scheduler, ctx_sched_job_h* job) +{ + //TODO + return false; +} diff --git a/testsuite/src/main.cpp b/testsuite/src/main.cpp index 2faa967..4c32689 100644 --- a/testsuite/src/main.cpp +++ b/testsuite/src/main.cpp @@ -16,21 +16,22 @@ #include "shared.h" #include "app-history/history.h" +#include "job-scheduler/JobSchedulerTest.h" using namespace test; -static void __signalHandler(int signo) +static void __signal_handler(int signo) { - if (signo == SIGINT && isMainloopRunning()) { + if (signo == SIGINT && is_mainloop_running()) { g_print(PURPLE("\nskipping the testcase...")); - stopMainloop(); + stop_mainloop(); } else { g_print(PURPLE("\nterminating... (SIGNAL %d)\n"), signo); exit(EXIT_SUCCESS); } } -static void __printUsage() +static void __print_usage() { g_print("Usage: context-test TESTSUITE\n\n"); g_print("TESTSUITE can be one of the followings:\n"); @@ -40,24 +41,23 @@ static void __printUsage() int main(int argc, char** argv) { - signal(SIGINT, __signalHandler); - signal(SIGHUP, __signalHandler); - signal(SIGTERM, __signalHandler); - signal(SIGQUIT, __signalHandler); - signal(SIGABRT, __signalHandler); + signal(SIGINT, __signal_handler); + signal(SIGHUP, __signal_handler); + signal(SIGTERM, __signal_handler); + signal(SIGQUIT, __signal_handler); if (argc < 2) { - __printUsage(); + __print_usage(); return EXIT_SUCCESS; } switch (argv[1][0]) { case 'a': - return testAppHistory(&argc, &argv); + return test_app_history(&argc, &argv); case 'j': - return EXIT_SUCCESS; + return test_job_scheduler(&argc, &argv); default: - __printUsage(); + __print_usage(); return EXIT_SUCCESS; } } diff --git a/testsuite/src/shared.cpp b/testsuite/src/shared.cpp index 38c5d6d..b93805d 100644 --- a/testsuite/src/shared.cpp +++ b/testsuite/src/shared.cpp @@ -18,21 +18,7 @@ static GMainLoop* __mainloop = NULL; -namespace test { - namespace value { - int int32 = 0; - double dbl = 0; - bool boolean = false; - std::string str = ""; - } - namespace flag { - bool skipTestCase = false; - bool newlineAfterTestCaseName = false; - } - int lastError = 0; -} - -void test::startMainloop() +void test::start_mainloop() { if (!__mainloop) { __mainloop = g_main_loop_new(NULL, FALSE); @@ -40,58 +26,59 @@ void test::startMainloop() } } -void test::stopMainloop() +void test::stop_mainloop() { if (__mainloop) { g_main_loop_quit(__mainloop); g_main_loop_unref(__mainloop); - test::flag::skipTestCase = true; __mainloop = NULL; } } -bool test::isMainloopRunning() +bool test::is_mainloop_running() { return (__mainloop != NULL); } -void test::cleanupVars() +int test::read_int() { - test::lastError = -1; - test::value::boolean = false; - test::value::int32 = 0; - test::value::dbl = 0; - test::value::str = ""; - test::flag::skipTestCase = false; + return read_int(""); } -bool test::getInputInt(const char *msg) +int test::read_int(const std::string& msg) { std::cout << msg; - std::getline(std::cin, test::value::str); - IF_FAIL_RETURN(!test::value::str.empty(), false); - test::value::int32 = atoi(test::value::str.c_str()); - return true; + std::string line; + std::getline(std::cin, line); + + if (line.empty()) + return INT_MIN; + + return atoi(line.c_str()); } -bool test::getInputStr(const char *msg) +std::string test::read_str() +{ + return read_str(""); +} + +std::string test::read_str(const std::string& msg) { std::cout << msg; - std::getline(std::cin, test::value::str); - IF_FAIL_RETURN(!test::value::str.empty(), false); - return true; + std::string line; + std::getline(std::cin, line); + + return line; } -void test::runTestCase(const char *tcName, bool (*tcFunc)(void)) +void test::run_test_case(const char *tcName, bool (*tcFunc)(void)) { IF_FAIL_VOID(tcName && tcFunc); g_print(CYAN("%s") ": ", tcName); - if (test::flag::newlineAfterTestCaseName) g_print("\n"); - test::cleanupVars(); bool result = tcFunc(); if (result) - g_print(GREEN("PASS") "\n"); + g_print("\n>> " GREEN("PASS") "\n"); } diff --git a/testsuite/src/shared.h b/testsuite/src/shared.h index 905a550..7359057 100644 --- a/testsuite/src/shared.h +++ b/testsuite/src/shared.h @@ -24,48 +24,35 @@ #include #include -#define ASSERT(condition) \ +#define ASSERT_RETURN(condition, ret) \ do { \ if (!(condition)) { \ - g_print("\n"); \ - if (!test::flag::skipTestCase) \ - g_print(RED("FAIL") ": %s(%d) > %s\n", __FUNCTION__, __LINE__, #condition); \ - return false; \ + g_print("\n>> " RED("FAIL") ": %s(%d) > %s\n", __FUNCTION__, __LINE__, #condition); \ + return ret; \ } \ } while (0) +#define ASSERT(condition) ASSERT_RETURN(condition, false) + #define ASSERT_CMPINT(left, comp, right) \ do { \ if (!((left) comp (right))) { \ - g_print("\n"); \ - if (!test::flag::skipTestCase) \ - g_print(RED("FAIL") ": %s(%d) > %s(%d) %s %s(%d)\n", __FUNCTION__, __LINE__, #left, (left), #comp, #right, (right)); \ + g_print("\n>> " RED("FAIL") ": %s(%d) > %s(%d) %s %s(%d)\n", __FUNCTION__, __LINE__, #left, (left), #comp, #right, (right)); \ return false; \ } \ } while (0) namespace test { - namespace value { - extern int int32; - extern double dbl; - extern bool boolean; - extern std::string str; - } - namespace flag { - extern bool skipTestCase; - extern bool newlineAfterTestCaseName; - } - extern int lastError; - - void startMainloop(); - void stopMainloop(); - bool isMainloopRunning(); + void start_mainloop(); + void stop_mainloop(); + bool is_mainloop_running(); - void cleanupVars(); - bool getInputInt(const char *msg); - bool getInputStr(const char *msg); + int read_int(); + int read_int(const std::string& msg); + std::string read_str(); + std::string read_str(const std::string& msg); - void runTestCase(const char *tcName, bool (*tcFunc)(void)); + void run_test_case(const char *tcName, bool (*tcFunc)(void)); } #endif