Ported ECalClient tests to use ETestServerFixture framework
[platform/upstream/evolution-data-server.git] / tests / libecal / client / test-client-get-attachment-uris.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 #include <stdlib.h>
4 #include <string.h>
5 #include <libecal/libecal.h>
6 #include <libical/ical.h>
7
8 #include "e-test-server-utils.h"
9
10 #define ATTACH1 "file:///tmp/file1.x"
11 #define ATTACH2 "file:///tmp/file2"
12 #define ATTACH3 "file:///tmp/dir/fileěščřžýáíé3"
13
14 static ETestServerClosure cal_closure =
15         { E_TEST_SERVER_CALENDAR, NULL, E_CAL_CLIENT_SOURCE_TYPE_EVENTS };
16
17 static void
18 add_attach (icalcomponent *icalcomp,
19             const gchar *uri)
20 {
21         gsize buf_size;
22         gchar *buf;
23         icalproperty *prop;
24         icalattach *attach;
25
26         g_return_if_fail (icalcomp != NULL);
27         g_return_if_fail (uri != NULL);
28
29         buf_size = 2 * strlen (uri);
30         buf = g_malloc0 (buf_size);
31         icalvalue_encode_ical_string (uri, buf, buf_size);
32         attach = icalattach_new_from_url (uri);
33         prop = icalproperty_new_attach (attach);
34         icalcomponent_add_property (icalcomp, prop);
35         icalattach_unref (attach);
36         g_free (buf);
37 }
38
39 static const gchar *
40 setup_cal (ECalClient *cal_client)
41 {
42         icalcomponent *icalcomp;
43         struct icaltimetype now;
44         gchar *uid = NULL;
45         GError *error = NULL;
46
47         now = icaltime_current_time_with_zone (icaltimezone_get_utc_timezone ());
48         icalcomp = icalcomponent_new (ICAL_VEVENT_COMPONENT);
49         icalcomponent_set_summary (icalcomp, "Test event summary");
50         icalcomponent_set_dtstart (icalcomp, now);
51         icalcomponent_set_dtend   (icalcomp, icaltime_from_timet (icaltime_as_timet (now) + 60 * 60 * 60, 0));
52         add_attach (icalcomp, ATTACH1);
53         add_attach (icalcomp, ATTACH2);
54         add_attach (icalcomp, ATTACH3);
55
56         if (!e_cal_client_create_object_sync (cal_client, icalcomp, &uid, NULL, &error))
57                 g_error ("create object sync: %s", error->message);
58
59         icalcomponent_free (icalcomp);
60         g_object_set_data_full (G_OBJECT (cal_client), "use-uid", uid, g_free);
61
62         return uid;
63 }
64
65 static void
66 manage_result (GSList *attachment_uris)
67 {
68         gboolean res;
69
70         g_assert (attachment_uris != NULL);
71         g_assert_cmpint (g_slist_length (attachment_uris), ==, 3);
72
73         res = g_slist_find_custom (attachment_uris, ATTACH1, g_str_equal)
74            && g_slist_find_custom (attachment_uris, ATTACH2, g_str_equal)
75            && g_slist_find_custom (attachment_uris, ATTACH3, g_str_equal);
76
77         if (!res) {
78                 GSList *au;
79
80                 g_printerr ("Failed: didn't return same three attachment uris, got instead:\n");
81                 for (au = attachment_uris; au; au = au->next)
82                         g_printerr ("\t'%s'\n", (const gchar *) au->data);
83
84                 g_assert_not_reached ();
85         }
86
87         e_client_util_free_string_slist (attachment_uris);
88 }
89
90 static void
91 test_get_attachment_uris_sync (ETestServerFixture *fixture,
92                                gconstpointer       user_data)
93 {
94         ECalClient *cal_client;
95         GError *error = NULL;
96         GSList *attachment_uris = NULL;
97         const gchar *uid;
98
99         cal_client = E_TEST_SERVER_UTILS_SERVICE (fixture, ECalClient);
100         uid = setup_cal (cal_client);
101
102         if (!e_cal_client_get_attachment_uris_sync (cal_client, uid, NULL, &attachment_uris, NULL, &error))
103                 g_error ("get attachment uris sync: %s", error->message);
104
105         manage_result (attachment_uris);
106 }
107
108 static void
109 async_attachment_uris_result_ready (GObject *source_object,
110                                     GAsyncResult *result,
111                                     gpointer user_data)
112 {
113         ECalClient *cal_client;
114         GError *error = NULL;
115         GSList *attachment_uris = NULL;
116         GMainLoop *loop = (GMainLoop *)user_data;
117
118         cal_client = E_CAL_CLIENT (source_object);
119
120         if (!e_cal_client_get_attachment_uris_finish (cal_client, result, &attachment_uris, &error))
121                 g_error ("get attachment uris finish: %s", error->message);
122
123         manage_result (attachment_uris);
124
125         g_main_loop_quit (loop);
126 }
127
128 static void
129 test_get_attachment_uris_async (ETestServerFixture *fixture,
130                                 gconstpointer       user_data)
131 {
132         ECalClient *cal_client;
133         const gchar *uid;
134
135         cal_client = E_TEST_SERVER_UTILS_SERVICE (fixture, ECalClient);
136         uid = setup_cal (cal_client);
137
138         e_cal_client_get_attachment_uris (cal_client, uid, NULL, NULL, async_attachment_uris_result_ready, fixture->loop);
139         g_main_loop_run (fixture->loop);
140 }
141
142 gint
143 main (gint argc,
144       gchar **argv)
145 {
146 #if !GLIB_CHECK_VERSION (2, 35, 1)
147         g_type_init ();
148 #endif
149         g_test_init (&argc, &argv, NULL);
150
151         g_test_add ("/ECalClient/GetAttachmentUris/Sync", ETestServerFixture, &cal_closure,
152                     e_test_server_utils_setup, test_get_attachment_uris_sync, e_test_server_utils_teardown);
153         g_test_add ("/ECalClient/GetAttachmentUris/Async", ETestServerFixture, &cal_closure,
154                     e_test_server_utils_setup, test_get_attachment_uris_async, e_test_server_utils_teardown);
155
156         return e_test_server_utils_run ();
157 }