Ported ECalClient tests to use ETestServerFixture framework
[platform/upstream/evolution-data-server.git] / tests / libecal / client / test-client-refresh.c
index 0c8b24b..3586822 100644 (file)
 #include <libical/ical.h>
 
 #include "client-test-utils.h"
+#include "e-test-server-utils.h"
 
-static gboolean
-test_sync (ECalClient *cal_client)
+static ETestServerClosure cal_closure =
+       { E_TEST_SERVER_CALENDAR, NULL, E_CAL_CLIENT_SOURCE_TYPE_EVENTS };
+
+
+static void
+setup_cal (ECalClient *cal_client)
 {
        GError *error = NULL;
+       icalcomponent *icalcomp;
+       struct icaltimetype now;
+       gchar *uid = NULL;
 
-       g_print ("Refresh supported: %s\n", e_client_check_refresh_supported (E_CLIENT (cal_client)) ? "yes" : "no");
+       now = icaltime_current_time_with_zone (icaltimezone_get_utc_timezone ());
+       icalcomp = icalcomponent_new (ICAL_VEVENT_COMPONENT);
+       icalcomponent_set_summary (icalcomp, "Test event summary");
+       icalcomponent_set_dtstart (icalcomp, now);
+       icalcomponent_set_dtend   (icalcomp, icaltime_from_timet (icaltime_as_timet (now) + 60 * 60 * 60, 0));
 
-       if (!e_client_refresh_sync (E_CLIENT (cal_client), NULL, &error)) {
-               report_error ("refresh sync", &error);
-               return FALSE;
-       }
+       if (!e_cal_client_create_object_sync (cal_client, icalcomp, &uid, NULL, &error))
+               g_error ("create object sync: %s", error->message);
 
-       return TRUE;
+       icalcomponent_free (icalcomp);
+       g_free (uid);
 }
 
-/* asynchronous callback with a main-loop running */
 static void
-async_refresh_result_ready (GObject *source_object,
-                            GAsyncResult *result,
-                            gpointer user_data)
+test_refresh_sync (ETestServerFixture *fixture,
+                  gconstpointer       user_data)
 {
-       ECalClient *cal_client;
+       ECalClient *cal;
        GError *error = NULL;
 
-       cal_client = E_CAL_CLIENT (source_object);
+       cal = E_TEST_SERVER_UTILS_SERVICE (fixture, ECalClient);
 
-       if (!e_client_refresh_finish (E_CLIENT (cal_client), result, &error)) {
-               report_error ("refresh finish", &error);
-               stop_main_loop (1);
-               return;
-       }
+       setup_cal (cal);
 
-       stop_main_loop (0);
+       g_print ("Refresh supported: %s\n", e_client_check_refresh_supported (E_CLIENT (cal)) ? "yes" : "no");
+
+       if (!e_client_refresh_sync (E_CLIENT (cal), NULL, &error))
+               g_error ("refresh sync: %s", error->message);
 }
 
-/* synchronously in idle with main-loop running */
-static gboolean
-test_sync_in_idle (gpointer user_data)
+static void
+async_refresh_result_ready (GObject *source_object,
+                            GAsyncResult *result,
+                            gpointer user_data)
 {
-       ECalClient *cal_client = user_data;
-
-       g_return_val_if_fail (cal_client != NULL, FALSE);
-       g_return_val_if_fail (E_IS_CAL_CLIENT (cal_client), FALSE);
-
-       if (!test_sync (cal_client)) {
-               stop_main_loop (1);
-               return FALSE;
-       }
+       ECalClient *cal_client;
+       GError *error = NULL;
+       GMainLoop *loop = (GMainLoop *)user_data;
 
-       g_print ("Refresh supported: %s\n", e_client_check_refresh_supported (E_CLIENT (cal_client)) ? "yes" : "no");
+       cal_client = E_CAL_CLIENT (source_object);
 
-       e_client_refresh (E_CLIENT (cal_client), NULL, async_refresh_result_ready, NULL);
+       if (!e_client_refresh_finish (E_CLIENT (cal_client), result, &error))
+               g_error ("refresh finish: %s", error->message);
 
-       return FALSE;
+       g_main_loop_quit (loop);
 }
 
-/* synchronously in a dedicated thread with main-loop running */
-static gpointer
-test_sync_in_thread (gpointer user_data)
+static void
+test_refresh_async (ETestServerFixture *fixture,
+                   gconstpointer       user_data)
 {
-       if (!test_sync (user_data)) {
-               stop_main_loop (1);
-               return NULL;
-       }
+       ECalClient *cal;
 
-       g_idle_add (test_sync_in_idle, user_data);
+       cal = E_TEST_SERVER_UTILS_SERVICE (fixture, ECalClient);
 
-       return NULL;
+       setup_cal (cal);
+
+       e_client_refresh (E_CLIENT (cal), NULL, async_refresh_result_ready, fixture->loop);
+       g_main_loop_run (fixture->loop);
 }
 
 gint
 main (gint argc,
       gchar **argv)
 {
-       ECalClient *cal_client;
-       GError *error = NULL;
-       icalcomponent *icalcomp;
-       struct icaltimetype now;
-       gchar *uid = NULL;
-
-       main_initialize ();
-
-       cal_client = new_temp_client (E_CAL_CLIENT_SOURCE_TYPE_EVENTS, NULL);
-       g_return_val_if_fail (cal_client != NULL, FALSE);
-
-       if (!e_client_open_sync (E_CLIENT (cal_client), FALSE, NULL, &error)) {
-               report_error ("client open sync", &error);
-               g_object_unref (cal_client);
-               return 1;
-       }
-
-       now = icaltime_current_time_with_zone (icaltimezone_get_utc_timezone ());
-       icalcomp = icalcomponent_new (ICAL_VEVENT_COMPONENT);
-       icalcomponent_set_summary (icalcomp, "Test event summary");
-       icalcomponent_set_dtstart (icalcomp, now);
-       icalcomponent_set_dtend   (icalcomp, icaltime_from_timet (icaltime_as_timet (now) + 60 * 60 * 60, 0));
+#if !GLIB_CHECK_VERSION (2, 35, 1)
+       g_type_init ();
+#endif
+       g_test_init (&argc, &argv, NULL);
 
-       if (!e_cal_client_create_object_sync (cal_client, icalcomp, &uid, NULL, &error)) {
-               report_error ("create object sync", &error);
-               icalcomponent_free (icalcomp);
-               g_object_unref (cal_client);
-               return 1;
-       }
-
-       icalcomponent_free (icalcomp);
-       g_free (uid);
+       g_test_add ("/ECalClient/Refresh/Sync", ETestServerFixture, &cal_closure,
+                   e_test_server_utils_setup, test_refresh_sync, e_test_server_utils_teardown);
+       g_test_add ("/ECalClient/Refresh/Async", ETestServerFixture, &cal_closure,
+                   e_test_server_utils_setup, test_refresh_async, e_test_server_utils_teardown);
 
-       /* synchronously without main-loop */
-       if (!test_sync (cal_client)) {
-               g_object_unref (cal_client);
-               return 1;
-       }
-
-       start_in_thread_with_main_loop (test_sync_in_thread, cal_client);
-
-       if (!e_client_remove_sync (E_CLIENT (cal_client), NULL, &error)) {
-               report_error ("client remove sync", &error);
-               g_object_unref (cal_client);
-               return 1;
-       }
-
-       g_object_unref (cal_client);
-
-       if (get_main_loop_stop_result () == 0)
-               g_print ("Test finished successfully.\n");
-
-       return get_main_loop_stop_result ();
+       return e_test_server_utils_run ();
 }
+