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;
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,
115 g_return_if_fail (loop == NULL);
117 loop = g_main_loop_new (NULL, FALSE);
122 g_main_loop_run (loop);
124 g_main_loop_unref (loop);
128 /* Starts new main-loop, but just before that calls 'func'.
129 * Main-loop is kept running, and this function blocks,
130 * until call of stop_main_loop (). */
132 start_main_loop (GThreadFunc func,
135 g_return_if_fail (loop == NULL);
137 do_start (func, data);
140 /* Starts new main-loop and then invokes func in a new thread.
141 * Main-loop is kept running, and this function blocks,
142 * until call of stop_main_loop (). */
144 start_in_thread_with_main_loop (GThreadFunc func,
147 struct IdleData *idle;
149 g_return_if_fail (func != NULL);
150 g_return_if_fail (loop == NULL);
154 idle = g_new0 (struct IdleData, 1);
157 idle->run_in_thread = TRUE;
159 g_idle_add (idle_cb, idle);
161 do_start (NULL, NULL);
164 /* Starts new main-loop and then invokes func in an idle callback.
165 * Main-loop is kept running, and this function blocks,
166 * until call of stop_main_loop (). */
168 start_in_idle_with_main_loop (GThreadFunc func,
171 struct IdleData *idle;
173 g_return_if_fail (func != NULL);
174 g_return_if_fail (loop == NULL);
178 idle = g_new0 (struct IdleData, 1);
181 idle->run_in_thread = FALSE;
183 g_idle_add (idle_cb, idle);
185 do_start (NULL, NULL);
188 /* Stops main-loop previously run by start_main_loop,
189 * start_in_thread_with_main_loop or start_in_idle_with_main_loop.
192 stop_main_loop (gint stop_result)
194 g_return_if_fail (loop != NULL);
196 main_stop_result = stop_result;
197 g_main_loop_quit (loop);
200 /* returns value used in stop_main_loop() */
202 get_main_loop_stop_result (void)
204 return main_stop_result;
208 foreach_configured_source (void (*func) (ESource *source))
210 gpointer foreach_async_data;
211 ESource *source = NULL;
213 g_return_if_fail (func != NULL);
217 foreach_async_data = foreach_configured_source_async_start (&source);
218 if (!foreach_async_data)
223 } while (foreach_configured_source_async_next (&foreach_async_data, &source));
226 struct ForeachConfiguredData
228 ESourceList *source_list;
229 GSList *current_group;
230 GSList *current_source;
234 foreach_configured_source_async_start (ESource **source)
236 struct ForeachConfiguredData *async_data;
237 ESourceList *source_list = NULL;
238 GError *error = NULL;
240 g_return_val_if_fail (source != NULL, NULL);
244 if (!e_book_client_get_sources (&source_list, &error)) {
245 report_error ("get addressbooks", &error);
249 g_return_val_if_fail (source_list != NULL, NULL);
251 async_data = g_new0 (struct ForeachConfiguredData, 1);
252 async_data->source_list = source_list;
253 async_data->current_group = e_source_list_peek_groups (source_list);
254 if (!async_data->current_group) {
255 gpointer ad = async_data;
257 foreach_configured_source_async_next (&ad, source);
261 async_data->current_source = e_source_group_peek_sources (async_data->current_group->data);
262 if (!async_data->current_source) {
263 gpointer ad = async_data;
265 if (foreach_configured_source_async_next (&ad, source))
271 *source = async_data->current_source->data;
277 foreach_configured_source_async_next (gpointer *foreach_async_data,
280 struct ForeachConfiguredData *async_data;
282 g_return_val_if_fail (foreach_async_data != NULL, FALSE);
283 g_return_val_if_fail (source != NULL, FALSE);
285 async_data = *foreach_async_data;
286 g_return_val_if_fail (async_data != NULL, FALSE);
287 g_return_val_if_fail (async_data->source_list != NULL, FALSE);
288 g_return_val_if_fail (async_data->current_group != NULL, FALSE);
290 if (async_data->current_source)
291 async_data->current_source = async_data->current_source->next;
292 if (async_data->current_source) {
293 *source = async_data->current_source->data;
298 async_data->current_group = async_data->current_group->next;
299 if (async_data->current_group)
300 async_data->current_source = e_source_group_peek_sources (async_data->current_group->data);
301 } while (async_data->current_group && !async_data->current_source);
303 if (async_data->current_source) {
304 *source = async_data->current_source->data;
308 g_object_unref (async_data->source_list);
311 *foreach_async_data = NULL;
317 new_temp_client (gchar **uri)
319 EBookClient *book_client;
321 gchar *abs_uri, *filename;
323 GError *error = NULL;
325 filename = g_build_filename (g_get_tmp_dir (), "e-book-client-test-XXXXXX/", NULL);
326 handle = g_mkstemp (filename);
331 g_return_val_if_fail (g_mkdir_with_parents (filename, 0700) == 0, NULL);
333 abs_uri = g_strconcat ("local://", filename, NULL);
336 source = e_source_new_with_absolute_uri ("Test book", abs_uri);
342 g_return_val_if_fail (source != NULL, NULL);
344 book_client = e_book_client_new (source, &error);
345 g_object_unref (source);
348 report_error ("new temp client", &error);
354 new_vcard_from_test_case (const gchar *case_name)
357 gchar *case_filename;
359 GError *error = NULL;
362 case_filename = g_strdup_printf ("%s.vcf", case_name);
363 filename = g_build_filename (SRCDIR, "..", "data", "vcards", case_filename, NULL);
364 file = g_file_new_for_path (filename);
365 if (!g_file_load_contents (file, NULL, &vcard, NULL, NULL, &error)) {
366 g_warning ("failed to read test contact file '%s': %s",
367 filename, error->message);
371 g_free (case_filename);
373 g_object_unref (file);
379 contacts_are_equal_shallow (EContact *a,
382 const gchar *uid_a, *uid_b;
384 /* Avoid warnings if one or more are NULL, to make this function
389 if (!E_IS_CONTACT (a) || !E_IS_CONTACT (b))
392 uid_a = e_contact_get_const (a, E_CONTACT_UID);
393 uid_b = e_contact_get_const (b, E_CONTACT_UID);
395 return g_strcmp0 (uid_a, uid_b) == 0;
399 add_contact_from_test_case_verify (EBookClient *book_client,
400 const gchar *case_name,
404 EContact *contact_orig;
405 EContact *contact_final;
407 GError *error = NULL;
409 vcard = new_vcard_from_test_case (case_name);
410 contact_orig = e_contact_new_from_vcard (vcard);
412 if (!e_book_client_add_contact_sync (book_client, contact_orig, &uid, NULL, &error)) {
413 report_error ("add contact sync", &error);
414 g_object_unref (contact_orig);
418 e_contact_set (contact_orig, E_CONTACT_UID, uid);
420 if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error)) {
421 report_error ("get contact sync", &error);
422 g_object_unref (contact_orig);
427 /* verify the contact was added "successfully" (not thorough) */
428 g_assert (contacts_are_equal_shallow (contact_orig, contact_final));
431 *contact = contact_final;
433 g_object_unref (contact_final);
434 g_object_unref (contact_orig);
441 add_contact_verify (EBookClient *book_client,
444 EContact *contact_final;
446 GError *error = NULL;
448 if (!e_book_client_add_contact_sync (book_client, contact, &uid, NULL, &error)) {
449 report_error ("add contact sync", &error);
453 e_contact_set (contact, E_CONTACT_UID, uid);
455 if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error)) {
456 report_error ("get contact sync", &error);
461 /* verify the contact was added "successfully" (not thorough) */
462 g_assert (contacts_are_equal_shallow (contact, contact_final));
464 g_object_unref (contact_final);