[evolution-data-server] Reworked test cases in tests/libebook/client directory



commit de7a6e5e123f88fb7574611a0405456ef0df7814
Author: Tristan Van Berkom <tristanvb openismus com>
Date:   Sat Mar 16 15:39:11 2013 +0900

    Reworked test cases in tests/libebook/client directory
    
      o Removed obsolete test cases:
    
          test-client-nonexistent-id:
              Test covered by test-client-remove-contact already
          test-client-examine: Unable to easily refactor
          test-client-search:  A test which only applies to an existing
                               addressbook, can't be useful in unit tests
          stress factories: Not worth refactoring, partly covered by tests
                            in test-server-utils/test-fixture.c already
    
      o Revived several test cases which were not previously running at make check
      o Cleaned up client-test-utils.[ch]: now only includes convenience functions
        for loading contacts from the test case vcards.

 tests/libebook/client/Makefile.am                  |   49 +--
 tests/libebook/client/client-test-utils.c          |  384 +------------------
 tests/libebook/client/client-test-utils.h          |   14 -
 ...ent-async.c => test-client-add-and-get-async.c} |   80 ++---
 ...est-client.c => test-client-add-and-get-sync.c} |   60 +--
 tests/libebook/client/test-client-examine.c        |  397 --------------------
 .../libebook/client/test-client-get-contact-uids.c |    1 -
 tests/libebook/client/test-client-nonexistent-id.c |   50 ---
 tests/libebook/client/test-client-revision-view.c  |    2 +-
 tests/libebook/client/test-client-search.c         |   72 ----
 tests/libebook/client/test-client-self.c           |  120 ++++--
 .../client/test-client-stress-factory--fifo.c      |   51 ---
 .../client/test-client-stress-factory--serial.c    |   38 --
 .../test-client-stress-factory--single-book.c      |   67 ----
 tests/libebook/client/test-client-stress-views.c   |  138 -------
 tests/libebook/client/test-client-uid-only-view.c  |    2 +-
 16 files changed, 163 insertions(+), 1362 deletions(-)
---
diff --git a/tests/libebook/client/Makefile.am b/tests/libebook/client/Makefile.am
index 2cb283d..746b8e5 100644
--- a/tests/libebook/client/Makefile.am
+++ b/tests/libebook/client/Makefile.am
@@ -28,23 +28,26 @@ libclient_test_utils_la_LIBADD =                            \
 TESTS =                                                                \
        test-client-refresh                                     \
        test-client-add-contact                                 \
-       test-client-preserve-uid                                \
        test-client-get-contact                                 \
        test-client-get-contact-uids                            \
+       test-client-modify-contact                              \
+       test-client-remove-contact                              \
+       test-client-remove-contact-by-uid                       \
+       test-client-remove-contacts                             \
+       test-client-add-and-get-sync                            \
+       test-client-add-and-get-async                           \
+       test-client-self                                        \
+       test-client-preserve-uid                                \
+       test-client-photo-is-uri                                \
        test-client-e164-param                                  \
         test-client-custom-summary                             \
        test-client-get-revision                                \
-       test-client-get-view                                    \
        test-client-write-write                                 \
+       test-client-get-view                                    \
        test-client-uid-only-view                               \
        test-client-revision-view                               \
        test-client-view-operations                             \
        test-client-suppress-notifications                      \
-       test-client-modify-contact                              \
-       test-client-remove-contact                              \
-       test-client-remove-contact-by-uid                       \
-       test-client-remove-contacts                             \
-       test-client-photo-is-uri                                \
        $(NULL)
 
 # The noinst tests are functional tests, not unit tests.
@@ -53,16 +56,6 @@ TESTS =                                                              \
 # not been ported to use ETestServerFixture yet.
 noinst_PROGRAMS =                                              \
        $(TESTS)                                                \
-       test-client-async                                       \
-       test-client-examine                                     \
-       test-client                                             \
-       test-client-nonexistent-id                              \
-       test-client-search                                      \
-       test-client-self                                        \
-       test-client-stress-views                                \
-       test-client-stress-factory--serial                      \
-       test-client-stress-factory--fifo                        \
-       test-client-stress-factory--single-book                 \
        $(NULL)
 
 TEST_CPPFLAGS =                                                        \
@@ -77,16 +70,14 @@ TEST_LIBS =                                                 \
        $(EVOLUTION_ADDRESSBOOK_LIBS)                           \
        $(NULL)
 
-test_client_LDADD=$(TEST_LIBS)
-test_client_CPPFLAGS=$(TEST_CPPFLAGS)
-test_client_async_LDADD=$(TEST_LIBS)
-test_client_async_CPPFLAGS=$(TEST_CPPFLAGS)
+test_client_add_and_get_sync_LDADD=$(TEST_LIBS)
+test_client_add_and_get_sync_CPPFLAGS=$(TEST_CPPFLAGS)
+test_client_add_and_get_async_LDADD=$(TEST_LIBS)
+test_client_add_and_get_async_CPPFLAGS=$(TEST_CPPFLAGS)
 test_client_add_contact_LDADD=$(TEST_LIBS)
 test_client_add_contact_CPPFLAGS=$(TEST_CPPFLAGS)
 test_client_preserve_uid_LDADD=$(TEST_LIBS)
 test_client_preserve_uid_CPPFLAGS=$(TEST_CPPFLAGS)
