Add a test for the e-book 'removeContacts' method.
authorTravis Reitter <treitter@gmail.com>
Wed, 9 Dec 2009 17:45:58 +0000 (09:45 -0800)
committerTravis Reitter <treitter@gmail.com>
Fri, 15 Jan 2010 21:29:57 +0000 (13:29 -0800)
addressbook/tests/ebook/Makefile.am
addressbook/tests/ebook/data/vcards/name-only.vcf [new file with mode: 0644]
addressbook/tests/ebook/ebook-test-utils.c
addressbook/tests/ebook/ebook-test-utils.h
addressbook/tests/ebook/test-ebook-commit-contact.c [new file with mode: 0644]

index 48b29a8..171b5f3 100644 (file)
@@ -28,6 +28,7 @@ noinst_PROGRAMS = \
        test-ebook                           \
        test-ebook-async                     \
        test-ebook-add-contact               \
+       test-ebook-commit-contact            \
        test-ebook-get-contact               \
        test-ebook-remove                    \
        test-ebook-remove-contact            \
@@ -59,6 +60,8 @@ test_ebook_async_LDADD=$(TEST_LIBS)
 test_ebook_async_CPPFLAGS=$(TEST_CPPFLAGS)
 test_ebook_add_contact_LDADD=$(TEST_LIBS)
 test_ebook_add_contact_CPPFLAGS=$(TEST_CPPFLAGS)
+test_ebook_commit_contact_LDADD=$(TEST_LIBS)
+test_ebook_commit_contact_CPPFLAGS=$(TEST_CPPFLAGS)
 test_ebook_get_contact_LDADD=$(TEST_LIBS)
 test_ebook_get_contact_CPPFLAGS=$(TEST_CPPFLAGS)
 test_ebook_remove_LDADD=$(TEST_LIBS)
diff --git a/addressbook/tests/ebook/data/vcards/name-only.vcf b/addressbook/tests/ebook/data/vcards/name-only.vcf
new file mode 100644 (file)
index 0000000..0566d7c
--- /dev/null
@@ -0,0 +1,3 @@
+BEGIN:VCARD
+FN:John Doe
+END:VCARD
index 5f37989..8efb722 100644 (file)
@@ -134,6 +134,61 @@ ebook_test_utils_book_async_add_contact (EBook       *book,
         }
 }
 
+void
+ebook_test_utils_book_commit_contact (EBook    *book,
+                                      EContact *contact)
+{
+        GError *error = NULL;
+
+        if (!e_book_commit_contact (book, contact, &error)) {
+                const char *uid;
+                const char *uri;
+
+                uid = (const char*) e_contact_get_const (contact, E_CONTACT_UID);
+                uri = e_book_get_uri (book);
+                g_warning ("failed to commit changes to contact '%s' to addressbook: `%s': %s",
+                                uid, uri, error->message);
+                exit(1);
+        }
+}
+
+static void
+commit_contact_cb (EBook            *book,
+                   EBookStatus       status,
+                   EBookTestClosure *closure)
+{
+        if (status != E_BOOK_ERROR_OK) {
+                g_warning ("failed to asynchronously commit the contact: "
+                                "status %d", status);
+                exit (1);
+        }
+
+        g_print ("successfully asynchronously committed the contact to the "
+                        "addressbook\n");
+        if (closure) {
+                (*closure->cb) (closure);
+                g_free (closure);
+        }
+}
+
+void
+ebook_test_utils_book_async_commit_contact (EBook       *book,
+                                            EContact    *contact,
+                                            GSourceFunc  callback,
+                                            gpointer     user_data)
+{
+        EBookTestClosure *closure;
+
+        closure = g_new0 (EBookTestClosure, 1);
+        closure->cb = callback;
+        closure->user_data = user_data;
+        if (e_book_async_commit_contact (book, contact,
+                                (EBookCallback) commit_contact_cb, closure)) {
+                g_warning ("failed to set up contact commit");
+                exit(1);
+        }
+}
+
 EContact*
 ebook_test_utils_book_get_contact (EBook      *book,
                                    const char *uid)
