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;
78 gboolean run_in_thread; /* FALSE to run in idle callback */
82 idle_cb (gpointer data)
84 struct IdleData *idle = data;
86 g_return_val_if_fail (idle != NULL, FALSE);
87 g_return_val_if_fail (idle->func != NULL, FALSE);
89 if (idle->run_in_thread) {
92 g_thread_create (idle->func, idle->data, FALSE, &error);
95 report_error ("create thread", &error);
99 idle->func (idle->data);
107 static GMainLoop *loop = NULL;
108 static gint main_stop_result = 0;
111 do_start (GThreadFunc func,
116 g_return_if_fail (loop == NULL);
118 loop = g_main_loop_new (NULL, FALSE);
123 g_main_loop_run (loop);
125 g_main_loop_unref (loop);
129 /* Starts new main-loop, but just before that calls 'func'.
130 * Main-loop is kept running, and this function blocks,
131 * until call of stop_main_loop (). */
133 start_main_loop (GThreadFunc func,
136 g_return_if_fail (loop == NULL);
138 do_start (func, data);
141 /* Starts new main-loop and then invokes func in a new thread.
142 * Main-loop is kept running, and this function blocks,
143 * until call of stop_main_loop (). */
145 start_in_thread_with_main_loop (GThreadFunc func,
148 struct IdleData *idle;
150 g_return_if_fail (func != NULL);
151 g_return_if_fail (loop == NULL);
155 idle = g_new0 (struct IdleData, 1);
158 idle->run_in_thread = TRUE;
160 g_idle_add (idle_cb, idle);
162 do_start (NULL, NULL);
165 /* Starts new main-loop and then invokes func in an idle callback.
166 * Main-loop is kept running, and this function blocks,
167 * until call of stop_main_loop (). */
169 start_in_idle_with_main_loop (GThreadFunc func,
172 struct IdleData *idle;
174 g_return_if_fail (func != NULL);
175 g_return_if_fail (loop == NULL);
179 idle = g_new0 (struct IdleData, 1);
182 idle->run_in_thread = FALSE;
184 g_idle_add (idle_cb, idle);
186 do_start (NULL, NULL);
189 /* Stops main-loop previously run by start_main_loop,
190 * start_in_thread_with_main_loop or start_in_idle_with_main_loop.
193 stop_main_loop (gint stop_result)
195 g_return_if_fail (loop != NULL);
197 main_stop_result = stop_result;
198 g_main_loop_quit (loop);
201 /* returns value used in stop_main_loop() */
203 get_main_loop_stop_result (void)
205 return main_stop_result;
209 foreach_configured_source (void (*func) (ESource *source))
211 gpointer foreach_async_data;
212 ESource *source = NULL;
214 g_return_if_fail (func != NULL);
218 foreach_async_data = foreach_configured_source_async_start (&source);
219 if (!foreach_async_data)
224 } while (foreach_configured_source_async_next (&foreach_async_data, &source));
227 struct ForeachConfiguredData
229 ESourceList *source_list;
230 GSList *current_group;
231 GSList *current_source;
235 foreach_configured_source_async_start (ESource **source)
237 struct ForeachConfiguredData *async_data;
238 ESourceList *source_list = NULL;
239 GError *error = NULL;
241 g_return_val_if_fail (source != NULL, NULL);
245 if (!e_book_client_get_sources (&source_list, &error)) {
246 report_error ("get addressbooks", &error);
250 g_return_val_if_fail (source_list != NULL, NULL);
252 async_data = g_new0 (struct ForeachConfiguredData, 1);
253 async_data->source_list = source_list;
254 async_data->current_group = e_source_list_peek_groups (source_list);
255 if (!async_data->current_group) {
256 gpointer ad = async_data;
258 foreach_configured_source_async_next (&ad, source);
262 async_data->current_source = e_source_group_peek_sources (async_data->current_group->data);
263 if (!async_data->current_source) {
264 gpointer ad = async_data;
266 if (foreach_configured_source_async_next (&ad, source))
272 *source = async_data->current_source->data;
278 foreach_configured_source_async_next (gpointer *foreach_async_data,
281 struct ForeachConfiguredData *async_data;
283 g_return_val_if_fail (foreach_async_data != NULL, FALSE);
284 g_return_val_if_fail (source != NULL, FALSE);
286 async_data = *foreach_async_data;
287 g_return_val_if_fail (async_data != NULL, FALSE);
288 g_return_val_if_fail (async_data->source_list != NULL, FALSE);
289 g_return_val_if_fail (async_data->current_group != NULL, FALSE);
291 if (async_data->current_source)
292 async_data->current_source = async_data->current_source->next;
293 if (async_data->current_source) {
294 *source = async_data->current_source->data;
299 async_data->current_group = async_data->current_group->next;
300 if (async_data->current_group)
301 async_data->current_source = e_source_group_peek_sources (async_data->current_group->data);
302 } while (async_data->current_group && !async_data->current_source);
304 if (async_data->current_source) {
305 *source = async_data->current_source->data;
309 g_object_unref (async_data->source_list);
312 *foreach_async_data = NULL;
318 new_temp_client (gchar **uri)
320 EBookClient *book_client;
322 gchar *abs_uri, *filename;
324 GError *error = NULL;
326 filename = g_build_filename (g_get_tmp_dir (), "e-book-client-test-XXXXXX/", NULL);
327 handle = g_mkstemp (filename);
332 g_return_val_if_fail (g_mkdir_with_parents (filename, 0700) == 0, NULL);
334 abs_uri = g_strconcat ("local://", filename, NULL);
337 source = e_source_new_with_absolute_uri ("Test book", abs_uri);
343 g_return_val_if_fail (source != NULL, NULL);
345 book_client = e_book_client_new (source, &error);
346 g_object_unref (source);
349 report_error ("new temp client", &error);
355 new_vcard_from_test_case (const gchar *case_name)
358 gchar *case_filename;
360 GError *error = NULL;
363 case_filename = g_strdup_printf ("%s.vcf", case_name);
364 filename = g_build_filename (SRCDIR, "..", "data", "vcards", case_filename, NULL);
365 file = g_file_new_for_path (filename);
366 if (!g_file_load_contents (file, NULL, &vcard, NULL, NULL, &error)) {
367 g_warning ("failed to read test contact file '%s': %s",
368 filename, error->message);
372 g_free (case_filename);
374 g_object_unref (file);
380 contacts_are_equal_shallow (EContact *a,
383 const gchar *uid_a, *uid_b;
385 /* Avoid warnings if one or more are NULL, to make this function
390 if (!E_IS_CONTACT (a) || !E_IS_CONTACT (b))
393 uid_a = e_contact_get_const (a, E_CONTACT_UID);
394 uid_b = e_contact_get_const (b, E_CONTACT_UID);
396 return g_strcmp0 (uid_a, uid_b) == 0;
400 add_contact_from_test_case_verify (EBookClient *book_client,
401 const gchar *case_name,
405 EContact *contact_orig;
406 EContact *contact_final;
408 GError *error = NULL;
410 vcard = new_vcard_from_test_case (case_name);
411 contact_orig = e_contact_new_from_vcard (vcard);
413 if (!e_book_client_add_contact_sync (book_client, contact_orig, &uid, NULL, &error)) {
414 report_error ("add contact sync", &error);
415 g_object_unref (contact_orig);
419 e_contact_set (contact_orig, E_CONTACT_UID, uid);
421 if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error)) {
422 report_error ("get contact sync", &error);
423 g_object_unref (contact_orig);
428 /* verify the contact was added "successfully" (not thorough) */
429 g_assert (contacts_are_equal_shallow (contact_orig, contact_final));
432 *contact = contact_final;
434 g_object_unref (contact_final);
435 g_object_unref (contact_orig);
442 add_contact_verify (EBookClient *book_client,
445 EContact *contact_final;
447 GError *error = NULL;
449 if (!e_book_client_add_contact_sync (book_client, contact, &uid, NULL, &error)) {
450 report_error ("add contact sync", &error);
454 e_contact_set (contact, E_CONTACT_UID, uid);
456 if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error)) {
457 report_error ("get contact sync", &error);
462 /* verify the contact was added "successfully" (not thorough) */
463 g_assert (contacts_are_equal_shallow (contact, contact_final));
465 g_object_unref (contact_final);