Ported ECalClient tests to use ETestServerFixture framework
[platform/upstream/evolution-data-server.git] / tests / libecal / client / test-client-send-objects.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 #include "client-test-utils.h" /* For print_icomp() */
9
10 static ETestServerClosure cal_closure =
11         { E_TEST_SERVER_CALENDAR, NULL, E_CAL_CLIENT_SOURCE_TYPE_EVENTS };
12
13 static icalcomponent *
14 create_object (void)
15 {
16         icalcomponent *icalcomp;
17         struct icaltimetype now;
18
19         now = icaltime_current_time_with_zone (icaltimezone_get_utc_timezone ());
20         icalcomp = icalcomponent_new (ICAL_VEVENT_COMPONENT);
21         icalcomponent_set_summary (icalcomp, "To-be-sent event summary");
22         icalcomponent_set_dtstart (icalcomp, now);
23         icalcomponent_set_dtend   (icalcomp, icaltime_from_timet (icaltime_as_timet (now) + 60 * 60 * 60, 0));
24
25         return icalcomp;
26 }
27
28 static void
29 manage_result (GSList *users,
30                icalcomponent *modified_icalcomp)
31 {
32         g_print ("Wishes to send to %d users", g_slist_length (users));
33         if (users) {
34                 GSList *u;
35
36                 g_print (": ");
37
38                 for (u = users; u; u = u->next)
39                         g_print ("%s%s", u == users ? "" : ", ", (const gchar *) u->data);
40         }
41         g_print ("\n");
42
43         if (!modified_icalcomp)
44                 g_print ("No modified icalcomp, would send the same\n");
45         else
46                 print_icomp (modified_icalcomp);
47
48         e_client_util_free_string_slist (users);
49         if (modified_icalcomp)
50                 icalcomponent_free (modified_icalcomp);
51 }
52
53 static void
54 test_send_objects_sync (ETestServerFixture *fixture,
55                         gconstpointer       user_data)
56 {
57         ECalClient *cal_client;
58         GError *error = NULL;
59         icalcomponent *icalcomp, *modified_icalcomp = NULL;
60         GSList *users = NULL;
61
62         cal_client = E_TEST_SERVER_UTILS_SERVICE (fixture, ECalClient);
63
64         icalcomp = create_object ();
65         if (!e_cal_client_send_objects_sync (cal_client, icalcomp, &users, &modified_icalcomp, NULL, &error))
66                 g_error ("send objects sync: %s", error->message);
67
68         icalcomponent_free (icalcomp);
69         manage_result (users, modified_icalcomp);
70 }
71
72 static void
73 async_send_result_ready (GObject *source_object,
74                          GAsyncResult *result,
75                          gpointer user_data)
76 {
77         ECalClient *cal_client;
78         GError *error = NULL;
79         GSList *users = NULL;
80         icalcomponent *modified_icalcomp = NULL;
81         GMainLoop *loop = (GMainLoop *)user_data;
82
83         cal_client = E_CAL_CLIENT (source_object);
84
85         if (!e_cal_client_send_objects_finish (cal_client, result, &users, &modified_icalcomp, &error))
86                 g_error ("send objects finish: %s", error->message);
87
88         manage_result (users, modified_icalcomp);
89         g_main_loop_quit (loop);
90 }
91
92 static void
93 test_send_objects_async (ETestServerFixture *fixture,
94                         gconstpointer       user_data)
95 {
96         ECalClient *cal_client;
97         icalcomponent *icalcomp;
98
99         cal_client = E_TEST_SERVER_UTILS_SERVICE (fixture, ECalClient);
100
101         icalcomp = create_object ();
102         g_assert (icalcomp);
103
104         e_cal_client_send_objects (cal_client, icalcomp, NULL, async_send_result_ready, fixture->loop);
105         icalcomponent_free (icalcomp);
106
107         g_main_loop_run (fixture->loop);
108 }
109
110 gint
111 main (gint argc,
112       gchar **argv)
113 {
114 #if !GLIB_CHECK_VERSION (2, 35, 1)
115         g_type_init ();
116 #endif
117         g_test_init (&argc, &argv, NULL);
118
119         g_test_add ("/ECalClient/SendObjects/Sync", ETestServerFixture, &cal_closure,
120                     e_test_server_utils_setup, test_send_objects_sync, e_test_server_utils_teardown);
121         g_test_add ("/ECalClient/SendObjects/Async", ETestServerFixture, &cal_closure,
122                     e_test_server_utils_setup, test_send_objects_async, e_test_server_utils_teardown);
123
124         return e_test_server_utils_run ();
125 }