-test_client_examine_LDADD=$(TEST_LIBS)
-test_client_examine_CPPFLAGS=$(TEST_CPPFLAGS)
 test_client_get_contact_LDADD=$(TEST_LIBS)
 test_client_get_contact_CPPFLAGS=$(TEST_CPPFLAGS)
 test_client_get_contact_uids_LDADD=$(TEST_LIBS)
@@ -121,19 +112,7 @@ test_client_photo_is_uri_LDADD=$(TEST_LIBS)
 test_client_photo_is_uri_CPPFLAGS=$(TEST_CPPFLAGS)
 test_client_write_write_LDADD=$(TEST_LIBS)
 test_client_write_write_CPPFLAGS=$(TEST_CPPFLAGS)
-test_client_stress_factory__fifo_LDADD=$(TEST_LIBS)
-test_client_stress_factory__fifo_CPPFLAGS=$(TEST_CPPFLAGS)
-test_client_stress_factory__serial_LDADD=$(TEST_LIBS)
-test_client_stress_factory__serial_CPPFLAGS=$(TEST_CPPFLAGS)
-test_client_stress_factory__single_book_LDADD=$(TEST_LIBS)
-test_client_stress_factory__single_book_CPPFLAGS=$(TEST_CPPFLAGS)
-test_client_nonexistent_id_LDADD=$(TEST_LIBS)
-test_client_nonexistent_id_CPPFLAGS=$(TEST_CPPFLAGS)
-test_client_search_LDADD=$(TEST_LIBS)
-test_client_search_CPPFLAGS=$(TEST_CPPFLAGS)
 test_client_self_LDADD=$(TEST_LIBS)
 test_client_self_CPPFLAGS=$(TEST_CPPFLAGS)
-test_client_stress_views_LDADD=$(TEST_LIBS)
-test_client_stress_views_CPPFLAGS=$(TEST_CPPFLAGS)
 
 -include $(top_srcdir)/git.mk
diff --git a/tests/libebook/client/client-test-utils.c b/tests/libebook/client/client-test-utils.c
index f9bda66..d7368c6 100644
--- a/tests/libebook/client/client-test-utils.c
+++ b/tests/libebook/client/client-test-utils.c
@@ -30,17 +30,6 @@
 #include "client-test-utils.h"
 
 void
-report_error (const gchar *operation,
-              GError **error)
-{
-       g_return_if_fail (operation != NULL);
-
-       g_printerr ("Failed to %s: %s\n", operation, (error && *error) ? (*error)->message : "Unknown error");
-
-       g_clear_error (error);
-}
-
-void
 print_email (EContact *contact)
 {
        const gchar *file_as = e_contact_get_const (contact, E_CONTACT_FILE_AS);
@@ -60,351 +49,6 @@ print_email (EContact *contact)
        g_print ("\n");
 }
 
