[evolution-data-server/treitter-test-suites] Add a test for the e-book 'removeContacts' method.



commit 4828de69410f83de52b8a109559c97ab5a4c6c72
Author: Travis Reitter <treitter gmail com>
Date:   Tue Dec 8 12:48:26 2009 -0800

    Add a test for the e-book 'removeContacts' method.

 addressbook/tests/ebook/Makefile.am                |    9 +
 addressbook/tests/ebook/data/vcards/simple-1.vcf   |    4 +
 addressbook/tests/ebook/data/vcards/simple-2.vcf   |    4 +
 addressbook/tests/ebook/ebook-test-utils.c         |  192 ++++++++++++++++++++
 addressbook/tests/ebook/ebook-test-utils.h         |   32 ++++
 addressbook/tests/ebook/test-ebook-add-contact.c   |   23 +--
 addressbook/tests/ebook/test-ebook-get-contact.c   |   25 +---
 .../tests/ebook/test-ebook-remove-contact-by-id.c  |   40 ++++
 .../tests/ebook/test-ebook-remove-contact.c        |   59 ++++++
 .../tests/ebook/test-ebook-remove-contacts.c       |   71 +++++++
 10 files changed, 414 insertions(+), 45 deletions(-)
---
diff --git a/addressbook/tests/ebook/Makefile.am b/addressbook/tests/ebook/Makefile.am
index cd7c7f2..48b29a8 100644
--- a/addressbook/tests/ebook/Makefile.am
+++ b/addressbook/tests/ebook/Makefile.am
@@ -30,6 +30,9 @@ noinst_PROGRAMS = \
 	test-ebook-add-contact               \
 	test-ebook-get-contact               \
 	test-ebook-remove                    \
+	test-ebook-remove-contact            \
+	test-ebook-remove-contact-by-id      \
+	test-ebook-remove-contacts           \
 	test-ebook-view                      \
 	test-nonexistent-id                  \
 	test-photo                           \
@@ -60,6 +63,12 @@ test_ebook_get_contact_LDADD=$(TEST_LIBS)
 test_ebook_get_contact_CPPFLAGS=$(TEST_CPPFLAGS)
 test_ebook_remove_LDADD=$(TEST_LIBS)
 test_ebook_remove_CPPFLAGS=$(TEST_CPPFLAGS)
+test_ebook_remove_contact_LDADD=$(TEST_LIBS)
+test_ebook_remove_contact_CPPFLAGS=$(TEST_CPPFLAGS)
+test_ebook_remove_contact_by_id_LDADD=$(TEST_LIBS)
+test_ebook_remove_contact_by_id_CPPFLAGS=$(TEST_CPPFLAGS)
+test_ebook_remove_contacts_LDADD=$(TEST_LIBS)
+test_ebook_remove_contacts_CPPFLAGS=$(TEST_CPPFLAGS)
 test_ebook_view_LDADD=$(TEST_LIBS)
 test_ebook_view_CPPFLAGS=$(TEST_CPPFLAGS)
 test_changes_LDADD=$(TEST_LIBS)
diff --git a/addressbook/tests/ebook/data/vcards/simple-1.vcf b/addressbook/tests/ebook/data/vcards/simple-1.vcf
new file mode 100644
index 0000000..ea6383c
--- /dev/null
+++ b/addressbook/tests/ebook/data/vcards/simple-1.vcf
@@ -0,0 +1,4 @@
+BEGIN:VCARD
+FN:Foo Bar
+EMAIL;INTERNET:foo bar example org
+END:VCARD
diff --git a/addressbook/tests/ebook/data/vcards/simple-2.vcf b/addressbook/tests/ebook/data/vcards/simple-2.vcf
new file mode 100644
index 0000000..4fa3707
--- /dev/null
+++ b/addressbook/tests/ebook/data/vcards/simple-2.vcf
@@ -0,0 +1,4 @@
+BEGIN:VCARD
+FN:Baz Qux
+EMAIL;INTERNET:baz qux example com
+END:VCARD
diff --git a/addressbook/tests/ebook/ebook-test-utils.c b/addressbook/tests/ebook/ebook-test-utils.c
index 649ee11..5f37989 100644
--- a/addressbook/tests/ebook/ebook-test-utils.c
+++ b/addressbook/tests/ebook/ebook-test-utils.c
@@ -32,6 +32,52 @@ ebook_test_utils_new_vcard_from_test_case (const char *case_name)
         return vcard;
 }
 
