1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
6 #include <libedataserver/libedataserver.h>
8 #include "client-test-utils.h"
11 report_error (const gchar *operation,
14 g_return_if_fail (operation != NULL);
16 g_printerr ("Failed to %s: %s\n", operation, (error && *error) ? (*error)->message : "Unknown error");
18 g_clear_error (error);
22 print_email (EContact *contact)
24 const gchar *file_as = e_contact_get_const (contact, E_CONTACT_FILE_AS);
25 const gchar *name_or_org = e_contact_get_const (contact, E_CONTACT_NAME_OR_ORG);
28 g_print (" Contact: %s\n", file_as);
29 g_print (" Name or org: %s\n", name_or_org);
30 g_print (" Email addresses:\n");
31 emails = e_contact_get (contact, E_CONTACT_EMAIL);
32 for (e = emails; e; e = e->next) {
33 g_print ("\t%s\n", (gchar *) e->data);
35 g_list_foreach (emails, (GFunc) g_free, NULL);
42 open_system_book (ESourceRegistry *registry,
43 gboolean only_if_exists)
46 EBookClient *book_client;
51 source = e_source_registry_ref_builtin_address_book (registry);
52 book_client = e_book_client_new (source, &error);
53 g_object_unref (source);
56 report_error ("create system addressbook", &error);
60 if (!e_client_open_sync (E_CLIENT (book_client), only_if_exists, NULL, &error)) {
61 g_object_unref (book_client);
62 report_error ("open client sync", &error);
70 main_initialize (void)
72 static gboolean initialized = FALSE;
78 e_gdbus_templates_init_main_thread ();
86 gboolean run_in_thread; /* FALSE to run in idle callback */
90 idle_cb (gpointer data)
92 struct IdleData *idle = data;
94 g_return_val_if_fail (idle != NULL, FALSE);
95 g_return_val_if_fail (idle->func != NULL, FALSE);
97 if (idle->run_in_thread) {
100 g_thread_create (idle->func, idle->data, FALSE, &error);
103 report_error ("create thread", &error);
107 idle->func (idle->data);
115 static GMainLoop *loop = NULL;
116 static gint main_stop_result = 0;
119 do_start (GThreadFunc func,
124 g_return_if_fail (loop == NULL);
126 loop = g_main_loop_new (NULL, FALSE);
131 g_main_loop_run (loop);
133 g_main_loop_unref (loop);
137 /* Starts new main-loop, but just before that calls 'func'.
138 * Main-loop is kept running, and this function blocks,
139 * until call of stop_main_loop (). */
141 start_main_loop (GThreadFunc func,
144 g_return_if_fail (loop == NULL);
146 do_start (func, data);
149 /* Starts new main-loop and then invokes func in a new thread.
150 * Main-loop is kept running, and this function blocks,
151 * until call of stop_main_loop (). */
153 start_in_thread_with_main_loop (GThreadFunc func,
156 struct IdleData *idle;
158 g_return_if_fail (func != NULL);
159 g_return_if_fail (loop == NULL);
163 idle = g_new0 (struct IdleData, 1);
166 idle->run_in_thread = TRUE;
168 g_idle_add (idle_cb, idle);
170 do_start (NULL, NULL);
173 /* Starts new main-loop and then invokes func in an idle callback.
174 * Main-loop is kept running, and this function blocks,
175 * until call of stop_main_loop (). */
177 start_in_idle_with_main_loop (GThreadFunc func,
180 struct IdleData *idle;
182 g_return_if_fail (func != NULL);
183 g_return_if_fail (loop == NULL);
187 idle = g_new0 (struct IdleData, 1);
190 idle->run_in_thread = FALSE;
192 g_idle_add (idle_cb, idle);
194 do_start (NULL, NULL);
197 /* Stops main-loop previously run by start_main_loop,
198 * start_in_thread_with_main_loop or start_in_idle_with_main_loop.
201 stop_main_loop (gint stop_result)
203 g_return_if_fail (loop != NULL);
205 main_stop_result = stop_result;
206 g_main_loop_quit (loop);
209 /* returns value used in stop_main_loop() */
211 get_main_loop_stop_result (void)
213 return main_stop_result;
217 foreach_configured_source (ESourceRegistry *registry,
218 void (*func) (ESource *source))
220 gpointer foreach_async_data;
221 ESource *source = NULL;
223 g_return_if_fail (func != NULL);
227 foreach_async_data = foreach_configured_source_async_start (registry, &source);
228 if (!foreach_async_data)
233 } while (foreach_configured_source_async_next (&foreach_async_data, &source));
236 struct ForeachConfiguredData {
241 foreach_configured_source_async_start (ESourceRegistry *registry,
244 struct ForeachConfiguredData *async_data;
245 const gchar *extension_name;
248 g_return_val_if_fail (source != NULL, NULL);
252 extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK;
253 list = e_source_registry_list_sources (registry, extension_name);
255 async_data = g_new0 (struct ForeachConfiguredData, 1);
256 async_data->list = list;
258 *source = async_data->list->data;
264 foreach_configured_source_async_next (gpointer *foreach_async_data,
267 struct ForeachConfiguredData *async_data;
269 g_return_val_if_fail (foreach_async_data != NULL, FALSE);
270 g_return_val_if_fail (source != NULL, FALSE);
272 async_data = *foreach_async_data;
273 g_return_val_if_fail (async_data != NULL, FALSE);
275 if (async_data->list) {
276 g_object_unref (async_data->list->data);
277 async_data->list = async_data->list->next;
279 if (async_data->list) {
280 *source = async_data->list->data;
286 *foreach_async_data = NULL;
294 ESourceRegistry *registry;
301 quit_idle (CreateBookData *data)
303 g_main_loop_quit (data->loop);
308 create_book_idle (CreateBookData *data)
310 GError *error = NULL;
312 data->source = e_source_registry_ref_source (data->registry, data->uid);
314 g_error ("Unable to fetch newly created source uid '%s' from the registry", data->uid);
316 data->book = e_book_client_new (data->source, &error);
318 g_error ("Unable to create the book: %s", error->message);
320 g_idle_add ((GSourceFunc) quit_idle, data);
326 register_source_idle (CreateBookData *data)
328 GError *error = NULL;
329 ESourceBackend *backend;
331 data->registry = e_source_registry_new_sync (NULL, &error);
333 g_error ("Unable to create the registry: %s", error->message);
335 data->scratch = e_source_new_with_uid (data->uid, NULL, &error);
337 g_error ("Failed to create source with uid '%s': %s", data->uid, error->message);
339 backend = e_source_get_extension (data->scratch, E_SOURCE_EXTENSION_ADDRESS_BOOK);
340 e_source_backend_set_backend_name (backend, "local");
342 if (!e_source_registry_commit_source_sync (data->registry, data->scratch, NULL, &error))
343 g_error ("Unable to add new source to the registry for uid %s: %s", data->uid, error->message);
345 /* XXX e_source_registry_commit_source_sync isnt really sync... or else
346 * we could call e_source_registry_ref_source() immediately
348 g_timeout_add (20, (GSourceFunc) create_book_idle, data);
354 ebook_test_utils_book_with_uid (const gchar *uid)
356 CreateBookData data = { 0, };
360 data.loop = g_main_loop_new (NULL, FALSE);
361 g_idle_add ((GSourceFunc) register_source_idle, &data);
362 g_main_loop_run (data.loop);
363 g_main_loop_unref (data.loop);
365 g_object_unref (data.scratch);
366 g_object_unref (data.source);
367 g_object_unref (data.registry);
373 new_temp_client (gchar **uri)
377 guint64 real_time = g_get_real_time ();
379 uid = g_strdup_printf ("test-book-%" G_GINT64_FORMAT, real_time);
380 book = ebook_test_utils_book_with_uid (uid);
383 *uri = g_strdup (uid);
391 new_vcard_from_test_case (const gchar *case_name)
394 gchar *case_filename;
396 GError *error = NULL;
399 case_filename = g_strdup_printf ("%s.vcf", case_name);
400 filename = g_build_filename (SRCDIR, "..", "data", "vcards", case_filename, NULL);
401 file = g_file_new_for_path (filename);
402 if (!g_file_load_contents (file, NULL, &vcard, NULL, NULL, &error)) {
404 "failed to read test contact file '%s': %s",
405 filename, error->message);
409 g_free (case_filename);
411 g_object_unref (file);
417 contacts_are_equal_shallow (EContact *a,
420 const gchar *uid_a, *uid_b;
422 /* Avoid warnings if one or more are NULL, to make this function
427 if (!E_IS_CONTACT (a) || !E_IS_CONTACT (b))
430 uid_a = e_contact_get_const (a, E_CONTACT_UID);
431 uid_b = e_contact_get_const (b, E_CONTACT_UID);
433 return g_strcmp0 (uid_a, uid_b) == 0;
437 add_contact_from_test_case_verify (EBookClient *book_client,
438 const gchar *case_name,
442 EContact *contact_orig;
443 EContact *contact_final;
445 GError *error = NULL;
447 vcard = new_vcard_from_test_case (case_name);
448 contact_orig = e_contact_new_from_vcard (vcard);
450 if (!e_book_client_add_contact_sync (book_client, contact_orig, &uid, NULL, &error)) {
451 report_error ("add contact sync", &error);
452 g_object_unref (contact_orig);
456 e_contact_set (contact_orig, E_CONTACT_UID, uid);
458 if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error)) {
459 report_error ("get contact sync", &error);
460 g_object_unref (contact_orig);
465 /* verify the contact was added "successfully" (not thorough) */
466 g_assert (contacts_are_equal_shallow (contact_orig, contact_final));
469 *contact = contact_final;
471 g_object_unref (contact_final);
472 g_object_unref (contact_orig);
479 add_contact_verify (EBookClient *book_client,
482 EContact *contact_final;
484 GError *error = NULL;
486 if (!e_book_client_add_contact_sync (book_client, contact, &uid, NULL, &error)) {
487 report_error ("add contact sync", &error);
491 e_contact_set (contact, E_CONTACT_UID, uid);
493 if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error)) {
494 report_error ("get contact sync", &error);
499 /* verify the contact was added "successfully" (not thorough) */
500 g_assert (contacts_are_equal_shallow (contact, contact_final));
502 g_object_unref (contact_final);