-EBookClient *
-open_system_book (ESourceRegistry *registry,
-                  gboolean only_if_exists)
-{
-       ESource *source;
-       EBookClient *book_client;
-       GError *error = NULL;
-
-       main_initialize ();
-
-       source = e_source_registry_ref_builtin_address_book (registry);
-       book_client = e_book_client_new (source, &error);
-       g_object_unref (source);
-
-       if (error) {
-               report_error ("create system addressbook", &error);
-               return NULL;
-       }
-
-       if (!e_client_open_sync (E_CLIENT (book_client), only_if_exists, NULL, &error)) {
-               g_object_unref (book_client);
-               report_error ("open client sync", &error);
-               return NULL;
-       }
-
-       return book_client;
-}
-
-void
-main_initialize (void)
-{
-       static gboolean initialized = FALSE;
-
-       if (initialized)
-               return;
-
-       g_type_init ();
-       e_gdbus_templates_init_main_thread ();
-
-       initialized = TRUE;
-}
-
-struct IdleData {
-       GThreadFunc func;
-       gpointer data;
-       gboolean run_in_thread; /* FALSE to run in idle callback */
-};
-
-static gboolean
-idle_cb (gpointer data)
-{
-       struct IdleData *idle = data;
-
-       g_return_val_if_fail (idle != NULL, FALSE);
-       g_return_val_if_fail (idle->func != NULL, FALSE);
-
-       if (idle->run_in_thread) {
-               GThread *thread;
-
-               thread = g_thread_new (NULL, idle->func, idle->data);
-               g_thread_unref (thread);
-       } else {
-               idle->func (idle->data);
-       }
-
-       g_free (idle);
-
-       return FALSE;
-}
-
-static GMainLoop *loop = NULL;
-static gint main_stop_result = 0;
-
-static void
-do_start (GThreadFunc func,
-          gpointer data)
-{
-       main_initialize ();
-
-       g_return_if_fail (loop == NULL);
-
-       loop = g_main_loop_new (NULL, FALSE);
-
-       if (func)
-               func (data);
-
-       g_main_loop_run (loop);
-
-       g_main_loop_unref (loop);
-       loop = NULL;
-}
-
-/* Starts new main-loop, but just before that calls 'func'.
- * Main-loop is kept running, and this function blocks,
- * until call of stop_main_loop (). */
-void
-start_main_loop (GThreadFunc func,
-                 gpointer data)
-{
-       g_return_if_fail (loop == NULL);
-
-       do_start (func, data);
-}
-
-/* Starts new main-loop and then invokes func in a new thread.
- * Main-loop is kept running, and this function blocks,
- * until call of stop_main_loop (). */
-void
-start_in_thread_with_main_loop (GThreadFunc func,
-                                gpointer data)
-{
-       struct IdleData *idle;
-
-       g_return_if_fail (func != NULL);
-       g_return_if_fail (loop == NULL);
-
-       main_initialize ();
-
-       idle = g_new0 (struct IdleData, 1);
-       idle->func = func;
-       idle->data = data;
-       idle->run_in_thread = TRUE;
-
-       g_idle_add (idle_cb, idle);
-
-       do_start (NULL, NULL);
-}
-
-/* Starts new main-loop and then invokes func in an idle callback.
- * Main-loop is kept running, and this function blocks,
- * until call of stop_main_loop (). */
-void
-start_in_idle_with_main_loop (GThreadFunc func,
-                              gpointer data)
-{
-       struct IdleData *idle;
-
-       g_return_if_fail (func != NULL);
-       g_return_if_fail (loop == NULL);
-
-       main_initialize ();
-
-       idle = g_new0 (struct IdleData, 1);
-       idle->func = func;
-       idle->data = data;
-       idle->run_in_thread = FALSE;
-
-       g_idle_add (idle_cb, idle);
-
-       do_start (NULL, NULL);
-}
-
-/* Stops main-loop previously run by start_main_loop,
- * start_in_thread_with_main_loop or start_in_idle_with_main_loop.
-*/
-void
-stop_main_loop (gint stop_result)
-{
-       g_return_if_fail (loop != NULL);
-
-       main_stop_result = stop_result;
-       g_main_loop_quit (loop);
-}
-
-/* returns value used in stop_main_loop() */
-gint
-get_main_loop_stop_result (void)
-{
-       return main_stop_result;
-}
-
-void
-foreach_configured_source (ESourceRegistry *registry,
-                           void (*func) (ESource *source))
-{
-       gpointer foreach_async_data;
-       ESource *source = NULL;
-
-       g_return_if_fail (func != NULL);
-
-       main_initialize ();
-
-       foreach_async_data = foreach_configured_source_async_start (registry, &source);
-       if (!foreach_async_data)
-               return;
-
-       do {
-               func (source);
-       } while (foreach_configured_source_async_next (&foreach_async_data, &source));
-}
-
-struct ForeachConfiguredData {
-       GList *list;
-};
-
-gpointer
-foreach_configured_source_async_start (ESourceRegistry *registry,
-                                       ESource **source)
-{
-       struct ForeachConfiguredData *async_data;
-       const gchar *extension_name;
-       GList *list;
-
-       g_return_val_if_fail (source != NULL, NULL);
-
-       main_initialize ();
-
-       extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK;
-       list = e_source_registry_list_sources (registry, extension_name);
-
-       async_data = g_new0 (struct ForeachConfiguredData, 1);
-       async_data->list = list;
-
-       *source = async_data->list->data;
-
-       return async_data;
-}
-
-gboolean
-foreach_configured_source_async_next (gpointer *foreach_async_data,
-                                      ESource **source)
-{
-       struct ForeachConfiguredData *async_data;
-
-       g_return_val_if_fail (foreach_async_data != NULL, FALSE);
-       g_return_val_if_fail (source != NULL, FALSE);
-
-       async_data = *foreach_async_data;
-       g_return_val_if_fail (async_data != NULL, FALSE);
-
-       if (async_data->list) {
-               g_object_unref (async_data->list->data);
-               async_data->list = async_data->list->next;
-       }
-       if (async_data->list) {
-               *source = async_data->list->data;
-               return TRUE;
-       }
-
-       g_free (async_data);
-
-       *foreach_async_data = NULL;
-
-       return FALSE;
-}
-
-typedef struct {
-       GMainLoop       *loop;
-       const gchar     *uid;
-       ESourceRegistry *registry;
-       ESource         *scratch;
-       ESource         *source;
-       EBookClient     *book;
-} CreateBookData;
-
-static gboolean
-quit_idle (CreateBookData *data)
-{
-       g_main_loop_quit (data->loop);
-       return FALSE;
-}
-
-static gboolean
-create_book_idle (CreateBookData *data)
-{
-       GError *error = NULL;
-
-       data->source = e_source_registry_ref_source (data->registry, data->uid);
-       if (!data->source)
-               g_error ("Unable to fetch newly created source uid '%s' from the registry", data->uid);
-
-       data->book = e_book_client_new (data->source, &error);
-       if (!data->book)
-               g_error ("Unable to create the book: %s", error->message);
-
-       g_idle_add ((GSourceFunc) quit_idle, data);
-
-       return FALSE;
-}
-
-static gboolean
-register_source_idle (CreateBookData *data)
-{
-       GError *error = NULL;
-       ESourceBackend  *backend;
-
-       data->registry = e_source_registry_new_sync (NULL, &error);
-       if (!data->registry)
-               g_error ("Unable to create the registry: %s", error->message);
-
-       data->scratch = e_source_new_with_uid (data->uid, NULL, &error);
-       if (!data->scratch)
-               g_error ("Failed to create source with uid '%s': %s", data->uid, error->message);
-
-       backend = e_source_get_extension (data->scratch, E_SOURCE_EXTENSION_ADDRESS_BOOK);
-       e_source_backend_set_backend_name (backend, "local");
-
-       if (!e_source_registry_commit_source_sync (data->registry, data->scratch, NULL, &error))
-               g_error ("Unable to add new source to the registry for uid %s: %s", data->uid, 
error->message);
-
-       /* XXX e_source_registry_commit_source_sync isnt really sync... or else
-        * we could call e_source_registry_ref_source() immediately
-        */
-       g_timeout_add (20, (GSourceFunc) create_book_idle, data);
-
-       return FALSE;
-}
-
-static EBookClient *
-ebook_test_utils_book_with_uid (const gchar *uid)
-{
-       CreateBookData data = { 0, };
-
-       data.uid = uid;
-
-       data.loop = g_main_loop_new (NULL, FALSE);
-       g_idle_add ((GSourceFunc) register_source_idle, &data);
-       g_main_loop_run (data.loop);
-       g_main_loop_unref (data.loop);
-
-       g_object_unref (data.scratch);
-       g_object_unref (data.source);
-       g_object_unref (data.registry);
-
-       return data.book;
-}
-
-EBookClient *
-new_temp_client (gchar **uri)
-{
-       EBookClient     *book;
-       gchar           *uid;
-       guint64          real_time = g_get_real_time ();
-
-       uid  = g_strdup_printf ("test-book-%" G_GINT64_FORMAT, real_time);
-       book = ebook_test_utils_book_with_uid (uid);
-
-       if (uri)
-               *uri = g_strdup (uid);
-
-       g_free (uid);
-
-       return book;
-}
-
 gchar *
 new_vcard_from_test_case (const gchar *case_name)
 {
@@ -465,20 +109,13 @@ add_contact_from_test_case_verify (EBookClient *book_client,
        vcard = new_vcard_from_test_case (case_name);
        contact_orig = e_contact_new_from_vcard (vcard);
        g_free (vcard);
-       if (!e_book_client_add_contact_sync (book_client, contact_orig, &uid, NULL, &error)) {
-               report_error ("add contact sync", &error);
-               g_object_unref (contact_orig);
-               return FALSE;
-       }
+       if (!e_book_client_add_contact_sync (book_client, contact_orig, &uid, NULL, &error))
+               g_error ("add contact sync: %s", error->message);
 
        e_contact_set (contact_orig, E_CONTACT_UID, uid);
 
-       if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error)) {
-               report_error ("get contact sync", &error);
-               g_object_unref (contact_orig);
-               g_free (uid);
-               return FALSE;
-       }
+       if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error))
+               g_error ("get contact sync: %s", error->message);
 
         /* verify the contact was added "successfully" (not thorough) */
        g_assert (contacts_are_equal_shallow (contact_orig, contact_final));