+char*
+ebook_test_utils_book_add_contact_from_test_case_verify (EBook       *book,
+                                                         const char  *case_name,
+                                                         EContact   **contact)
+{
+        char *vcard;
+        EContact *contact_orig;
+        EContact *contact_final;
+        char *uid;
+
+        vcard = ebook_test_utils_new_vcard_from_test_case (case_name);
+        contact_orig = e_contact_new_from_vcard (vcard);
+        uid = g_strdup (ebook_test_utils_book_add_contact (book, contact_orig));
+        contact_final = ebook_test_utils_book_get_contact (book, uid);
+
+        /* verify the contact was added "successfully" (not thorough) */
+        g_assert (ebook_test_utils_contacts_are_equal_shallow (contact_orig, contact_final));
+
+        if (contact)
+                *contact = g_object_ref (contact_final);
+
+        return uid;
+}
+
+/* This is not a thorough comparison (which is difficult, assuming we give the
+ * back-ends leniency in implementation) and is best suited for simple tests */
+gboolean
+ebook_test_utils_contacts_are_equal_shallow (EContact *a,
+                                             EContact *b)
+
+{
+        const char *uid_a, *uid_b;
+
+        /* Avoid warnings if one or more are NULL, to make this function
+         * "NULL-friendly" */
+        if (!a && !b)
+                return TRUE;
+        if (!E_IS_CONTACT (a) || !E_IS_CONTACT (b))
+                return FALSE;
+
+        uid_a = e_contact_get_const (a, E_CONTACT_UID);
+        uid_b = e_contact_get_const (b, E_CONTACT_UID);
+
+        return g_strcmp0 (uid_a, uid_b) == 0;
+}
+
 const char*
 ebook_test_utils_book_add_contact (EBook    *book,
                                    EContact *contact)
@@ -150,6 +196,152 @@ ebook_test_utils_book_async_get_contact (EBook       *book,
         }
 }
 
