Ported ECalClient tests to use ETestServerFixture framework
[platform/upstream/evolution-data-server.git] / tests / libecal / client / test-client-modify-object.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 #include <stdlib.h>
4 #include <libecal/libecal.h>
5 #include <libical/ical.h>
6
7 #include "e-test-server-utils.h"
8
9 static ETestServerClosure cal_closure =
10         { E_TEST_SERVER_CALENDAR, NULL, E_CAL_CLIENT_SOURCE_TYPE_EVENTS };
11
12 #define EVENT_SUMMARY "Creation of new test event"
13
14 static void
15 setup_cal (ECalClient *cal_client)
16 {
17         struct icaltimetype now;
18         icalcomponent *icalcomp;
19         gchar *uid = NULL;
20         GError *error = NULL;
21
22         now = icaltime_current_time_with_zone (icaltimezone_get_utc_timezone ());
23         icalcomp = icalcomponent_new (ICAL_VEVENT_COMPONENT);
24         icalcomponent_set_summary (icalcomp, "Initial" EVENT_SUMMARY);
25         icalcomponent_set_dtstart (icalcomp, now);
26         icalcomponent_set_dtend   (icalcomp, icaltime_from_timet (icaltime_as_timet (now) + 60 * 60 * 60, 0));
27
28         if (!e_cal_client_create_object_sync (cal_client, icalcomp, &uid, NULL, &error))
29                 g_error ("create object sync: %s", error->message);
30
31         icalcomponent_free (icalcomp);
32         g_object_set_data_full (G_OBJECT (cal_client), "use-uid", uid, g_free);
33 }
34
35 static void
36 test_result (icalcomponent *icalcomp)
37 {
38         g_assert (icalcomp != NULL);
39         g_assert_cmpstr (icalcomponent_get_summary (icalcomp), ==, EVENT_SUMMARY);
40 }
41
42 static void
43 test_modify_object_sync (ETestServerFixture *fixture,
44                          gconstpointer       user_data)
45 {
46         ECalClient *cal_client;
47         GError *error = NULL;
48         icalcomponent *icalcomp = NULL;
49         const gchar *uid;
50
51         cal_client = E_TEST_SERVER_UTILS_SERVICE (fixture, ECalClient);
52
53         setup_cal (cal_client);
54         uid = g_object_get_data (G_OBJECT (cal_client), "use-uid");
55         g_assert (uid != NULL);
56
57         if (!e_cal_client_get_object_sync (cal_client, uid, NULL, &icalcomp, NULL, &error))
58                 g_error ("get object sync: %s", error->message);
59
60         icalcomponent_set_summary (icalcomp, EVENT_SUMMARY);
61
62         if (!e_cal_client_modify_object_sync (cal_client, icalcomp, CALOBJ_MOD_ALL, NULL, &error))
63                 g_error ("modify object sync: %s", error->message);
64
65         icalcomponent_free (icalcomp);
66
67         if (!e_cal_client_get_object_sync (cal_client, uid, NULL, &icalcomp, NULL, &error))
68                 g_error ("get object sync after modification: %s", error->message);
69
70         test_result (icalcomp);
71         icalcomponent_free (icalcomp);
72 }
73
74 static void
75 async_modify_result_ready (GObject *source_object,
76                            GAsyncResult *result,
77                            gpointer user_data)
78 {
79         ECalClient *cal_client;
80         GError *error = NULL;
81         icalcomponent *icalcomp = NULL;
82         const gchar *uid;
83         GMainLoop *loop = (GMainLoop *)user_data;
84
85         cal_client = E_CAL_CLIENT (source_object);
86         uid = g_object_get_data (G_OBJECT (cal_client), "use-uid");
87
88         if (!e_cal_client_modify_object_finish (cal_client, result, &error))
89                 g_error ("modify object finish: %s", error->message);
90
91         if (!e_cal_client_get_object_sync (cal_client, uid, NULL, &icalcomp, NULL, &error))
92                 g_error ("get object sync after async modification: %s", error->message);
93
94         test_result (icalcomp);
95         icalcomponent_free (icalcomp);
96
97         g_main_loop_quit (loop);
98 }
99
100 static void
101 test_modify_object_async (ETestServerFixture *fixture,
102                           gconstpointer       user_data)
103 {
104         ECalClient *cal_client;
105         GError *error = NULL;
106         icalcomponent *icalcomp = NULL;
107         const gchar *uid;
108
109         cal_client = E_TEST_SERVER_UTILS_SERVICE (fixture, ECalClient);
110
111         setup_cal (cal_client);
112         uid = g_object_get_data (G_OBJECT (cal_client), "use-uid");
113         g_assert (uid != NULL);
114
115         if (!e_cal_client_get_object_sync (cal_client, uid, NULL, &icalcomp, NULL, &error))
116                 g_error ("get object sync: %s", error->message);
117
118         icalcomponent_set_summary (icalcomp, EVENT_SUMMARY);
119
120         e_cal_client_modify_object (cal_client, icalcomp, CALOBJ_MOD_ALL, NULL, async_modify_result_ready, fixture->loop);
121         icalcomponent_free (icalcomp);
122
123         g_main_loop_run (fixture->loop);
124 }
125
126 gint
127 main (gint argc,
128       gchar **argv)
129 {
130 #if !GLIB_CHECK_VERSION (2, 35, 1)
131         g_type_init ();
132 #endif
133         g_test_init (&argc, &argv, NULL);
134
135         g_test_add ("/ECalClient/ModifyObject/Sync", ETestServerFixture, &cal_closure,
136                     e_test_server_utils_setup, test_modify_object_sync, e_test_server_utils_teardown);
137         g_test_add ("/ECalClient/ModifyObject/Async", ETestServerFixture, &cal_closure,
138                     e_test_server_utils_setup, test_modify_object_async, e_test_server_utils_teardown);
139
140         return e_test_server_utils_run ();
141 }