@@ -501,18 +138,13 @@ add_contact_verify (EBookClient *book_client,
        gchar *uid;
        GError *error = NULL;
 
-       if (!e_book_client_add_contact_sync (book_client, contact, &uid, NULL, &error)) {
-               report_error ("add contact sync", &error);
-               return FALSE;
-       }
+       if (!e_book_client_add_contact_sync (book_client, contact, &uid, NULL, &error))
+               g_error ("add contact sync: %s", error->message);
 
        e_contact_set (contact, E_CONTACT_UID, uid);
 
-       if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error)) {
-               report_error ("get contact sync", &error);
-               g_free (uid);
-               return FALSE;
-       }
+       if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error))
+               g_error ("get contact sync: %s", error->message);
 
         /* verify the contact was added "successfully" (not thorough) */
        g_assert (contacts_are_equal_shallow (contact, contact_final));
diff --git a/tests/libebook/client/client-test-utils.h b/tests/libebook/client/client-test-utils.h
index 2537735..7b42232 100644
--- a/tests/libebook/client/client-test-utils.h
+++ b/tests/libebook/client/client-test-utils.h
@@ -27,22 +27,8 @@
 
 #include <libebook/libebook.h>
 
-void report_error (const gchar *operation, GError **error);
 void print_email (EContact *contact);
