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,
11 g_return_if_fail (operation != NULL);
13 g_printerr ("Failed to %s: %s\n", operation, (error && *error) ? (*error)->message : "Unknown error");
15 g_clear_error (error);
19 print_email (EContact *contact)
21 const gchar *file_as = e_contact_get_const (contact, E_CONTACT_FILE_AS);
22 const gchar *name_or_org = e_contact_get_const (contact, E_CONTACT_NAME_OR_ORG);
25 g_print (" Contact: %s\n", file_as);
26 g_print (" Name or org: %s\n", name_or_org);
27 g_print (" Email addresses:\n");
28 emails = e_contact_get (contact, E_CONTACT_EMAIL);
29 for (e = emails; e; e = e->next) {
30 g_print ("\t%s\n", (gchar *)e->data);
32 g_list_foreach (emails, (GFunc) g_free, NULL);
39 open_system_book (gboolean only_if_exists)
41 EBookClient *book_client;
46 book_client = e_book_client_new_system (&error);
48 report_error ("create system addressbook", &error);
52 if (!e_client_open_sync (E_CLIENT (book_client), only_if_exists, NULL, &error)) {
53 g_object_unref (book_client);
54 report_error ("open client sync", &error);
62 main_initialize (void)
64 static gboolean initialized = FALSE;
70 #if !GLIB_CHECK_VERSION(2,31,0)
80 gboolean run_in_thread; /* FALSE to run in idle callback */
84 idle_cb (gpointer data)
86 struct IdleData *idle = data;
88 g_return_val_if_fail (idle != NULL, FALSE);
89 g_return_val_if_fail (idle->func != NULL, FALSE);
91 if (idle->run_in_thread) {
94 g_thread_create (idle->func, idle->data, FALSE, &error);
97 report_error ("create thread", &error);
101 idle->func (idle->data);
109 static GMainLoop *loop = NULL;
110 static gint main_stop_result = 0;
113 do_start (GThreadFunc func,
118 g_return_if_fail (loop == NULL);
120 loop = g_main_loop_new (NULL, FALSE);
125 g_main_loop_run (loop);
127 g_main_loop_unref (loop);
131 /* Starts new main-loop, but just before that calls 'func'.
132 * Main-loop is kept running, and this function blocks,
133 * until call of stop_main_loop (). */
135 start_main_loop (GThreadFunc func,
138 g_return_if_fail (loop == NULL);
140 do_start (func, data);
143 /* Starts new main-loop and then invokes func in a new thread.
144 * Main-loop is kept running, and this function blocks,
145 * until call of stop_main_loop (). */
147 start_in_thread_with_main_loop (GThreadFunc func,
150 struct IdleData *idle;
152 g_return_if_fail (func != NULL);
153 g_return_if_fail (loop == NULL);
157 idle = g_new0 (struct IdleData, 1);
160 idle->run_in_thread = TRUE;
162 g_idle_add (idle_cb, idle);
164 do_start (NULL, NULL);
167 /* Starts new main-loop and then invokes func in an idle callback.
168 * Main-loop is kept running, and this function blocks,
169 * until call of stop_main_loop (). */
171 start_in_idle_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 = FALSE;
186 g_idle_add (idle_cb, idle);
188 do_start (NULL, NULL);
191 /* Stops main-loop previously run by start_main_loop,
192 * start_in_thread_with_main_loop or start_in_idle_with_main_loop.
195 stop_main_loop (gint stop_result)
197 g_return_if_fail (loop != NULL);
199 main_stop_result = stop_result;
200 g_main_loop_quit (loop);
203 /* returns value used in stop_main_loop() */
205 get_main_loop_stop_result (void)
207 return main_stop_result;
211 foreach_configured_source (void (*func) (ESource *source))
213 gpointer foreach_async_data;
214 ESource *source = NULL;
216 g_return_if_fail (func != NULL);
220 foreach_async_data = foreach_configured_source_async_start (&source);
221 if (!foreach_async_data)
226 } while (foreach_configured_source_async_next (&foreach_async_data, &source));
229 struct ForeachConfiguredData
231 ESourceList *source_list;
232 GSList *current_group;
233 GSList *current_source;
237 foreach_configured_source_async_start (ESource **source)
239 struct ForeachConfiguredData *async_data;
240 ESourceList *source_list = NULL;
241 GError *error = NULL;
243 g_return_val_if_fail (source != NULL, NULL);
247 if (!e_book_client_get_sources (&source_list, &error)) {
248 report_error ("get addressbooks", &error);
252 g_return_val_if_fail (source_list != NULL, NULL);
254 async_data = g_new0 (struct ForeachConfiguredData, 1);
255 async_data->source_list = source_list;
256 async_data->current_group = e_source_list_peek_groups (source_list);
257 if (!async_data->current_group) {
258 gpointer ad = async_data;
260 foreach_configured_source_async_next (&ad, source);
264 async_data->current_source = e_source_group_peek_sources (async_data->current_group->data);
265 if (!async_data->current_source) {
266 gpointer ad = async_data;
268 if (foreach_configured_source_async_next (&ad, source))
274 *source = async_data->current_source->data;
280 foreach_configured_source_async_next (gpointer *foreach_async_data,
283 struct ForeachConfiguredData *async_data;
285 g_return_val_if_fail (foreach_async_data != NULL, FALSE);
286 g_return_val_if_fail (source != NULL, FALSE);
288 async_data = *foreach_async_data;
289 g_return_val_if_fail (async_data != NULL, FALSE);
290 g_return_val_if_fail (async_data->source_list != NULL, FALSE);
291 g_return_val_if_fail (async_data->current_group != NULL, FALSE);
293 if (async_data->current_source)
294 async_data->current_source = async_data->current_source->next;
295 if (async_data->current_source) {
296 *source = async_data->current_source->data;
301 async_data->current_group = async_data->current_group->next;
302 if (async_data->current_group)
303 async_data->current_source = e_source_group_peek_sources (async_data->current_group->data);
304 } while (async_data->current_group && !async_data->current_source);
306 if (async_data->current_source) {
307 *source = async_data->current_source->data;
311 g_object_unref (async_data->source_list);
314 *foreach_async_data = NULL;
320 new_temp_client (gchar **uri)
322 EBookClient *book_client;
324 gchar *abs_uri, *filename;
326 GError *error = NULL;
328 filename = g_build_filename (g_get_tmp_dir (), "e-book-client-test-XXXXXX/", NULL);
329 handle = g_mkstemp (filename);
334 g_return_val_if_fail (g_mkdir_with_parents (filename, 0700) == 0, NULL);
336 abs_uri = g_strconcat ("local://", filename, NULL);
339 source = e_source_new_with_absolute_uri ("Test book", abs_uri);
345 g_return_val_if_fail (source != NULL, NULL);
347 book_client = e_book_client_new (source, &error);
348 g_object_unref (source);
351 report_error ("new temp client", &error);
357 new_vcard_from_test_case (const gchar *case_name)
360 gchar *case_filename;
362 GError *error = NULL;
365 case_filename = g_strdup_printf ("%s.vcf", case_name);
366 filename = g_build_filename (SRCDIR, "..", "data", "vcards", case_filename, NULL);
367 file = g_file_new_for_path (filename);
368 if (!g_file_load_contents (file, NULL, &vcard, NULL, NULL, &error)) {
369 g_warning ("failed to read test contact file '%s': %s",
370 filename, error->message);
374 g_free (case_filename);
376 g_object_unref (file);
382 contacts_are_equal_shallow (EContact *a,
385 const gchar *uid_a, *uid_b;
387 /* Avoid warnings if one or more are NULL, to make this function
392 if (!E_IS_CONTACT (a) || !E_IS_CONTACT (b))
395 uid_a = e_contact_get_const (a, E_CONTACT_UID);
396 uid_b = e_contact_get_const (b, E_CONTACT_UID);
398 return g_strcmp0 (uid_a, uid_b) == 0;
402 add_contact_from_test_case_verify (EBookClient *book_client,
403 const gchar *case_name,
407 EContact *contact_orig;
408 EContact *contact_final;
410 GError *error = NULL;
412 vcard = new_vcard_from_test_case (case_name);
413 contact_orig = e_contact_new_from_vcard (vcard);
415 if (!e_book_client_add_contact_sync (book_client, contact_orig, &uid, NULL, &error)) {
416 report_error ("add contact sync", &error);
417 g_object_unref (contact_orig);
421 e_contact_set (contact_orig, E_CONTACT_UID, uid);
423 if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error)) {
424 report_error ("get contact sync", &error);
425 g_object_unref (contact_orig);
430 /* verify the contact was added "successfully" (not thorough) */
431 g_assert (contacts_are_equal_shallow (contact_orig, contact_final));
434 *contact = contact_final;
436 g_object_unref (contact_final);
437 g_object_unref (contact_orig);
444 add_contact_verify (EBookClient *book_client,
447 EContact *contact_final;
449 GError *error = NULL;
451 if (!e_book_client_add_contact_sync (book_client, contact, &uid, NULL, &error)) {
452 report_error ("add contact sync", &error);
456 e_contact_set (contact, 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);
464 /* verify the contact was added "successfully" (not thorough) */
465 g_assert (contacts_are_equal_shallow (contact, contact_final));
467 g_object_unref (contact_final);