Ported ECalClient tests to use ETestServerFixture framework
[platform/upstream/evolution-data-server.git] / tests / libecal / client / test-client-add-timezone.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 "client-test-utils.h"
8 #include "e-test-server-utils.h"
9
10 #define TZID_NEW "XYZ"
11 #define TZNAME_NEW "Ex Wye Zee"
12
13 static ETestServerClosure cal_closure =
14         { E_TEST_SERVER_CALENDAR, NULL, E_CAL_CLIENT_SOURCE_TYPE_EVENTS };
15
16 static void
17 test_add_timezone_sync (ETestServerFixture *fixture,
18                         gconstpointer       user_data)
19 {
20         ECalClient *cal_client;
21         icalproperty *property;
22         icalcomponent *component;
23         icaltimezone *zone;
24         icaltimezone *zone2 = NULL;
25         GError *error = NULL;
26
27         /* Build up new timezone */
28         component = icalcomponent_new_vtimezone ();
29         property = icalproperty_new_tzid (TZID_NEW);
30         icalcomponent_add_property (component, property);
31         property = icalproperty_new_tzname (TZNAME_NEW);
32         icalcomponent_add_property (component, property);
33         zone = icaltimezone_new ();
34         icaltimezone_set_component (zone, component);
35
36         cal_client = E_TEST_SERVER_UTILS_SERVICE (fixture, ECalClient);
37
38         if (!e_cal_client_add_timezone_sync (cal_client, zone, NULL, &error))
39                 g_error ("add timezone sync: %s", error->message);
40
41         if (!e_cal_client_get_timezone_sync (cal_client, TZID_NEW, &zone2, NULL, &error))
42                 g_error ("get timezone sync: %s", error->message);
43
44         if (!zone2)
45                 g_error ("Failure: get timezone returned NULL");
46
47         g_assert_cmpstr (icaltimezone_get_tzid (zone), ==, icaltimezone_get_tzid (zone2));
48         g_assert_cmpstr (icaltimezone_get_tznames (zone), ==, icaltimezone_get_tznames (zone2));
49
50         icaltimezone_free (zone, TRUE);
51 }
52
53 typedef struct {
54         icaltimezone *zone;
55         GMainLoop *loop;
56 } AsyncData;
57
58 static void
59 async_read_result_ready (GObject *source_object,
60                          GAsyncResult *result,
61                          gpointer user_data)
62 {
63         ECalClient *cal_client;
64         GError *error = NULL;
65         AsyncData *data = (AsyncData *)user_data;
66         icaltimezone *zone1 = data->zone, *zone2 = NULL;
67
68         cal_client = E_CAL_CLIENT (source_object);
69
70         if (!e_cal_client_get_timezone_finish (cal_client, result, &zone2, &error))
71                 g_error ("get timezone finish: %s", error->message);
72
73         if (!zone2)
74                 g_error ("Failure: get timezone returned NULL");
75
76         g_assert_cmpstr (icaltimezone_get_tzid (zone1), ==, icaltimezone_get_tzid (zone2));
77         g_assert_cmpstr (icaltimezone_get_tznames (zone1), ==, icaltimezone_get_tznames (zone2));
78
79         g_main_loop_quit (data->loop);
80 }
81
82 static void
83 async_write_result_ready (GObject *source_object,
84                           GAsyncResult *result,
85                           gpointer user_data)
86 {
87         ECalClient *cal_client;
88         GError *error = NULL;
89
90         g_return_if_fail (user_data != NULL);
91         cal_client = E_CAL_CLIENT (source_object);
92
93         if (!e_cal_client_add_timezone_finish (cal_client, result, &error))
94                 g_error ("add timezone finish: %s", error->message);
95
96         e_cal_client_get_timezone (cal_client, TZID_NEW, NULL, async_read_result_ready, user_data);
97 }
98
99 static void
100 test_add_timezone_async (ETestServerFixture *fixture,
101                          gconstpointer       user_data)
102 {
103         ECalClient *cal_client;
104         icalproperty *property;
105         icalcomponent *component;
106         icaltimezone *zone;
107         AsyncData data;
108
109         /* Build up new timezone */
110         component = icalcomponent_new_vtimezone ();
111         property = icalproperty_new_tzid (TZID_NEW);
112         icalcomponent_add_property (component, property);
113         property = icalproperty_new_tzname (TZNAME_NEW);
114         icalcomponent_add_property (component, property);
115         zone = icaltimezone_new ();
116         icaltimezone_set_component (zone, component);
117
118         cal_client = E_TEST_SERVER_UTILS_SERVICE (fixture, ECalClient);
119
120         data.zone = zone;
121         data.loop = fixture->loop;
122         e_cal_client_add_timezone (cal_client, zone, NULL, async_write_result_ready, &data);
123         g_main_loop_run (fixture->loop);
124
125         icaltimezone_free (zone, TRUE);
126 }
127
128 gint
129 main (gint argc,
130       gchar **argv)
131 {
132 #if !GLIB_CHECK_VERSION (2, 35, 1)
133         g_type_init ();
134 #endif
135         g_test_init (&argc, &argv, NULL);
136
137         g_test_add ("/ECalClient/AddTimezone/Sync", ETestServerFixture, &cal_closure,
138                     e_test_server_utils_setup, test_add_timezone_sync, e_test_server_utils_teardown);
139         g_test_add ("/ECalClient/AddTimezone/Async", ETestServerFixture, &cal_closure,
140                     e_test_server_utils_setup, test_add_timezone_async, e_test_server_utils_teardown);
141
142         return e_test_server_utils_run ();
143 }