-EBookClient *open_system_book (ESourceRegistry *registry, gboolean only_if_exists);
 
-void main_initialize (void);
-void start_main_loop (GThreadFunc func, gpointer data);
-void start_in_thread_with_main_loop (GThreadFunc func, gpointer data);
-void start_in_idle_with_main_loop (GThreadFunc func, gpointer data);
-void stop_main_loop (gint stop_result);
-gint get_main_loop_stop_result (void);
-
-void foreach_configured_source (ESourceRegistry *registry, void (*func) (ESource *source));
-gpointer foreach_configured_source_async_start (ESourceRegistry *registry, ESource **source);
-gboolean foreach_configured_source_async_next (gpointer *foreach_async_data, ESource **source);
-
-EBookClient *new_temp_client (gchar **uri);
 gboolean add_contact_from_test_case_verify (EBookClient *book_client, const gchar *case_name, EContact 
**contact);
 gboolean add_contact_verify (EBookClient *book_client, EContact *contact);
 gchar *new_vcard_from_test_case (const gchar *case_name);
diff --git a/tests/libebook/client/test-client-async.c b/tests/libebook/client/test-client-add-and-get-async.c
similarity index 74%
rename from tests/libebook/client/test-client-async.c
rename to tests/libebook/client/test-client-add-and-get-async.c
index 25d11c9..29db28b 100644
--- a/tests/libebook/client/test-client-async.c
+++ b/tests/libebook/client/test-client-add-and-get-async.c
@@ -8,13 +8,20 @@
 
 static ETestServerClosure book_closure = { E_TEST_SERVER_ADDRESS_BOOK, NULL, 0 };
 
+#define N_CONTACTS 5
+
+static gint fetched_contacts = 0;
+static gint fetched_uids = 0;
+static gint added_contacts = 0;
+
+
 static void
