Import the command-line testsuite from 2.4 37/58937/1
authorMu-Woong Lee <muwoong.lee@samsung.com>
Fri, 5 Feb 2016 07:15:02 +0000 (16:15 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Fri, 5 Feb 2016 07:15:02 +0000 (16:15 +0900)
Change-Id: I02e5a7b5cf719caa1fa6df735c96a0e59a3a525b
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
13 files changed:
CMakeLists.txt
packaging/context.spec
testsuite/CMakeLists.txt [new file with mode: 0644]
testsuite/context-test.manifest [new file with mode: 0644]
testsuite/src/history/history.cpp [new file with mode: 0644]
testsuite/src/history/history.h [new file with mode: 0644]
testsuite/src/main.cpp [new file with mode: 0644]
testsuite/src/shared.cpp [new file with mode: 0644]
testsuite/src/shared.h [new file with mode: 0644]
testsuite/src/trigger/condition.cpp [new file with mode: 0644]
testsuite/src/trigger/condition.h [new file with mode: 0644]
testsuite/src/trigger/event.cpp [new file with mode: 0644]
testsuite/src/trigger/event.h [new file with mode: 0644]

index 6207e59..f3c5480 100644 (file)
@@ -57,3 +57,5 @@ CONFIGURE_FILE(
        @ONLY
 )
 INSTALL(FILES ${CMAKE_SOURCE_DIR}/${PROJECT_NAME}.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
+
+ADD_SUBDIRECTORY(testsuite)
index cbc94d5..2af7a11 100644 (file)
@@ -76,3 +76,14 @@ Tizen Context Framework Native API (Development)
 %defattr(-,root,root,-)
 %{_includedir}/context-service/*.h
 %{_libdir}/pkgconfig/%{name}.pc
+
+%package test
+Summary:    Tizen Context Framework Testsuite
+Group:      Service/Context
+
+%description test
+Tizen Context Framework Testsuite
+
+%files test
+%manifest testsuite/context-test.manifest
+%{_bindir}/context-test
diff --git a/testsuite/CMakeLists.txt b/testsuite/CMakeLists.txt
new file mode 100644 (file)
index 0000000..65981a5
--- /dev/null
@@ -0,0 +1,13 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT(context-test)
+SET(TEST_EXEC context-test)
+
+# Source List
+FILE(GLOB_RECURSE TEST_SRCS src/*.cpp)
+
+ADD_EXECUTABLE(${TEST_EXEC} ${TEST_SRCS})
+TARGET_LINK_LIBRARIES(${TEST_EXEC} ${api_pkg_LDFLAGS} ${target} -pie)
+SET_TARGET_PROPERTIES(${TEST_EXEC} PROPERTIES COMPILE_FLAGS ${API_EXTRA_CFLAGS})
+SET_TARGET_PROPERTIES(${TEST_EXEC} PROPERTIES COMPILE_DEFINITIONS "LOG_TAG=\"CONTEXT-TEST\"")
+
+INSTALL(TARGETS ${TEST_EXEC} DESTINATION ${CMAKE_INSTALL_BINDIR})
diff --git a/testsuite/context-test.manifest b/testsuite/context-test.manifest
new file mode 100644 (file)
index 0000000..6b238b2
--- /dev/null
@@ -0,0 +1,8 @@
+<manifest>
+       <request>
+               <domain name="_"/>
+       </request>
+       <assign>
+               <filesystem path="/usr/bin/context-test" exec_label="User"/>
+       </assign>
+</manifest>
diff --git a/testsuite/src/history/history.cpp b/testsuite/src/history/history.cpp
new file mode 100644 (file)
index 0000000..c49401a
--- /dev/null
@@ -0,0 +1,382 @@
+/*
+ * Copyright (c) 2015 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 <context_history.h>
+#include <context_history_types_internal.h>
+
+#include "../shared.h"
+#include "history.h"
+
+using namespace ctx::test;
+
+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 __release_resource()
+{
+       context_history_list_destroy(list_h);
+       list_h = NULL;
+
+       context_history_record_destroy(record_h);
+       record_h = NULL;
+
+       context_history_filter_destroy(filter_h);
+       filter_h = NULL;
+}
+
+int test_history(int *argc, char ***argv)
+{
+       err = context_history_create(&history_h);
+       if (err != ERR_NONE) {
+               g_print(RED("Memory allocation failed\n"));
+               return 0;
+       }
+
+       ctx::test::newline_after_tcname = true;
+
+       while (true) {
+               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");
+               g_print("   5. Peak Time for Music         6. Peak Time for Video\n");
+               g_print("   7. Common Setting for App      8. Common Setting for Music\n");
+               g_print("   9. Common Setting for Video   10. Frequently Communicated Address\n");
+
+               while (!get_input_int("(to exit, ctrl-c)? ")) {;}
+
+               switch (int_val) {
+               case 1:
+                       run_testcase("/history/recently_used_app", _history_app_recently);
+                       break;
+               case 2:
+                       run_testcase("/history/frequently_used_app", _history_app_frequently);
+                       break;
+               case 3:
+                       run_testcase("/history/rarely_used_app", _history_app_rarely);
+                       break;
+               case 4:
+                       run_testcase("/history/peak_time_for_app", _history_app_peak_time);
+                       break;
+               case 5:
+                       run_testcase("/history/peak_time_for_music", _history_music_peak_time);
+                       break;
+               case 6:
+                       run_testcase("/history/peak_time_for_video", _history_video_peak_time);
+                       break;
+               case 7:
+                       run_testcase("/history/common_setting_for_app", _history_app_setting);
+                       break;
+               case 8:
+                       run_testcase("/history/common_setting_for_music", _history_music_setting);
+                       break;
+               case 9:
+                       run_testcase("/history/common_setting_for_video", _history_video_setting);
+                       break;
+               case 10:
+                       run_testcase("/history/frequently_communicated_address", _history_comm_frequently);
+                       break;
+               default:
+                       g_print("Invalid number\n");
+                       break;
+               }
+
+               __release_resource();
+       };
+
+       context_history_destroy(history_h);
+       return 0;
+}
+
+static void __filter_set_int(context_history_filter_e type, const char *key)
+{
+       if (filter_h == NULL)
+               context_history_filter_create(&filter_h);
+
+       std::string msg = " ";
+       msg = msg + key + "? ";
+
+       if (get_input_int(msg.c_str()))
+               context_history_filter_set_int(filter_h, type, int_val);
+}
+
+static void __filter_set_str(context_history_filter_e type, const char *key)
+{
+       if (filter_h == NULL)
+               context_history_filter_create(&filter_h);
+
+       std::string msg = " ";
+       msg = msg + key + "? ";
+
+       if (get_input_str(msg.c_str()))
+               context_history_filter_set_string(filter_h, type, str_val.c_str());
+}
+
+static bool __get_list(context_history_data_e type)
+{
+       err = context_history_get_list(history_h, type, filter_h, &list_h);
+
+       if (err == ERR_NOT_SUPPORTED) {
+               g_print(YELLOW("Not Supported") "\n");
+               return false;
+       }
+
+       if (err == ERR_NO_DATA) {
+               g_print(YELLOW("No Data") "\n");
+               return false;
+       }
+
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+       return true;
+}
+
+static bool __iterate_list(const char *int_attr[], const char *str_attr[])
+{
+       int cnt = 0;
+       err = context_history_list_get_count(list_h, &cnt);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       for (int i = 0; i < cnt; ++i) {
+               err = context_history_list_get_current(list_h, &record_h);
+               ASSERT_CMPINT(err, ==, ERR_NONE);
+               g_print("[%d] ", i);
+
+               /* Integer attributes */
+               for (int j = 0; int_attr[j] != NULL; ++j) {
+                       err = context_history_record_get_int(record_h, int_attr[j], &int_val);
+                       ASSERT_CMPINT(err, ==, ERR_NONE);
+                       g_print("%s(%d) ", int_attr[j], int_val);
+               }
+
+               /* String attributes */
+               for (int j = 0; str_attr[j] != NULL; ++j) {
+                       char *tmp = NULL;
+                       err = context_history_record_get_string(record_h, str_attr[j], &tmp);
+                       ASSERT_CMPINT(err, ==, ERR_NONE);
+                       g_print("%s(%s) ", str_attr[j], tmp);
+                       g_free(tmp);
+               }
+
+               if (i < cnt - 1) {
+                       err = context_history_list_move_next(list_h);
+                       ASSERT_CMPINT(err, ==, ERR_NONE);
+               }
+
+               context_history_record_destroy(record_h);
+               record_h = NULL;
+               g_print("\n");
+       }
+
+       return true;
+}
+
+bool _history_app_recently()
+{
+       const char *int_attr[] = {CONTEXT_HISTORY_TOTAL_COUNT, CONTEXT_HISTORY_TOTAL_DURATION, CONTEXT_HISTORY_LAST_TIME, NULL};
+       const char *str_attr[] = {CONTEXT_HISTORY_APP_ID, NULL};
+
+       __filter_set_int(CONTEXT_HISTORY_FILTER_RESULT_SIZE, FILTER_RESULT_SIZE);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_TIME_SPAN, FILTER_TIME_SPAN);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_START_TIME, FILTER_START_TIME);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_END_TIME, FILTER_END_TIME);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_AUDIO_JACK, FILTER_AUDIO_JACK);
+       __filter_set_str(CONTEXT_HISTORY_FILTER_WIFI_BSSID, FILTER_WIFI_BSSID);
+
+       bool_val = __get_list(CONTEXT_HISTORY_RECENTLY_USED_APP);
+       IF_FAIL_RETURN(bool_val, false);
+
+       bool_val = __iterate_list(int_attr, str_attr);
+       IF_FAIL_RETURN(bool_val, false);
+
+       return true;
+}
+
+bool _history_app_frequently()
+{
+       const char *int_attr[] = {CONTEXT_HISTORY_TOTAL_COUNT, CONTEXT_HISTORY_TOTAL_DURATION, CONTEXT_HISTORY_LAST_TIME, NULL};
+       const char *str_attr[] = {CONTEXT_HISTORY_APP_ID, NULL};
+
+       __filter_set_int(CONTEXT_HISTORY_FILTER_RESULT_SIZE, FILTER_RESULT_SIZE);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_TIME_SPAN, FILTER_TIME_SPAN);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_START_TIME, FILTER_START_TIME);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_END_TIME, FILTER_END_TIME);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_AUDIO_JACK, FILTER_AUDIO_JACK);
+       __filter_set_str(CONTEXT_HISTORY_FILTER_WIFI_BSSID, FILTER_WIFI_BSSID);
+
+       bool_val = __get_list(CONTEXT_HISTORY_FREQUENTLY_USED_APP);
+       IF_FAIL_RETURN(bool_val, false);
+
+       bool_val = __iterate_list(int_attr, str_attr);
+       IF_FAIL_RETURN(bool_val, false);
+
+       return true;
+}
+
+bool _history_app_rarely()
+{
+       const char *int_attr[] = {CONTEXT_HISTORY_TOTAL_COUNT, CONTEXT_HISTORY_TOTAL_DURATION, CONTEXT_HISTORY_LAST_TIME, NULL};
+       const char *str_attr[] = {CONTEXT_HISTORY_APP_ID, NULL};
+
+       __filter_set_int(CONTEXT_HISTORY_FILTER_RESULT_SIZE, FILTER_RESULT_SIZE);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_TIME_SPAN, FILTER_TIME_SPAN);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_START_TIME, FILTER_START_TIME);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_END_TIME, FILTER_END_TIME);
+
+       bool_val = __get_list(CONTEXT_HISTORY_RARELY_USED_APP);
+       IF_FAIL_RETURN(bool_val, false);
+
+       bool_val = __iterate_list(int_attr, str_attr);
+       IF_FAIL_RETURN(bool_val, false);
+
+       return true;
+}
+
+bool _history_app_peak_time()
+{
+       const char *int_attr[] = {CONTEXT_HISTORY_HOUR_OF_DAY, CONTEXT_HISTORY_TOTAL_COUNT, NULL};
+       const char *str_attr[] = {NULL};
+
+       __filter_set_int(CONTEXT_HISTORY_FILTER_RESULT_SIZE, FILTER_RESULT_SIZE);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_TIME_SPAN, FILTER_TIME_SPAN);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_START_TIME, FILTER_START_TIME);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_END_TIME, FILTER_END_TIME);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_DAY_OF_WEEK, FILTER_DAY_OF_WEEK);
+       __filter_set_str(CONTEXT_HISTORY_FILTER_APP_ID, FILTER_APP_ID);
+
+       bool_val = __get_list(CONTEXT_HISTORY_PEAK_TIME_FOR_APP);
+       IF_FAIL_RETURN(bool_val, false);
+
+       bool_val = __iterate_list(int_attr, str_attr);
+       IF_FAIL_RETURN(bool_val, false);
+
+       return true;
+}
+
+bool _history_app_setting()
+{
+       const char *int_attr[] = {CONTEXT_HISTORY_AUDIO_JACK, CONTEXT_HISTORY_SYSTEM_VOLUME, CONTEXT_HISTORY_MEDIA_VOLUME, NULL};
+       const char *str_attr[] = {NULL};
+
+       __filter_set_int(CONTEXT_HISTORY_FILTER_TIME_SPAN, FILTER_TIME_SPAN);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_START_TIME, FILTER_START_TIME);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_END_TIME, FILTER_END_TIME);
+       __filter_set_str(CONTEXT_HISTORY_FILTER_APP_ID, FILTER_APP_ID);
+
+       bool_val = __get_list(CONTEXT_HISTORY_COMMON_SETTING_FOR_APP);
+       IF_FAIL_RETURN(bool_val, false);
+
+       bool_val = __iterate_list(int_attr, str_attr);
+       IF_FAIL_RETURN(bool_val, false);
+
+       return true;
+}
+
+bool _history_music_peak_time()
+{
+       const char *int_attr[] = {CONTEXT_HISTORY_HOUR_OF_DAY, CONTEXT_HISTORY_TOTAL_COUNT, NULL};
+       const char *str_attr[] = {NULL};
+
+       __filter_set_int(CONTEXT_HISTORY_FILTER_RESULT_SIZE, FILTER_RESULT_SIZE);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_TIME_SPAN, FILTER_TIME_SPAN);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_START_TIME, FILTER_START_TIME);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_END_TIME, FILTER_END_TIME);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_DAY_OF_WEEK, FILTER_DAY_OF_WEEK);
+
+       bool_val = __get_list(CONTEXT_HISTORY_PEAK_TIME_FOR_MUSIC);
+       IF_FAIL_RETURN(bool_val, false);
+
+       bool_val = __iterate_list(int_attr, str_attr);
+       IF_FAIL_RETURN(bool_val, false);
+
+       return true;
+}
+
+bool _history_music_setting()
+{
+       const char *int_attr[] = {CONTEXT_HISTORY_AUDIO_JACK, CONTEXT_HISTORY_SYSTEM_VOLUME, CONTEXT_HISTORY_MEDIA_VOLUME, NULL};
+       const char *str_attr[] = {NULL};
+
+       __filter_set_int(CONTEXT_HISTORY_FILTER_TIME_SPAN, FILTER_TIME_SPAN);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_START_TIME, FILTER_START_TIME);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_END_TIME, FILTER_END_TIME);
+
+       bool_val = __get_list(CONTEXT_HISTORY_COMMON_SETTING_FOR_MUSIC);
+       IF_FAIL_RETURN(bool_val, false);
+
+       bool_val = __iterate_list(int_attr, str_attr);
+       IF_FAIL_RETURN(bool_val, false);
+
+       return true;
+}
+
+bool _history_video_peak_time()
+{
+       const char *int_attr[] = {CONTEXT_HISTORY_HOUR_OF_DAY, CONTEXT_HISTORY_TOTAL_COUNT, NULL};
+       const char *str_attr[] = {NULL};
+
+       __filter_set_int(CONTEXT_HISTORY_FILTER_RESULT_SIZE, FILTER_RESULT_SIZE);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_TIME_SPAN, FILTER_TIME_SPAN);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_START_TIME, FILTER_START_TIME);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_END_TIME, FILTER_END_TIME);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_DAY_OF_WEEK, FILTER_DAY_OF_WEEK);
+
+       bool_val = __get_list(CONTEXT_HISTORY_PEAK_TIME_FOR_VIDEO);
+       IF_FAIL_RETURN(bool_val, false);
+
+       bool_val = __iterate_list(int_attr, str_attr);
+       IF_FAIL_RETURN(bool_val, false);
+
+       return true;
+}
+
+bool _history_video_setting()
+{
+       const char *int_attr[] = {CONTEXT_HISTORY_AUDIO_JACK, CONTEXT_HISTORY_SYSTEM_VOLUME, CONTEXT_HISTORY_MEDIA_VOLUME, NULL};
+       const char *str_attr[] = {NULL};
+
+       __filter_set_int(CONTEXT_HISTORY_FILTER_TIME_SPAN, FILTER_TIME_SPAN);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_START_TIME, FILTER_START_TIME);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_END_TIME, FILTER_END_TIME);
+
+       bool_val = __get_list(CONTEXT_HISTORY_COMMON_SETTING_FOR_VIDEO);
+       IF_FAIL_RETURN(bool_val, false);
+
+       bool_val = __iterate_list(int_attr, str_attr);
+       IF_FAIL_RETURN(bool_val, false);
+
+       return true;
+}
+
+bool _history_comm_frequently()
+{
+       const char *int_attr[] = {CONTEXT_HISTORY_TOTAL_COUNT, CONTEXT_HISTORY_TOTAL_DURATION, CONTEXT_HISTORY_LAST_TIME, NULL};
+       const char *str_attr[] = {CONTEXT_HISTORY_ADDRESS, NULL};
+
+       __filter_set_int(CONTEXT_HISTORY_FILTER_RESULT_SIZE, FILTER_RESULT_SIZE);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_TIME_SPAN, FILTER_TIME_SPAN);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_START_TIME, FILTER_START_TIME);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_END_TIME, FILTER_END_TIME);
+       __filter_set_int(CONTEXT_HISTORY_FILTER_COMMUNICATION_TYPE, FILTER_COMMUNICATION_TYPE);
+
+       bool_val = __get_list(CONTEXT_HISTORY_FREQUENTLY_COMMUNICATED_ADDRESS);
+       IF_FAIL_RETURN(bool_val, false);
+
+       bool_val = __iterate_list(int_attr, str_attr);
+       IF_FAIL_RETURN(bool_val, false);
+
+       return true;
+}
diff --git a/testsuite/src/history/history.h b/testsuite/src/history/history.h
new file mode 100644 (file)
index 0000000..18cb075
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2015 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_TEST_HISTORY_H__
+#define __CONTEXT_TEST_HISTORY_H__
+
+int test_history(int *argc, char ***argv);
+
+bool _history_app_recently();
+bool _history_app_frequently();
+bool _history_app_rarely();
+bool _history_app_peak_time();
+bool _history_app_setting();
+
+bool _history_music_peak_time();
+bool _history_music_setting();
+bool _history_video_peak_time();
+bool _history_video_setting();
+
+bool _history_comm_frequently();
+
+#endif
diff --git a/testsuite/src/main.cpp b/testsuite/src/main.cpp
new file mode 100644 (file)
index 0000000..11f7ae9
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2015 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 "trigger/condition.h"
+#include "trigger/event.h"
+#include "history/history.h"
+
+static void signal_handler(int signo)
+{
+       if (signo == SIGINT && ctx::test::is_mainloop_running()) {
+               g_print(PURPLE("\nskipping the testcase..."));
+               ctx::test::stop_mainloop();
+       } else {
+               g_print(PURPLE("\nterminating... (SIGNAL %d)\n"), signo);
+               exit(EXIT_SUCCESS);
+       }
+}
+
+static void print_usage()
+{
+       g_print("Usage: context-test TESTSUITE\n\n");
+       g_print("TESTSUITE can be one of the followings:\n");
+       g_print("  h\t for testing all contextual history items\n");
+       g_print("  c\t for testing all contextual trigger conditions\n");
+       g_print("  e\t for testing all contextual trigger events\n");
+}
+
+int main(int argc, char** argv)
+{
+       signal(SIGINT,  signal_handler);
+       signal(SIGHUP,  signal_handler);
+       signal(SIGTERM, signal_handler);
+       signal(SIGQUIT, signal_handler);
+       signal(SIGABRT, signal_handler);
+
+       if (argc < 2) {
+               print_usage();
+               return EXIT_SUCCESS;
+       }
+
+       switch (argv[1][0]) {
+       case 'h':
+               return test_history(&argc, &argv);
+       case 'c':
+               return test_trigger_condition(&argc, &argv);
+       case 'e':
+               return test_trigger_event(&argc, &argv);
+       default:
+               print_usage();
+               return EXIT_SUCCESS;
+       }
+}
diff --git a/testsuite/src/shared.cpp b/testsuite/src/shared.cpp
new file mode 100644 (file)
index 0000000..a52e126
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2015 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"
+
+static bool __is_mainloop_running = false;
+
+namespace ctx {
+       namespace test {
+               GMainLoop *mainloop = NULL;
+               int callback_count = 0;
+               int err = 0;
+               int req_id = 0;
+               int int_val = 0;
+               bool bool_val = false;
+               bool skip_tc = false;
+               bool newline_after_tcname = false;
+               ctx::json json_val = EMPTY_JSON_OBJECT;
+               std::string str_val = "";
+       }
+}
+
+void ctx::test::start_mainloop()
+{
+       if (!__is_mainloop_running) {
+               __is_mainloop_running = true;
+               mainloop = g_main_loop_new(NULL, FALSE);
+               g_main_loop_run(mainloop);
+       }
+}
+
+void ctx::test::stop_mainloop()
+{
+       if (__is_mainloop_running) {
+               g_main_loop_quit(mainloop);
+               g_main_loop_unref(mainloop);
+               ctx::test::skip_tc = true;
+               __is_mainloop_running = false;
+       }
+}
+
+bool ctx::test::is_mainloop_running()
+{
+       return __is_mainloop_running;
+}
+
+void ctx::test::cleanup_vars()
+{
+       ctx::test::callback_count = 5;
+       ctx::test::err = -1;
+       ctx::test::req_id = -1;
+       ctx::test::json_val = EMPTY_JSON_OBJECT;
+       ctx::test::bool_val = false;
+       ctx::test::int_val = 0;
+       ctx::test::str_val = "";
+       ctx::test::skip_tc = false;
+}
+
+bool ctx::test::get_input_int(const char *msg)
+{
+       std::cout << msg;
+       std::getline(std::cin, str_val);
+       IF_FAIL_RETURN(!str_val.empty(), false);
+       int_val = atoi(str_val.c_str());
+       return true;
+}
+
+bool ctx::test::get_input_str(const char *msg)
+{
+       std::cout << msg;
+       std::getline(std::cin, str_val);
+       IF_FAIL_RETURN(!str_val.empty(), false);
+       return true;
+}
+
+void ctx::test::run_testcase(const char *tc_name, bool (*tc_func)(void))
+{
+       IF_FAIL_VOID(tc_name && tc_func);
+
+       g_print(CYAN("%s") ": ", tc_name);
+       if (newline_after_tcname)
+               g_print("\n");
+
+       cleanup_vars();
+       bool result = tc_func();
+       if (result)
+               g_print(GREEN("PASS") "\n");
+}
diff --git a/testsuite/src/shared.h b/testsuite/src/shared.h
new file mode 100644 (file)
index 0000000..4e0c4eb
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2015 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_TEST_SHARED_H__
+#define __CONTEXT_TEST_SHARED_H__
+
+#include <cstdlib>
+#include <iostream>
+#include <string>
+#include <glib.h>
+#include <types_internal.h>
+#include <json.h>
+
+#define CALLBACK_COUNT 5
+
+#define ASSERT(condition) \
+       do { \
+               if (!(condition)) { \
+                       g_print("\n"); \
+                       if (!ctx::test::skip_tc) \
+                               g_print(RED("FAIL") ": %s(%d) > %s\n", __FUNCTION__, __LINE__, #condition); \
+                       return false; \
+               } \
+       } while (0)
+
+#define ASSERT_CMPINT(left, comp, right) \
+       do { \
+               if (!((left) comp (right))) { \
+                       g_print("\n"); \
+                       if (!ctx::test::skip_tc) \
+                               g_print(RED("FAIL") ": %s(%d) > %s(%d) %s %s(%d)\n", __FUNCTION__, __LINE__, #left, (left), #comp, #right, (right)); \
+                       return false; \
+               } \
+       } while (0)
+
+#define ASSERT_CONTAIN_STR(JSONOBJ, VKEY) \
+       do { \
+               ctx::test::bool_val = (JSONOBJ).get(NULL, (VKEY), &ctx::test::str_val); \
+               if (!ctx::test::bool_val) { \
+                       g_print("\n"); \
+                       if (!ctx::test::skip_tc) \
+                               g_print(RED("FAIL") ": %s(%d) > Getting the string '%s'\n", __FUNCTION__, __LINE__, (VKEY)); \
+                       return false; \
+               } \
+               g_print("%s(%s) ", (VKEY), ctx::test::str_val.c_str()); \
+       } while (0)
+
+#define ASSERT_CONTAIN_INT(JSONOBJ, VKEY) \
+       do { \
+               ctx::test::bool_val = (JSONOBJ).get(NULL, (VKEY), &ctx::test::int_val); \
+               if (!ctx::test::bool_val) { \
+                       g_print("\n"); \
+                       if (!ctx::test::skip_tc) \
+                               g_print(RED("FAIL") ": %s(%d) > Getting the integer '%s'\n", __FUNCTION__, __LINE__, (VKEY)); \
+                       return false; \
+               } \
+               g_print("%s(%d) ", (VKEY), ctx::test::int_val); \
+       } while (0)
+
+namespace ctx {
+       namespace test {
+               extern GMainLoop *mainloop;
+               extern int callback_count;
+               extern int err;
+               extern int req_id;
+               extern int int_val;
+               extern bool bool_val;
+               extern bool skip_tc;
+               extern bool newline_after_tcname;
+               extern ctx::json json_val;
+               extern std::string str_val;
+
+               void start_mainloop();
+               void stop_mainloop();
+               bool is_mainloop_running();
+
+               void cleanup_vars();
+               bool get_input_int(const char *msg);
+               bool get_input_str(const char *msg);
+
+               void run_testcase(const char *tc_name, bool (*tc_func)(void));
+       }
+}
+
+#endif
diff --git a/testsuite/src/trigger/condition.cpp b/testsuite/src/trigger/condition.cpp
new file mode 100644 (file)
index 0000000..899d29f
--- /dev/null
@@ -0,0 +1,469 @@
+/*
+ * Copyright (c) 2015 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 <request_handler.h>
+#include <context_trigger.h>
+#include <context_trigger_types_internal.h>
+#include <context_history.h>
+
+#include "../shared.h"
+#include "condition.h"
+
+using namespace ctx::test;
+
+int test_trigger_condition(int *argc, char ***argv)
+{
+       run_testcase("/trigger/cond/battery", _trigger_cond_battery);
+       run_testcase("/trigger/cond/charger", _trigger_cond_charger);
+       run_testcase("/trigger/cond/gps", _trigger_cond_gps);
+       run_testcase("/trigger/cond/headphone", _trigger_cond_headphone);
+       run_testcase("/trigger/cond/usb", _trigger_cond_usb);
+       run_testcase("/trigger/cond/wifi", _trigger_cond_wifi);
+       run_testcase("/trigger/cond/psmode", _trigger_cond_psmode);
+       run_testcase("/trigger/cond/call", _trigger_cond_call);
+
+       run_testcase("/trigger/cond/app_freq1", _trigger_cond_app_use_freq1);
+       run_testcase("/trigger/cond/app_freq2", _trigger_cond_app_use_freq2);
+       run_testcase("/trigger/cond/app_freq3", _trigger_cond_app_use_freq3);
+       run_testcase("/trigger/cond/app_freq4", _trigger_cond_app_use_freq4);
+
+       run_testcase("/trigger/cond/comm_freq1", _trigger_cond_comm_freq1);
+       run_testcase("/trigger/cond/comm_freq2", _trigger_cond_comm_freq2);
+       run_testcase("/trigger/cond/comm_freq3", _trigger_cond_comm_freq3);
+       run_testcase("/trigger/cond/comm_freq4", _trigger_cond_comm_freq4);
+
+       run_testcase("/trigger/cond/music_freq1", _trigger_cond_music_freq1);
+       run_testcase("/trigger/cond/music_freq2", _trigger_cond_music_freq2);
+       run_testcase("/trigger/cond/music_freq3", _trigger_cond_music_freq3);
+       run_testcase("/trigger/cond/music_freq4", _trigger_cond_music_freq4);
+
+       run_testcase("/trigger/cond/video_freq1", _trigger_cond_video_freq1);
+       run_testcase("/trigger/cond/video_freq2", _trigger_cond_video_freq2);
+       run_testcase("/trigger/cond/video_freq3", _trigger_cond_video_freq3);
+       run_testcase("/trigger/cond/video_freq4", _trigger_cond_video_freq4);
+
+       return 0;
+}
+
+static bool __support(context_trigger_condition_e cond)
+{
+       err = context_trigger_rule_condition_is_supported(cond, &bool_val);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       if (!bool_val) {
+               g_print(YELLOW("Not Supported") "\n");
+               return false;
+       }
+
+       return true;
+}
+
+bool _trigger_cond_battery()
+{
+       if (!__support(CONTEXT_TRIGGER_CONDITION_BATTERY)) return false;
+
+       err = ctx::request_handler::read_sync(CT_CONDITION_BATTERY, NULL, &req_id, &json_val);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_LEVEL);
+       ASSERT_CONTAIN_INT(json_val, CONTEXT_TRIGGER_IS_CHARGING);
+
+       return true;
+}
+
+bool _trigger_cond_charger()
+{
+       if (!__support(CONTEXT_TRIGGER_CONDITION_CHARGER)) return false;
+
+       err = ctx::request_handler::read_sync(CT_CONDITION_CHARGER, NULL, &req_id, &json_val);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+       ASSERT_CONTAIN_INT(json_val, CONTEXT_TRIGGER_IS_CONNECTED);
+
+       return true;
+}
+
+bool _trigger_cond_gps()
+{
+       if (!__support(CONTEXT_TRIGGER_CONDITION_GPS)) return false;
+
+       err = ctx::request_handler::read_sync(CT_CONDITION_GPS, NULL, &req_id, &json_val);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_STATE);
+
+       return true;
+}
+
+bool _trigger_cond_headphone()
+{
+       if (!__support(CONTEXT_TRIGGER_CONDITION_HEADPHONE)) return false;
+
+       err = ctx::request_handler::read_sync(CT_CONDITION_HEADPHONE, NULL, &req_id, &json_val);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       ASSERT_CONTAIN_INT(json_val, CONTEXT_TRIGGER_IS_CONNECTED);
+       if (int_val == CONTEXT_TRIGGER_FALSE) return true;
+
+       ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_TYPE);
+
+       return true;
+}
+
+bool _trigger_cond_usb()
+{
+       if (!__support(CONTEXT_TRIGGER_CONDITION_USB)) return false;
+
+       err = ctx::request_handler::read_sync(CT_CONDITION_USB, NULL, &req_id, &json_val);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       ASSERT_CONTAIN_INT(json_val, CONTEXT_TRIGGER_IS_CONNECTED);
+
+       return true;
+}
+
+bool _trigger_cond_wifi()
+{
+       if (!__support(CONTEXT_TRIGGER_CONDITION_WIFI)) return false;
+
+       err = ctx::request_handler::read_sync(CT_CONDITION_WIFI, NULL, &req_id, &json_val);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_STATE);
+
+       if (str_val == CONTEXT_TRIGGER_CONNECTED)
+               ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_BSSID);
+
+       return true;
+}
+
+bool _trigger_cond_psmode()
+{
+       if (!__support(CONTEXT_TRIGGER_CONDITION_POWER_SAVING_MODE)) return false;
+
+       err = ctx::request_handler::read_sync(CT_CONDITION_POWER_SAVING_MODE, NULL, &req_id, &json_val);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       ASSERT_CONTAIN_INT(json_val, CONTEXT_TRIGGER_IS_ENABLED);
+
+       return true;
+}
+
+bool _trigger_cond_call()
+{
+       if (!__support(CONTEXT_TRIGGER_CONDITION_CALL)) return false;
+
+       err = ctx::request_handler::read_sync(CT_CONDITION_CALL, NULL, &req_id, &json_val);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_STATE);
+       if (str_val == CONTEXT_TRIGGER_IDLE) return true;
+
+       ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_MEDIUM);
+       ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_ADDRESS);
+
+       return true;
+}
+
+static std::string& __get_objective_app_id()
+{
+       static std::string app_id;
+
+       if (!app_id.empty())
+               return app_id;
+
+       int list_size = 0;
+       context_history_h handle = NULL;
+       context_history_filter_h filter = NULL;
+       context_history_list_h list = NULL;
+
+       context_history_create(&handle);
+       context_history_filter_create(&filter);
+       context_history_filter_set_int(filter, CONTEXT_HISTORY_FILTER_RESULT_SIZE, 1);
+       context_history_get_list(handle, CONTEXT_HISTORY_FREQUENTLY_USED_APP, filter, &list);
+       context_history_list_get_count(list, &list_size);
+
+       if (list_size > 0) {
+               char *buffer = NULL;
+               context_history_record_h record = NULL;
+               context_history_list_get_current(list, &record);
+               context_history_record_get_string(record, CONTEXT_HISTORY_APP_ID, &buffer);
+               if (buffer) {
+                       app_id = buffer;
+                       g_free(buffer);
+               }
+               context_history_record_destroy(record);
+       }
+
+       context_history_list_destroy(list);
+       context_history_filter_destroy(filter);
+       context_history_destroy(handle);
+
+       if (app_id.empty())
+               app_id = "org.tizen.gallery";
+
+       return app_id;
+}
+
+static bool __trigger_cond_app_use_freq_base(ctx::json option)
+{
+       if (!__support(CONTEXT_TRIGGER_CONDITION_APP_USE_FREQUENCY)) return false;
+
+       err = ctx::request_handler::read_sync(CT_CONDITION_APP_USE_FREQUENCY, &option, &req_id, &json_val);
+       if (err == ERR_NO_DATA) {
+               g_print(RED("No Data "));
+               return true;
+       }
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_APP_ID);
+       ASSERT_CONTAIN_INT(json_val, CONTEXT_TRIGGER_RANK);
+       ASSERT_CONTAIN_INT(json_val, CONTEXT_TRIGGER_TOTAL_COUNT);
+
+       return true;
+}
+
+bool _trigger_cond_app_use_freq1()
+{
+       /* Case 1: App ID only */
+       ctx::json option;
+       option.set(NULL, CONTEXT_TRIGGER_APP_ID, __get_objective_app_id());
+
+       return __trigger_cond_app_use_freq_base(option);
+}
+
+bool _trigger_cond_app_use_freq2()
+{
+       /* Case 2: App ID, Day of Week */
+       ctx::json option;
+       option.set(NULL, CONTEXT_TRIGGER_APP_ID, __get_objective_app_id());
+       option.set(NULL, CONTEXT_TRIGGER_DAY_OF_WEEK, CONTEXT_TRIGGER_WEEKDAY);
+
+       return __trigger_cond_app_use_freq_base(option);
+}
+
+bool _trigger_cond_app_use_freq3()
+{
+       /* Case 3: App ID, Time of Day */
+       ctx::json option;
+       option.set(NULL, CONTEXT_TRIGGER_APP_ID, __get_objective_app_id());
+       option.set(NULL, CONTEXT_TRIGGER_TIME_OF_DAY, "10-16");
+
+       return __trigger_cond_app_use_freq_base(option);
+}
+
+bool _trigger_cond_app_use_freq4()
+{
+       /* Case 3: App ID, Time of Day, Day of Week */
+       ctx::json option;
+       option.set(NULL, CONTEXT_TRIGGER_APP_ID, __get_objective_app_id());
+       option.set(NULL, CONTEXT_TRIGGER_DAY_OF_WEEK, CONTEXT_TRIGGER_WEEKDAY);
+       option.set(NULL, CONTEXT_TRIGGER_TIME_OF_DAY, "10-16");
+
+       return __trigger_cond_app_use_freq_base(option);
+}
+
+static std::string& __get_objective_address()
+{
+       static std::string address;
+
+       if (!address.empty())
+               return address;
+
+       int list_size = 0;
+       context_history_h handle = NULL;
+       context_history_filter_h filter = NULL;
+       context_history_list_h list = NULL;
+
+       context_history_create(&handle);
+       context_history_filter_create(&filter);
+       context_history_filter_set_int(filter, CONTEXT_HISTORY_FILTER_RESULT_SIZE, 1);
+       context_history_get_list(handle, CONTEXT_HISTORY_FREQUENTLY_COMMUNICATED_ADDRESS, filter, &list);
+       context_history_list_get_count(list, &list_size);
+
+       if (list_size > 0) {
+               char *buffer = NULL;
+               context_history_record_h record = NULL;
+               context_history_list_get_current(list, &record);
+               context_history_record_get_string(record, CONTEXT_HISTORY_ADDRESS, &buffer);
+               if (buffer) {
+                       address = buffer;
+                       g_free(buffer);
+               }
+               context_history_record_destroy(record);
+       }
+
+       context_history_list_destroy(list);
+       context_history_filter_destroy(filter);
+       context_history_destroy(handle);
+
+       if (address.empty())
+               address = "000-000-0000";
+
+       return address;
+}
+
+static bool __trigger_cond_comm_freq_base(ctx::json option)
+{
+       if (!__support(CONTEXT_TRIGGER_CONDITION_COMMUNICATION_FREQUENCY)) return false;
+
+       err = ctx::request_handler::read_sync(CT_CONDITION_COMMUNICATION_FREQUENCY, &option, &req_id, &json_val);
+       if (err == ERR_NO_DATA) {
+               g_print(RED("No Data "));
+               return true;
+       }
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_ADDRESS);
+       ASSERT_CONTAIN_INT(json_val, CONTEXT_TRIGGER_RANK);
+       ASSERT_CONTAIN_INT(json_val, CONTEXT_TRIGGER_TOTAL_COUNT);
+
+       return true;
+}
+
+bool _trigger_cond_comm_freq1()
+{
+       /* Case 1: Address only */
+       ctx::json option;
+       option.set(NULL, CONTEXT_TRIGGER_ADDRESS, __get_objective_address());
+
+       return __trigger_cond_comm_freq_base(option);
+}
+
+bool _trigger_cond_comm_freq2()
+{
+       /* Case 2: Address, Day of Week */
+       ctx::json option;
+       option.set(NULL, CONTEXT_TRIGGER_ADDRESS, __get_objective_address());
+       option.set(NULL, CONTEXT_TRIGGER_DAY_OF_WEEK, CONTEXT_TRIGGER_WEEKDAY);
+
+       return __trigger_cond_comm_freq_base(option);
+}
+
+bool _trigger_cond_comm_freq3()
+{
+       /* Case 3: Address, Time of Day */
+       ctx::json option;
+       option.set(NULL, CONTEXT_TRIGGER_ADDRESS, __get_objective_address());
+       option.set(NULL, CONTEXT_TRIGGER_TIME_OF_DAY, "10-16");
+
+       return __trigger_cond_comm_freq_base(option);
+}
+
+bool _trigger_cond_comm_freq4()
+{
+       /* Case 4: Address, Day of Week, Time of Day */
+       ctx::json option;
+       option.set(NULL, CONTEXT_TRIGGER_ADDRESS, __get_objective_address());
+       option.set(NULL, CONTEXT_TRIGGER_DAY_OF_WEEK, CONTEXT_TRIGGER_WEEKDAY);
+       option.set(NULL, CONTEXT_TRIGGER_TIME_OF_DAY, "10-16");
+
+       return __trigger_cond_comm_freq_base(option);
+}
+
+static bool __trigger_cond_media_freq_base(context_trigger_condition_e item, const char *item_str, ctx::json option)
+{
+       if (!__support(item)) return false;
+
+       err = ctx::request_handler::read_sync(item_str, &option, &req_id, &json_val);
+       if (err == ERR_NO_DATA) {
+               g_print(YELLOW("No Data "));
+               return true;
+       }
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+       ASSERT_CONTAIN_INT(json_val, CONTEXT_TRIGGER_TOTAL_COUNT);
+
+       return true;
+}
+
+bool _trigger_cond_music_freq1()
+{
+       ctx::json option;
+
+       return __trigger_cond_media_freq_base(
+                       CONTEXT_TRIGGER_CONDITION_MUSIC_PLAYBACK_FREQUENCY,
+                       CT_CONDITION_MUSIC_PLAYBACK_FREQUENCY, option);
+}
+
+bool _trigger_cond_music_freq2()
+{
+       ctx::json option;
+       option.set(NULL, CONTEXT_TRIGGER_DAY_OF_WEEK, CONTEXT_TRIGGER_WEEKDAY);
+
+       return __trigger_cond_media_freq_base(
+                       CONTEXT_TRIGGER_CONDITION_MUSIC_PLAYBACK_FREQUENCY,
+                       CT_CONDITION_MUSIC_PLAYBACK_FREQUENCY, option);
+}
+
+bool _trigger_cond_music_freq3()
+{
+       ctx::json option;
+       option.set(NULL, CONTEXT_TRIGGER_TIME_OF_DAY, "10-16");
+
+       return __trigger_cond_media_freq_base(
+                       CONTEXT_TRIGGER_CONDITION_MUSIC_PLAYBACK_FREQUENCY,
+                       CT_CONDITION_MUSIC_PLAYBACK_FREQUENCY, option);
+}
+
+bool _trigger_cond_music_freq4()
+{
+       ctx::json option;
+       option.set(NULL, CONTEXT_TRIGGER_DAY_OF_WEEK, CONTEXT_TRIGGER_WEEKDAY);
+       option.set(NULL, CONTEXT_TRIGGER_TIME_OF_DAY, "10-16");
+
+       return __trigger_cond_media_freq_base(
+                       CONTEXT_TRIGGER_CONDITION_MUSIC_PLAYBACK_FREQUENCY,
+                       CT_CONDITION_MUSIC_PLAYBACK_FREQUENCY, option);
+}
+
+bool _trigger_cond_video_freq1()
+{
+       ctx::json option;
+
+       return __trigger_cond_media_freq_base(
+                       CONTEXT_TRIGGER_CONDITION_VIDEO_PLAYBACK_FREQUENCY,
+                       CT_CONDITION_VIDEO_PLAYBACK_FREQUENCY, option);
+}
+
+bool _trigger_cond_video_freq2()
+{
+       ctx::json option;
+       option.set(NULL, CONTEXT_TRIGGER_DAY_OF_WEEK, CONTEXT_TRIGGER_WEEKDAY);
+
+       return __trigger_cond_media_freq_base(
+                       CONTEXT_TRIGGER_CONDITION_VIDEO_PLAYBACK_FREQUENCY,
+                       CT_CONDITION_VIDEO_PLAYBACK_FREQUENCY, option);
+}
+
+bool _trigger_cond_video_freq3()
+{
+       ctx::json option;
+       option.set(NULL, CONTEXT_TRIGGER_TIME_OF_DAY, "10-16");
+
+       return __trigger_cond_media_freq_base(
+                       CONTEXT_TRIGGER_CONDITION_VIDEO_PLAYBACK_FREQUENCY,
+                       CT_CONDITION_VIDEO_PLAYBACK_FREQUENCY, option);
+}
+
+bool _trigger_cond_video_freq4()
+{
+       ctx::json option;
+       option.set(NULL, CONTEXT_TRIGGER_DAY_OF_WEEK, CONTEXT_TRIGGER_WEEKDAY);
+       option.set(NULL, CONTEXT_TRIGGER_TIME_OF_DAY, "10-16");
+
+       return __trigger_cond_media_freq_base(
+                       CONTEXT_TRIGGER_CONDITION_VIDEO_PLAYBACK_FREQUENCY,
+                       CT_CONDITION_VIDEO_PLAYBACK_FREQUENCY, option);
+}
diff --git a/testsuite/src/trigger/condition.h b/testsuite/src/trigger/condition.h
new file mode 100644 (file)
index 0000000..b5683f1
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2015 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_TEST_TRIGGER_CONDITION_H__
+#define __CONTEXT_TEST_TRIGGER_CONDITION_H__
+
+int test_trigger_condition(int *argc, char ***argv);
+
+bool _trigger_cond_battery();
+bool _trigger_cond_charger();
+bool _trigger_cond_gps();
+bool _trigger_cond_headphone();
+bool _trigger_cond_usb();
+bool _trigger_cond_wifi();
+bool _trigger_cond_psmode();
+bool _trigger_cond_call();
+
+bool _trigger_cond_app_use_freq1();
+bool _trigger_cond_app_use_freq2();
+bool _trigger_cond_app_use_freq3();
+bool _trigger_cond_app_use_freq4();
+
+bool _trigger_cond_comm_freq1();
+bool _trigger_cond_comm_freq2();
+bool _trigger_cond_comm_freq3();
+bool _trigger_cond_comm_freq4();
+
+bool _trigger_cond_music_freq1();
+bool _trigger_cond_music_freq2();
+bool _trigger_cond_music_freq3();
+bool _trigger_cond_music_freq4();
+
+bool _trigger_cond_video_freq1();
+bool _trigger_cond_video_freq2();
+bool _trigger_cond_video_freq3();
+bool _trigger_cond_video_freq4();
+
+#endif
diff --git a/testsuite/src/trigger/event.cpp b/testsuite/src/trigger/event.cpp
new file mode 100644 (file)
index 0000000..ade9d38
--- /dev/null
@@ -0,0 +1,320 @@
+/*
+ * Copyright (c) 2015 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 <request_handler.h>
+#include <context_trigger.h>
+#include <context_trigger_types_internal.h>
+
+#include "../shared.h"
+#include "event.h"
+
+using namespace ctx::test;
+
+static void __domain_cb(const char *item, int rid, int error, ctx::json data)
+{
+       g_print("[%d] %s: Error=%#x, Data=%s\n", callback_count--, item, error, data.str().c_str());
+
+       if (callback_count == 0) {
+               json_val = data;
+               stop_mainloop();
+       }
+}
+
+int test_trigger_event(int *argc, char ***argv)
+{
+       g_test_init(argc, argv, NULL);
+
+       ctx::request_handler::register_callback(CT_EVENT_BATTERY, __domain_cb);
+       ctx::request_handler::register_callback(CT_EVENT_CHARGER, __domain_cb);
+       ctx::request_handler::register_callback(CT_EVENT_GPS, __domain_cb);
+       ctx::request_handler::register_callback(CT_EVENT_HEADPHONE, __domain_cb);
+       ctx::request_handler::register_callback(CT_EVENT_POWER_SAVING_MODE, __domain_cb);
+       ctx::request_handler::register_callback(CT_EVENT_USB, __domain_cb);
+       ctx::request_handler::register_callback(CT_EVENT_WIFI, __domain_cb);
+       ctx::request_handler::register_callback(CT_EVENT_CALL, __domain_cb);
+       ctx::request_handler::register_callback(CT_EVENT_EMAIL, __domain_cb);
+       ctx::request_handler::register_callback(CT_EVENT_MESSAGE, __domain_cb);
+       ctx::request_handler::register_callback(CT_EVENT_ACTIVITY_STATIONARY, __domain_cb);
+       ctx::request_handler::register_callback(CT_EVENT_ACTIVITY_WALKING, __domain_cb);
+       ctx::request_handler::register_callback(CT_EVENT_ACTIVITY_RUNNING, __domain_cb);
+       ctx::request_handler::register_callback(CT_EVENT_ACTIVITY_IN_VEHICLE, __domain_cb);
+       ctx::request_handler::register_callback(CT_EVENT_PLACE, __domain_cb);
+
+       run_testcase("/trigger/event/battery", _trigger_event_battery);
+       run_testcase("/trigger/event/charger", _trigger_event_charger);
+       run_testcase("/trigger/event/gps", _trigger_event_gps);
+       run_testcase("/trigger/event/headphone", _trigger_event_headphone);
+       run_testcase("/trigger/event/usb", _trigger_event_usb);
+       run_testcase("/trigger/event/wifi", _trigger_event_wifi);
+       run_testcase("/trigger/event/psmode", _trigger_event_psmode);
+       run_testcase("/trigger/event/call", _trigger_event_call);
+       run_testcase("/trigger/event/email", _trigger_event_email);
+       run_testcase("/trigger/event/message", _trigger_event_message);
+       run_testcase("/trigger/event/activity", _trigger_event_activity);
+       run_testcase("/trigger/event/place", _trigger_event_place);
+
+       return g_test_run();
+}
+
+static bool __support(context_trigger_event_e ev)
+{
+       err = context_trigger_rule_event_is_supported(ev, &bool_val);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       if (!bool_val) {
+               g_print(YELLOW("Not Supported") "\n");
+               return false;
+       }
+
+       return true;
+}
+
+bool _trigger_event_battery()
+{
+       if (!__support(CONTEXT_TRIGGER_EVENT_BATTERY)) return false;
+
+       err = ctx::request_handler::subscribe(CT_EVENT_BATTERY, NULL, &req_id, NULL);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       g_print("\n> Change the battery lavel (or charging status) %d times.\n", CALLBACK_COUNT);
+       start_mainloop();
+
+       ctx::request_handler::unsubscribe(CT_EVENT_BATTERY, req_id);
+
+       ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_LEVEL);
+       ASSERT_CONTAIN_INT(json_val, CONTEXT_TRIGGER_IS_CHARGING);
+
+       return true;
+}
+
+bool _trigger_event_charger()
+{
+       if (!__support(CONTEXT_TRIGGER_EVENT_CHARGER)) return false;
+
+       err = ctx::request_handler::subscribe(CT_EVENT_CHARGER, NULL, &req_id, NULL);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       g_print("\n> Change the charger connection state %d times.\n", CALLBACK_COUNT);
+       start_mainloop();
+
+       ctx::request_handler::unsubscribe(CT_EVENT_CHARGER, req_id);
+
+       ASSERT_CONTAIN_INT(json_val, CONTEXT_TRIGGER_IS_CONNECTED);
+
+       return true;
+}
+
+bool _trigger_event_gps()
+{
+       if (!__support(CONTEXT_TRIGGER_EVENT_GPS)) return false;
+
+       err = ctx::request_handler::subscribe(CT_EVENT_GPS, NULL, &req_id, NULL);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       g_print("\n> Change the GPS status %d times.\n", CALLBACK_COUNT);
+       start_mainloop();
+
+       ctx::request_handler::unsubscribe(CT_EVENT_GPS, req_id);
+
+       ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_STATE);
+
+       return true;
+}
+
+bool _trigger_event_headphone()
+{
+       if (!__support(CONTEXT_TRIGGER_EVENT_HEADPHONE)) return false;
+
+       err = ctx::request_handler::subscribe(CT_EVENT_HEADPHONE, NULL, &req_id, NULL);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       g_print("\n> Connect/disconnect a headphone %d times.\n", CALLBACK_COUNT);
+       start_mainloop();
+
+       ctx::request_handler::unsubscribe(CT_EVENT_HEADPHONE, req_id);
+
+       ASSERT_CONTAIN_INT(json_val, CONTEXT_TRIGGER_IS_CONNECTED);
+       if (int_val == CONTEXT_TRIGGER_FALSE) return true;
+
+       ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_TYPE);
+
+       return true;
+}
+
+bool _trigger_event_usb()
+{
+       if (!__support(CONTEXT_TRIGGER_EVENT_USB)) return false;
+
+       err = ctx::request_handler::subscribe(CT_EVENT_USB, NULL, &req_id, NULL);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       g_print("\n> Connect/disconnect USB %d times.\n", CALLBACK_COUNT);
+       start_mainloop();
+
+       ctx::request_handler::unsubscribe(CT_EVENT_USB, req_id);
+
+       ASSERT_CONTAIN_INT(json_val, CONTEXT_TRIGGER_IS_CONNECTED);
+
+       return true;
+}
+
+bool _trigger_event_wifi()
+{
+       if (!__support(CONTEXT_TRIGGER_EVENT_WIFI)) return false;
+
+       err = ctx::request_handler::subscribe(CT_EVENT_WIFI, NULL, &req_id, NULL);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       g_print("\n> Change the WiFi state %d times.\n", CALLBACK_COUNT);
+       start_mainloop();
+
+       ctx::request_handler::unsubscribe(CT_EVENT_WIFI, req_id);
+
+       ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_STATE);
+
+       if (str_val == CONTEXT_TRIGGER_CONNECTED)
+               ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_BSSID);
+
+       return true;
+}
+
+bool _trigger_event_psmode()
+{
+       if (!__support(CONTEXT_TRIGGER_EVENT_POWER_SAVING_MODE)) return false;
+
+       err = ctx::request_handler::subscribe(CT_EVENT_POWER_SAVING_MODE, NULL, &req_id, NULL);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       g_print("\n> Turn on/off the power-saving mode %d times.\n", CALLBACK_COUNT);
+       start_mainloop();
+
+       ctx::request_handler::unsubscribe(CT_EVENT_POWER_SAVING_MODE, req_id);
+
+       ASSERT_CONTAIN_INT(json_val, CONTEXT_TRIGGER_IS_ENABLED);
+
+       return true;
+}
+
+bool _trigger_event_call()
+{
+       if (!__support(CONTEXT_TRIGGER_EVENT_CALL)) return false;
+
+       err = ctx::request_handler::subscribe(CT_EVENT_CALL, NULL, &req_id, NULL);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       g_print("\n> Make a phone call to this device to change call state %d times.\n", CALLBACK_COUNT);
+       start_mainloop();
+
+       ctx::request_handler::unsubscribe(CT_EVENT_CALL, req_id);
+
+       ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_STATE);
+       if (str_val == CONTEXT_TRIGGER_IDLE) return true;
+
+       ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_MEDIUM);
+       ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_ADDRESS);
+
+       return true;
+}
+
+bool _trigger_event_email()
+{
+       if (!__support(CONTEXT_TRIGGER_EVENT_EMAIL)) return false;
+
+       err = ctx::request_handler::subscribe(CT_EVENT_EMAIL, NULL, &req_id, NULL);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       g_print("\n> Send/receive emails %d times.\n", CALLBACK_COUNT);
+       start_mainloop();
+
+       ctx::request_handler::unsubscribe(CT_EVENT_EMAIL, req_id);
+
+       ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_EVENT);
+
+       return true;
+}
+
+bool _trigger_event_message()
+{
+       if (!__support(CONTEXT_TRIGGER_EVENT_MESSAGE)) return false;
+
+       err = ctx::request_handler::subscribe(CT_EVENT_MESSAGE, NULL, &req_id, NULL);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       g_print("\n> Receive SMS/MMS %d times.\n", CALLBACK_COUNT);
+       start_mainloop();
+
+       ctx::request_handler::unsubscribe(CT_EVENT_MESSAGE, req_id);
+
+       ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_EVENT);
+       ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_TYPE);
+
+       return true;
+}
+
+bool _trigger_event_activity()
+{
+       if (!__support(CONTEXT_TRIGGER_EVENT_ACTIVITY_STATIONARY)) return false;
+       if (!__support(CONTEXT_TRIGGER_EVENT_ACTIVITY_WALKING)) return false;
+       if (!__support(CONTEXT_TRIGGER_EVENT_ACTIVITY_RUNNING)) return false;
+       if (!__support(CONTEXT_TRIGGER_EVENT_ACTIVITY_IN_VEHICLE)) return false;
+
+       int req_ids[4];
+
+       err = ctx::request_handler::subscribe(CT_EVENT_ACTIVITY_STATIONARY, NULL, &req_ids[0], NULL);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       err = ctx::request_handler::subscribe(CT_EVENT_ACTIVITY_WALKING, NULL, &req_ids[1], NULL);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       err = ctx::request_handler::subscribe(CT_EVENT_ACTIVITY_RUNNING, NULL, &req_ids[2], NULL);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       err = ctx::request_handler::subscribe(CT_EVENT_ACTIVITY_IN_VEHICLE, NULL, &req_ids[3], NULL);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       g_print("\n> Mimic any type of activity %d times.\n", CALLBACK_COUNT);
+       start_mainloop();
+
+       ctx::request_handler::unsubscribe(CT_EVENT_ACTIVITY_STATIONARY, req_ids[0]);
+       ctx::request_handler::unsubscribe(CT_EVENT_ACTIVITY_WALKING, req_ids[1]);
+       ctx::request_handler::unsubscribe(CT_EVENT_ACTIVITY_RUNNING, req_ids[2]);
+       ctx::request_handler::unsubscribe(CT_EVENT_ACTIVITY_IN_VEHICLE, req_ids[3]);
+
+       ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_EVENT);
+       ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_ACCURACY);
+
+       return true;
+}
+
+bool _trigger_event_place()
+{
+       if (!__support(CONTEXT_TRIGGER_EVENT_PLACE)) return false;
+
+       ctx::json option;
+       option.set(NULL, CONTEXT_TRIGGER_PLACE_ID, 2);  /* 2 => office */
+
+       err = ctx::request_handler::subscribe(CT_EVENT_PLACE, &option, &req_id, NULL);
+       ASSERT_CMPINT(err, ==, ERR_NONE);
+
+       g_print("\n> Mimic fence in/out event to office %d times.\n", CALLBACK_COUNT);
+       start_mainloop();
+
+       ctx::request_handler::unsubscribe(CT_EVENT_PLACE, req_id);
+
+       ASSERT_CONTAIN_STR(json_val, CONTEXT_TRIGGER_EVENT);
+
+       return true;
+}
diff --git a/testsuite/src/trigger/event.h b/testsuite/src/trigger/event.h
new file mode 100644 (file)
index 0000000..f1a758c
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2015 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_TEST_TRIGGER_EVENT_H__
+#define __CONTEXT_TEST_TRIGGER_EVENT_H__
+
+int test_trigger_event(int *argc, char ***argv);
+
+bool _trigger_event_battery();
+bool _trigger_event_charger();
+bool _trigger_event_gps();
+bool _trigger_event_headphone();
+bool _trigger_event_usb();
+bool _trigger_event_wifi();
+bool _trigger_event_psmode();
+bool _trigger_event_call();
+bool _trigger_event_email();
+bool _trigger_event_message();
+bool _trigger_event_activity();
+bool _trigger_event_place();
+
+#endif