+void
+ebook_test_utils_book_remove_contact (EBook      *book,
+                                      const char *uid)
+{
+        GError *error = NULL;
+
+        if (!e_book_remove_contact (book, uid, &error)) {
+                const char *uri;
+
+                uri = e_book_get_uri (book);
+                g_warning ("failed to remove contact '%s' from addressbook: `%s': %s",
+                                uid, uri, error->message);
+                exit(1);
+        }
+}
+
+static void
+remove_contact_cb (EBook            *book,
+                   EBookStatus       status,
+                   EBookTestClosure *closure)
+{
+        if (status != E_BOOK_ERROR_OK) {
+                g_warning ("failed to asynchronously remove the contact: "
+                                "status %d", status);
+                exit (1);
+        }
+
+        g_print ("successfully asynchronously removed the contact\n");
+
+        if (closure) {
+                (*closure->cb) (closure->user_data);
+                g_free (closure);
+        }
+}
+
+void
+ebook_test_utils_book_async_remove_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_remove_contact (book, contact,
+                                (EBookCallback) remove_contact_cb,
+                                closure)) {
+                g_warning ("failed to set up async removeContacts (for a single contact)");
+                exit(1);
+        }
+}
+
+static void
+remove_contact_by_id_cb (EBook            *book,
+                         EBookStatus       status,
+                         EBookTestClosure *closure)
+{
+        if (status != E_BOOK_ERROR_OK) {
+                g_warning ("failed to asynchronously remove the contact by id: "
+                                "status %d", status);
+                exit (1);
+        }
+
+        g_print ("successfully asynchronously removed the contact by id\n");
+
+        if (closure) {
+                (*closure->cb) (closure->user_data);
+                g_free (closure);
+        }
+}
+
+void
+ebook_test_utils_book_async_remove_contact_by_id (EBook       *book,
+                                                  const char  *uid,
+                                                  GSourceFunc  callback,
+                                                  gpointer     user_data)
+{
+        EBookTestClosure *closure;
+
+        closure = g_new0 (EBookTestClosure, 1);
+        closure->cb = callback;
+        closure->user_data = user_data;
+        if (e_book_async_remove_contact_by_id (book, uid,
+                                (EBookCallback) remove_contact_by_id_cb,
+                                closure)) {
+                g_warning ("failed to set up async removeContacts (by id)");
+                exit(1);
+        }
+}
+
+void
+ebook_test_utils_book_remove_contacts (EBook *book,
+                                       GList *ids)
+{
+        GError *error = NULL;
+
+        if (!e_book_remove_contacts (book, ids, &error)) {
+                const char *uri;
+
+                uri = e_book_get_uri (book);
+                g_warning ("failed to remove contacts from addressbook: `%s': %s",
+                                uri, error->message);
+                exit(1);
+        }
+}
+
+static void
+remove_contacts_cb (EBook            *book,
+                    EBookStatus       status,
+                    EBookTestClosure *closure)
+{
+        if (status != E_BOOK_ERROR_OK) {
+                g_warning ("failed to asynchronously remove the contacts: "
+                                "status %d", status);
+                exit (1);
+        }
+
+        g_print ("successfully asynchronously removed the contacts\n");
+
+        if (closure) {
+                (*closure->cb) (closure->user_data);
+                g_free (closure);
+        }
+}
+
+void
+ebook_test_utils_book_async_remove_contacts (EBook       *book,
+                                             GList       *uids,
+                                             GSourceFunc  callback,
+                                             gpointer     user_data)
+{
+        EBookTestClosure *closure;
+
+        closure = g_new0 (EBookTestClosure, 1);
+        closure->cb = callback;
+        closure->user_data = user_data;
+        if (e_book_async_remove_contacts (book, uids,
+                                (EBookCallback) remove_contacts_cb,
+                                closure)) {
+                g_warning ("failed to set up async removeContacts");
+                exit(1);
+        }
+}
+
 EBook*
 ebook_test_utils_book_new_temp (char **uri)
 {
diff --git a/addressbook/tests/ebook/ebook-test-utils.h b/addressbook/tests/ebook/ebook-test-utils.h
index 6a0f58f..497109e 100644
--- a/addressbook/tests/ebook/ebook-test-utils.h
+++ b/addressbook/tests/ebook/ebook-test-utils.h
@@ -36,6 +36,15 @@ typedef struct {
 char*
 ebook_test_utils_new_vcard_from_test_case (const char *case_name);
 
+char*
+ebook_test_utils_book_add_contact_from_test_case_verify (EBook       *book,
+                                                         const char  *case_name,
+							 EContact   **contact);
+
+gboolean
+ebook_test_utils_contacts_are_equal_shallow (EContact *a,
+                                             EContact *b);
+
 EBook*
 ebook_test_utils_book_new_temp (char **uri);
 
@@ -58,6 +67,29 @@ ebook_test_utils_book_async_get_contact (EBook       *book,
                                          gpointer     user_data);
 
 void
+ebook_test_utils_book_remove_contact (EBook      *book,
+                                      const char *uid);
+void
+ebook_test_utils_book_async_remove_contact (EBook       *book,
+					    EContact    *contact,
+					    GSourceFunc  callback,
+					    gpointer     user_data);
+void
+ebook_test_utils_book_async_remove_contact_by_id (EBook       *book,
+                                                  const char  *uid,
+                                                  GSourceFunc  callback,
+                                                  gpointer     user_data);
+
+void
+ebook_test_utils_book_remove_contacts (EBook *book,
+                                       GList *ids);
+void
+ebook_test_utils_book_async_remove_contacts (EBook       *book,
+                                             GList       *uids,
+                                             GSourceFunc  callback,
+                                             gpointer     user_data);
+
+void
 ebook_test_utils_book_open (EBook    *book,
                             gboolean  only_if_exists);
 
diff --git a/addressbook/tests/ebook/test-ebook-add-contact.c b/addressbook/tests/ebook/test-ebook-add-contact.c
index 4297c93..b352057 100644
--- a/addressbook/tests/ebook/test-ebook-add-contact.c
+++ b/addressbook/tests/ebook/test-ebook-add-contact.c
@@ -14,7 +14,6 @@ main (gint argc, gchar **argv)
 	EContact *contact;
 	EContact *contact_final;
 	char *uid;
-	const char *contact_final_uid;
 
 	g_type_init ();
 
@@ -24,31 +23,12 @@ main (gint argc, gchar **argv)
 	book = ebook_test_utils_book_new_temp (NULL);
 	ebook_test_utils_book_open (book, FALSE);
 
-	vcard = ebook_test_utils_new_vcard_from_test_case ("simple-1");
-	contact = e_contact_new_from_vcard (vcard);
-	uid = g_strdup (ebook_test_utils_book_add_contact (book, contact));
-
 	/*
 	 * Sync version
 	 */
-	contact_final = ebook_test_utils_book_get_contact (book, uid);
-	contact_final_uid = e_contact_get_const (contact_final, E_CONTACT_UID);
-
-	/* This is not a thorough comparison (which is difficult, assuming we
-	 * give the back-ends leniency in implementation), since that's better
-	 * suited to more advanced tests */
-	if (g_strcmp0 (uid, contact_final_uid)) {
-		const char *uri;
-
-		uri = e_book_get_uri (book);
-
-		g_warning ("retrieved contact uid '%s' does not match added "
-				"contact uid '%s'", contact_final_uid, uid);
-		exit(1);
-	}
+	uid = ebook_test_utils_book_add_contact_from_test_case_verify (book, "simple-1", &contact_final);
 
 	g_print ("successfully added and retrieved contact '%s'\n", uid);
-	g_object_unref (contact);
 	g_object_unref (contact_final);
 
 	ebook_test_utils_book_remove (book);
@@ -58,6 +38,7 @@ main (gint argc, gchar **argv)
 	 */
 	book = ebook_test_utils_book_new_temp (NULL);
 	ebook_test_utils_book_open (book, FALSE);
+	vcard = ebook_test_utils_new_vcard_from_test_case ("simple-1");
 	contact = e_contact_new_from_vcard (vcard);
 
 	loop = g_main_loop_new (NULL, TRUE);
diff --git a/addressbook/tests/ebook/test-ebook-get-contact.c b/addressbook/tests/ebook/test-ebook-get-contact.c
index ff3dcda..6bcce72 100644
--- a/addressbook/tests/ebook/test-ebook-get-contact.c
+++ b/addressbook/tests/ebook/test-ebook-get-contact.c
@@ -10,11 +10,8 @@ main (gint argc, gchar **argv)
 {
 	EBook *book;
 	GMainLoop *loop;
-	char *vcard;
-	EContact *contact;
 	EContact *contact_final;
 	const char *uid;
-	const char *contact_final_uid;
 
 	g_type_init ();
 
@@ -24,31 +21,12 @@ main (gint argc, gchar **argv)
 	book = ebook_test_utils_book_new_temp (NULL);
 	ebook_test_utils_book_open (book, FALSE);
 
-	vcard = ebook_test_utils_new_vcard_from_test_case ("simple-1");
-	contact = e_contact_new_from_vcard (vcard);
-	uid = ebook_test_utils_book_add_contact (book, contact);
-
 	/*
 	 * Sync version
 	 */
-	contact_final = ebook_test_utils_book_get_contact (book, uid);
-	contact_final_uid = e_contact_get_const (contact_final, E_CONTACT_UID);
-
-	/* This is not a thorough comparison (which is difficult, assuming we
-	 * give the back-ends leniency in implementation), since that's better
-	 * suited to more advanced tests */
-	if (g_strcmp0 (uid, contact_final_uid)) {
-		const char *uri;
-
-		uri = e_book_get_uri (book);
-
-		g_warning ("retrieved contact uid '%s' does not match added "
-				"contact uid '%s'", contact_final_uid, uid);
-		exit(1);
-	}
+	uid = ebook_test_utils_book_add_contact_from_test_case_verify (book, "simple-1", &contact_final);
 
 	g_print ("successfully added and retrieved contact '%s'\n", uid);
-	g_object_unref (contact);
 	g_object_unref (contact_final);
 
 	/*
@@ -60,7 +38,6 @@ main (gint argc, gchar **argv)
 	g_main_loop_run (loop);
 
 	ebook_test_utils_book_remove (book);
-	g_free (vcard);
 
 	return 0;
 }
diff --git a/addressbook/tests/ebook/test-ebook-remove-contact-by-id.c b/addressbook/tests/ebook/test-ebook-remove-contact-by-id.c
new file mode 100644
index 0000000..8b99cf0
--- /dev/null
+++ b/addressbook/tests/ebook/test-ebook-remove-contact-by-id.c
@@ -0,0 +1,40 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+#include <libebook/e-book.h>
+
+#include "ebook-test-utils.h"
+
+gint 
+main (gint argc, gchar **argv)
+{       
+        EBook *book;
+        GMainLoop *loop;
+        char *uid; 
+        
+        g_type_init ();
+        
+        /*
+         * Setup
+         */
+        book = ebook_test_utils_book_new_temp (NULL);
+        ebook_test_utils_book_open (book, FALSE);
+
+        /*
+         * 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, "simple-1", NULL);
+
+        loop = g_main_loop_new (NULL, TRUE);
+        ebook_test_utils_book_async_remove_contact_by_id (book, uid,
+                        (GSourceFunc) g_main_loop_quit, loop);
+        
+        g_main_loop_run (loop);
+        
+        ebook_test_utils_book_remove (book);
+	g_free (uid);
+        
+        return 0;
+}
diff --git a/addressbook/tests/ebook/test-ebook-remove-contact.c b/addressbook/tests/ebook/test-ebook-remove-contact.c
new file mode 100644
index 0000000..b307280
--- /dev/null
+++ b/addressbook/tests/ebook/test-ebook-remove-contact.c
@@ -0,0 +1,59 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+#include <libebook/e-book.h>
+
+#include "ebook-test-utils.h"
+
+gint
+main (gint argc, gchar **argv)
+{
+        EBook *book;
+        GMainLoop *loop;
+        EContact *contact_final;
+        char *uid;
+
+        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, "simple-1", NULL);
+        ebook_test_utils_book_remove_contact (book, uid);
+	contact_final = NULL;
+	e_book_get_contact (book, uid, &contact_final, NULL);
+
+        g_assert (contact_final == NULL);
+        g_print ("successfully added and removed contact '%s'\n", uid);
+
+        ebook_test_utils_book_remove (book);
+        g_free (uid);
+
+        /*
+         * Async version
+         */
+        book = ebook_test_utils_book_new_temp (NULL);
+        ebook_test_utils_book_open (book, FALSE);
+
+	contact_final = NULL;
+	/* contact_final has 2 refs by the end of this */
+	uid = ebook_test_utils_book_add_contact_from_test_case_verify (book, "simple-1", &contact_final);
+
+        loop = g_main_loop_new (NULL, TRUE);
+	/* contact_final is unref'd by e_book_remove_contact() here */
+        ebook_test_utils_book_async_remove_contact (book, contact_final,
+                        (GSourceFunc) g_main_loop_quit, loop);
+
+        g_main_loop_run (loop);
+
+        ebook_test_utils_book_remove (book);
+	g_object_unref (contact_final);
+	g_free (uid);
+
+        return 0;
+}
diff --git a/addressbook/tests/ebook/test-ebook-remove-contacts.c b/addressbook/tests/ebook/test-ebook-remove-contacts.c
new file mode 100644
index 0000000..369050a
--- /dev/null
+++ b/addressbook/tests/ebook/test-ebook-remove-contacts.c
@@ -0,0 +1,71 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+#include <libebook/e-book.h>
+
+#include "ebook-test-utils.h"
+
+gint 
+main (gint argc, gchar **argv)
+{       
+        EBook *book;
+        GMainLoop *loop;
+        EContact *contact_final;
+        char *uid_1, *uid_2; 
+	GList *uids = NULL;
+        
+        g_type_init ();
+        
+        /*
+         * Setup
+         */
+        book = ebook_test_utils_book_new_temp (NULL);
+        ebook_test_utils_book_open (book, FALSE);
+
+        /*
+         * Sync version
+         */
+	uid_1 = ebook_test_utils_book_add_contact_from_test_case_verify (book, "simple-1", NULL);
+	uid_2 = ebook_test_utils_book_add_contact_from_test_case_verify (book, "simple-2", NULL);
+	uids = g_list_prepend (uids, uid_1);
+	uids = g_list_prepend (uids, uid_2);
+        ebook_test_utils_book_remove_contacts (book, uids);
+
+	contact_final = NULL;
+	e_book_get_contact (book, uid_1, &contact_final, NULL);
+        g_assert (contact_final == NULL);
+
+	e_book_get_contact (book, uid_2, &contact_final, NULL);
+        g_assert (contact_final == NULL);
+
+        g_print ("successfully added and removed contacts\n");
+
+        ebook_test_utils_book_remove (book);
+        g_free (uid_1);
+        g_free (uid_2);
+	g_list_free (uids);
+        
+        /*
+         * Async version
+         */
+        book = ebook_test_utils_book_new_temp (NULL);
+        ebook_test_utils_book_open (book, FALSE);
+
+	uid_1 = ebook_test_utils_book_add_contact_from_test_case_verify (book, "simple-1", NULL);
+	uid_2 = ebook_test_utils_book_add_contact_from_test_case_verify (book, "simple-2", NULL);
+	uids = NULL;
+	uids = g_list_prepend (uids, uid_1);
+	uids = g_list_prepend (uids, uid_2);
+
+        loop = g_main_loop_new (NULL, TRUE);
+        ebook_test_utils_book_async_remove_contacts (book, uids,
+                        (GSourceFunc) g_main_loop_quit, loop);
+        
+        g_main_loop_run (loop);
+
+        ebook_test_utils_book_remove (book);
+        g_free (uid_1);
+        g_free (uid_2);
+	g_list_free (uids);
+
+        return 0;
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]