-print_all_uids_cb (GObject *source_object,
+count_all_uids_cb (GObject *source_object,
                    GAsyncResult *result,
                    gpointer user_data)
 {
        EBookClient *book_client;
-       GSList *uids = NULL, *u;
+       GSList *uids = NULL;
        GError *error = NULL;
        GMainLoop *loop = (GMainLoop *) user_data;
 
@@ -24,11 +31,7 @@ print_all_uids_cb (GObject *source_object,
        if (!e_book_client_get_contacts_uids_finish (book_client, result, &uids, &error))
                g_error ("get contacts uids finish: %s", error->message);
 
-       for (u = uids; u; u = u->next) {
-               const gchar *uid = u->data;
-
-               g_print ("   uid:'%s'\n", uid);
-       }
+       fetched_uids = g_slist_length (uids);
 
        g_slist_foreach (uids, (GFunc) g_free, NULL);
        g_slist_free (uids);
@@ -37,14 +40,14 @@ print_all_uids_cb (GObject *source_object,
 }
 
 static void
-print_all_emails_cb (GObject *source_object,
-                     GAsyncResult *result,
-                     gpointer user_data)
+count_all_contacts_cb (GObject *source_object,
+                      GAsyncResult *result,
+                      gpointer user_data)
 {
        EBookClient *book_client;
        EBookQuery *query;
        gchar *sexp;
-       GSList *contacts = NULL, *c;
+       GSList *contacts = NULL;
        GError *error = NULL;
        GMainLoop *loop = (GMainLoop *) user_data;
 
@@ -54,11 +57,7 @@ print_all_emails_cb (GObject *source_object,
        if (!e_book_client_get_contacts_finish (book_client, result, &contacts, &error))
                g_error ("get contacts finish: %s", error->message);
 
-       for (c = contacts; c; c = c->next) {
-               EContact *contact = E_CONTACT (c->data);
-
-               print_email (contact);
-       }
+       fetched_contacts = g_slist_length (contacts);
 
        g_slist_foreach (contacts, (GFunc) g_object_unref, NULL);
        g_slist_free (contacts);
@@ -67,14 +66,14 @@ print_all_emails_cb (GObject *source_object,
        sexp = e_book_query_to_string (query);
        e_book_query_unref (query);
 
-       e_book_client_get_contacts_uids (book_client, sexp, NULL, print_all_uids_cb, loop);
+       e_book_client_get_contacts_uids (book_client, sexp, NULL, count_all_uids_cb, loop);
 
        g_free (sexp);
 }
 
 static void
-print_all_emails (EBookClient *book_client,
-                  GMainLoop *loop)
+count_all_contacts (EBookClient *book_client,
+                   GMainLoop *loop)
 {
        EBookQuery *query;
        gchar *sexp;
@@ -83,15 +82,15 @@ print_all_emails (EBookClient *book_client,
        sexp = e_book_query_to_string (query);
        e_book_query_unref (query);
 
-       e_book_client_get_contacts (book_client, sexp, NULL, print_all_emails_cb, loop);
+       e_book_client_get_contacts (book_client, sexp, NULL, count_all_contacts_cb, loop);
 
        g_free (sexp);
 }
 
 static void
-print_email_cb (GObject *source_object,
-                GAsyncResult *result,
-                gpointer user_data)
+get_contact_cb (GObject *source_object,
+               GAsyncResult *result,
+               gpointer user_data)
 {
        EBookClient *book_client;
        EContact *contact = NULL;
@@ -104,24 +103,20 @@ print_email_cb (GObject *source_object,
        if (!e_book_client_get_contact_finish (book_client, result, &contact, &error)) {
                g_error ("get contact finish: %s", error->message);
        } else {
-               print_email (contact);
                g_object_unref (contact);
        }
 
-       printf ("printing all contacts\n");
-       print_all_emails (book_client, loop);
+       count_all_contacts (book_client, loop);
 }
 
 static void
-print_one_email (EBookClient *book_client,
-                 GSList *uids,
-                 GMainLoop *loop)
+get_contact_async (EBookClient *book_client,
+                  GSList *uids,
+                  GMainLoop *loop)
 {
        const gchar *uid = uids->data;
 
-       e_book_client_get_contact (book_client, uid, NULL, print_email_cb, loop);
-
-       e_util_free_string_slist (uids);
+       e_book_client_get_contact (book_client, uid, NULL, get_contact_cb, loop);
 }
 
 static void
@@ -131,7 +126,7 @@ contacts_added_cb (GObject *source_object,
 {
        EBookClient *book_client;
        GError *error = NULL;
-       GSList *uids = NULL, *l;
+       GSList *uids = NULL;
        GMainLoop *loop = (GMainLoop *) user_data;
 
        book_client = E_BOOK_CLIENT (source_object);
@@ -139,16 +134,10 @@ contacts_added_cb (GObject *source_object,
        if (!e_book_client_add_contacts_finish (book_client, result, &uids, &error))
                g_error ("client open finish: %s", error->message);
 
-       printf ("Added contacts uids are:\n");
-       for (l = uids; l; l = l->next) {
-               const gchar *uid = l->data;
+       added_contacts = g_slist_length (uids);
+       get_contact_async (book_client, uids, loop);
 
-               printf ("\t%s\n", uid);
-       }
-       printf ("\n");
-
-       printf ("printing one contact\n");
-       print_one_email (book_client, uids, loop);
+       e_util_free_string_slist (uids);
 }
 
 static void
@@ -197,9 +186,12 @@ test_async (ETestServerFixture *fixture,
 
        book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);
 
-       printf ("Adding contacts\n");
        add_contacts (book_client, fixture->loop);
        g_main_loop_run (fixture->loop);
+
+       g_assert_cmpint (added_contacts, ==, N_CONTACTS);
+       g_assert_cmpint (fetched_contacts, ==, N_CONTACTS);
+       g_assert_cmpint (fetched_uids, ==, N_CONTACTS);
 }
 
 gint
@@ -212,7 +204,7 @@ main (gint argc,
        g_test_init (&argc, &argv, NULL);
 
        g_test_add (
-               "/EBookClient/AsyncTest", ETestServerFixture, &book_closure,
+               "/EBookClient/AddAndGet/Async", ETestServerFixture, &book_closure,
                e_test_server_utils_setup, test_async, e_test_server_utils_teardown);
 
        return e_test_server_utils_run ();
diff --git a/tests/libebook/client/test-client.c b/tests/libebook/client/test-client-add-and-get-sync.c
similarity index 63%
rename from tests/libebook/client/test-client.c
rename to tests/libebook/client/test-client-add-and-get-sync.c
index 06e4298..3b126ab 100644
--- a/tests/libebook/client/test-client.c
+++ b/tests/libebook/client/test-client-add-and-get-sync.c
@@ -8,72 +8,54 @@
 
 static ETestServerClosure book_closure = { E_TEST_SERVER_ADDRESS_BOOK, NULL, 0 };
 
+#define N_CONTACTS 6
+
+static gint fetched_contacts = 0;
+static gint fetched_uids = 0;
+
 static void
-print_all_uids (EBookClient *book)
+count_all_uids (EBookClient *book)
 {
        GError *error = NULL;
        EBookQuery *query;
        gchar *sexp;
-       gboolean result;
-       GSList *uids, *u;
+       GSList *uids;
 
        query = e_book_query_field_exists (E_CONTACT_FULL_NAME);
        sexp = e_book_query_to_string (query);
        e_book_query_unref (query);
 
-       result = e_book_client_get_contacts_uids_sync (book, sexp, &uids, NULL, &error);
+       if (!e_book_client_get_contacts_uids_sync (book, sexp, &uids, NULL, &error))
+               g_error ("Error getting contact uids: %s", error->message);
 
        g_free (sexp);
 
-       if (!result) {
-               fprintf (stderr, "Error getting uid list: %s\n", error ? error->message : "Unknown error");
-               if (error)
-                       g_error_free (error);
-               exit (1);
-       }
-
-       for (u = uids; u; u = u->next) {
-               const gchar *uid = u->data;
-
-               g_print ("   uid:'%s'\n", uid);
-       }
+       fetched_uids = g_slist_length (uids);
 
        g_slist_foreach (uids, (GFunc) g_free, NULL);
        g_slist_free (uids);
 }
 
 static void
-print_all_emails (EBookClient *book)
+count_all_contacts (EBookClient *book)
 {
        GError *error = NULL;
        EBookQuery *query;
        gchar *sexp;
-       gboolean result;
-       GSList *cards, *c;
+       GSList *cards;
 
        query = e_book_query_field_exists (E_CONTACT_FULL_NAME);
        sexp = e_book_query_to_string (query);
        e_book_query_unref (query);
 
-       result = e_book_client_get_contacts_sync (book, sexp, &cards, NULL, &error);
+       if (!e_book_client_get_contacts_sync (book, sexp, &cards, NULL, &error))
+               g_error ("Error getting contacts: %s", error->message);
 
        g_free (sexp);
 
-       if (!result) {
-               fprintf (stderr, "Error getting card list: %s\n", error ? error->message : "Unknown error");
-               if (error)
-                       g_error_free (error);
-               exit (1);
-       }
-
-       for (c = cards; c; c = c->next) {
-               EContact *contact = E_CONTACT (c->data);
-
-               print_email (contact);
-
-               g_object_unref (contact);
-       }
+       fetched_contacts = g_slist_length (cards);
 
+       g_slist_foreach (cards, (GFunc) g_object_unref, NULL);
        g_slist_free (cards);
 }
 
@@ -96,11 +78,11 @@ test_client (ETestServerFixture *fixture,
                g_error ("Failed to add contacts");
        }
 
-       printf ("printing all contacts\n");
-       print_all_emails (book_client);
+       count_all_contacts (book_client);
+       count_all_uids (book_client);
 
-       printf ("printing all uids\n");
-       print_all_uids (book_client);
+       g_assert_cmpint (fetched_contacts, ==, N_CONTACTS);
+       g_assert_cmpint (fetched_uids, ==, N_CONTACTS);
 }
 
 gint
@@ -113,7 +95,7 @@ main (gint argc,
        g_test_init (&argc, &argv, NULL);
 
        g_test_add (
-               "/EBookClient/BasicTest", ETestServerFixture, &book_closure,
+               "/EBookClient/AddAndGet/Sync", ETestServerFixture, &book_closure,
                e_test_server_utils_setup, test_client, e_test_server_utils_teardown);
 
        return e_test_server_utils_run ();
diff --git a/tests/libebook/client/test-client-get-contact-uids.c 
b/tests/libebook/client/test-client-get-contact-uids.c
index 45be60b..4fc8667 100644
--- a/tests/libebook/client/test-client-get-contact-uids.c
+++ b/tests/libebook/client/test-client-get-contact-uids.c
@@ -53,7 +53,6 @@ contacts_ready_cb (GObject *source_object,
 
        if (!e_book_client_get_contacts_uids_finish (E_BOOK_CLIENT (source_object), result, &contacts, 
&error)) {
                g_error ("get contact finish: %s", error->message);
-               stop_main_loop (1);
        } else {
 
                g_assert_cmpint (g_slist_length (contacts), ==, 1);
diff --git a/tests/libebook/client/test-client-revision-view.c 
b/tests/libebook/client/test-client-revision-view.c
index f99a5b1..f8f2979 100644
--- a/tests/libebook/client/test-client-revision-view.c
+++ b/tests/libebook/client/test-client-revision-view.c
@@ -23,7 +23,7 @@ add_contact (EBookClient *client)
        e_contact_set (contact, E_CONTACT_FULL_NAME, "Micheal Jackson");
 
        if (!add_contact_verify (client, contact))
-               stop_main_loop (1);
+               g_error ("Failed to add Micheal Jackson");
 
        g_object_unref (contact);
 }
diff --git a/tests/libebook/client/test-client-self.c b/tests/libebook/client/test-client-self.c
index 1a1b4cc..d6a520f 100644
--- a/tests/libebook/client/test-client-self.c
+++ b/tests/libebook/client/test-client-self.c
@@ -3,50 +3,94 @@
 #include <stdlib.h>
 #include <libebook/libebook.h>
 
+#include "e-test-server-utils.h"
 #include "client-test-utils.h"
 
+static ETestServerClosure registry_closure = { E_TEST_SERVER_NONE, NULL, 0 };
+
+static void
+test_get_self (ETestServerFixture *fixture,
+              gconstpointer user_data)
+{
+       EBookClient *client;
+       EContact    *contact;
+       GError      *error = NULL;
+
+       if (!e_book_client_get_self (fixture->registry, &contact, &client, &error))
+               g_error ("failed to get self contact: %s", error->message);
+
+       if (!client)
+               g_error ("e_book_client_get_self(): No client returned");
+
+       if (!contact)
+               g_error ("e_book_client_get_self(): No contact returned");
+
+       g_object_unref (contact);
+       g_object_unref (client);
+}
+
+static void
+test_set_self (ETestServerFixture *fixture,
+              gconstpointer user_data)
+{
+       ESource     *source;
+       EBookClient *client;
+       EContact    *loaded_contact = NULL;
+       EContact    *self_contact = NULL;
+       GError      *error = NULL;
+       const gchar *added_uid, *self_uid;
+
+       /* Open the system addressbook */
+       source = e_source_registry_ref_builtin_address_book (fixture->registry);
+       client = (EBookClient *)e_book_client_connect_sync (source, NULL, &error);
+       g_object_unref (source);
+       if (!client)
+               g_error ("Error connecting to system addressbook: %s", error->message);
+
+       /* Add contact to addressbook */
+       g_assert (add_contact_from_test_case_verify (client, "simple-1", &loaded_contact));
+
+       /* Set contact as self */
+       if (!e_book_client_set_self (client, loaded_contact, &error))
+               g_error ("Error setting self: %s", error->message);
+
+       g_object_unref (client);
+       client = NULL;
+
+       if (!e_book_client_get_self (fixture->registry, &self_contact, &client, &error))
+               g_error ("failed to get self contact: %s", error->message);
+
+       if (!client)
+               g_error ("e_book_client_get_self(): No client returned");
+
+       if (!self_contact)
+               g_error ("e_book_client_get_self(): No contact returned");
+
+       /* Assert the fetched contact is the right one */
+       added_uid = e_contact_get_const (loaded_contact, E_CONTACT_UID);
+       self_uid = e_contact_get_const (self_contact, E_CONTACT_UID);
+       g_assert_cmpstr (added_uid, ==, self_uid);
+
+       g_object_unref (self_contact);
+       g_object_unref (loaded_contact);
+       g_object_unref (client);
+}
+
 gint
 main (gint argc,
       gchar **argv)
 {
-       EBookClient *book_client = NULL;
-       ESourceRegistry *registry;
-       EContact *contact = NULL;
-       GError *error = NULL;
-       gchar *vcard;
-
-       main_initialize ();
-
-       printf ("getting the self contact\n");
-
-       registry = e_source_registry_new_sync (NULL, &error);
-       if (error != NULL)
-               g_error ("%s", error->message);
-
-       if (!e_book_client_get_self (registry, &contact, &book_client, &error)) {
-               report_error ("get self", &error);
-               return 1;
-       }
-
-       if (!contact) {
-               fprintf (stderr, " * Self contact not set\n");
-               if (book_client)
-                       g_object_unref (book_client);
-               return 0;
-       }
-
-       if (!book_client) {
-               fprintf (stderr, " * Book client for a self contact not returned\n");
-               g_object_unref (contact);
-               return 1;
-       }
-
-       vcard = e_vcard_to_string (E_VCARD (contact), EVC_FORMAT_VCARD_30);
-       printf ("self contact = \n%s\n", vcard);
-       g_free (vcard);
+#if !GLIB_CHECK_VERSION (2, 35, 1)
+       g_type_init ();
+#endif
+       g_test_init (&argc, &argv, NULL);
 
-       g_object_unref (contact);
-       g_object_unref (book_client);
+       g_test_add (
+               "/EBookClient/Self/Get", ETestServerFixture, &registry_closure,
+               e_test_server_utils_setup, test_get_self, e_test_server_utils_teardown);
+       g_test_add (
+               "/EBookClient/Self/Set", ETestServerFixture, &registry_closure,
+               e_test_server_utils_setup, test_set_self, e_test_server_utils_teardown);
 
-       return 0;
+       return e_test_server_utils_run ();
 }
diff --git a/tests/libebook/client/test-client-uid-only-view.c 
b/tests/libebook/client/test-client-uid-only-view.c
index 2f83fb2..aab3b4c 100644
--- a/tests/libebook/client/test-client-uid-only-view.c
+++ b/tests/libebook/client/test-client-uid-only-view.c
@@ -47,7 +47,7 @@ add_contact (EBookClient *client)
        e_contact_set (contact, E_CONTACT_FULL_NAME, "Micheal Jackson");
 
        if (!add_contact_verify (client, contact))
-               stop_main_loop (1);
+               g_error ("Failed to add Micheal Jackson");
 
        g_object_unref (contact);
 }


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