Adapt libecal to the new ESource API.
[platform/upstream/evolution-data-server.git] / tests / libecal / ecal-test-utils.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2009 Intel Corporation
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU Lesser General Public
7  * License as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  *
19  * Author: Travis Reitter (travis.reitter@collabora.co.uk)
20  */
21
22 #include <stdlib.h>
23 #include <gio/gio.h>
24 #include <libecal/e-cal.h>
25
26 #include "ecal-test-utils.h"
27
28 void
29 test_print (const gchar *format,
30             ...)
31 {
32         va_list args;
33         const gchar *debug_string;
34         static gboolean debug_set = FALSE;
35         static gboolean debug = FALSE;
36
37         if (!debug_set) {
38                 debug_string = g_getenv ("EDS_TEST_DEBUG");
39                 if (debug_string) {
40                         debug = (g_ascii_strtoll (debug_string, NULL, 10) >= 1);
41                 }
42                 debug_set = TRUE;
43         }
44
45         if (debug) {
46                 va_start (args, format);
47                 vprintf (format, args);
48                 va_end (args);
49         }
50 }
51
52 ECal *
53 ecal_test_utils_cal_new_temp (gchar **uri,
54                               ECalSourceType type)
55 {
56         ECal *cal;
57         gchar *file_template;
58         gchar *uri_result;
59
60         file_template = g_build_filename (g_get_tmp_dir (),
61                         "ecal-test-XXXXXX/", NULL);
62         g_mkstemp (file_template);
63
64         uri_result = g_strconcat ("local:", file_template, NULL);
65         if (!uri_result) {
66                 g_error ("failed to convert %s to a 'local:' URI", file_template);
67         }
68         g_free (file_template);
69
70         /* FIXME We don't build ECals from URIs anymore. */
71         /* cal = ecal_test_utils_cal_new_from_uri (uri_result, type); */
72         cal = NULL;
73
74         if (uri)
75                 *uri = g_strdup (uri_result);
76
77         g_free (uri_result);
78
79         return cal;
80 }
81
82 void
83 ecal_test_utils_cal_open (ECal *cal,
84                           gboolean only_if_exists)
85 {
86         GError *error = NULL;
87
88         if (!e_cal_open (cal, only_if_exists, &error)) {
89                 ESource *source;
90                 const gchar *uid;
91
92                 source = e_cal_get_source (cal);
93                 uid = e_source_get_uid (source);
94
95                 g_warning ("failed to open calendar: `%s': %s", uid,
96                                 error->message);
97                 exit (1);
98         }
99 }
100
101 static void
102 open_ex_cb (ECal *cal,
103          const GError *error,
104          ECalTestClosure *closure)
105 {
106         if (FALSE) {
107         } else if (error && error->code == E_CALENDAR_STATUS_BUSY) {
108                 test_print ("calendar server is busy; waiting...");
109                 return;
110         } else if (error) {
111                 g_warning ("failed to asynchronously remove the calendar: "
112                                 "status %d (%s)", error->code, error->message);
113                 exit (1);
114         }
115
116         closure->cal = cal;
117
118         test_print ("successfully asynchronously removed the temporary "
119                         "calendar\n");
120         if (closure->cb)
121                 (*closure->cb) (closure);
122
123         g_signal_handlers_disconnect_by_func (cal, open_ex_cb, closure);
124         g_free (closure);
125 }
126
127 void
128 ecal_test_utils_cal_async_open (ECal *cal,
129                                 gboolean only_if_exists,
130                                 GSourceFunc callback,
131                                 gpointer user_data)
132 {
133         ECalTestClosure *closure;
134
135         closure = g_new0 (ECalTestClosure, 1);
136         closure->cb = callback;
137         closure->user_data = user_data;
138
139         g_signal_connect (G_OBJECT (cal), "cal_opened_ex", G_CALLBACK (open_ex_cb), closure);
140         e_cal_open_async (cal, only_if_exists);
141 }
142
143 gchar *
144 ecal_test_utils_cal_get_alarm_email_address (ECal *cal)
145 {
146         GError *error = NULL;
147         gchar *address = NULL;
148
149         if (!e_cal_get_alarm_email_address (cal, &address, &error)) {
150                 g_warning ("failed to get alarm email address; %s\n", error->message);
151                 exit (1);
152         }
153         test_print ("successfully got the alarm email address\n");
154
155         return address;
156 }
157
158 gchar *
159 ecal_test_utils_cal_get_cal_address (ECal *cal)
160 {
161         GError *error = NULL;
162         gchar *address = NULL;
163
164         if (!e_cal_get_cal_address (cal, &address, &error)) {
165                 g_warning ("failed to get calendar address; %s\n", error->message);
166                 exit (1);
167         }
168         test_print ("successfully got the calendar address\n");
169
170         return address;
171 }
172
173 gchar *
174 ecal_test_utils_cal_get_ldap_attribute (ECal *cal)
175 {
176         GError *error = NULL;
177         gchar *attr = NULL;
178
179         if (!e_cal_get_ldap_attribute (cal, &attr, &error)) {
180                 g_warning ("failed to get ldap attribute; %s\n", error->message);
181                 exit (1);
182         }
183         test_print ("successfully got the ldap attribute\n");
184
185         return attr;
186 }
187
188 static const gchar *
189 b2s (gboolean value)
190 {
191         return value ? "true" : "false";
192 }
193
194 void
195 ecal_test_utils_cal_get_capabilities (ECal *cal)
196 {
197         test_print ("calendar capabilities:\n");
198         test_print ("        One alarm only:                  %s\n"
199                  "        Organizers must attend meetings: %s\n"
200                  "        Organizers must accept meetings: %s\n"
201                  "        Master object for recurrences:   %s\n"
202                  "        Can save schedules:              %s\n"
203                  "        No alarm repeat:                 %s\n"
204                  "        No audio alarms:                 %s\n"
205                  "        No display alarms:               %s\n"
206                  "        No email alarms:                 %s\n"
207                  "        No procedure alarms:             %s\n"
208                  "        No task assignment:              %s\n"
209                  "        No 'this and future':            %s\n"
210                  "        No 'this and prior':             %s\n"
211                  "        No transparency:                 %s\n"
212                  "        Organizer not email address:     %s\n"
213                  "        Remove alarms:                   %s\n"
214                  "        Create messages:                 %s\n"
215                  "        No conv. to assigned task:       %s\n"
216                  "        No conv. to recurring:           %s\n"
217                  "        No general options:              %s\n"
218                  "        Requires send options:           %s\n"
219                  "        Delegate supported:              %s\n"
220                  "        No organizer required:           %s\n"
221                  "        Delegate to many:                %s\n"
222                  "        Has unaccepted meeting:          %s\n"
223                  ,
224                  b2s (e_cal_get_one_alarm_only (cal)),
225                  b2s (e_cal_get_organizer_must_attend (cal)),
226                  b2s (e_cal_get_organizer_must_accept (cal)),
227                  b2s (e_cal_get_recurrences_no_master (cal)),
228                  b2s (e_cal_get_save_schedules (cal)),
229                  b2s (e_cal_get_static_capability (cal,
230                                  CAL_STATIC_CAPABILITY_NO_ALARM_REPEAT)),
231                  b2s (e_cal_get_static_capability (cal,
232                                  CAL_STATIC_CAPABILITY_NO_AUDIO_ALARMS)),
233                  b2s (e_cal_get_static_capability (cal,
234                                  CAL_STATIC_CAPABILITY_NO_DISPLAY_ALARMS)),
235                  b2s (e_cal_get_static_capability (cal,
236                                  CAL_STATIC_CAPABILITY_NO_EMAIL_ALARMS)),
237                  b2s (e_cal_get_static_capability (cal,
238                                  CAL_STATIC_CAPABILITY_NO_PROCEDURE_ALARMS)),
239                  b2s (e_cal_get_static_capability (cal,
240                                  CAL_STATIC_CAPABILITY_NO_TASK_ASSIGNMENT)),
241                  b2s (e_cal_get_static_capability (cal,
242                                  CAL_STATIC_CAPABILITY_NO_THISANDFUTURE)),
243                  b2s (e_cal_get_static_capability (cal,
244                                  CAL_STATIC_CAPABILITY_NO_THISANDPRIOR)),
245                  b2s (e_cal_get_static_capability (cal,
246                                  CAL_STATIC_CAPABILITY_NO_TRANSPARENCY)),
247                  b2s (e_cal_get_static_capability (cal,
248                                  CAL_STATIC_CAPABILITY_ORGANIZER_NOT_EMAIL_ADDRESS)),
249                  b2s (e_cal_get_static_capability (cal,
250                                  CAL_STATIC_CAPABILITY_REMOVE_ALARMS)),
251                  b2s (e_cal_get_static_capability (cal,
252                                  CAL_STATIC_CAPABILITY_CREATE_MESSAGES)),
253                  b2s (e_cal_get_static_capability (cal,
254                                  CAL_STATIC_CAPABILITY_NO_CONV_TO_ASSIGN_TASK)),
255                  b2s (e_cal_get_static_capability (cal,
256                                  CAL_STATIC_CAPABILITY_NO_CONV_TO_RECUR)),
257                  b2s (e_cal_get_static_capability (cal,
258                                  CAL_STATIC_CAPABILITY_NO_GEN_OPTIONS)),
259                  b2s (e_cal_get_static_capability (cal,
260                                  CAL_STATIC_CAPABILITY_REQ_SEND_OPTIONS)),
261                  b2s (e_cal_get_static_capability (cal,
262                                  CAL_STATIC_CAPABILITY_DELEGATE_SUPPORTED)),
263                  b2s (e_cal_get_static_capability (cal,
264                                  CAL_STATIC_CAPABILITY_NO_ORGANIZER)),
265                  b2s (e_cal_get_static_capability (cal,
266                                  CAL_STATIC_CAPABILITY_DELEGATE_TO_MANY)),
267                  b2s (e_cal_get_static_capability (cal,
268                                  CAL_STATIC_CAPABILITY_HAS_UNACCEPTED_MEETING))
269                  );
270 }
271
272 void
273 ecal_test_utils_cal_assert_objects_equal_shallow (icalcomponent *a,
274                                                   icalcomponent *b)
275 {
276         const gchar *uid_a, *uid_b;
277
278         if (!icalcomponent_is_valid (a) && !icalcomponent_is_valid (b)) {
279                 g_warning ("both components invalid");
280                 return;
281         }
282
283         if (!icalcomponent_is_valid (a) || !icalcomponent_is_valid (b)) {
284                 g_error ("exactly one of the components being compared is invalid");
285         }
286
287         uid_a = icalcomponent_get_uid (a);
288         uid_b = icalcomponent_get_uid (b);
289         if (g_strcmp0 (uid_a, uid_b)) {
290                 g_error ("icomponents not equal:\n"
291                          "        uid A: '%s'\n"
292                          "        uid b: '%s'\n",
293                          uid_a, uid_b);
294         }
295 }
296
297 void
298 ecal_test_utils_cal_assert_e_cal_components_equal (ECalComponent *a,
299                                                    ECalComponent *b)
300 {
301         icalcomponent *ical_a, *ical_b;
302         ECalComponentTransparency transp_a, transp_b;
303
304         ical_a = e_cal_component_get_icalcomponent (a);
305         ical_b = e_cal_component_get_icalcomponent (b);
306         ecal_test_utils_cal_assert_objects_equal_shallow (ical_a, ical_b);
307
308         /* Dumping icalcomp into a string is not useful as the retrieved object
309          * has some generated information like timestamps. We compare
310          * member values we set during creation*/
311         g_assert (e_cal_component_event_dates_match (a, b));
312
313         e_cal_component_get_transparency (a, &transp_a);
314         e_cal_component_get_transparency (b, &transp_b);
315         g_assert (transp_a == transp_b);
316 }
317
318 icalcomponent *
319 ecal_test_utils_cal_get_object (ECal *cal,
320                                 const gchar *uid)
321 {
322         GError *error = NULL;
323         icalcomponent *component = NULL;
324
325         if (!e_cal_get_object (cal, uid, NULL, &component, &error)) {
326                 g_warning ("failed to get icalcomponent object '%s'; %s\n", uid, error->message);
327                 exit (1);
328         }
329         if (!icalcomponent_is_valid (component)) {
330                 g_warning ("retrieved icalcomponent is invalid\n");
331                 exit (1);
332         }
333         test_print ("successfully got the icalcomponent object '%s'\n", uid);
334
335         return component;
336 }
337
338 void
339 ecal_test_utils_cal_modify_object (ECal *cal,
340                                    icalcomponent *component,
341                                    CalObjModType mod_type)
342 {
343         GError *error = NULL;
344
345         if (!icalcomponent_is_valid (component)) {
346                 g_warning (G_STRLOC ": icalcomponent argument is invalid\n");
347                 exit (1);
348         }
349         if (!e_cal_modify_object (cal, component, mod_type, &error)) {
350                 g_warning ("failed to modify icalcomponent object; %s\n", error->message);
351                 exit (1);
352         }
353         test_print ("successfully modified the icalcomponent object\n");
354 }
355
356 void
357 ecal_test_utils_cal_remove_object (ECal *cal,
358                                    const gchar *uid)
359 {
360         GError *error = NULL;
361
362         if (!e_cal_remove_object (cal, uid, &error)) {
363                 g_warning ("failed to remove icalcomponent object '%s'; %s\n", uid, error->message);
364                 exit (1);
365         }
366         test_print ("successfully remoed the icalcomponent object '%s'\n", uid);
367 }
368
369 icalcomponent *
370 ecal_test_utils_cal_get_default_object (ECal *cal)
371 {
372         GError *error = NULL;
373         icalcomponent *component = NULL;
374
375         if (!e_cal_get_default_object (cal, &component, &error)) {
376                 g_warning ("failed to get default icalcomponent object; %s\n", error->message);
377                 exit (1);
378         }
379         if (!icalcomponent_is_valid (component)) {
380                 g_warning ("default icalcomponent is invalid\n");
381                 exit (1);
382         }
383         test_print ("successfully got the default icalcomponent object\n");
384
385         return component;
386 }
387
388 GList *
389 ecal_test_utils_cal_get_object_list (ECal *cal,
390                                      const gchar *query)
391 {
392         GError *error = NULL;
393         GList *objects = NULL;
394
395         if (!e_cal_get_object_list (cal, query, &objects, &error)) {
396                 g_warning ("failed to get list of icalcomponent objects for query '%s'; %s\n", query, error->message);
397                 exit (1);
398         }
399         test_print ("successfully got list of icalcomponent objects for the query '%s'\n", query);
400
401         return objects;
402 }
403
404 GList *
405 ecal_test_utils_cal_get_objects_for_uid (ECal *cal,
406                                          const gchar *uid)
407 {
408         GError *error = NULL;
409         GList *objects = NULL;
410
411         if (!e_cal_get_objects_for_uid (cal, uid, &objects, &error)) {
412                 g_warning ("failed to get icalcomponent objects for UID '%s'; %s\n", uid, error->message);
413                 exit (1);
414         }
415         test_print ("successfully got objects for the icalcomponent with UID '%s'\n", uid);
416
417         return objects;
418 }
419
420 gchar *
421 ecal_test_utils_cal_create_object (ECal *cal,
422                                    icalcomponent *component)
423 {
424         GError *error = NULL;
425         gchar *uid = NULL;
426         gchar *ical_string = NULL;
427
428         if (!icalcomponent_is_valid (component)) {
429                 g_warning ("supplied icalcomponent is invalid\n");
430                 exit (1);
431         }
432
433         if (!e_cal_create_object (cal, component, &uid, &error)) {
434                 g_warning ("failed to get create an icalcomponent object; %s\n", error->message);
435                 exit (1);
436         }
437
438         ical_string = icalcomponent_as_ical_string (component);
439         test_print ("successfully created icalcomponent object '%s'\n%s\n", uid,
440                         ical_string);
441         g_free (ical_string);
442
443         return uid;
444 }
445
446 static void
447 cal_set_mode_cb (ECal *cal,
448                  ECalendarStatus status,
449                  CalMode mode,
450                  ECalTestClosure *closure)
451 {
452         if (FALSE) {
453         } else if (status == E_CALENDAR_STATUS_BUSY) {
454                 test_print ("calendar server is busy; waiting...");
455                 return;
456         } else if (status != E_CALENDAR_STATUS_OK) {
457                 g_warning ("failed to asynchronously remove the calendar: "
458                                 "status %d", status);
459                 exit (1);
460         }
461
462         closure->mode = mode;
463
464         test_print ("successfully set the calendar mode to %d\n", mode);
465         if (closure->cb)
466                 (*closure->cb) (closure);
467
468         g_signal_handlers_disconnect_by_func (cal, cal_set_mode_cb, closure);
469         g_free (closure);
470 }
471
472 void
473 ecal_test_utils_cal_set_mode (ECal *cal,
474                               CalMode mode,
475                               GSourceFunc callback,
476                               gpointer user_data)
477 {
478         ECalTestClosure *closure;
479
480         closure = g_new0 (ECalTestClosure, 1);
481         closure->cb = callback;
482         closure->user_data = user_data;
483
484         g_signal_connect (G_OBJECT (cal), "cal_set_mode", G_CALLBACK (cal_set_mode_cb), closure);
485         e_cal_set_mode (cal, mode);
486 }
487
488 void
489 ecal_test_utils_create_component (ECal *cal,
490                                   const gchar *dtstart,
491                                   const gchar *dtstart_tzid,
492                                   const gchar *dtend,
493                                   const gchar *dtend_tzid,
494                                   const gchar *summary,
495                                   ECalComponent **comp_out,
496                                   gchar **uid_out)
497 {
498         ECalComponent *comp;
499         icalcomponent *icalcomp;
500         struct icaltimetype tt;
501         ECalComponentText text;
502         ECalComponentDateTime dt;
503         gchar *uid;
504
505         comp = e_cal_component_new ();
506         /* set fields */
507         e_cal_component_set_new_vtype (comp, E_CAL_COMPONENT_EVENT);
508         text.value = summary;
509         text.altrep = NULL;
510         e_cal_component_set_summary (comp, &text);
511         tt = icaltime_from_string (dtstart);
512         dt.value = &tt;
513         dt.tzid = dtstart_tzid;
514         e_cal_component_set_dtstart (comp, &dt);
515         tt = icaltime_from_string (dtend);
516         dt.value = &tt;
517         dt.tzid = dtend_tzid;
518         e_cal_component_set_dtend (comp, &dt);
519         e_cal_component_set_transparency (comp, E_CAL_COMPONENT_TRANSP_OPAQUE);
520
521         e_cal_component_commit_sequence (comp);
522         icalcomp = e_cal_component_get_icalcomponent (comp);
523
524         uid = ecal_test_utils_cal_create_object (cal, icalcomp);
525         e_cal_component_commit_sequence (comp);
526
527         *comp_out = comp;
528         *uid_out = uid;
529 }
530
531 void
532 ecal_test_utils_cal_component_set_icalcomponent (ECalComponent *e_component,
533                                                  icalcomponent *component)
534 {
535         if (!e_cal_component_set_icalcomponent (e_component, component)) {
536                 g_error ("Could not set icalcomponent\n");
537         }
538 }
539
540 icaltimezone *
541 ecal_test_utils_cal_get_timezone (ECal *cal,
542                                   const gchar *tzid)
543 {
544         GError *error = NULL;
545         icaltimezone *zone = NULL;
546
547         if (!e_cal_get_timezone (cal, tzid, &zone, &error)) {
548                 g_warning ("failed to get icaltimezone* for ID '%s'; %s\n", tzid, error->message);
549                 exit (1);
550         }
551         test_print ("successfully got icaltimezone* for ID '%s'\n", tzid);
552
553         return zone;
554 }
555
556 void
557 ecal_test_utils_cal_add_timezone (ECal *cal,
558                                   icaltimezone *zone)
559 {
560         GError *error = NULL;
561         const gchar *name;
562
563         name = icaltimezone_get_display_name (zone);
564
565         if (!e_cal_add_timezone (cal, zone, &error)) {
566                 g_warning ("failed to add icaltimezone '%s'; %s\n", name, error->message);
567                 exit (1);
568         }
569         test_print ("successfully added icaltimezone '%s'\n", name);
570 }
571
572 void
573 ecal_test_utils_cal_set_default_timezone (ECal *cal,
574                                           icaltimezone *zone)
575 {
576         GError *error = NULL;
577         const gchar *name;
578
579         name = icaltimezone_get_display_name (zone);
580
581         if (!e_cal_set_default_timezone (cal, zone, &error)) {
582                 g_warning ("failed to set default icaltimezone '%s'; %s\n", name, error->message);
583                 exit (1);
584         }
585         test_print ("successfully set default icaltimezone '%s'\n", name);
586 }
587
588 GList *
589 ecal_test_utils_cal_get_free_busy (ECal *cal,
590                                    GList *users,
591                                    time_t start,
592                                    time_t end)
593 {
594         GList *free_busy = NULL;
595         GList *l = NULL;
596         GError *error = NULL;
597
598         if (!e_cal_get_free_busy (cal, users, start, end, &free_busy, &error)) {
599                 g_error ("Test free/busy : Could not retrieve free busy information :  %s\n", error->message);
600         }
601         if (free_busy) {
602                 test_print ("Printing free/busy information\n");
603
604                 for (l = free_busy; l; l = l->next) {
605                         gchar *comp_string;
606                         ECalComponent *comp = E_CAL_COMPONENT (l->data);
607
608                         comp_string = e_cal_component_get_as_string (comp);
609                         test_print ("%s\n", comp_string);
610                         g_free (comp_string);
611                 }
612         } else {
613                 g_error ("got empty free/busy information");
614         }
615
616         return free_busy;
617 }
618
619 void
620 ecal_test_utils_cal_send_objects (ECal *cal,
621                                   icalcomponent *component,
622                                   GList **users,
623                                   icalcomponent **component_final)
624 {
625         GList *l = NULL;
626         GError *error = NULL;
627
628         if (!e_cal_send_objects (cal, component, users, component_final, &error)) {
629                 g_error ("sending objects: %s\n", error->message);
630         }
631
632         test_print ("successfully sent the objects to the following users:\n");
633         if (g_list_length (*users) <= 0) {
634                 test_print ("        (none)\n");
635                 return;
636         }
637         for (l = *users; l; l = l->next) {
638                 test_print ("        %s\n", (const gchar *) l->data);
639         }
640 }
641
642 void
643 ecal_test_utils_cal_receive_objects (ECal *cal,
644                                      icalcomponent *component)
645 {
646         GError *error = NULL;
647
648         if (!e_cal_receive_objects (cal, component, &error)) {
649                 g_error ("receiving objects: %s\n", error->message);
650         }
651
652         test_print ("successfully received the objects\n");
653 }
654
655 ECalView *
656 ecal_test_utils_get_query (ECal *cal,
657                            const gchar *sexp)
658 {
659         GError *error = NULL;
660         ECalView *query = NULL;
661
662         if (!e_cal_get_query (cal, sexp, &query, &error)) {
663                 g_error (G_STRLOC ": Unable to obtain calendar view: %s\n",
664                                 error->message);
665         }
666         test_print ("successfully retrieved calendar view for query '%s'", sexp);
667
668         return query;
669 }