[evolution-data-server/treitter-client-gdbus] Port the e-book 'getContact' method to gdbus and add a test program to check for regressions.



commit 0319bb68a4cc21edae110e7baf88eea63242e425
Author: Travis Reitter <treitter gmail com>
Date:   Thu Dec 3 16:54:35 2009 -0800

    Port the e-book 'getContact' method to gdbus and add a test program to check for regressions.

 addressbook/libebook/e-book.c                     |    9 ++-
 addressbook/libebook/e-data-book-gdbus-bindings.h |   12 ++--
 addressbook/tests/ebook/Makefile.am               |    3 +
 addressbook/tests/ebook/ebook-test-utils.c        |   93 ++++++++++++++++++++-
 addressbook/tests/ebook/ebook-test-utils.h        |   40 ++++++++--
 addressbook/tests/ebook/test-ebook-get-contact.c  |   64 ++++++++++++++
 6 files changed, 203 insertions(+), 18 deletions(-)
---
diff --git a/addressbook/libebook/e-book.c b/addressbook/libebook/e-book.c
index 72857b2..b46fb6c 100644
--- a/addressbook/libebook/e-book.c
+++ b/addressbook/libebook/e-book.c
@@ -912,7 +912,7 @@ e_book_get_contact (EBook       *book,
 	e_return_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
 
 	LOCK_CONN ();
-	org_gnome_evolution_dataserver_addressbook_Book_get_contact (book->priv->proxy, id, &vcard, &err);
+	e_data_book_gdbus_get_contact_sync (book->priv->gdbus_proxy, id, &vcard, &err);
 	UNLOCK_CONN ();
 
 	if (vcard) {
@@ -923,7 +923,10 @@ e_book_get_contact (EBook       *book,
 }
 
 static void
-get_contact_reply(DBusGProxy *proxy, gchar *vcard, GError *error, gpointer user_data)
+get_contact_reply (GDBusProxy *proxy,
+		   gchar      *vcard,
+		   GError     *error,
+		   gpointer    user_data)
 {
 	AsyncData *data = user_data;
 	EBookContactCallback cb = data->callback;
@@ -979,7 +982,7 @@ e_book_async_get_contact (EBook                 *book,
 	data->closure = closure;
 
 	LOCK_CONN ();
-	org_gnome_evolution_dataserver_addressbook_Book_get_contact_async (book->priv->proxy, id, get_contact_reply, data);
+	e_data_book_gdbus_get_contact (book->priv->gdbus_proxy, id, get_contact_reply, data);
 	UNLOCK_CONN ();
 
 	return 0;
diff --git a/addressbook/libebook/e-data-book-gdbus-bindings.h b/addressbook/libebook/e-data-book-gdbus-bindings.h
index bc09d79..4566d3f 100644
--- a/addressbook/libebook/e-data-book-gdbus-bindings.h
+++ b/addressbook/libebook/e-data-book-gdbus-bindings.h
@@ -184,7 +184,7 @@ get_contact_demarshal_retvals (GVariant *retvals, char **OUT_vcard)
 
 static gboolean
 e_data_book_gdbus_get_contact_sync (GDBusProxy  *proxy,
-				    char       **IN_uid,
+				    const char  *IN_uid,
 				    char       **OUT_vcard,
 			            GError     **error)
 
@@ -225,10 +225,10 @@ get_contact_cb (GDBusProxy *proxy,
 }
 
 static void
-e_data_book_factory_gdbus_get_contact (GDBusProxy *proxy,
-				       const char *IN_uid,
-				       e_data_book_gdbus_get_contact_reply callback,
-				       gpointer    user_data)
+e_data_book_gdbus_get_contact (GDBusProxy                          *proxy,
+			       const char                          *IN_uid,
+			       e_data_book_gdbus_get_contact_reply  callback,
+			       gpointer                             user_data)
 {
         GVariant *parameters;
         Closure *closure;
@@ -239,7 +239,7 @@ e_data_book_factory_gdbus_get_contact (GDBusProxy *proxy,
         closure->cb = G_CALLBACK (callback);
         closure->user_data = user_data;
 
-        g_dbus_proxy_invoke_method (proxy, "getContact", parameters, -1, NULL, (GAsyncReadyCallback*) get_contact_cb, closure);
+        g_dbus_proxy_invoke_method (proxy, "getContact", parameters, -1, NULL, (GAsyncReadyCallback) get_contact_cb, closure);
 }
 
 
diff --git a/addressbook/tests/ebook/Makefile.am b/addressbook/tests/ebook/Makefile.am
index b0b95dc..159e71e 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-get-contact               \
 	test-ebook-remove                    \
 	test-ebook-view                      \
 	test-nonexistent-id                  \
@@ -52,6 +53,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_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_view_LDADD=$(TEST_LIBS)
diff --git a/addressbook/tests/ebook/ebook-test-utils.c b/addressbook/tests/ebook/ebook-test-utils.c
index 7ecba11..1497760 100644
--- a/addressbook/tests/ebook/ebook-test-utils.c
+++ b/addressbook/tests/ebook/ebook-test-utils.c
@@ -1,23 +1,105 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 
 #include <stdlib.h>
+#include <glib.h>
 #include <libebook/e-book.h>
 
 #include "ebook-test-utils.h"
 
+char*
+ebook_test_utils_book_add_contact (EBook    *book,
+                                   EContact *contact)
+{
+        GError *error = NULL;
+
+        if (!e_book_add_contact (book, contact, &error)) {
+                const char *uri;
+
+                uri = e_book_get_uri (book);
+                g_warning ("failed to add contact to addressbook: `%s': %s",
+                                uri, error->message);
+                exit(1);
+        }
+
+        return e_contact_get (contact, E_CONTACT_UID);
+}
+
+EContact*
+ebook_test_utils_book_get_contact (EBook      *book,
+                                   const char *uid)
+{
+        EContact *contact = NULL;
+        GError *error = NULL;
+
+        if (!e_book_get_contact (book, uid, &contact, &error)) {
+                const char *uri;
+
+                uri = e_book_get_uri (book);
+                g_warning ("failed to get contact '%s' in addressbook: `%s': "
+                                "%s", uid, uri, error->message);
+                exit(1);
+        }
+
+        return contact;
+}
+
+static void
+get_contact_cb (EBook            *book,
+                EBookStatus       status,
+                EContact         *contact,
+                EBookTestClosure *closure)
+{
+        const char *uid;
+
+        if (status != E_BOOK_ERROR_OK) {
+                g_warning ("failed to asynchronously get the contact: "
+                                "status %d", status);
+                exit (1);
+        }                       
+
+        uid = e_contact_get_const (contact, E_CONTACT_UID);
+        g_print ("successfully asynchronously retrieved the contact '%s'\n",
+                        uid);
+
+        if (closure) {
+                (*closure->cb) (closure->user_data);
+                g_free (closure);
+        }
+}
+
+void   
+ebook_test_utils_book_async_get_contact (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_get_contact (book, uid,
+                                (EBookContactCallback) get_contact_cb,
+                                closure)) {
+                g_warning ("failed to set up async getContact");
+                exit(1);
+        }
+}
+
 EBook*
 ebook_test_utils_book_new_temp (char **uri)
 {
         EBook *book;
 	GError *error = NULL;
 	gchar *file_template;
+        char *uri_result;
 
         file_template = g_build_filename (g_get_tmp_dir (),
                         "ebook-test-XXXXXX/", NULL);
 	g_mkstemp (file_template);
 
-	*uri = g_filename_to_uri (file_template, NULL, &error);
-	if (!*uri) {
+	uri_result = g_filename_to_uri (file_template, NULL, &error);
+	if (!uri_result) {
                 g_warning ("failed to convert %s to an URI: %s", file_template,
                                 error->message);
 		exit (1);
@@ -26,13 +108,18 @@ ebook_test_utils_book_new_temp (char **uri)
 
 	/* create a temp addressbook in /tmp */
 	g_print ("loading addressbook\n");
-	book = e_book_new_from_uri (*uri, &error);
+	book = e_book_new_from_uri (uri_result, &error);
 	if (!book) {
                 g_warning ("failed to create addressbook: `%s': %s", *uri,
                                 error->message);
 		exit(1);
 	}
 
+        if (uri)
+                *uri = g_strdup (uri_result);
+
+        g_free (uri_result);
+
         return book;
 }
 
diff --git a/addressbook/tests/ebook/ebook-test-utils.h b/addressbook/tests/ebook/ebook-test-utils.h
index 282c1c9..c1ee18a 100644
--- a/addressbook/tests/ebook/ebook-test-utils.h
+++ b/addressbook/tests/ebook/ebook-test-utils.h
@@ -22,18 +22,46 @@
 #ifndef _EBOOK_TEST_UTILS_H
 #define _EBOOK_TEST_UTILS_H
 
+#include <glib.h>
 #include <libebook/e-book.h>
 
+/* TODO: build up a library of vcards as separate files; don't hardcode here */
+#define EBOOK_TEST_UTILS_VCARD_SIMPLE \
+"BEGIN:VCARD\n\
+FN:Foo Bar\n\
+EMAIL;INTERNET:foo bar example org\n\
+END:VCARD"
+
 typedef struct {
         GSourceFunc    cb;
         gpointer       user_data;
 } EBookTestClosure;
 
-EBook* ebook_test_utils_book_new_temp (char **uri);
-void   ebook_test_utils_book_open (EBook *book, gboolean only_if_exists);
-void   ebook_test_utils_book_remove (EBook *book);
-void   ebook_test_utils_book_async_remove (EBook          *book,
-                                           GSourceFunc     callback,
-                                           gpointer        user_data);
+EBook*
+ebook_test_utils_book_new_temp (char **uri);
+
+char*
+ebook_test_utils_book_add_contact (EBook    *book,
+                                   EContact *contact);
+
+EContact*
+ebook_test_utils_book_get_contact (EBook      *book,
+                                   const char *uid);
+void   
+ebook_test_utils_book_async_get_contact (EBook       *book,
+                                         const char  *uid,
+                                         GSourceFunc  callback,
+                                         gpointer     user_data);
+
+void
+ebook_test_utils_book_open (EBook    *book,
+                            gboolean  only_if_exists);
+
+void
+ebook_test_utils_book_remove (EBook *book);
+void
+ebook_test_utils_book_async_remove (EBook          *book,
+                                    GSourceFunc     callback,
+                                    gpointer        user_data);
 
 #endif /* _EBOOK_TEST_UTILS_H */
diff --git a/addressbook/tests/ebook/test-ebook-get-contact.c b/addressbook/tests/ebook/test-ebook-get-contact.c
new file mode 100644
index 0000000..f6c2c49
--- /dev/null
+++ b/addressbook/tests/ebook/test-ebook-get-contact.c
@@ -0,0 +1,64 @@
+/* -*- 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);
+
+        /*
+         * Async version
+         */
+        loop = g_main_loop_new (NULL, TRUE);
+        ebook_test_utils_book_async_get_contact (book, uid,
+                        (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]