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