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



commit 31c6bba1b1a13e253c9425fe2d827b5622b14397
Author: Travis Reitter <treitter gmail com>
Date:   Thu Dec 3 17:44:26 2009 -0800

    Add a test program for the e-book 'addContact' method.

 addressbook/tests/ebook/Makefile.am              |    3 +
 addressbook/tests/ebook/ebook-test-utils.c       |   38 ++++++++++++
 addressbook/tests/ebook/ebook-test-utils.h       |    5 ++
 addressbook/tests/ebook/test-ebook-add-contact.c |   71 ++++++++++++++++++++++
 4 files changed, 117 insertions(+), 0 deletions(-)
---
diff --git a/addressbook/tests/ebook/Makefile.am b/addressbook/tests/ebook/Makefile.am
index 159e71e..cd7c7f2 100644
--- a/addressbook/tests/ebook/Makefile.am
+++ b/addressbook/tests/ebook/Makefile.am
@@ -27,6 +27,7 @@ noinst_PROGRAMS = \
 	test-date                            \
 	test-ebook                           \
 	test-ebook-async                     \
+	test-ebook-add-contact               \
 	test-ebook-get-contact               \
 	test-ebook-remove                    \
 	test-ebook-view                      \
@@ -53,6 +54,8 @@ test_ebook_LDADD=$(TEST_LIBS)
 test_ebook_CPPFLAGS=$(TEST_CPPFLAGS)
 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_get_contact_LDADD=$(TEST_LIBS)
 test_ebook_get_contact_CPPFLAGS=$(TEST_CPPFLAGS)
 test_ebook_remove_LDADD=$(TEST_LIBS)
diff --git a/addressbook/tests/ebook/ebook-test-utils.c b/addressbook/tests/ebook/ebook-test-utils.c
index 1497760..5c085ae 100644
--- a/addressbook/tests/ebook/ebook-test-utils.c
+++ b/addressbook/tests/ebook/ebook-test-utils.c
@@ -24,6 +24,44 @@ ebook_test_utils_book_add_contact (EBook    *book,
         return e_contact_get (contact, E_CONTACT_UID);
 }
 
+static void
+add_contact_cb (EBook            *book,
+                EBookStatus       status,
+                const char       *uid,
+                EBookTestClosure *closure)
+{
+        if (status != E_BOOK_ERROR_OK) {
+                g_warning ("failed to asynchronously add the contact '%s': "
+                                "status %d", uid, status);
+                exit (1);
+        }
+
+        g_print ("successfully asynchronously added the contact "
+                        "addressbook\n");
+        if (closure) {
+                (*closure->cb) (closure->user_data);
+                g_free (closure);
+        }
+}
+
+void
+ebook_test_utils_book_async_add_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_add_contact (book, contact,
+                                (EBookIdCallback) add_contact_cb, closure)) {
+                g_warning ("failed to set up contact add");
+                exit(1);
+        }
+}
+
 EContact*
 ebook_test_utils_book_get_contact (EBook      *book,
                                    const char *uid)
diff --git a/addressbook/tests/ebook/ebook-test-utils.h b/addressbook/tests/ebook/ebook-test-utils.h
index c1ee18a..516a6bf 100644
--- a/addressbook/tests/ebook/ebook-test-utils.h
+++ b/addressbook/tests/ebook/ebook-test-utils.h
@@ -43,6 +43,11 @@ ebook_test_utils_book_new_temp (char **uri);
 char*
 ebook_test_utils_book_add_contact (EBook    *book,
                                    EContact *contact);
+void
+ebook_test_utils_book_async_add_contact (EBook       *book,
+                                         EContact    *contact,
+                                         GSourceFunc  callback,
+                                         gpointer     user_data);
 
 EContact*
 ebook_test_utils_book_get_contact (EBook      *book,
diff --git a/addressbook/tests/ebook/test-ebook-add-contact.c b/addressbook/tests/ebook/test-ebook-add-contact.c
new file mode 100644
index 0000000..5e23c46
--- /dev/null
+++ b/addressbook/tests/ebook/test-ebook-add-contact.c
@@ -0,0 +1,71 @@
+/* -*- 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"
+
+gint
+main (gint argc, gchar **argv)
+{
+	EBook *book;
+        GMainLoop *loop;
+        EContact *contact;
+        EContact *contact_final;
+        char *uid;
+        const char *contact_final_uid;
+
+	g_type_init ();
+
+        /*
+         * Setup
+         */
+        book = ebook_test_utils_book_new_temp (NULL);
+        ebook_test_utils_book_open (book, FALSE);
+
+        contact = e_contact_new_from_vcard (EBOOK_TEST_UTILS_VCARD_SIMPLE);
+        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);
+        }
+
+        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);
+
+        /*
+         * Async version
+         */
+        book = ebook_test_utils_book_new_temp (NULL);
+        ebook_test_utils_book_open (book, FALSE);
+        contact = e_contact_new_from_vcard (EBOOK_TEST_UTILS_VCARD_SIMPLE);
+        
+        loop = g_main_loop_new (NULL, TRUE);
+        ebook_test_utils_book_async_add_contact (book, contact,
+                        (GSourceFunc) g_main_loop_quit, loop);
+                        
+        g_free (uid);
+        g_main_loop_run (loop);
+
+        ebook_test_utils_book_remove (book);
+
+	return 0;
+}



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