1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2011, 2012 Red Hat, Inc. (www.redhat.com)
4 * Copyright (C) 2012 Intel Corporation
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU Lesser General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
20 * Authors: Milan Crha <mcrha@redhat.com>
21 * Matthew Barnes <mbarnes@redhat.com>
22 * Tristan Van Berkom <tristanvb@openismus.com>
28 #include <libedataserver/libedataserver.h>
30 #include "client-test-utils.h"
33 report_error (const gchar *operation,
36 g_return_if_fail (operation != NULL);
38 g_printerr ("Failed to %s: %s\n", operation, (error && *error) ? (*error)->message : "Unknown error");
40 g_clear_error (error);
44 print_email (EContact *contact)
46 const gchar *file_as = e_contact_get_const (contact, E_CONTACT_FILE_AS);
47 const gchar *name_or_org = e_contact_get_const (contact, E_CONTACT_NAME_OR_ORG);
50 g_print (" Contact: %s\n", file_as);
51 g_print (" Name or org: %s\n", name_or_org);
52 g_print (" Email addresses:\n");
53 emails = e_contact_get (contact, E_CONTACT_EMAIL);
54 for (e = emails; e; e = e->next) {
55 g_print ("\t%s\n", (gchar *) e->data);
57 g_list_foreach (emails, (GFunc) g_free, NULL);
64 open_system_book (ESourceRegistry *registry,
65 gboolean only_if_exists)
68 EBookClient *book_client;
73 source = e_source_registry_ref_builtin_address_book (registry);
74 book_client = e_book_client_new (source, &error);
75 g_object_unref (source);
78 report_error ("create system addressbook", &error);
82 if (!e_client_open_sync (E_CLIENT (book_client), only_if_exists, NULL, &error)) {
83 g_object_unref (book_client);
84 report_error ("open client sync", &error);
92 main_initialize (void)
94 static gboolean initialized = FALSE;
100 e_gdbus_templates_init_main_thread ();
108 gboolean run_in_thread; /* FALSE to run in idle callback */
112 idle_cb (gpointer data)
114 struct IdleData *idle = data;
116 g_return_val_if_fail (idle != NULL, FALSE);
117 g_return_val_if_fail (idle->func != NULL, FALSE);
119 if (idle->run_in_thread) {
122 thread = g_thread_new (NULL, idle->func, idle->data);
123 g_thread_unref (thread);
125 idle->func (idle->data);
133 static GMainLoop *loop = NULL;
134 static gint main_stop_result = 0;
137 do_start (GThreadFunc func,
142 g_return_if_fail (loop == NULL);
144 loop = g_main_loop_new (NULL, FALSE);
149 g_main_loop_run (loop);
151 g_main_loop_unref (loop);
155 /* Starts new main-loop, but just before that calls 'func'.
156 * Main-loop is kept running, and this function blocks,
157 * until call of stop_main_loop (). */
159 start_main_loop (GThreadFunc func,
162 g_return_if_fail (loop == NULL);
164 do_start (func, data);
167 /* Starts new main-loop and then invokes func in a new thread.
168 * Main-loop is kept running, and this function blocks,
169 * until call of stop_main_loop (). */
171 start_in_thread_with_main_loop (GThreadFunc func,
174 struct IdleData *idle;
176 g_return_if_fail (func != NULL);
177 g_return_if_fail (loop == NULL);
181 idle = g_new0 (struct IdleData, 1);
184 idle->run_in_thread = TRUE;
186 g_idle_add (idle_cb, idle);
188 do_start (NULL, NULL);
191 /* Starts new main-loop and then invokes func in an idle callback.
192 * Main-loop is kept running, and this function blocks,
193 * until call of stop_main_loop (). */
195 start_in_idle_with_main_loop (GThreadFunc func,
198 struct IdleData *idle;
200 g_return_if_fail (func != NULL);
201 g_return_if_fail (loop == NULL);
205 idle = g_new0 (struct IdleData, 1);
208 idle->run_in_thread = FALSE;
210 g_idle_add (idle_cb, idle);
212 do_start (NULL, NULL);
215 /* Stops main-loop previously run by start_main_loop,
216 * start_in_thread_with_main_loop or start_in_idle_with_main_loop.
219 stop_main_loop (gint stop_result)
221 g_return_if_fail (loop != NULL);
223 main_stop_result = stop_result;
224 g_main_loop_quit (loop);
227 /* returns value used in stop_main_loop() */
229 get_main_loop_stop_result (void)
231 return main_stop_result;
235 foreach_configured_source (ESourceRegistry *registry,
236 void (*func) (ESource *source))
238 gpointer foreach_async_data;
239 ESource *source = NULL;
241 g_return_if_fail (func != NULL);
245 foreach_async_data = foreach_configured_source_async_start (registry, &source);
246 if (!foreach_async_data)
251 } while (foreach_configured_source_async_next (&foreach_async_data, &source));
254 struct ForeachConfiguredData {
259 foreach_configured_source_async_start (ESourceRegistry *registry,
262 struct ForeachConfiguredData *async_data;
263 const gchar *extension_name;
266 g_return_val_if_fail (source != NULL, NULL);
270 extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK;
271 list = e_source_registry_list_sources (registry, extension_name);
273 async_data = g_new0 (struct ForeachConfiguredData, 1);
274 async_data->list = list;
276 *source = async_data->list->data;
282 foreach_configured_source_async_next (gpointer *foreach_async_data,
285 struct ForeachConfiguredData *async_data;
287 g_return_val_if_fail (foreach_async_data != NULL, FALSE);
288 g_return_val_if_fail (source != NULL, FALSE);
290 async_data = *foreach_async_data;
291 g_return_val_if_fail (async_data != NULL, FALSE);
293 if (async_data->list) {
294 g_object_unref (async_data->list->data);
295 async_data->list = async_data->list->next;
297 if (async_data->list) {
298 *source = async_data->list->data;
304 *foreach_async_data = NULL;
312 ESourceRegistry *registry;
319 quit_idle (CreateBookData *data)
321 g_main_loop_quit (data->loop);
326 create_book_idle (CreateBookData *data)
328 GError *error = NULL;
330 data->source = e_source_registry_ref_source (data->registry, data->uid);
332 g_error ("Unable to fetch newly created source uid '%s' from the registry", data->uid);
334 data->book = e_book_client_new (data->source, &error);
336 g_error ("Unable to create the book: %s", error->message);
338 g_idle_add ((GSourceFunc) quit_idle, data);
344 register_source_idle (CreateBookData *data)
346 GError *error = NULL;
347 ESourceBackend *backend;
349 data->registry = e_source_registry_new_sync (NULL, &error);
351 g_error ("Unable to create the registry: %s", error->message);
353 data->scratch = e_source_new_with_uid (data->uid, NULL, &error);
355 g_error ("Failed to create source with uid '%s': %s", data->uid, error->message);
357 backend = e_source_get_extension (data->scratch, E_SOURCE_EXTENSION_ADDRESS_BOOK);
358 e_source_backend_set_backend_name (backend, "local");
360 if (!e_source_registry_commit_source_sync (data->registry, data->scratch, NULL, &error))
361 g_error ("Unable to add new source to the registry for uid %s: %s", data->uid, error->message);
363 /* XXX e_source_registry_commit_source_sync isnt really sync... or else
364 * we could call e_source_registry_ref_source() immediately
366 g_timeout_add (20, (GSourceFunc) create_book_idle, data);
372 ebook_test_utils_book_with_uid (const gchar *uid)
374 CreateBookData data = { 0, };
378 data.loop = g_main_loop_new (NULL, FALSE);
379 g_idle_add ((GSourceFunc) register_source_idle, &data);
380 g_main_loop_run (data.loop);
381 g_main_loop_unref (data.loop);
383 g_object_unref (data.scratch);
384 g_object_unref (data.source);
385 g_object_unref (data.registry);
391 new_temp_client (gchar **uri)
395 guint64 real_time = g_get_real_time ();
397 uid = g_strdup_printf ("test-book-%" G_GINT64_FORMAT, real_time);
398 book = ebook_test_utils_book_with_uid (uid);
401 *uri = g_strdup (uid);
409 new_vcard_from_test_case (const gchar *case_name)
412 gchar *case_filename;
414 GError *error = NULL;
417 case_filename = g_strdup_printf ("%s.vcf", case_name);
418 filename = g_build_filename (SRCDIR, "..", "data", "vcards", case_filename, NULL);
419 file = g_file_new_for_path (filename);
420 if (!g_file_load_contents (file, NULL, &vcard, NULL, NULL, &error)) {
422 "failed to read test contact file '%s': %s",
423 filename, error->message);
427 g_free (case_filename);
429 g_object_unref (file);
435 contacts_are_equal_shallow (EContact *a,
438 const gchar *uid_a, *uid_b;
440 /* Avoid warnings if one or more are NULL, to make this function
445 if (!E_IS_CONTACT (a) || !E_IS_CONTACT (b))
448 uid_a = e_contact_get_const (a, E_CONTACT_UID);
449 uid_b = e_contact_get_const (b, E_CONTACT_UID);
451 return g_strcmp0 (uid_a, uid_b) == 0;
455 add_contact_from_test_case_verify (EBookClient *book_client,
456 const gchar *case_name,
460 EContact *contact_orig;
461 EContact *contact_final;
463 GError *error = NULL;
465 vcard = new_vcard_from_test_case (case_name);
466 contact_orig = e_contact_new_from_vcard (vcard);
468 if (!e_book_client_add_contact_sync (book_client, contact_orig, &uid, NULL, &error)) {
469 report_error ("add contact sync", &error);
470 g_object_unref (contact_orig);
474 e_contact_set (contact_orig, E_CONTACT_UID, uid);
476 if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error)) {
477 report_error ("get contact sync", &error);
478 g_object_unref (contact_orig);
483 /* verify the contact was added "successfully" (not thorough) */
484 g_assert (contacts_are_equal_shallow (contact_orig, contact_final));
487 *contact = contact_final;
489 g_object_unref (contact_final);
490 g_object_unref (contact_orig);
497 add_contact_verify (EBookClient *book_client,
500 EContact *contact_final;
502 GError *error = NULL;
504 if (!e_book_client_add_contact_sync (book_client, contact, &uid, NULL, &error)) {
505 report_error ("add contact sync", &error);
509 e_contact_set (contact, E_CONTACT_UID, uid);
511 if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error)) {
512 report_error ("get contact sync", &error);
517 /* verify the contact was added "successfully" (not thorough) */
518 g_assert (contacts_are_equal_shallow (contact, contact_final));
520 g_object_unref (contact_final);