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 g_return_val_if_fail (source != NULL, NULL);
75 book_client = e_book_client_new (source, &error);
76 g_object_unref (source);
79 report_error ("create system addressbook", &error);
83 if (!e_client_open_sync (E_CLIENT (book_client), only_if_exists, NULL, &error)) {
84 g_object_unref (book_client);
85 report_error ("open client sync", &error);
93 main_initialize (void)
95 static gboolean initialized = FALSE;
101 e_gdbus_templates_init_main_thread ();
109 gboolean run_in_thread; /* FALSE to run in idle callback */
113 idle_cb (gpointer data)
115 struct IdleData *idle = data;
117 g_return_val_if_fail (idle != NULL, FALSE);
118 g_return_val_if_fail (idle->func != NULL, FALSE);
120 if (idle->run_in_thread) {
123 thread = g_thread_new (NULL, idle->func, idle->data);
124 g_thread_unref (thread);
126 idle->func (idle->data);
134 static GMainLoop *loop = NULL;
135 static gint main_stop_result = 0;
138 do_start (GThreadFunc func,
143 g_return_if_fail (loop == NULL);
145 loop = g_main_loop_new (NULL, FALSE);
150 g_main_loop_run (loop);
152 g_main_loop_unref (loop);
156 /* Starts new main-loop, but just before that calls 'func'.
157 * Main-loop is kept running, and this function blocks,
158 * until call of stop_main_loop (). */
160 start_main_loop (GThreadFunc func,
163 g_return_if_fail (loop == NULL);
165 do_start (func, data);
168 /* Starts new main-loop and then invokes func in a new thread.
169 * Main-loop is kept running, and this function blocks,
170 * until call of stop_main_loop (). */
172 start_in_thread_with_main_loop (GThreadFunc func,
175 struct IdleData *idle;
177 g_return_if_fail (func != NULL);
178 g_return_if_fail (loop == NULL);
182 idle = g_new0 (struct IdleData, 1);
185 idle->run_in_thread = TRUE;
187 g_idle_add (idle_cb, idle);
189 do_start (NULL, NULL);
192 /* Starts new main-loop and then invokes func in an idle callback.
193 * Main-loop is kept running, and this function blocks,
194 * until call of stop_main_loop (). */
196 start_in_idle_with_main_loop (GThreadFunc func,
199 struct IdleData *idle;
201 g_return_if_fail (func != NULL);
202 g_return_if_fail (loop == NULL);
206 idle = g_new0 (struct IdleData, 1);
209 idle->run_in_thread = FALSE;
211 g_idle_add (idle_cb, idle);
213 do_start (NULL, NULL);
216 /* Stops main-loop previously run by start_main_loop,
217 * start_in_thread_with_main_loop or start_in_idle_with_main_loop.
220 stop_main_loop (gint stop_result)
222 g_return_if_fail (loop != NULL);
224 main_stop_result = stop_result;
225 g_main_loop_quit (loop);
228 /* returns value used in stop_main_loop() */
230 get_main_loop_stop_result (void)
232 return main_stop_result;
236 foreach_configured_source (ESourceRegistry *registry,
237 void (*func) (ESource *source))
239 gpointer foreach_async_data;
240 ESource *source = NULL;
242 g_return_if_fail (func != NULL);
246 foreach_async_data = foreach_configured_source_async_start (registry, &source);
247 if (!foreach_async_data)
252 } while (foreach_configured_source_async_next (&foreach_async_data, &source));
255 struct ForeachConfiguredData {
260 foreach_configured_source_async_start (ESourceRegistry *registry,
263 struct ForeachConfiguredData *async_data;
264 const gchar *extension_name;
267 g_return_val_if_fail (source != NULL, NULL);
271 extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK;
272 list = e_source_registry_list_sources (registry, extension_name);
274 async_data = g_new0 (struct ForeachConfiguredData, 1);
275 async_data->list = list;
277 *source = async_data->list->data;
283 foreach_configured_source_async_next (gpointer *foreach_async_data,
286 struct ForeachConfiguredData *async_data;
288 g_return_val_if_fail (foreach_async_data != NULL, FALSE);
289 g_return_val_if_fail (source != NULL, FALSE);
291 async_data = *foreach_async_data;
292 g_return_val_if_fail (async_data != NULL, FALSE);
294 if (async_data->list) {
295 g_object_unref (async_data->list->data);
296 async_data->list = async_data->list->next;
298 if (async_data->list) {
299 *source = async_data->list->data;
305 *foreach_async_data = NULL;
313 ESourceRegistry *registry;
320 quit_idle (CreateBookData *data)
322 g_main_loop_quit (data->loop);
327 create_book_idle (CreateBookData *data)
329 GError *error = NULL;
331 data->source = e_source_registry_ref_source (data->registry, data->uid);
333 g_error ("Unable to fetch newly created source uid '%s' from the registry", data->uid);
335 data->book = e_book_client_new (data->source, &error);
337 g_error ("Unable to create the book: %s", error->message);
339 g_idle_add ((GSourceFunc) quit_idle, data);
345 register_source_idle (CreateBookData *data)
347 GError *error = NULL;
348 ESourceBackend *backend;
350 data->registry = e_source_registry_new_sync (NULL, &error);
352 g_error ("Unable to create the registry: %s", error->message);
354 data->scratch = e_source_new_with_uid (data->uid, NULL, &error);
356 g_error ("Failed to create source with uid '%s': %s", data->uid, error->message);
358 backend = e_source_get_extension (data->scratch, E_SOURCE_EXTENSION_ADDRESS_BOOK);
359 e_source_backend_set_backend_name (backend, "local");
361 if (!e_source_registry_commit_source_sync (data->registry, data->scratch, NULL, &error))
362 g_error ("Unable to add new source to the registry for uid %s: %s", data->uid, error->message);
364 /* XXX e_source_registry_commit_source_sync isnt really sync... or else
365 * we could call e_source_registry_ref_source() immediately
367 g_timeout_add (20, (GSourceFunc) create_book_idle, data);
373 ebook_test_utils_book_with_uid (const gchar *uid)
375 CreateBookData data = { 0, };
379 data.loop = g_main_loop_new (NULL, FALSE);
380 g_idle_add ((GSourceFunc) register_source_idle, &data);
381 g_main_loop_run (data.loop);
382 g_main_loop_unref (data.loop);
384 g_object_unref (data.scratch);
385 g_object_unref (data.source);
386 g_object_unref (data.registry);
392 new_temp_client (gchar **uri)
396 guint64 real_time = g_get_real_time ();
398 uid = g_strdup_printf ("test-book-%" G_GINT64_FORMAT, real_time);
399 book = ebook_test_utils_book_with_uid (uid);
402 *uri = g_strdup (uid);
410 new_vcard_from_test_case (const gchar *case_name)
413 gchar *case_filename;
415 GError *error = NULL;
418 case_filename = g_strdup_printf ("%s.vcf", case_name);
419 filename = g_build_filename (SRCDIR, "..", "data", "vcards", case_filename, NULL);
420 file = g_file_new_for_path (filename);
421 if (!g_file_load_contents (file, NULL, &vcard, NULL, NULL, &error)) {
423 "failed to read test contact file '%s': %s",
424 filename, error->message);
428 g_free (case_filename);
430 g_object_unref (file);
436 contacts_are_equal_shallow (EContact *a,
439 const gchar *uid_a, *uid_b;
441 /* Avoid warnings if one or more are NULL, to make this function
446 if (!E_IS_CONTACT (a) || !E_IS_CONTACT (b))
449 uid_a = e_contact_get_const (a, E_CONTACT_UID);
450 uid_b = e_contact_get_const (b, E_CONTACT_UID);
452 return g_strcmp0 (uid_a, uid_b) == 0;
456 add_contact_from_test_case_verify (EBookClient *book_client,
457 const gchar *case_name,
461 EContact *contact_orig;
462 EContact *contact_final;
464 GError *error = NULL;
466 vcard = new_vcard_from_test_case (case_name);
467 contact_orig = e_contact_new_from_vcard (vcard);
469 if (!e_book_client_add_contact_sync (book_client, contact_orig, &uid, NULL, &error)) {
470 report_error ("add contact sync", &error);
471 g_object_unref (contact_orig);
475 e_contact_set (contact_orig, E_CONTACT_UID, uid);
477 if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error)) {
478 report_error ("get contact sync", &error);
479 g_object_unref (contact_orig);
484 /* verify the contact was added "successfully" (not thorough) */
485 g_assert (contacts_are_equal_shallow (contact_orig, contact_final));
488 *contact = contact_final;
490 g_object_unref (contact_final);
491 g_object_unref (contact_orig);
498 add_contact_verify (EBookClient *book_client,
501 EContact *contact_final;
503 GError *error = NULL;
505 if (!e_book_client_add_contact_sync (book_client, contact, &uid, NULL, &error)) {
506 report_error ("add contact sync", &error);
510 e_contact_set (contact, E_CONTACT_UID, uid);
512 if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error)) {
513 report_error ("get contact sync", &error);
518 /* verify the contact was added "successfully" (not thorough) */
519 g_assert (contacts_are_equal_shallow (contact, contact_final));
521 g_object_unref (contact_final);