--- /dev/null
+/*
+ * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * 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 <stdio.h>
+#include <glib.h>
+#include <glib-object.h>
+#include <glib-unix.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <time.h>
+#include <sys/time.h>
+#include "util.h"
+
+#include <account.h>
+#include <sync_adapter.h>
+#include <sync_manager.h>
+
+bool bRequestSent = FALSE;
+
+int jobId[3] = {0, 0, 0};
+
+int cbSet = 0;
+
+GMainLoop *global_loop = NULL;
+
+
+void print_menu()
+{
+ while (bRequestSent)
+ sleep(1);
+
+ printf(_BOLDRED"\nSyncManager Test Menu\n"_RESET
+ _GREEN"1.Set Sync Callbacks\n"_RESET
+ "2.Unset Sync Callbacks\n"
+ "3.OnDemand Sync\n"
+ _GREEN"4.Periodic Sync\n"_RESET
+ _GREEN"5.DataChange Sync\n"_RESET
+ "6.Foreach Sync\n"
+ _GREEN"7.Remove All Sync\n"_RESET
+ "q.Exit :\n");
+}
+
+
+/*
+ * Callback for receiving the result of sync start operation
+ * It is invoked in the following cases
+ * - sync_manager_on_demand_sync_job() which was called in Sync Adapter is complete
+ * - In the case of using sync_manager_add_periodic_sync_job() in Sync Adapter,
+ * it is called after some times which is set as period interval later
+ * - In the case of using sync_manager_add_data_change_sync_job() in Sync Adapter,
+ * it is called when status change is detected in DB for specific capability
+ */
+static bool
+handle_start_sync_cb(account_h account, const char *sync_job_name, const char *sync_capability, bundle *sync_job_user_data)
+{
+ if (sync_job_name) {
+ _D("Sync Job Name [%s]", sync_job_name);
+ if (!strcmp(sync_job_name, "OnDemandSyncJob")) {
+ _D("OnDemand Sync Job Name [%s]", sync_job_name);
+ printf(_BOLDWHITE"OnDemandSyncJob callback is invoked.\n"_RESET);
+ jobId[0] = 0;
+ } else {
+ _D("Periodic Sync Job Name [%s]", sync_job_name);
+ printf(_BOLDWHITE"PeriodicSyncJob callback is invoked.\n"_RESET);
+ }
+ } else {
+ _D("Data Change Sync Job Capability [%s]", sync_capability);
+ printf(_BOLDWHITE"DataChangeSyncJob callback is invoked. [SYNC_SUPPORTS_CAPABILITY_IMAGE]\n"_RESET);
+ }
+
+ printf(_BOLDWHITE"Sync start operation is complete.\n"_RESET);
+
+ return true;
+}
+
+
+/*
+ * Callback for receiving the result of sync stop operation
+ */
+static void
+handle_stop_sync_cb(account_h account, const char *sync_job_name, const char *sync_capability, bundle *sync_job_user_data)
+{
+ if (sync_job_name) {
+ _D("Sync Job Name [%s]", sync_job_name);
+ if (!strcmp(sync_job_name, "OnDemandSyncJob")) {
+ _D("OnDemand Sync Job Name [%s]", sync_job_name);
+ printf(_BOLDWHITE"OnDemandSyncJob callback is invoked.\n"_RESET);
+ jobId[0] = 0;
+ } else {
+ _D("Periodic Sync Job Name [%s]", sync_job_name);
+ printf(_BOLDWHITE"PeriodicSyncJob callback is invoked.\n"_RESET);
+ }
+ } else {
+ _D("Data Change Sync Job Capability [%s]", sync_capability);
+ printf(_BOLDWHITE"DataChangeSyncJob callback is invoked. [SYNC_SUPPORTS_CAPABILITY_IMAGE]\n"_RESET);
+ }
+
+ printf(_BOLDWHITE"Sync stop operation is complete.\n"_RESET);
+}
+
+
+bool
+foreach_sync_job_cb(account_h account, const char *sync_job_name, const char *sync_capability, int sync_job_id, bundle* sync_job_user_data, void *user_data)
+{
+ if (sync_job_name) {
+ _D("Sync Job Name [%s]", sync_job_name);
+ if (!strcmp(sync_job_name, "OnDemandSyncJob")) {
+ printf(_BOLDCYAN"OnDemandSyncJob is pended.\n"_RESET);
+ jobId[0] = sync_job_id;
+ } else {
+ printf(_BOLDCYAN"PeriodicSyncJob is registered.\n"_RESET);
+ jobId[1] = sync_job_id;
+ }
+ } else {
+ _D("Data Change Sync Job Capability [%s]", sync_capability);
+ printf(_BOLDCYAN"DataChangeSyncJob is registered. [SYNC_SUPPORTS_CAPABILITY_IMAGE]\n"_RESET);
+ jobId[2] = sync_job_id;
+ }
+
+ return true;
+}
+
+
+void stop_api_test()
+{
+ int idx, ret;
+
+ /* whether it is exist */
+ for (idx = 0; idx < 3; idx++) {
+ if (jobId[idx] != 0) {
+ ret = sync_manager_remove_sync_job(jobId[idx]);
+ if (ret != SYNC_ERROR_NONE) {
+ if (idx == 0)
+ printf(_RED"Failed to remove pended OnDemand Sync.\n"_RESET);
+ else if (idx == 1)
+ printf(_RED"Failed to remove Periodic Sync.\n"_RESET);
+ else if (idx == 2)
+ printf(_RED"Failed to remove DataChange Sync.\n"_RESET);
+ } else {
+ jobId[idx] = 0;
+ }
+ }
+ }
+
+ /* remove and unset */
+ ret = sync_adapter_unset_callbacks();
+ if (ret == SYNC_ERROR_NONE)
+ printf(_BOLDCYAN"Callbacks are unset.\n"_RESET);
+ else
+ printf(_RED"Failed to unset Callbacks.\n"_RESET);
+}
+
+
+int sync_manager_api_receive_test(void)
+{
+ int ret = SYNC_ERROR_NONE;
+ char a[10];
+ int id = 0;
+ int idx;
+
+ while (1) {
+ print_menu();
+
+ ret = read(0, a, 10);
+ switch (a[0]) {
+ case '1':
+ /* Set Sync Callbacks */
+ if (cbSet == 0) {
+ ret = sync_adapter_set_callbacks(handle_start_sync_cb, handle_stop_sync_cb);
+ if (ret == SYNC_ERROR_NONE) {
+ printf(_BOLDCYAN"Callbacks are set.\n"_RESET);
+ cbSet = 1;
+ } else {
+ printf(_RED"Failed to set Callbacks.\n"_RESET);
+ }
+ } else {
+ printf(_RED"Callbacks are already set.\n"_RESET);
+ }
+ break;
+ case '2':
+ if (cbSet == 0) {
+ printf(_RED"Callbacks are already unset.\n"_RESET);
+ break;
+ }
+ /* check remained sync jobs */
+ for (idx = 0; idx < 3; idx++) {
+ if (jobId[idx] != 0) {
+ ret = sync_manager_remove_sync_job(jobId[idx]);
+ if (ret != SYNC_ERROR_NONE) {
+ if (idx == 0)
+ printf(_RED"Failed to remove pended OnDemand Sync.\n"_RESET);
+ else if (idx == 1)
+ printf(_RED"Failed to remove Periodic Sync.\n"_RESET);
+ else if (idx == 2)
+ printf(_RED"Failed to remove DataChange Sync.\n"_RESET);
+ }
+ }
+ }
+ /* Unset Sync Callbacks */
+ ret = sync_adapter_unset_callbacks();
+ if (ret == SYNC_ERROR_NONE) {
+ printf(_BOLDCYAN"Callbacks are unset.\n"_RESET);
+ cbSet = 0;
+ } else {
+ printf(_RED"Failed to unset Callbacks.\n"_RESET);
+ }
+ break;
+ case '3':
+ if (cbSet == 0) {
+ printf(_RED"Callbacks should be set first.\n"_RESET
+ _BOLDCYAN"Please try select 1.Set Sync Callbacks\n");
+ break;
+ }
+ /* OnDemand Sync */
+ ret = sync_manager_on_demand_sync_job(NULL, "OnDemandSyncJob", SYNC_OPTION_NONE, NULL, &id);
+ if (ret == SYNC_ERROR_NONE) {
+ printf(_BOLDCYAN"Request OnDemand Sync.\n"_RESET);
+ jobId[0] = id;
+ sleep(2);
+ } else {
+ printf(_RED"Failed to request OnDemand Sync.\n"_RESET);
+ }
+ break;
+ case '4':
+ if (cbSet == 0) {
+ printf(_RED"Callbacks should be set first.\n"_RESET
+ _BOLDCYAN"Please try select 1.Set Sync Callbacks\n");
+ break;
+ }
+ /* Periodic Sync */
+ ret = sync_manager_add_periodic_sync_job(NULL, "PeriodicSyncJob", SYNC_PERIOD_INTERVAL_30MIN, SYNC_OPTION_EXPEDITED, NULL, &id);
+ if (ret == SYNC_ERROR_NONE) {
+ printf(_BOLDCYAN"Request Periodic Sync with Priority [30mins].\n"_RESET);
+ jobId[1] = id;
+ sleep(2);
+ } else {
+ printf(_RED"Failed to request Periodic Sync.\n"_RESET);
+ }
+ break;
+ case '5':
+ if (cbSet == 0) {
+ printf(_RED"Callbacks should be set first.\n"_RESET
+ _BOLDCYAN"Please try select 1.Set Sync Callbacks\n");
+ break;
+ }
+ /* DataChange Sync */
+ ret = sync_manager_add_data_change_sync_job(NULL, SYNC_SUPPORTS_CAPABILITY_IMAGE, SYNC_OPTION_EXPEDITED, NULL, &id);
+ if (ret == SYNC_ERROR_NONE) {
+ printf(_BOLDCYAN"Request DataChange Sync with Priority [image].\n"_RESET);
+ jobId[2] = id;
+ sleep(2);
+ } else {
+ printf(_RED"Failed to request DataChange Sync.\n"_RESET);
+ }
+ break;
+ case '6':
+ if (cbSet == 0) {
+ printf(_RED"Callbacks should be set first.\n"_RESET
+ _BOLDCYAN"Please try select 1.Set Sync Callbacks\n");
+ break;
+ }
+ /* Foreach Sync */
+ ret = sync_manager_foreach_sync_job(foreach_sync_job_cb, NULL);
+ if (ret == SYNC_ERROR_NONE) {
+ printf(_BOLDWHITE"Request Foreach Sync.\n"_RESET);
+ sleep(2);
+ } else {
+ printf(_RED"Failed to request Foreach Sync.\n"_RESET);
+ }
+ break;
+ case '7':
+ /* whether it is exist */
+ printf(_BOLDWHITE"Remove All Sync jobs.\n"_RESET);
+ /* Remove All Sync */
+ for (idx = 0; idx < 3; idx++) {
+ if (jobId[idx] != 0) {
+ ret = sync_manager_remove_sync_job(jobId[idx]);
+ if (ret != SYNC_ERROR_NONE) {
+ if (idx == 0)
+ printf(_RED"Failed to remove pended OnDemand Sync.\n"_RESET);
+ else if (idx == 1)
+ printf(_RED"Failed to remove Periodic Sync.\n"_RESET);
+ else if (idx == 2)
+ printf(_RED"Failed to remove DataChange Sync.\n"_RESET);
+ } else {
+ jobId[idx] = 0;
+ }
+ }
+ }
+ break;
+ case 'q':
+ stop_api_test();
+ return 0;
+ default:
+ break;
+ }
+ }
+}
+
+
+void* start_api_test()
+{
+ bRequestSent = FALSE;
+
+ cbSet = 0;
+
+ int ret = sync_manager_api_receive_test();
+ if (ret)
+ printf(_BOLDCYAN"Test Finished\n"_RESET);
+
+ printf(_BOLDCYAN"Bye\n"_RESET);
+
+ g_main_loop_quit(global_loop);
+
+ return NULL;
+}
+
+
+static gint sigterm_cb(void *data)
+{
+ stop_api_test();
+
+ printf(_BOLDCYAN"signal terminated callback\n"_RESET);
+ printf(_BOLDCYAN"Farewell\n"_RESET);
+
+ g_main_loop_quit((GMainLoop*)data);
+
+ return FALSE;
+}
+
+
+int main(int argc, char **argv)
+{
+ GMainLoop *loop;
+
+ pthread_t thd;
+
+ printf("\n");
+ printf(_YELLOW"##################################\n");
+ printf(_YELLOW"######## SyncManager Test ########\n");
+ printf(_YELLOW"##################################\n"_RESET);
+
+ /* start transaction manager */
+ if (pthread_create(&thd, NULL, start_api_test, NULL) != 0)
+ return -1;
+
+ /* mainloop of main thread */
+ global_loop = loop = g_main_loop_new(NULL, FALSE);
+
+ /* installing signal handlers */
+ g_unix_signal_add_full(G_PRIORITY_HIGH, SIGINT, sigterm_cb, loop, NULL);
+ g_unix_signal_add_full(G_PRIORITY_HIGH, SIGTERM, sigterm_cb, loop, NULL);
+
+ /* start application's main loop */
+ g_main_loop_run(loop);
+
+ /* cleanup after mainloop */
+ g_main_loop_unref(loop);
+
+ return 0;
+}
+
--- /dev/null
+/*
+ * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * 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 __TIZEN_SYNCTEST_UTIL_H__
+#define __TIZEN_SYNCTEST_UTIL_H__
+
+#include <dlog.h>
+
+#undef LOG_TAG
+#define LOG_TAG "SYNCTEST"
+
+#define _RESET "\033[0m"
+#define _BLACK "\033[30m" /* Black */
+#define _RED "\033[31m" /* Red */
+#define _GREEN "\033[32m" /* Green */
+#define _YELLOW "\033[33m" /* Yellow */
+#define _BLUE "\033[34m" /* Blue */
+#define _PURPLE "\033[35m" /* Purple */
+#define _CYAN "\033[36m" /* Cyan */
+#define _WHITE "\033[37m" /* White */
+#define _BOLDBLACK "\033[1m\033[30m" /* Bold Black */
+#define _BOLDRED "\033[1m\033[31m" /* Bold Red */
+#define _BOLDGREEN "\033[1m\033[32m" /* Bold Green */
+#define _BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
+#define _BOLDBLUE "\033[1m\033[34m" /* Bold Blue */
+#define _BOLDPURPLE "\033[1m\033[35m" /* Bold Purple */
+#define _BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
+#define _BOLDWHITE "\033[1m\033[37m" /* Bold White */
+
+#define _I(fmt, arg...) do { LOGI(fmt, ##arg); } while (0)
+#define _W(fmt, arg...) do { LOGW(fmt, ##arg); } while (0)
+#define _E(fmt, arg...) do { LOGE(fmt, ##arg); } while (0)
+#define _D(fmt, arg...) do { LOGD(fmt, ##arg); } while (0)
+#define _SE(fmt, arg...) do { LOGE(fmt, ##arg); } while (0)
+#define _SD(fmt, arg...) do { LOGD(fmt, ##arg); } while (0)
+#define _SW(fmt, arg...) do { LOGW(fmt, ##arg); } while (0)
+
+#define _free(p) do { if (p) { free(p); p = NULL; } } while (0)
+#define _FUNC_ENTER do { LOGD("start"); } while (0)
+#define _FUNC_LEAVE do { LOGD("end"); } while (0)
+
+#endif /* __TIZEN_SYNCTEST_UTIL_H__ */
+