1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
5 #include <libedataserver/e-gdbus-templates.h>
7 #include "client-test-utils.h"
10 report_error (const gchar *operation,
13 g_return_if_fail (operation != NULL);
15 g_printerr ("Failed to %s: %s\n", operation, (error && *error) ? (*error)->message : "Unknown error");
17 g_clear_error (error);
21 print_email (EContact *contact)
23 const gchar *file_as = e_contact_get_const (contact, E_CONTACT_FILE_AS);
24 const gchar *name_or_org = e_contact_get_const (contact, E_CONTACT_NAME_OR_ORG);
27 g_print (" Contact: %s\n", file_as);
28 g_print (" Name or org: %s\n", name_or_org);
29 g_print (" Email addresses:\n");
30 emails = e_contact_get (contact, E_CONTACT_EMAIL);
31 for (e = emails; e; e = e->next) {
32 g_print ("\t%s\n", (gchar *)e->data);
34 g_list_foreach (emails, (GFunc) g_free, NULL);
41 open_system_book (gboolean only_if_exists)
43 EBookClient *book_client;
48 book_client = e_book_client_new_system (&error);
50 report_error ("create system addressbook", &error);
54 if (!e_client_open_sync (E_CLIENT (book_client), only_if_exists, NULL, &error)) {
55 g_object_unref (book_client);
56 report_error ("open client sync", &error);
64 main_initialize (void)
66 static gboolean initialized = FALSE;
72 e_gdbus_templates_init_main_thread ();
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 if (foreach_configured_source_async_next (&ad, source))
266 async_data->current_source = e_source_group_peek_sources (async_data->current_group->data);
267 if (!async_data->current_source) {
268 gpointer ad = async_data;
270 if (foreach_configured_source_async_next (&ad, source))
276 *source = async_data->current_source->data;
282 foreach_configured_source_async_next (gpointer *foreach_async_data,
285 struct ForeachConfiguredData *async_data;
287 g_return_val_if_fail (foreach_async_data != NULL, FALSE);
288 g_return_val_if_fail (source != NULL, FALSE);
290 async_data = *foreach_async_data;
291 g_return_val_if_fail (async_data != NULL, FALSE);
292 g_return_val_if_fail (async_data->source_list != NULL, FALSE);
293 g_return_val_if_fail (async_data->current_group != NULL, FALSE);
295 if (async_data->current_source)
296 async_data->current_source = async_data->current_source->next;
297 if (async_data->current_source) {
298 *source = async_data->current_source->data;
303 async_data->current_group = async_data->current_group->next;
304 if (async_data->current_group)
305 async_data->current_source = e_source_group_peek_sources (async_data->current_group->data);
306 } while (async_data->current_group && !async_data->current_source);
308 if (async_data->current_source) {
309 *source = async_data->current_source->data;
313 g_object_unref (async_data->source_list);
316 *foreach_async_data = NULL;
322 new_temp_client (gchar **uri)
324 EBookClient *book_client;
326 gchar *abs_uri, *filename;
328 GError *error = NULL;
330 filename = g_build_filename (g_get_tmp_dir (), "e-book-client-test-XXXXXX/", NULL);
331 handle = g_mkstemp (filename);
336 g_return_val_if_fail (g_mkdir_with_parents (filename, 0700) == 0, NULL);
338 abs_uri = g_strconcat ("local://", filename, NULL);
341 source = e_source_new_with_absolute_uri ("Test book", abs_uri);
347 g_return_val_if_fail (source != NULL, NULL);
349 book_client = e_book_client_new (source, &error);
350 g_object_unref (source);
353 report_error ("new temp client", &error);
359 new_vcard_from_test_case (const gchar *case_name)
362 gchar *case_filename;
364 GError *error = NULL;
367 case_filename = g_strdup_printf ("%s.vcf", case_name);
368 filename = g_build_filename (SRCDIR, "..", "data", "vcards", case_filename, NULL);
369 file = g_file_new_for_path (filename);
370 if (!g_file_load_contents (file, NULL, &vcard, NULL, NULL, &error)) {
371 g_warning ("failed to read test contact file '%s': %s",
372 filename, error->message);
376 g_free (case_filename);
378 g_object_unref (file);
384 contacts_are_equal_shallow (EContact *a,
387 const gchar *uid_a, *uid_b;
389 /* Avoid warnings if one or more are NULL, to make this function
394 if (!E_IS_CONTACT (a) || !E_IS_CONTACT (b))
397 uid_a = e_contact_get_const (a, E_CONTACT_UID);
398 uid_b = e_contact_get_const (b, E_CONTACT_UID);
400 return g_strcmp0 (uid_a, uid_b) == 0;
404 add_contact_from_test_case_verify (EBookClient *book_client,
405 const gchar *case_name,
409 EContact *contact_orig;
410 EContact *contact_final;
412 GError *error = NULL;
414 vcard = new_vcard_from_test_case (case_name);
415 contact_orig = e_contact_new_from_vcard (vcard);
417 if (!e_book_client_add_contact_sync (book_client, contact_orig, &uid, NULL, &error)) {
418 report_error ("add contact sync", &error);
419 g_object_unref (contact_orig);
423 e_contact_set (contact_orig, E_CONTACT_UID, uid);
425 if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error)) {
426 report_error ("get contact sync", &error);
427 g_object_unref (contact_orig);
432 /* verify the contact was added "successfully" (not thorough) */
433 g_assert (contacts_are_equal_shallow (contact_orig, contact_final));
436 *contact = contact_final;
438 g_object_unref (contact_final);
439 g_object_unref (contact_orig);
446 add_contact_verify (EBookClient *book_client,
449 EContact *contact_final;
451 GError *error = NULL;
453 if (!e_book_client_add_contact_sync (book_client, contact, &uid, NULL, &error)) {
454 report_error ("add contact sync", &error);
458 e_contact_set (contact, E_CONTACT_UID, uid);
460 if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error)) {
461 report_error ("get contact sync", &error);
466 /* verify the contact was added "successfully" (not thorough) */
467 g_assert (contacts_are_equal_shallow (contact, contact_final));
469 g_object_unref (contact_final);