index 497109e..74dd96f 100644 (file)
@@ -57,6 +57,15 @@ ebook_test_utils_book_async_add_contact (EBook       *book,
                                          GSourceFunc  callback,
                                          gpointer     user_data);
 
+void
+ebook_test_utils_book_commit_contact (EBook    *book,
+                                      EContact *contact);
+void
+ebook_test_utils_book_async_commit_contact (EBook       *book,
+                                            EContact    *contact,
+                                            GSourceFunc  callback,
+                                            gpointer     user_data);
+
 EContact*
 ebook_test_utils_book_get_contact (EBook      *book,
                                    const char *uid);
diff --git a/addressbook/tests/ebook/test-ebook-commit-contact.c b/addressbook/tests/ebook/test-ebook-commit-contact.c
new file mode 100644 (file)
index 0000000..fe181e8
--- /dev/null
@@ -0,0 +1,97 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+#include <stdlib.h>
+#include <libebook/e-book.h>
+
+#include "ebook-test-utils.h"
+
+#define EMAIL_ADD "foo@bar.com"
+
+static EBook *book;
+static char *uid;
+
+static void
+verify_precommit_and_prepare_contact (EContact *contact)
+{
+       EVCardAttribute *attr;
+
+       /* ensure there is no email address to begin with, then add one */
+       g_assert (!e_vcard_get_attribute (E_VCARD (contact), EVC_EMAIL));
+       attr = e_vcard_attribute_new (NULL, EVC_EMAIL);
+       e_vcard_add_attribute_with_value (E_VCARD (contact), attr, EMAIL_ADD);
+}
+
+static void
+verify_commit (EContact *contact)
+{
+       EVCardAttribute *attr;
+       char *email_value;
+
+       g_assert ((attr = e_vcard_get_attribute (E_VCARD (contact), EVC_EMAIL)));
+       g_assert (e_vcard_attribute_is_single_valued (attr));
+       email_value = e_vcard_attribute_get_value (attr);
+       g_assert (!g_strcmp0 (email_value, EMAIL_ADD));
+}
+
+static gboolean
+commit_verify_cb (EBookTestClosure *closure)
+{
+       EContact *contact;
+
+       contact = ebook_test_utils_book_get_contact (book, uid);
+       verify_commit (contact);
+
+       g_main_loop_quit ((GMainLoop*) (closure->user_data));
+
+       return FALSE;
+}
+
+gint
+main (gint argc, gchar **argv)
+{
+       GMainLoop *loop;
+       EContact *contact;
+
+       g_type_init ();
+
+       /*
+        * Setup
+        */
+       book = ebook_test_utils_book_new_temp (NULL);
+       ebook_test_utils_book_open (book, FALSE);
+
+       /*
+        * Sync version
+        */
+       uid = ebook_test_utils_book_add_contact_from_test_case_verify (book, "name-only", &contact);
+       verify_precommit_and_prepare_contact (contact);
+       ebook_test_utils_book_commit_contact (book, contact);
+
+       verify_commit (contact);
+
+       g_print ("successfully committed changes to contact contact '%s'\n", uid);
+       g_object_unref (contact);
+       g_free (uid);
+
+       ebook_test_utils_book_remove (book);
+
+       /*
+        * Async version
+        */
+       book = ebook_test_utils_book_new_temp (NULL);
+       ebook_test_utils_book_open (book, FALSE);
+       uid = ebook_test_utils_book_add_contact_from_test_case_verify (book, "name-only", &contact);
+
+       verify_precommit_and_prepare_contact (contact);
+
+       loop = g_main_loop_new (NULL, TRUE);
+       ebook_test_utils_book_async_commit_contact (book, contact,
+                       (GSourceFunc) commit_verify_cb, loop);
+
+       g_main_loop_run (loop);
+
+       g_free (uid);
+       ebook_test_utils_book_remove (book);
+
+       return 0;
+}