1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
6 #include <libedataserver/libedataserver.h>
8 #include "client-test-utils.h"
11 report_error (const gchar *operation,
14 g_return_if_fail (operation != NULL);
16 g_printerr ("Failed to %s: %s\n", operation, (error && *error) ? (*error)->message : "Unknown error");
18 g_clear_error (error);
22 print_email (EContact *contact)
24 const gchar *file_as = e_contact_get_const (contact, E_CONTACT_FILE_AS);
25 const gchar *name_or_org = e_contact_get_const (contact, E_CONTACT_NAME_OR_ORG);
28 g_print (" Contact: %s\n", file_as);
29 g_print (" Name or org: %s\n", name_or_org);
30 g_print (" Email addresses:\n");
31 emails = e_contact_get (contact, E_CONTACT_EMAIL);
32 for (e = emails; e; e = e->next) {
33 g_print ("\t%s\n", (gchar *) e->data);
35 g_list_foreach (emails, (GFunc) g_free, NULL);
42 open_system_book (ESourceRegistry *registry,
43 gboolean only_if_exists)
46 EBookClient *book_client;
51 source = e_source_registry_ref_builtin_address_book (registry);
52 book_client = e_book_client_new (source, &error);
53 g_object_unref (source);
56 report_error ("create system addressbook", &error);
60 if (!e_client_open_sync (E_CLIENT (book_client), only_if_exists, NULL, &error)) {
61 g_object_unref (book_client);
62 report_error ("open client sync", &error);
70 main_initialize (void)
72 static gboolean initialized = FALSE;
78 e_gdbus_templates_init_main_thread ();
86 gboolean run_in_thread; /* FALSE to run in idle callback */
90 idle_cb (gpointer data)
92 struct IdleData *idle = data;
94 g_return_val_if_fail (idle != NULL, FALSE);
95 g_return_val_if_fail (idle->func != NULL, FALSE);
97 if (idle->run_in_thread) {
100 g_thread_create (idle->func, idle->data, FALSE, &error);
103 report_error ("create thread", &error);
107 idle->func (idle->data);
115 static GMainLoop *loop = NULL;
116 static gint main_stop_result = 0;
119 do_start (GThreadFunc func,
124 g_return_if_fail (loop == NULL);
126 loop = g_main_loop_new (NULL, FALSE);
131 g_main_loop_run (loop);
133 g_main_loop_unref (loop);
137 /* Starts new main-loop, but just before that calls 'func'.
138 * Main-loop is kept running, and this function blocks,
139 * until call of stop_main_loop (). */
141 start_main_loop (GThreadFunc func,
144 g_return_if_fail (loop == NULL);
146 do_start (func, data);
149 /* Starts new main-loop and then invokes func in a new thread.
150 * Main-loop is kept running, and this function blocks,
151 * until call of stop_main_loop (). */
153 start_in_thread_with_main_loop (GThreadFunc func,
156 struct IdleData *idle;
158 g_return_if_fail (func != NULL);
159 g_return_if_fail (loop == NULL);
163 idle = g_new0 (struct IdleData, 1);
166 idle->run_in_thread = TRUE;
168 g_idle_add (idle_cb, idle);
170 do_start (NULL, NULL);
173 /* Starts new main-loop and then invokes func in an idle callback.
174 * Main-loop is kept running, and this function blocks,
175 * until call of stop_main_loop (). */
177 start_in_idle_with_main_loop (GThreadFunc func,
180 struct IdleData *idle;
182 g_return_if_fail (func != NULL);
183 g_return_if_fail (loop == NULL);
187 idle = g_new0 (struct IdleData, 1);
190 idle->run_in_thread = FALSE;
192 g_idle_add (idle_cb, idle);
194 do_start (NULL, NULL);
197 /* Stops main-loop previously run by start_main_loop,
198 * start_in_thread_with_main_loop or start_in_idle_with_main_loop.
201 stop_main_loop (gint stop_result)
203 g_return_if_fail (loop != NULL);
205 main_stop_result = stop_result;
206 g_main_loop_quit (loop);
209 /* returns value used in stop_main_loop() */
211 get_main_loop_stop_result (void)
213 return main_stop_result;
217 foreach_configured_source (ESourceRegistry *registry,
218 void (*func) (ESource *source))
220 gpointer foreach_async_data;
221 ESource *source = NULL;
223 g_return_if_fail (func != NULL);
227 foreach_async_data = foreach_configured_source_async_start (registry, &source);
228 if (!foreach_async_data)
233 } while (foreach_configured_source_async_next (&foreach_async_data, &source));
236 struct ForeachConfiguredData {
241 foreach_configured_source_async_start (ESourceRegistry *registry,
244 struct ForeachConfiguredData *async_data;
245 const gchar *extension_name;
248 g_return_val_if_fail (source != NULL, NULL);
252 extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK;
253 list = e_source_registry_list_sources (registry, extension_name);
255 async_data = g_new0 (struct ForeachConfiguredData, 1);
256 async_data->list = list;
258 *source = async_data->list->data;
264 foreach_configured_source_async_next (gpointer *foreach_async_data,
267 struct ForeachConfiguredData *async_data;
269 g_return_val_if_fail (foreach_async_data != NULL, FALSE);
270 g_return_val_if_fail (source != NULL, FALSE);
272 async_data = *foreach_async_data;
273 g_return_val_if_fail (async_data != NULL, FALSE);
275 if (async_data->list) {
276 g_object_unref (async_data->list->data);
277 async_data->list = async_data->list->next;
279 if (async_data->list) {
280 *source = async_data->list->data;
286 *foreach_async_data = NULL;
292 new_temp_client (gchar **uri)
294 #if 0 /* ACCOUNT_MGMT */
295 EBookClient *book_client;
297 gchar *abs_uri, *filename;
299 GError *error = NULL;
301 filename = g_build_filename (g_get_tmp_dir (), "e-book-client-test-XXXXXX/", NULL);
302 handle = g_mkstemp (filename);
307 g_return_val_if_fail (g_mkdir_with_parents (filename, 0700) == 0, NULL);
309 abs_uri = g_strconcat ("local://", filename, NULL);
312 source = e_source_new_with_absolute_uri ("Test book", abs_uri);
318 g_return_val_if_fail (source != NULL, NULL);
320 book_client = e_book_client_new (source, &error);
321 g_object_unref (source);
324 report_error ("new temp client", &error);
327 #endif /* ACCOUNT_MGMT */
333 new_vcard_from_test_case (const gchar *case_name)
336 gchar *case_filename;
338 GError *error = NULL;
341 case_filename = g_strdup_printf ("%s.vcf", case_name);
342 filename = g_build_filename (SRCDIR, "..", "data", "vcards", case_filename, NULL);
343 file = g_file_new_for_path (filename);
344 if (!g_file_load_contents (file, NULL, &vcard, NULL, NULL, &error)) {
345 g_warning ("failed to read test contact file '%s': %s",
346 filename, error->message);
350 g_free (case_filename);
352 g_object_unref (file);
358 contacts_are_equal_shallow (EContact *a,
361 const gchar *uid_a, *uid_b;
363 /* Avoid warnings if one or more are NULL, to make this function
368 if (!E_IS_CONTACT (a) || !E_IS_CONTACT (b))
371 uid_a = e_contact_get_const (a, E_CONTACT_UID);
372 uid_b = e_contact_get_const (b, E_CONTACT_UID);
374 return g_strcmp0 (uid_a, uid_b) == 0;
378 add_contact_from_test_case_verify (EBookClient *book_client,
379 const gchar *case_name,
383 EContact *contact_orig;
384 EContact *contact_final;
386 GError *error = NULL;
388 vcard = new_vcard_from_test_case (case_name);
389 contact_orig = e_contact_new_from_vcard (vcard);
391 if (!e_book_client_add_contact_sync (book_client, contact_orig, &uid, NULL, &error)) {
392 report_error ("add contact sync", &error);
393 g_object_unref (contact_orig);
397 e_contact_set (contact_orig, E_CONTACT_UID, uid);
399 if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error)) {
400 report_error ("get contact sync", &error);
401 g_object_unref (contact_orig);
406 /* verify the contact was added "successfully" (not thorough) */
407 g_assert (contacts_are_equal_shallow (contact_orig, contact_final));
410 *contact = contact_final;
412 g_object_unref (contact_final);
413 g_object_unref (contact_orig);
420 add_contact_verify (EBookClient *book_client,
423 EContact *contact_final;
425 GError *error = NULL;
427 if (!e_book_client_add_contact_sync (book_client, contact, &uid, NULL, &error)) {
428 report_error ("add contact sync", &error);
432 e_contact_set (contact, E_CONTACT_UID, uid);
434 if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error)) {
435 report_error ("get contact sync", &error);
440 /* verify the contact was added "successfully" (not thorough) */
441 g_assert (contacts_are_equal_shallow (contact, contact_final));
443 g_object_unref (contact_final);