Coding style and whitespace cleanups.
[platform/upstream/evolution-data-server.git] / tests / libecal / test-ecal-get-query.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 #include <stdlib.h>
4 #include <libecal/e-cal.h>
5 #include <libical/ical.h>
6
7 #include "ecal-test-utils.h"
8
9 #define COMPLETE_TIMEOUT 30
10
11 #define EVENT_SUMMARY "Creation of the initial test event"
12 #define INITIAL_BEGIN_TIME     "20040109T090000Z"
13 #define INITIAL_BEGIN_TIMEZONE "UTC"
14 #define INITIAL_END_TIME       "20040109T103000"
15 #define INITIAL_END_TIMEZONE   "UTC"
16 #define FINAL_BEGIN_TIME       "20091221T090000Z"
17 #define FINAL_BEGIN_TIMEZONE   "UTC"
18
19 static void complete_timeout_cb (gpointer user_data) __attribute__ ((noreturn));
20
21 static GMainLoop *loop;
22 static guint complete_timeout_id;
23 static guint alter_cal_id;
24
25 typedef enum {
26         SUBTEST_OBJECTS_ADDED,
27         SUBTEST_OBJECTS_MODIFIED,
28         SUBTEST_OBJECTS_REMOVED,
29         SUBTEST_VIEW_DONE,
30         NUM_SUBTESTS,
31 } SubTestId;
32
33 static void
34 subtest_passed (SubTestId id)
35 {
36         static guint subtests_complete = 0;
37
38         subtests_complete |= (1 << id);
39
40         if (subtests_complete == ((1 << NUM_SUBTESTS) - 1))
41                 g_main_loop_quit (loop);
42 }
43
44 static void
45 objects_added_cb (GObject *object, GList *objects, gpointer data)
46 {
47         GList *l;
48
49         for (l = objects; l; l = l->next)
50                 test_print ("Object added %s\n", icalcomponent_get_uid (l->data));
51
52         subtest_passed (SUBTEST_OBJECTS_ADDED);
53 }
54
55 static void
56 objects_modified_cb (GObject *object, GList *objects, gpointer data)
57 {
58         GList *l;
59
60         for (l = objects; l; l = l->next)
61                 test_print ("Object modified %s\n", icalcomponent_get_uid (l->data));
62
63         subtest_passed (SUBTEST_OBJECTS_MODIFIED);
64 }
65
66 static void
67 objects_removed_cb (GObject *object, GList *objects, gpointer data)
68 {
69         GList *l;
70
71         for (l = objects; l; l = l->next) {
72                 ECalComponentId *id = l->data;
73
74                 test_print ("Object removed: uid: %s, rid: %s\n", id->uid,
75                                 id->rid);
76         }
77
78         subtest_passed (SUBTEST_OBJECTS_REMOVED);
79 }
80
81 static void
82 view_complete_cb (GObject *object, ECalendarStatus status, const gchar *error_msg, gpointer data)
83 {
84         test_print ("View complete (status: %d, error_msg:%s\n", status, error_msg ? error_msg : "NULL");
85
86         g_source_remove (complete_timeout_id);
87
88         subtest_passed (SUBTEST_VIEW_DONE);
89 }
90
91 static void
92 complete_timeout_cb (gpointer user_data)
93 {
94         g_error ("failed to complete all the pieces of the test in time");
95 }
96
97 static gboolean
98 alter_cal_cb (ECal *cal)
99 {
100         ECalComponent *e_component;
101         ECalComponent *e_component_final;
102         icalcomponent *component;
103         icalcomponent *component_final;
104         struct icaltimetype icaltime;
105         gchar *uid;
106
107         /* create a calendar object */
108         ecal_test_utils_create_component (cal, INITIAL_BEGIN_TIME,
109                         INITIAL_BEGIN_TIMEZONE, INITIAL_END_TIME,
110                         INITIAL_END_TIMEZONE, EVENT_SUMMARY, &e_component,
111                         &uid);
112         component = e_cal_component_get_icalcomponent (e_component);
113
114         component_final = ecal_test_utils_cal_get_object (cal, uid);
115         ecal_test_utils_cal_assert_objects_equal_shallow (component,
116                         component_final);
117         icalcomponent_free (component_final);
118
119         /* make and commit changes to the object */
120         icaltime = icaltime_from_string (FINAL_BEGIN_TIME);
121         icalcomponent_set_dtstart (component, icaltime);
122         ecal_test_utils_cal_component_set_icalcomponent (e_component,
123                         component);
124         ecal_test_utils_cal_modify_object (cal, component, CALOBJ_MOD_ALL);
125
126         /* verify the modification */
127         component_final = ecal_test_utils_cal_get_object (cal, uid);
128         e_component_final = e_cal_component_new ();
129         ecal_test_utils_cal_component_set_icalcomponent (e_component_final,
130                                 component_final);
131
132         ecal_test_utils_cal_assert_e_cal_components_equal (e_component,
133                         e_component_final);
134
135         /* remove the object */
136         ecal_test_utils_cal_remove_object (cal, uid);
137
138         /* Clean-up */
139         ecal_test_utils_cal_remove (cal);
140
141         g_object_unref (e_component_final);
142         g_free (uid);
143         icalcomponent_free (component);
144
145         return FALSE;
146 }
147
148 gint
149 main (gint argc, gchar **argv)
150 {
151         ECal *cal;
152         gchar *uri = NULL;
153         ECalView *view = NULL;
154
155         g_type_init ();
156
157         cal = ecal_test_utils_cal_new_temp (&uri, E_CAL_SOURCE_TYPE_EVENT);
158         ecal_test_utils_cal_open (cal, FALSE);
159
160         view = ecal_test_utils_get_query (cal, "(contains? \"any\" \"event\")");
161
162         /* monitor changes to the calendar */
163         g_signal_connect (G_OBJECT (view), "objects_added",
164                         G_CALLBACK (objects_added_cb), cal);
165         g_signal_connect (G_OBJECT (view), "objects_modified",
166                         G_CALLBACK (objects_modified_cb), cal);
167         g_signal_connect (G_OBJECT (view), "objects_removed",
168                         G_CALLBACK (objects_removed_cb), cal);
169         g_signal_connect (G_OBJECT (view), "view_complete",
170                         G_CALLBACK (view_complete_cb), cal);
171
172         e_cal_view_start (view);
173
174         loop = g_main_loop_new (NULL, TRUE);
175         alter_cal_id = g_idle_add ((GSourceFunc) alter_cal_cb, cal);
176         complete_timeout_id = g_timeout_add_seconds (COMPLETE_TIMEOUT,
177                         (GSourceFunc) complete_timeout_cb, cal);
178
179         g_main_loop_run (loop);
180
181         g_object_unref (view);
182
183         return 0;
184 }