1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
6
#include <libebook/e-book.h>
8
#include "ebook-test-utils.h"
11
test_print (const gchar *format,
15
const gchar *debug_string;
16
static gboolean debug_set = FALSE;
17
static gboolean debug = FALSE;
20
debug_string = g_getenv ("EDS_TEST_DEBUG");
22
debug = (g_ascii_strtoll (debug_string, NULL, 10) >= 1);
28
va_start (args, format);
29
vprintf (format, args);
35
ebook_test_utils_callback_quit (gpointer user_data)
37
EBookTestClosure *closure = user_data;
38
g_main_loop_quit ((GMainLoop*) closure->user_data);
44
ebook_test_utils_new_vcard_from_test_case (const gchar *case_name)
52
case_filename = g_strdup_printf ("%s.vcf", case_name);
53
filename = g_build_filename (SRCDIR, EBOOK_TEST_UTILS_DATA_DIR, EBOOK_TEST_UTILS_VCARDS_DIR, case_filename, NULL);
54
file = g_file_new_for_path (filename);
55
if (!g_file_load_contents (file, NULL, &vcard, NULL, NULL, &error)) {
56
g_warning ("failed to read test contact file '%s': %s",
57
filename, error->message);
61
g_free (case_filename);
63
g_object_unref (file);
69
ebook_test_utils_book_add_contact_from_test_case_verify (EBook *book,
70
const gchar *case_name,
74
EContact *contact_orig;
75
EContact *contact_final;
78
vcard = ebook_test_utils_new_vcard_from_test_case (case_name);
79
contact_orig = e_contact_new_from_vcard (vcard);
80
uid = g_strdup (ebook_test_utils_book_add_contact (book, contact_orig));
81
contact_final = ebook_test_utils_book_get_contact (book, uid);
83
/* verify the contact was added "successfully" (not thorough) */
84
g_assert (ebook_test_utils_contacts_are_equal_shallow (contact_orig, contact_final));
87
*contact = g_object_ref (contact_final);
92
/* This is not a thorough comparison (which is difficult, assuming we give the
93
* back-ends leniency in implementation) and is best suited for simple tests */
95
ebook_test_utils_contacts_are_equal_shallow (EContact *a,
99
const gchar *uid_a, *uid_b;
101
/* Avoid warnings if one or more are NULL, to make this function
105
if (!E_IS_CONTACT (a) || !E_IS_CONTACT (b))
108
uid_a = e_contact_get_const (a, E_CONTACT_UID);
109
uid_b = e_contact_get_const (b, E_CONTACT_UID);
111
return g_strcmp0 (uid_a, uid_b) == 0;
115
ebook_test_utils_book_add_contact (EBook *book,
118
GError *error = NULL;
120
if (!e_book_add_contact (book, contact, &error)) {
123
uri = e_book_get_uri (book);
124
g_warning ("failed to add contact to addressbook: `%s': %s",
125
uri, error->message);
129
return e_contact_get_const (contact, E_CONTACT_UID);
133
add_contact_cb (EBook *book,
136
EBookTestClosure *closure)
139
g_warning ("failed to asynchronously add the contact '%s': "
140
"status %d (%s)", uid, error->code, error->message);
144
test_print ("successfully asynchronously added the contact "
147
(*closure->cb) (closure);
153
ebook_test_utils_book_async_add_contact (EBook *book,
155
GSourceFunc callback,
158
EBookTestClosure *closure;
160
closure = g_new0 (EBookTestClosure, 1);
161
closure->cb = callback;
162
closure->user_data = user_data;
163
if (!e_book_add_contact_async (book, contact,
164
(EBookIdAsyncCallback) add_contact_cb, closure)) {
165
g_warning ("failed to set up contact add");
171
ebook_test_utils_book_commit_contact (EBook *book,
174
GError *error = NULL;
176
if (!e_book_commit_contact (book, contact, &error)) {
180
uid = (const gchar *) e_contact_get_const (contact, E_CONTACT_UID);
181
uri = e_book_get_uri (book);
182
g_warning ("failed to commit changes to contact '%s' to addressbook: `%s': %s",
183
uid, uri, error->message);
189
commit_contact_cb (EBook *book,
191
EBookTestClosure *closure)
194
g_warning ("failed to asynchronously commit the contact: "
195
"status %d (%s)", error->code, error->message);
199
test_print ("successfully asynchronously committed the contact to the "
202
(*closure->cb) (closure);
208
ebook_test_utils_book_async_commit_contact (EBook *book,
210
GSourceFunc callback,
213
EBookTestClosure *closure;
215
closure = g_new0 (EBookTestClosure, 1);
216
closure->cb = callback;
217
closure->user_data = user_data;
218
if (!e_book_commit_contact_async (book, contact,
219
(EBookAsyncCallback) commit_contact_cb, closure)) {
220
g_warning ("failed to set up contact commit");
226
ebook_test_utils_book_get_contact (EBook *book,
229
EContact *contact = NULL;
230
GError *error = NULL;
232
if (!e_book_get_contact (book, uid, &contact, &error)) {
235
uri = e_book_get_uri (book);
236
g_warning ("failed to get contact '%s' in addressbook: `%s': "
237
"%s", uid, uri, error->message);
245
get_contact_cb (EBook *book,
248
EBookTestClosure *closure)
253
g_warning ("failed to asynchronously get the contact: "
254
"status %d (%s)", error->code, error->message);
258
uid = e_contact_get_const (contact, E_CONTACT_UID);
259
test_print ("successfully asynchronously retrieved the contact '%s'\n",
263
(*closure->cb) (closure);
269
ebook_test_utils_book_async_get_contact (EBook *book,
271
GSourceFunc callback,
274
EBookTestClosure *closure;
276
closure = g_new0 (EBookTestClosure, 1);
277
closure->cb = callback;
278
closure->user_data = user_data;
279
if (!e_book_get_contact_async (book, uid,
280
(EBookContactAsyncCallback) get_contact_cb,
282
g_warning ("failed to set up async getContact");
288
ebook_test_utils_book_get_required_fields (EBook *book)
290
GList *fields = NULL;
291
GError *error = NULL;
293
if (!e_book_get_required_fields (book, &fields, &error)) {
296
uri = e_book_get_uri (book);
297
g_warning ("failed to get required fields for addressbook "
298
"`%s': %s", uri, error->message);
306
get_required_fields_cb (EBook *book,
309
EBookTestClosure *closure)
312
g_warning ("failed to asynchronously get the required fields: "
313
"status %d (%s)", error->code, error->message);
317
closure->list = fields;
319
test_print ("successfully asynchronously retrieved the required fields\n");
322
(*closure->cb) (closure);
328
ebook_test_utils_book_async_get_required_fields (EBook *book,
329
GSourceFunc callback,
332
EBookTestClosure *closure;
334
closure = g_new0 (EBookTestClosure, 1);
335
closure->cb = callback;
336
closure->user_data = user_data;
337
if (!e_book_get_required_fields_async (book,
338
(EBookEListAsyncCallback) get_required_fields_cb,
340
g_warning ("failed to set up async getRequiredFields");
346
ebook_test_utils_book_get_static_capabilities (EBook *book)
348
GError *error = NULL;
351
if (!(caps = e_book_get_static_capabilities (book, &error))) {
354
uri = e_book_get_uri (book);
355
g_warning ("failed to get capabilities for addressbook: `%s': "
356
"%s", uri, error->message);
364
ebook_test_utils_book_get_supported_auth_methods (EBook *book)
366
GList *fields = NULL;
367
GError *error = NULL;
369
if (!e_book_get_supported_auth_methods (book, &fields, &error)) {
372
uri = e_book_get_uri (book);
373
g_warning ("failed to get supported auth methods for "
374
"addressbook `%s': %s", uri, error->message);
382
get_supported_auth_methods_cb (EBook *book,
385
EBookTestClosure *closure)
388
g_warning ("failed to asynchronously get the supported auth "
389
"methods: status %d (%s)", error->code, error->message);
393
closure->list = methods;
395
test_print ("successfully asynchronously retrieved the supported auth "
399
(*closure->cb) (closure);
405
ebook_test_utils_book_async_get_supported_auth_methods (EBook *book,
406
GSourceFunc callback,
409
EBookTestClosure *closure;
411
closure = g_new0 (EBookTestClosure, 1);
412
closure->cb = callback;
413
closure->user_data = user_data;
414
if (!e_book_get_supported_auth_methods_async (book,
415
(EBookEListAsyncCallback) get_supported_auth_methods_cb,
417
g_warning ("failed to set up async getSupportedAuthMethods");
423
ebook_test_utils_book_get_supported_fields (EBook *book)
425
GList *fields = NULL;
426
GError *error = NULL;
428
if (!e_book_get_supported_fields (book, &fields, &error)) {
431
uri = e_book_get_uri (book);
432
g_warning ("failed to get supported fields for addressbook "
433
"`%s': %s", uri, error->message);
441
get_supported_fields_cb (EBook *book,
444
EBookTestClosure *closure)
447
g_warning ("failed to asynchronously get the supported fields: "
448
"status %d (%s)", error->code, error->message);
452
closure->list = fields;
454
test_print ("successfully asynchronously retrieved the supported fields\n");
457
(*closure->cb) (closure);
463
ebook_test_utils_book_async_get_supported_fields (EBook *book,
464
GSourceFunc callback,
467
EBookTestClosure *closure;
469
closure = g_new0 (EBookTestClosure, 1);
470
closure->cb = callback;
471
closure->user_data = user_data;
472
if (!e_book_get_supported_fields_async (book,
473
(EBookEListAsyncCallback) get_supported_fields_cb,
475
g_warning ("failed to set up async getSupportedFields");
481
ebook_test_utils_book_remove_contact (EBook *book,
484
GError *error = NULL;
486
if (!e_book_remove_contact (book, uid, &error)) {
489
uri = e_book_get_uri (book);
490
g_warning ("failed to remove contact '%s' from addressbook: `%s': %s",
491
uid, uri, error->message);
497
remove_contact_cb (EBook *book,
499
EBookTestClosure *closure)
502
g_warning ("failed to asynchronously remove the contact: "
503
"status %d (%s)", error->code, error->message);
507
test_print ("successfully asynchronously removed the contact\n");
510
(*closure->cb) (closure);
516
ebook_test_utils_book_async_remove_contact (EBook *book,
518
GSourceFunc callback,
521
EBookTestClosure *closure;
523
closure = g_new0 (EBookTestClosure, 1);
524
closure->cb = callback;
525
closure->user_data = user_data;
526
if (!e_book_remove_contact_async (book, contact,
527
(EBookAsyncCallback) remove_contact_cb,
529
g_warning ("failed to set up async removeContacts (for a single contact)");
535
remove_contact_by_id_cb (EBook *book,
537
EBookTestClosure *closure)
540
g_warning ("failed to asynchronously remove the contact by id: "
541
"status %d (%s)", error->code, error->message);
545
test_print ("successfully asynchronously removed the contact by id\n");
548
(*closure->cb) (closure);
554
ebook_test_utils_book_async_remove_contact_by_id (EBook *book,
556
GSourceFunc callback,
559
EBookTestClosure *closure;
561
closure = g_new0 (EBookTestClosure, 1);
562
closure->cb = callback;
563
closure->user_data = user_data;
564
if (!e_book_remove_contact_by_id_async (book, uid,
565
(EBookAsyncCallback) remove_contact_by_id_cb,
567
g_warning ("failed to set up async removeContacts (by id)");
573
ebook_test_utils_book_remove_contacts (EBook *book,
576
GError *error = NULL;
578
if (!e_book_remove_contacts (book, ids, &error)) {
581
uri = e_book_get_uri (book);
582
g_warning ("failed to remove contacts from addressbook: `%s': %s",
583
uri, error->message);
589
remove_contacts_cb (EBook *book,
591
EBookTestClosure *closure)
594
g_warning ("failed to asynchronously remove the contacts: "
595
"status %d (%s)", error->code, error->message);
599
test_print ("successfully asynchronously removed the contacts\n");
602
(*closure->cb) (closure);
608
ebook_test_utils_book_async_remove_contacts (EBook *book,
610
GSourceFunc callback,
613
EBookTestClosure *closure;
615
closure = g_new0 (EBookTestClosure, 1);
616
closure->cb = callback;
617
closure->user_data = user_data;
618
if (!e_book_remove_contacts_async (book, uids,
619
(EBookAsyncCallback) remove_contacts_cb,
621
g_warning ("failed to set up async removeContacts");
627
ebook_test_utils_book_new_from_uri (const gchar *uri)
630
GError *error = NULL;
632
test_print ("loading addressbook\n");
633
book = e_book_new_from_uri (uri, &error);
635
g_error ("failed to create addressbook: `%s': %s", uri,
643
ebook_test_utils_book_new_temp (gchar **uri)
646
gchar *file_template;
649
file_template = g_build_filename (g_get_tmp_dir (),
650
"ebook-test-XXXXXX/", NULL);
651
g_mkstemp (file_template);
653
uri_result = g_strconcat ("local:", file_template, NULL);
655
g_warning ("failed to convert %s to a 'local:' URI", file_template);
658
g_free (file_template);
660
book = ebook_test_utils_book_new_from_uri (uri_result);
663
*uri = g_strdup (uri_result);
671
ebook_test_utils_book_open (EBook *book,
672
gboolean only_if_exists)
674
GError *error = NULL;
676
if (!e_book_open (book, only_if_exists, &error)) {
679
uri = e_book_get_uri (book);
681
g_warning ("failed to open addressbook: `%s': %s", uri,
688
ebook_test_utils_book_remove (EBook *book)
690
GError *error = NULL;
692
if (!e_book_remove (book, &error)) {
693
g_warning ("failed to remove book; %s\n", error->message);
696
test_print ("successfully removed the temporary addressbook\n");
698
g_object_unref (book);
702
remove_cb (EBook *book, const GError *error, EBookTestClosure *closure)
705
g_warning ("failed to asynchronously remove the book: "
706
"status %d (%s)", error->code, error->message);
710
test_print ("successfully asynchronously removed the temporary "
713
(*closure->cb) (closure);
717
ebook_test_utils_book_async_remove (EBook *book,
718
GSourceFunc callback,
721
EBookTestClosure *closure;
723
closure = g_new0 (EBookTestClosure, 1);
724
closure->cb = callback;
725
closure->user_data = user_data;
726
if (!e_book_remove_async (book, (EBookAsyncCallback) remove_cb, closure)) {
727
g_warning ("failed to set up book removal");
733
ebook_test_utils_book_get_book_view (EBook *book,
737
GError *error = NULL;
739
if (!e_book_get_book_view (book, query, NULL, -1, view, &error)) {
742
uri = e_book_get_uri (book);
744
g_warning ("failed to get view for addressbook: `%s': %s", uri,
751
get_book_view_cb (EBook *book,
754
EBookTestClosure *closure)
757
g_warning ("failed to asynchronously get book view for the "
758
"book: status %d (%s)", error->code, error->message);
762
closure->view = view;
764
test_print ("successfully asynchronously retrieved the book view\n");
766
(*closure->cb) (closure);
770
ebook_test_utils_book_async_get_book_view (EBook *book,
772
GSourceFunc callback,
775
EBookTestClosure *closure;
777
closure = g_new0 (EBookTestClosure, 1);
778
closure->cb = callback;
779
closure->user_data = user_data;
780
if (!e_book_get_book_view_async (book, query, NULL, -1, (EBookBookViewAsyncCallback) get_book_view_cb, closure)) {
781
g_warning ("failed to set up book view retrieval");