1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 #include <libebook/libebook.h>
5 #include "client-test-utils.h"
6 #include "e-test-server-utils.h"
8 static ETestServerClosure book_closure = { E_TEST_SERVER_ADDRESS_BOOK, NULL, 0 };
10 static const gchar *photo_data =
11 "/9j / 4AAQSkZJRgABAQEARwBHAAD//gAXQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q / 9sAQwAIBgYHB\
12 gUIBwcHCQkICgwUDQwLCwwZEhMPFB0aHx4dGhwcICQuJyAiLCMcHCg3KSwwMTQ0NB8nOT04Mjw\
13 uMzQy / 9sAQwEJCQkMCwwYDQ0YMiEcITIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyM\
14 jIyMjIyMjIyMjIyMjIyMjIy / 8AAEQgAMgAyAwEiAAIRAQMRAf / EABsAAQACAwEBAAAAAAAAAAA\
15 AAAAHCAQFBgID / 8QAMBAAAgEDAQYEBQQDAAAAAAAAAQIDAAQRBQYSEyExQQdhcYEiI0JRkRQVM\
16 qFiguH / xAAaAQADAQEBAQAAAAAAAAAAAAAABAUCBgED / 8QAIxEAAgICAQQCAwAAAAAAAAAAAAE\
17 CAwQRQRITITEUYQUiUf / aAAwDAQACEQMRAD8An + sHUtWtNKjVrmQ7754cajLvjrgfbzPIdzWdV\
18 fds9pJb3XdQkMrcFZGj + HqY0bdVV9Tz / wBia + N9vbjvkaxMb5E9N6SJB1HxLEEjJaWsUjD6QzS\
19 MPXdGB7E1zV74t63HINy1s4F7CWCTn77wrA0TY86jY3N1qsUk6wxBxBDvYjLHkoUH4j3JP / a0V\
20 3s1CvF / QM9tKpw0THeU + TLkj8VLnmzT8y0n9FujBx5bioba / rZLWx3iPZ7RzLp95GtnqRGVTez\
21 HNjruH7 / 4n + 67iqpq7Qi3uYWMMsNynfnE6sM8 / Lr6VamFi0KMepUE1Sx7XZHbI + fjxos1H0z3S\
22 lKYEjzISI2I64OKqsyu8sck2QYrmPjBvpIYg598Vauoh8VtlY7JW2isoBwpPl6hGByZTyD + o6E\
23 +h7UtlVOcPHA/+PyI1Wal6Zp7vaC / 06wnTTLtEeUDiKwzu4H8vI9AM9Tiuctkng1Nnk1G5cOoY\
24 ifB4nI / jB7VjWuoT21qPmwXUCHKlphHKvqG5N6g0 / cLi / Rg88FhbkbxlaUSu3kqpnn6kDzqGqb\
25 NdPB0XyK4 / svZr9RVntL50GePdcKEDqzhVBx7sKtPpayppNosxzKIlDHzxUFeG2zo2n2kivWhK\
26 6PpHwwoTnfk65J7kZyT9z5VYADAwKuYtfRA5zPv7tnjgUpSmREV8bq1hvbWW1uY1khlUo6MMhg\
27 eor7UoAje18FtmLe9eeQT3EXPcglkJRPbv71EWu7Dajp2o3MGmlRCkjKQ30jPUe1WlrlNW0Rpt\
28 TleNB84DnjkD0P9VlxT4Nqck9pmn8JuFp2zo0cgCWFi2e7555 / NSHXLadso2m3sU0NxlV65HM+\
29 VdTW3rgwvsUpSvAFKUoAUxSlAClKUAKUpQB//2Q==";
31 static GMainLoop *loop = NULL;
32 static gchar *micheal_jackson_uid = NULL;
33 static gchar *james_brown_uid = NULL;
35 /* Decide what to do with every "view-completed" signal */
37 ITERATION_SWAP_FACE = 0,
38 ITERATION_DELETE_JAMES,
39 ITERATION_UPDATE_MICHEAL,
40 ITERATION_DELETE_MICHEAL,
43 static gint iteration = ITERATION_SWAP_FACE;
46 print_contact (EContact *contact)
48 EContactPhoto *photo = e_contact_get (contact, E_CONTACT_PHOTO);
50 g_assert (photo != NULL);
51 g_assert (photo->type == E_CONTACT_PHOTO_TYPE_URI);
52 g_print ("Test passed with photo uri: %s\n", photo->data.uri);
56 objects_added (EBookView *book_view,
57 const GSList *contacts)
61 for (l = (GSList *) contacts; l; l = l->next) {
62 print_contact (l->data);
67 objects_modified (EBookView *book_view,
68 const GSList *contacts)
72 for (l = (GSList *) contacts; l; l = l->next) {
73 print_contact (l->data);
78 objects_removed (EBookClientView *book_view,
83 for (l = (GSList *) ids; l; l = l->next) {
84 g_print ("Removed contact: %s\n", (gchar *) l->data);
88 /* This provokes the backend to handle a cross-referenced photo
89 * between contacts, how the backend handles this is it's choice,
90 * we should test that when deleting one of the contacts, the other
91 * contact does not lose it's photo on disk as a result.
94 give_james_brown_micheal_jacksons_face (EBookClient *book)
96 EContact *micheal = NULL, *james = NULL;
97 EContactPhoto *micheal_face;
98 EContactPhoto *james_face;
101 if (!e_book_client_get_contact_sync (book, micheal_jackson_uid, &micheal, NULL, &error))
102 g_error ("Unable to get micheal jackson's contact information: %s", error->message);
104 if (!e_book_client_get_contact_sync (book, james_brown_uid, &james, NULL, &error))
105 g_error ("Unable to get james brown's contact information: %s", error->message);
110 micheal_face = e_contact_get (micheal, E_CONTACT_PHOTO);
111 g_assert (micheal_face->type == E_CONTACT_PHOTO_TYPE_URI);
113 james_face = g_new (EContactPhoto, 1);
114 james_face->type = E_CONTACT_PHOTO_TYPE_URI;
115 james_face->data.uri = g_strdup (micheal_face->data.uri);
117 e_contact_set (james, E_CONTACT_PHOTO, james_face);
119 g_print ("Giving james brown micheal jacksons face: %s\n", micheal_face->data.uri);
121 if (!e_book_client_modify_contact_sync (book, james, NULL, &error))
122 g_error ("Failed to modify contact with cross referenced photo: %s", error->message);
126 update_contact_inline (EBookClient *book,
129 EContact *contact = NULL;
130 EContactPhoto *photo;
133 GError *error = NULL;
135 if (!e_book_client_get_contact_sync (book, uid, &contact, NULL, &error))
136 g_error ("Unable to get contact: %s", error->message);
140 data = g_base64_decode (photo_data, &length);
142 photo = g_new (EContactPhoto, 1);
143 photo->type = E_CONTACT_PHOTO_TYPE_INLINED;
144 photo->data.inlined.mime_type = NULL;
145 photo->data.inlined.data = data;
146 photo->data.inlined.length = length;
149 e_contact_set (contact, E_CONTACT_PHOTO, photo);
151 if (!e_book_client_modify_contact_sync (book, contact, NULL, &error))
152 g_error ("Failed to modify contact with inline photo data: %s", error->message);
155 /* This assertion is made a couple of times in the view-complete
156 * handler, we run it to ensure that binary blobs and cross-referenced
157 * photo uris exist on disk while they should */
159 assert_uri_exists (EBookClient *book,
163 EContactPhoto *photo;
164 const gchar *filename;
165 GError *error = NULL;
167 if (!e_book_client_get_contact_sync (book, uid, &contact, NULL, &error))
168 g_error ("Unable to get contact: %s", error->message);
172 photo = e_contact_get (contact, E_CONTACT_PHOTO);
174 g_assert (photo->type == E_CONTACT_PHOTO_TYPE_URI);
176 filename = g_filename_from_uri (photo->data.uri, NULL, NULL);
179 /* The file should absolutely exist at this point */
180 g_assert (g_file_test (filename, G_FILE_TEST_EXISTS));
184 complete (EBookClientView *view,
187 EBookClient *book = e_book_client_view_ref_client (view);
188 GError *local_error = NULL;
190 g_print ("View complete, iteration %d\n", iteration);
192 /* We get another "complete" notification after removing or modifying a contact */
193 switch (iteration++) {
194 case ITERATION_SWAP_FACE:
195 give_james_brown_micheal_jacksons_face (book);
197 case ITERATION_DELETE_JAMES:
198 assert_uri_exists (book, james_brown_uid);
200 if (!e_book_client_remove_contact_by_uid_sync (book, james_brown_uid, NULL, &local_error))
201 g_error ("Error removing contact: %s", local_error->message);
203 g_free (james_brown_uid);
204 james_brown_uid = NULL;
206 case ITERATION_UPDATE_MICHEAL:
207 assert_uri_exists (book, micheal_jackson_uid);
209 update_contact_inline (book, micheal_jackson_uid);
211 case ITERATION_DELETE_MICHEAL:
212 assert_uri_exists (book, micheal_jackson_uid);
214 if (!e_book_client_remove_contact_by_uid_sync (book, micheal_jackson_uid, NULL, &local_error))
215 g_error ("Error removing contact: %s", local_error->message);
217 g_free (micheal_jackson_uid);
218 micheal_jackson_uid = NULL;
220 case ITERATION_FINISH:
221 e_book_client_view_stop (view, NULL);
222 g_object_unref (view);
223 g_main_loop_quit (loop);
226 g_assert_not_reached ();
230 g_object_unref (book);
234 setup_and_start_view (EBookClientView *view)
236 GError *error = NULL;
238 g_signal_connect (view, "objects-added", G_CALLBACK (objects_added), NULL);
239 g_signal_connect (view, "objects-removed", G_CALLBACK (objects_removed), NULL);
240 g_signal_connect (view, "objects-modified", G_CALLBACK (objects_modified), NULL);
241 g_signal_connect (view, "complete", G_CALLBACK (complete), NULL);
243 e_book_client_view_set_fields_of_interest (view, NULL, &error);
245 g_error ("set fields of interest: %s", error->message);
247 e_book_client_view_start (view, &error);
249 g_error ("start view: %s", error->message);
253 add_contact_inline (EBookClient *book)
256 EContactPhoto *photo;
260 contact = e_contact_new ();
262 data = g_base64_decode (photo_data, &length);
264 photo = g_new (EContactPhoto, 1);
265 photo->type = E_CONTACT_PHOTO_TYPE_INLINED;
266 photo->data.inlined.mime_type = NULL;
267 photo->data.inlined.data = data;
268 photo->data.inlined.length = length;
271 e_contact_set (contact, E_CONTACT_PHOTO, photo);
272 e_contact_set (contact, E_CONTACT_FULL_NAME, "Micheal Jackson");
274 if (!add_contact_verify (book, contact))
275 g_error ("Failed to add contact");
277 micheal_jackson_uid = e_contact_get (contact, E_CONTACT_UID);
281 add_contact_uri (EBookClient *book)
284 EContactPhoto *photo;
286 contact = e_contact_new ();
288 photo = g_new (EContactPhoto, 1);
289 photo->type = E_CONTACT_PHOTO_TYPE_URI;
290 photo->data.uri = g_strdup ("http://en.wikipedia.org/wiki/File:Jamesbrown4.jpg");
293 e_contact_set (contact, E_CONTACT_PHOTO, photo);
294 e_contact_set (contact, E_CONTACT_FULL_NAME, "James Brown");
296 if (!add_contact_verify (book, contact))
297 g_error ("Failed to add contact");
299 james_brown_uid = e_contact_get (contact, E_CONTACT_UID);
303 test_photo_is_uri (ETestServerFixture *fixture,
304 gconstpointer user_data)
306 EBookClient *book_client;
307 EBookClientView *view;
309 GError *error = NULL;
312 book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);
314 add_contact_inline (book_client);
315 add_contact_uri (book_client);
317 query = e_book_query_any_field_contains ("");
318 sexp = e_book_query_to_string (query);
319 e_book_query_unref (query);
320 if (!e_book_client_get_view_sync (book_client, sexp, &view, NULL, &error))
321 g_error ("get book view sync: %s", error->message);
325 setup_and_start_view (view);
327 loop = fixture->loop;
328 g_main_loop_run (loop);
335 #if !GLIB_CHECK_VERSION (2, 35, 1)
338 g_test_init (&argc, &argv, NULL);
341 "/EBookClient/PhotoIsUri", ETestServerFixture, &book_closure,
342 e_test_server_utils_setup, test_photo_is_uri, e_test_server_utils_teardown);
344 return e_test_server_utils_run ();