Add e_book_client_view_ref_client().
[platform/upstream/evolution-data-server.git] / tests / libebook / client / test-client-suppress-notifications.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 #include <stdlib.h>
4 #include <libebook/libebook.h>
5
6 #include "client-test-utils.h"
7 #include "e-test-server-utils.h"
8
9 static ETestServerClosure book_closure = { E_TEST_SERVER_ADDRESS_BOOK, NULL, 0 };
10 static ETestServerClosure direct_book_closure = { E_TEST_SERVER_DIRECT_ADDRESS_BOOK, NULL, 0 };
11
12 #define NOTIFICATION_WAIT 2000
13
14 static gboolean loading_view;
15
16 static void
17 add_contact (EBookClient *book_client)
18 {
19         g_return_if_fail (add_contact_from_test_case_verify (book_client, "name-only", NULL));
20 }
21
22 static void
23 setup_book (EBookClient *book_client)
24 {
25         if (!add_contact_from_test_case_verify (book_client, "simple-1", NULL) ||
26             !add_contact_from_test_case_verify (book_client, "simple-2", NULL))
27                 g_error ("Failed to add contacts");
28 }
29
30 static void
31 finish_test (EBookClientView *view,
32              GMainLoop *loop)
33 {
34         e_book_client_view_stop (view, NULL);
35         g_object_unref (view);
36
37         g_main_loop_quit (loop);
38 }
39
40 static void
41 objects_added (EBookClientView *view,
42                const GSList *contacts,
43                gpointer user_data)
44 {
45         const GSList *l;
46         GMainLoop *loop = (GMainLoop *) user_data;
47
48         /* We quit the mainloop and the test succeeds if we get the notification
49          * for the contact we add after loading the view completes */
50         for (l = contacts; l; l = l->next) {
51                 print_email (l->data);
52         }
53
54         if (loading_view)
55                 g_error ("Expected no contact additions while loading the view");
56         else {
57                 finish_test (view, loop);
58         }
59
60 }
61
62 static void
63 objects_removed (EBookClientView *view,
64                  const GSList *ids)
65 {
66         const GSList *l;
67
68         if (loading_view)
69                 g_error ("Expected no contact removals while loading the view");
70
71         for (l = ids; l; l = l->next) {
72                 printf ("   Removed contact: %s\n", (gchar *) l->data);
73         }
74 }
75
76 static void
77 complete (EBookClientView *view,
78           const GError *error)
79 {
80         EBookClient *client;
81
82         client = e_book_client_view_ref_client (view);
83
84         /* Now add a contact and assert that we received notification */
85         loading_view = FALSE;
86         add_contact (client);
87
88         g_object_unref (client);
89 }
90
91 static void
92 setup_and_start_view (EBookClientView *view,
93                       GMainLoop *loop)
94 {
95         GError *error = NULL;
96
97         g_signal_connect (view, "objects-added", G_CALLBACK (objects_added), loop);
98         g_signal_connect (view, "objects-removed", G_CALLBACK (objects_removed), loop);
99         g_signal_connect (view, "complete", G_CALLBACK (complete), loop);
100
101         e_book_client_view_set_fields_of_interest (view, NULL, &error);
102         if (error)
103                 g_error ("set fields of interest: %s", error->message);
104
105         /* Set flags to 0, i.e. unflag E_BOOK_VIEW_NOTIFY_INITIAL */
106         e_book_client_view_set_flags (view, 0, &error);
107         if (error)
108                 g_error ("set view flags: %s", error->message);
109         loading_view = TRUE;
110
111         e_book_client_view_start (view, &error);
112         if (error)
113                 g_error ("start view: %s", error->message);
114
115 }
116
117 static void
118 get_view_cb (GObject *source_object,
119              GAsyncResult *result,
120              gpointer user_data)
121 {
122         GMainLoop *loop = (GMainLoop *) user_data;
123         EBookClientView *view;
124         GError *error = NULL;
125
126         if (!e_book_client_get_view_finish (E_BOOK_CLIENT (source_object), result, &view, &error)) {
127                 g_error ("get view finish: %s", error->message);
128         }
129
130         setup_and_start_view (view, loop);
131 }
132
133 static void
134 test_suppress_notifications_sync (ETestServerFixture *fixture,
135                                   gconstpointer user_data)
136 {
137         EBookClient *book_client;
138         EBookQuery *query;
139         EBookClientView *view;
140         gchar *sexp;
141         GError *error = NULL;
142
143         book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);
144
145         setup_book (book_client);
146
147         query = e_book_query_any_field_contains ("");
148         sexp = e_book_query_to_string (query);
149         e_book_query_unref (query);
150         if (!e_book_client_get_view_sync (book_client, sexp, &view, NULL, &error)) {
151                 g_error ("get book view sync: %s", error->message);
152                 g_free (sexp);
153                 g_object_unref (book_client);
154         }
155
156         g_free (sexp);
157
158         setup_and_start_view (view, fixture->loop);
159         g_main_loop_run (fixture->loop);
160 }
161
162 static void
163 test_suppress_notifications_async (ETestServerFixture *fixture,
164                                    gconstpointer user_data)
165 {
166         EBookClient *book_client;
167         EBookQuery *query;
168         gchar *sexp;
169
170         book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);
171
172         setup_book (book_client);
173         query = e_book_query_any_field_contains ("");
174         sexp = e_book_query_to_string (query);
175         e_book_query_unref (query);
176
177         e_book_client_get_view (book_client, sexp, NULL, get_view_cb, fixture->loop);
178
179         g_free (sexp);
180         g_main_loop_run (fixture->loop);
181 }
182
183 gint
184 main (gint argc,
185       gchar **argv)
186 {
187 #if !GLIB_CHECK_VERSION (2, 35, 1)
188         g_type_init ();
189 #endif
190         g_test_init (&argc, &argv, NULL);
191
192         g_test_add (
193                 "/EBookClient/SuppressNotifications/Sync", ETestServerFixture, &book_closure,
194                 e_test_server_utils_setup, test_suppress_notifications_sync, e_test_server_utils_teardown);
195         g_test_add (
196                 "/EBookClient/SuppressNotifications/Async", ETestServerFixture, &book_closure,
197                 e_test_server_utils_setup, test_suppress_notifications_async, e_test_server_utils_teardown);
198         g_test_add (
199                 "/EBookClient/DirectAccess/SuppressNotifications/Sync", ETestServerFixture, &direct_book_closure,
200                 e_test_server_utils_setup, test_suppress_notifications_sync, e_test_server_utils_teardown);
201         g_test_add (
202                 "/EBookClient/DirectAccess/SuppressNotifications/Async", ETestServerFixture, &direct_book_closure,
203                 e_test_server_utils_setup, test_suppress_notifications_async, e_test_server_utils_teardown);
204
205         return e_test_server_utils_run ();
206 }