1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
5 #include "client-test-utils.h"
8 report_error (const gchar *operation, GError **error)
10 g_return_if_fail (operation != NULL);
12 g_printerr ("Failed to %s: %s\n", operation, (error && *error) ? (*error)->message : "Unknown error");
14 g_clear_error (error);
18 print_email (EContact *contact)
20 const gchar *file_as = e_contact_get_const (contact, E_CONTACT_FILE_AS);
21 const gchar *name_or_org = e_contact_get_const (contact, E_CONTACT_NAME_OR_ORG);
24 g_print (" Contact: %s\n", file_as);
25 g_print (" Name or org: %s\n", name_or_org);
26 g_print (" Email addresses:\n");
27 emails = e_contact_get (contact, E_CONTACT_EMAIL);
28 for (e = emails; e; e = e->next) {
29 g_print ("\t%s\n", (gchar *)e->data);
31 g_list_foreach (emails, (GFunc) g_free, NULL);
38 open_system_book (gboolean only_if_exists)
40 EBookClient *book_client;
45 book_client = e_book_client_new_system (&error);
47 report_error ("create system addressbook", &error);
51 if (!e_client_open_sync (E_CLIENT (book_client), only_if_exists, NULL, &error)) {
52 g_object_unref (book_client);
53 report_error ("open client sync", &error);
61 main_initialize (void)
63 static gboolean initialized = FALSE;
77 gboolean run_in_thread; /* FALSE to run in idle callback */
81 idle_cb (gpointer data)
83 struct IdleData *idle = data;
85 g_return_val_if_fail (idle != NULL, FALSE);
86 g_return_val_if_fail (idle->func != NULL, FALSE);
88 if (idle->run_in_thread) {
91 g_thread_create (idle->func, idle->data, FALSE, &error);
94 report_error ("create thread", &error);
98 idle->func (idle->data);
106 static GMainLoop *loop = NULL;
107 static gint main_stop_result = 0;
110 do_start (GThreadFunc func, gpointer data)
114 g_return_if_fail (loop == NULL);
116 loop = g_main_loop_new (NULL, FALSE);
121 g_main_loop_run (loop);
123 g_main_loop_unref (loop);
127 /* Starts new main-loop, but just before that calls 'func'.
128 Main-loop is kept running, and this function blocks,
129 until call of stop_main_loop ().
132 start_main_loop (GThreadFunc func, gpointer data)
134 g_return_if_fail (loop == NULL);
136 do_start (func, data);
139 /* Starts new main-loop and then invokes func in a new thread.
140 Main-loop is kept running, and this function blocks,
141 until call of stop_main_loop ().
144 start_in_thread_with_main_loop (GThreadFunc func, gpointer data)
146 struct IdleData *idle;
148 g_return_if_fail (func != NULL);
149 g_return_if_fail (loop == NULL);
153 idle = g_new0 (struct IdleData, 1);
156 idle->run_in_thread = TRUE;
158 g_idle_add (idle_cb, idle);
160 do_start (NULL, NULL);
163 /* Starts new main-loop and then invokes func in an idle callback.
164 Main-loop is kept running, and this function blocks,
165 until call of stop_main_loop ().
168 start_in_idle_with_main_loop (GThreadFunc func, gpointer data)
170 struct IdleData *idle;
172 g_return_if_fail (func != NULL);
173 g_return_if_fail (loop == NULL);
177 idle = g_new0 (struct IdleData, 1);
180 idle->run_in_thread = FALSE;
182 g_idle_add (idle_cb, idle);
184 do_start (NULL, NULL);
187 /* Stops main-loop previously run by start_main_loop,
188 start_in_thread_with_main_loop or start_in_idle_with_main_loop.
191 stop_main_loop (gint stop_result)
193 g_return_if_fail (loop != NULL);
195 main_stop_result = stop_result;
196 g_main_loop_quit (loop);
199 /* returns value used in stop_main_loop() */
201 get_main_loop_stop_result (void)
203 return main_stop_result;
207 foreach_configured_source (void (*func) (ESource *source))
209 gpointer foreach_async_data;
210 ESource *source = NULL;
212 g_return_if_fail (func != NULL);
216 foreach_async_data = foreach_configured_source_async_start (&source);
217 if (!foreach_async_data)
222 } while (foreach_configured_source_async_next (&foreach_async_data, &source));
225 struct ForeachConfiguredData
227 ESourceList *source_list;
228 GSList *current_group;
229 GSList *current_source;
233 foreach_configured_source_async_start (ESource **source)
235 struct ForeachConfiguredData *async_data;
236 ESourceList *source_list = NULL;
237 GError *error = NULL;
239 g_return_val_if_fail (source != NULL, NULL);
243 if (!e_book_client_get_sources (&source_list, &error)) {
244 report_error ("get addressbooks", &error);
248 g_return_val_if_fail (source_list != NULL, NULL);
250 async_data = g_new0 (struct ForeachConfiguredData, 1);
251 async_data->source_list = source_list;
252 async_data->current_group = e_source_list_peek_groups (source_list);
253 if (!async_data->current_group) {
254 gpointer ad = async_data;
256 foreach_configured_source_async_next (&ad, source);
260 async_data->current_source = e_source_group_peek_sources (async_data->current_group->data);
261 if (!async_data->current_source) {
262 gpointer ad = async_data;
264 if (foreach_configured_source_async_next (&ad, source))
270 *source = async_data->current_source->data;
276 foreach_configured_source_async_next (gpointer *foreach_async_data, ESource **source)
278 struct ForeachConfiguredData *async_data;
280 g_return_val_if_fail (foreach_async_data != NULL, FALSE);
281 g_return_val_if_fail (source != NULL, FALSE);
283 async_data = *foreach_async_data;
284 g_return_val_if_fail (async_data != NULL, FALSE);
285 g_return_val_if_fail (async_data->source_list != NULL, FALSE);
286 g_return_val_if_fail (async_data->current_group != NULL, FALSE);
288 if (async_data->current_source)
289 async_data->current_source = async_data->current_source->next;
290 if (async_data->current_source) {
291 *source = async_data->current_source->data;
296 async_data->current_group = async_data->current_group->next;
297 if (async_data->current_group)
298 async_data->current_source = e_source_group_peek_sources (async_data->current_group->data);
299 } while (async_data->current_group && !async_data->current_source);
301 if (async_data->current_source) {
302 *source = async_data->current_source->data;
306 g_object_unref (async_data->source_list);
309 *foreach_async_data = NULL;
315 new_temp_client (gchar **uri)
317 EBookClient *book_client;
319 gchar *abs_uri, *filename;
321 GError *error = NULL;
323 filename = g_build_filename (g_get_tmp_dir (), "e-book-client-test-XXXXXX/", NULL);
324 handle = g_mkstemp (filename);
329 g_return_val_if_fail (g_mkdir_with_parents (filename, 0700) == 0, NULL);
331 abs_uri = g_strconcat ("local://", filename, NULL);
334 source = e_source_new_with_absolute_uri ("Test book", abs_uri);
340 g_return_val_if_fail (source != NULL, NULL);
342 book_client = e_book_client_new (source, &error);
343 g_object_unref (source);
346 report_error ("new temp client", &error);
352 new_vcard_from_test_case (const gchar *case_name)
355 gchar *case_filename;
357 GError *error = NULL;
360 case_filename = g_strdup_printf ("%s.vcf", case_name);
361 filename = g_build_filename (SRCDIR, "..", "data", "vcards", case_filename, NULL);
362 file = g_file_new_for_path (filename);
363 if (!g_file_load_contents (file, NULL, &vcard, NULL, NULL, &error)) {
364 g_warning ("failed to read test contact file '%s': %s",
365 filename, error->message);
369 g_free (case_filename);
371 g_object_unref (file);
377 contacts_are_equal_shallow (EContact *a, EContact *b)
379 const gchar *uid_a, *uid_b;
381 /* Avoid warnings if one or more are NULL, to make this function
386 if (!E_IS_CONTACT (a) || !E_IS_CONTACT (b))
389 uid_a = e_contact_get_const (a, E_CONTACT_UID);
390 uid_b = e_contact_get_const (b, E_CONTACT_UID);
392 return g_strcmp0 (uid_a, uid_b) == 0;
396 add_contact_from_test_case_verify (EBookClient *book_client, const gchar *case_name, EContact **contact)
399 EContact *contact_orig;
400 EContact *contact_final;
402 GError *error = NULL;
404 vcard = new_vcard_from_test_case (case_name);
405 contact_orig = e_contact_new_from_vcard (vcard);
407 if (!e_book_client_add_contact_sync (book_client, contact_orig, &uid, NULL, &error)) {
408 report_error ("add contact sync", &error);
409 g_object_unref (contact_orig);
413 e_contact_set (contact_orig, E_CONTACT_UID, uid);
415 if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error)) {
416 report_error ("get contact sync", &error);
417 g_object_unref (contact_orig);
422 /* verify the contact was added "successfully" (not thorough) */
423 g_assert (contacts_are_equal_shallow (contact_orig, contact_final));
426 *contact = contact_final;
428 g_object_unref (contact_final);
429 g_object_unref (contact_orig);
436 add_contact_verify (EBookClient *book_client, EContact *contact)
438 EContact *contact_final;
440 GError *error = NULL;
442 if (!e_book_client_add_contact_sync (book_client, contact, &uid, NULL, &error)) {
443 report_error ("add contact sync", &error);
447 e_contact_set (contact, E_CONTACT_UID, uid);
449 if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error)) {
450 report_error ("get contact sync", &error);
455 /* verify the contact was added "successfully" (not thorough) */
456 g_assert (contacts_are_equal_shallow (contact, contact_final));
458 g_object_unref (contact_final);