[evolution-data-server/openismus-work-master: 73/73] Adding test-client-cursor-operations.c test.



commit fadebeadace386ee8910dc93376fa049c9082f43
Author: Tristan Van Berkom <tristanvb openismus com>
Date:   Thu Jul 4 17:38:24 2013 +0900

    Adding test-client-cursor-operations.c test.

 tests/libebook/client/Makefile.am                  |    6 +
 .../client/test-client-cursor-operations.c         | 1014 ++++++++++++++++++++
 2 files changed, 1020 insertions(+), 0 deletions(-)
---
diff --git a/tests/libebook/client/Makefile.am b/tests/libebook/client/Makefile.am
index 1dd7c0e..92d4460 100644
--- a/tests/libebook/client/Makefile.am
+++ b/tests/libebook/client/Makefile.am
@@ -12,6 +12,8 @@ libclient_test_utils_la_CPPFLAGS =                            \
        -I$(top_builddir)/calendar                              \
        -I$(top_srcdir)/tests/test-server-utils                 \
        -I$(top_builddir)/tests/test-server-utils               \
+       -I$(top_srcdir)/private                                 \
+       -I$(top_builddir)/private                               \
        -DSRCDIR=\""$(abs_srcdir)"\"                            \
        $(EVOLUTION_ADDRESSBOOK_CFLAGS)                         \
        $(CAMEL_CFLAGS)                                         \
@@ -20,6 +22,7 @@ libclient_test_utils_la_CPPFLAGS =                            \
 libclient_test_utils_la_LIBADD =                               \
        $(top_builddir)/addressbook/libebook/libebook-1.2.la    \
        $(top_builddir)/tests/test-server-utils/libetestserverutils.la  \
+       $(top_builddir)/private/libedbus-private.la             \
        $(EVOLUTION_ADDRESSBOOK_LIBS)                           \
        $(CAMEL_LIBS)                                           \
        $(NULL)
@@ -49,6 +52,7 @@ TESTS =                                                               \
        test-client-view-operations                             \
        test-client-suppress-notifications                      \
        test-client-cursor-create                               \
+       test-client-cursor-operations                           \
        $(NULL)
 
 # The noinst tests are functional tests, not unit tests.
@@ -117,5 +121,7 @@ test_client_self_LDADD=$(TEST_LIBS)
 test_client_self_CPPFLAGS=$(TEST_CPPFLAGS)
 test_client_cursor_create_LDADD=$(TEST_LIBS)
 test_client_cursor_create_CPPFLAGS=$(TEST_CPPFLAGS)
+test_client_cursor_operations_LDADD=$(TEST_LIBS)
+test_client_cursor_operations_CPPFLAGS=$(TEST_CPPFLAGS)
 
 -include $(top_srcdir)/git.mk
diff --git a/tests/libebook/client/test-client-cursor-operations.c 
b/tests/libebook/client/test-client-cursor-operations.c
new file mode 100644
index 0000000..67d7141
--- /dev/null
+++ b/tests/libebook/client/test-client-cursor-operations.c
@@ -0,0 +1,1014 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2013 Intel Corporation
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU Lesser General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ * Authors: Tristan Van Berkom <tristanvb openismus com>
+ */
+#include <libebook/libebook.h>
+
+#include "e-test-server-utils.h"
+#include "client-test-utils.h"
+#include "e-dbus-localed.h"
+
+
+/* This is based on the sorted-%02d.vcf contacts from 1-20,
+ * a full table describing the expected order under various
+ * locales can be found in tests/libedata-book/data-test-utils.h
+ */
+#define N_SORTED_CONTACTS    20
+
+/* 13 contacts in the test data have an email address ending with ".com" */
+#define N_FILTERED_CONTACTS  13
+
+/* How long to wait before aborting an async test */
+#define TIMEOUT              5 * 1000
+
+typedef struct _CursorClosure    CursorClosure;
+typedef struct _CursorFixture    CursorFixture;
+typedef enum   _CursorTestType   CursorTestType;
+typedef struct _CursorTest       CursorTest;
+
+/* Main Cursor Test Fixture */
+static void           cursor_fixture_setup          (CursorFixture      *fixture,
+                                                    gconstpointer       user_data);
+static void           cursor_fixture_teardown       (CursorFixture      *fixture,
+                                                    gconstpointer       user_data);
+static void           cursor_fixture_set_locale     (CursorFixture      *fixture,
+                                                    const gchar        *locale);
+static void           cursor_fixture_timeout_start  (CursorFixture      *fixture,
+                                                    const gchar        *error_message);
+static void           cursor_fixture_timeout_cancel (CursorFixture      *fixture);
+
+
+/* Main Cursor Test Closure */
+static CursorClosure *cursor_closure_new            (gboolean            async,
+                                                    gboolean            dra,
+                                                    const gchar        *locale);
+static void           cursor_closure_free           (CursorClosure      *closure);
+static void           cursor_test_free              (CursorTest         *test);
+static void           cursor_closure_add            (CursorClosure      *closure,
+                                                    const gchar        *path);
+
+/* Move By Tests */
+static void           cursor_closure_move_by        (CursorClosure      *closure,
+                                                    EBookCursorOrigin   origin,
+                                                    gint                count,
+                                                    gint                expected,
+                                                    ...);
+static void           cursor_test_move_by           (CursorFixture      *fixture,
+                                                    CursorClosure      *closure,
+                                                    CursorTest         *test);
+static void           cursor_test_move_by_free      (CursorTest         *test);
+
+
+/* Set Sexp Tests */
+static void           cursor_closure_set_sexp       (CursorClosure      *closure,
+                                                    EBookQuery         *query,
+                                                    gboolean            expect_success);
+static void           cursor_test_set_sexp          (CursorFixture      *fixture,
+                                                    CursorClosure      *closure,
+                                                    CursorTest         *test);
+static void           cursor_test_set_sexp_free     (CursorTest         *test);
+
+/* Position Tests */
+static void           cursor_closure_position       (CursorClosure      *closure,
+                                                    gint                total,
+                                                    gint                position);
+static void           cursor_test_position          (CursorFixture      *fixture,
+                                                    CursorClosure      *closure,
+                                                    CursorTest         *test);
+static void           cursor_test_position_free     (CursorTest         *test);
+
+
+struct _CursorFixture {
+       ETestServerFixture parent_fixture;
+
+       EBookClientCursor *cursor;
+       EContact          *contacts[N_SORTED_CONTACTS];
+       EBookQuery        *query;
+       guint              cursor_timeout_id;
+
+       EDBusLocale1      *locale1;
+       guint              own_id;
+};
+
+struct _CursorClosure {
+       ETestServerClosure   parent_closure;
+       gchar               *locale;
+       gboolean             async;
+       GList               *tests;
+};
+
+enum _CursorTestType {
+       CURSOR_TEST_MOVE_BY,
+       CURSOR_TEST_SET_SEXP,
+       CURSOR_TEST_POSITION,
+};
+
+struct _CursorTest {
+       CursorTestType type;
+};
+
+/******************************************************
+ *                Main Cursor Test Fixture            *
+ ******************************************************/
+#define N_SORT_FIELDS 2
+static EContactField sort_fields[] = { E_CONTACT_FAMILY_NAME, E_CONTACT_GIVEN_NAME };
+static EBookSortType sort_types[] = { E_BOOK_SORT_ASCENDING, E_BOOK_SORT_ASCENDING };
+
+static gboolean
+cursor_timeout (const gchar *error_message)
+{
+       g_error ("%s", error_message);
+
+       return FALSE;
+}
+
+static void
+cursor_fixture_timeout_start (CursorFixture  *fixture,
+                             const gchar    *error_message)
+{
+       fixture->cursor_timeout_id =
+               g_timeout_add (TIMEOUT, (GSourceFunc) cursor_timeout,
+                              (gpointer)error_message);
+}
+
+static void
+cursor_fixture_timeout_cancel (CursorFixture *fixture)
+{
+       if (fixture->cursor_timeout_id) {
+               g_source_remove (fixture->cursor_timeout_id);
+               fixture->cursor_timeout_id = 0;
+       }
+}
+
+static void
+cursor_fixture_add_contacts (CursorFixture      *fixture,
+                            ETestServerClosure *closure)
+{
+       EBookClient *book_client;
+       GError      *error = NULL;
+       GSList      *contacts = NULL;
+       gint         i;
+
+       book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);
+
+       for (i = 0; i < N_SORTED_CONTACTS; i++) {
+               gchar *case_name = g_strdup_printf ("sorted-%d", i + 1);
+               gchar *vcard;
+               EContact *contact;
+
+               vcard    = new_vcard_from_test_case (case_name);
+               contact  = e_contact_new_from_vcard (vcard);
+               contacts = g_slist_prepend (contacts, contact);
+               g_free (vcard);
+               g_free (case_name);
+
+               fixture->contacts[i] = contact;
+       }
+
+       if (!e_book_client_add_contacts_sync (book_client, contacts, NULL, NULL, &error)) { 
+
+               /* Dont complain here, we re-use the same addressbook for multiple tests
+                * and we can't add the same contacts twice
+                */
+               if (g_error_matches (error, E_BOOK_CLIENT_ERROR,
+                                    E_BOOK_CLIENT_ERROR_CONTACT_ID_ALREADY_EXISTS))
+                       g_clear_error (&error);
+               else
+                       g_error ("Failed to add test contacts: %s", error->message);
+       }
+
+       g_slist_free (contacts);
+
+
+}
+
+static void
+cursor_ready_cb (GObject *source_object,
+                GAsyncResult *result,
+                gpointer user_data)
+{
+       CursorFixture *fixture = (CursorFixture *)user_data;
+       ETestServerFixture *server_fixture = (ETestServerFixture *)fixture;
+       GError *error = NULL;
+
+       if (!e_book_client_get_cursor_finish (E_BOOK_CLIENT (source_object),
+                                             result, &(fixture->cursor), &error))
+               g_error ("Failed to create a cursor: %s", error->message);
+
+       g_main_loop_quit (server_fixture->loop);
+}
+
+static void
+cursor_fixture_setup (CursorFixture *fixture,
+                     gconstpointer  user_data)
+{
+       CursorClosure      *closure        = (CursorClosure *)user_data;
+       ETestServerClosure *server_closure = (ETestServerClosure *)user_data;
+       ETestServerFixture *server_fixture = (ETestServerFixture *)fixture;
+       EBookClient        *book_client;
+       GError             *error = NULL;
+       gchar              *sexp = NULL;
+
+       e_test_server_utils_setup (server_fixture, user_data);
+
+       /* Set the initial locale before adding the contacts */
+       if (closure->locale)
+               cursor_fixture_set_locale (fixture, closure->locale);
+       else
+               cursor_fixture_set_locale (fixture, "en_US.UTF-8");
+
+       cursor_fixture_add_contacts (fixture, server_closure);
+
+       book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);
+
+       /* Allow a surrounding fixture setup
+        * to add a query here
+        */
+       if (fixture->query) {
+               sexp = e_book_query_to_string (fixture->query);
+               e_book_query_unref (fixture->query);
+               fixture->query = NULL;
+       }
+
+       if (server_closure->use_async_connect) {
+
+               e_book_client_get_cursor (book_client,
+                                         sexp,
+                                         sort_fields,
+                                         sort_types,
+                                         N_SORT_FIELDS,
+                                         NULL,
+                                         cursor_ready_cb,
+                                         fixture);
+
+               cursor_fixture_timeout_start (fixture, "Timeout reached while trying to create a cursor");
+               g_main_loop_run (server_fixture->loop);
+               cursor_fixture_timeout_cancel (fixture);
+
+       } else {
+
+               if (!e_book_client_get_cursor_sync (book_client,
+                                                   sexp,
+                                                   sort_fields,
+                                                   sort_types,
+                                                   N_SORT_FIELDS,
+                                                   &fixture->cursor,
+                                                   NULL, &error))
+                       g_error ("Failed to create a cursor with an empty query: %s", error->message);
+       }
+
+       g_free (sexp);
+}
+
+static void
+cursor_fixture_teardown (CursorFixture *fixture,
+                        gconstpointer  user_data)
+{
+       ETestServerFixture *server_fixture = (ETestServerFixture *)fixture;
+       gint i;
+
+       g_object_unref (fixture->cursor);
+       for (i = 0; i < N_SORTED_CONTACTS; i++) {
+               if (fixture->contacts[i])
+                       g_clear_object (&(fixture->contacts[i]));
+       }
+
+       if (fixture->locale1)
+               g_object_unref (fixture->locale1);
+
+       if (fixture->own_id > 0)
+               g_bus_unown_name (fixture->own_id);
+
+       e_test_server_utils_teardown (server_fixture, user_data);
+}
+
+typedef struct {
+       CursorFixture *fixture;
+       const gchar *locale;
+} ChangeLocaleData;
+
+static void
+book_client_locale_change (EBookClient *book,
+                          GParamSpec  *pspec,
+                          ChangeLocaleData *data)
+{
+       ETestServerFixture *base_fixture = (ETestServerFixture *)data->fixture;
+
+       if (!g_strcmp0 (e_book_client_get_locale (book), data->locale))
+               g_main_loop_quit (base_fixture->loop);
+}
+
+static void
+cursor_fixture_set_locale (CursorFixture *fixture,
+                          const gchar   *locale)
+{
+       ETestServerFixture *server_fixture = (ETestServerFixture *)fixture;
+       EBookClient *book_client;
+       ChangeLocaleData data = { fixture, locale };
+       gulong handler_id;
+       gchar *strv[2] = { NULL, NULL };
+
+       book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);
+
+       /* We're already in the right locale */
+       if (g_strcmp0 (locale, e_book_client_get_locale (book_client)) == 0)
+               return;
+
+       if (!fixture->locale1) {
+               GDBusConnection *bus;
+               GError *error = NULL;
+
+               /* We use the 'org.freedesktop.locale1 on the session bus instead
+                * of the system bus only for testing purposes... in real life
+                * this service is on the system bus.
+                */
+               bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
+               if (!bus)
+                       g_error ("Failed to get system bus: %s", error->message);
+
+               fixture->locale1 = e_dbus_locale1_skeleton_new ();
+
+               /* Set initial locale before exporting on the bus */
+               strv[0] = g_strdup_printf ("LANG=%s", locale);
+               e_dbus_locale1_set_locale (fixture->locale1, (const gchar * const *)strv);
+               g_free (strv[0]);
+
+               if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (fixture->locale1),
+                                                      bus, "/org/freedesktop/locale1", &error))
+                       g_error ("Failed to export org.freedesktop.locale1: %s", error->message);
+
+               fixture->own_id =
+                       g_bus_own_name_on_connection (bus,
+                                                     "org.freedesktop.locale1",
+                                                     G_BUS_NAME_OWNER_FLAGS_REPLACE,
+                                                     NULL, NULL, NULL, NULL);
+
+               g_object_unref (bus);
+       } else {
+               /* Send locale change message */
+               strv[0] = g_strdup_printf ("LANG=%s", locale);
+               e_dbus_locale1_set_locale (fixture->locale1, (const gchar * const *)strv);
+               g_free (strv[0]);
+       }
+
+       handler_id = g_signal_connect (book_client, "notify::locale",
+                                      G_CALLBACK (book_client_locale_change), &data);
+
+
+       cursor_fixture_timeout_start (fixture, "Timeout reached while waiting for locale change");
+       g_main_loop_run (server_fixture->loop);
+       cursor_fixture_timeout_cancel (fixture);
+
+       g_signal_handler_disconnect (book_client, handler_id);
+}
+
+static void
+cursor_fixture_test (CursorFixture *fixture,
+                    gconstpointer  user_data)
+{
+       CursorClosure *closure = (CursorClosure *)user_data;
+       GList         *l;
+
+       /* Run all the tests */
+       for (l = closure->tests; l; l = l->next) {
+               CursorTest *test = l->data;
+
+               switch (test->type) {
+               case CURSOR_TEST_MOVE_BY:
+                       cursor_test_move_by (fixture, closure, test);
+                       break;
+               case CURSOR_TEST_SET_SEXP:
+                       cursor_test_set_sexp (fixture, closure, test);
+                       break;
+               case CURSOR_TEST_POSITION:
+                       cursor_test_position (fixture, closure, test);
+                       break;
+               }
+       }
+}
+
+/******************************************************
+ *                Main Cursor Test Closure            *
+ ******************************************************/
+static void
+cursor_test_free (CursorTest    *test)
+{
+       switch (test->type) {
+       case CURSOR_TEST_MOVE_BY:
+               cursor_test_move_by_free (test);
+               break;
+       case CURSOR_TEST_SET_SEXP:
+               cursor_test_set_sexp_free (test);
+               break;
+       case CURSOR_TEST_POSITION:
+               cursor_test_position_free (test);
+               break;
+       }
+}
+
+static void
+cursor_closure_free (CursorClosure *closure)
+{
+       if (closure) {
+               g_list_free_full (closure->tests, (GDestroyNotify)cursor_test_free);
+               g_free (closure->locale);
+               g_slice_free (CursorClosure, closure);
+       }
+}
+
+static CursorClosure *
+cursor_closure_new (gboolean       async,
+                   gboolean       dra,
+                   const gchar   *locale)
+{
+       CursorClosure *closure = g_slice_new0 (CursorClosure);
+       ETestServerClosure *server_closure = (ETestServerClosure *)closure;
+
+       if (dra)
+               server_closure->type = E_TEST_SERVER_DIRECT_ADDRESS_BOOK;
+       else
+               server_closure->type = E_TEST_SERVER_ADDRESS_BOOK;
+
+       server_closure->use_async_connect = async;
+       server_closure->destroy_closure_func = (GDestroyNotify)cursor_closure_free;
+
+       closure->locale = g_strdup (locale);
+       closure->async  = async;
+
+       return closure;
+}
+
+static void
+cursor_closure_add (CursorClosure *closure,
+                   const gchar   *path)
+{
+       g_test_add (path, CursorFixture, closure,
+                   cursor_fixture_setup,
+                   cursor_fixture_test,
+                   cursor_fixture_teardown);
+}
+
+/******************************************************
+ *                     Move By Tests                  *
+ ******************************************************/
+typedef struct {
+       CursorTestType    type;
+
+       EBookCursorOrigin origin;   /* The origin to move from */
+       gint              count;    /* The amount to move the cursor by (positive or negative) */
+       gint              expected; /* The amount of actual results expected */
+       gint              expected_order[N_SORTED_CONTACTS]; /* The expected results */
+} CursorTestMoveBy;
+
+typedef struct {
+       CursorFixture    *fixture;
+       CursorTestMoveBy *test;
+} MoveByReadyData;
+
+static gint
+find_contact_link (EContact        *contact,
+                  const gchar     *uid)
+{
+       const gchar *contact_uid = 
+               (const gchar *)e_contact_get_const (contact, E_CONTACT_UID);
+
+       return g_strcmp0 (contact_uid, uid);
+}
+
+static void
+assert_contacts_order_slist (GSList      *results,
+                            GSList      *uids)
+{
+       gint position = -1;
+       GSList *link, *l;
+
+       /* Assert that all passed UIDs are found in the
+        * results, and that those UIDs are in the
+        * specified order.
+        */
+       for (l = uids; l; l = l->next) {
+               const gchar *uid = l->data;
+               gint new_position;
+
+               link = g_slist_find_custom (results, uid, (GCompareFunc)find_contact_link);
+               if (!link)
+                       g_error ("Specified uid '%s' was not found in results", uid);
+
+               new_position = g_slist_position (results, link);
+               g_assert_cmpint (new_position, >, position);
+               position = new_position;
+       }
+}
+
+static void
+move_by_print_results (GSList      *results,
+                      GSList      *uids)
+{
+       GSList *l;
+
+       if (g_getenv ("TEST_DEBUG") == NULL)
+               return;
+
+       g_print ("\nPRINTING EXPECTED RESULTS:\n");
+       for (l = uids; l; l = l->next) {
+               gchar *uid = l->data;
+
+               g_print ("\t%s\n", uid);
+       }
+       g_print ("\nEXPECTED RESULTS FINISHED\n");
+
+       g_print ("\nPRINTING RESULTS:\n");
+       for (l = results; l; l = l->next) {
+               EContact *contact = l->data;
+               gchar    *vcard = e_vcard_to_string (E_VCARD (contact), EVC_FORMAT_VCARD_30);
+
+               g_print ("\n%s\n", vcard);
+               g_free (vcard);
+       }
+       g_print ("\nRESULT LIST FINISHED\n");
+}
+
+static void
+cursor_test_assert_results (CursorFixture    *fixture,
+                           CursorTestMoveBy *test,
+                           GSList           *results)
+{
+       GSList *uids = NULL;
+       gint    i;
+
+       for (i = 0; i < test->expected; i++) {
+               gchar *uid;
+               gint   index = test->expected_order[i];
+
+               uid = (gchar *)e_contact_get_const (fixture->contacts[index], E_CONTACT_UID);
+               uids = g_slist_append (uids, uid);
+       }
+
+       move_by_print_results (results, uids);
+
+       /* Assert the exact amount of requested results */
+       g_assert_cmpint (g_slist_length (results), ==, test->expected);
+       assert_contacts_order_slist (results, uids);
+       g_slist_free (uids);
+}
+
+static void
+cursor_test_move_by_free (CursorTest *test)
+{
+       CursorTestMoveBy *move_by = (CursorTestMoveBy *)test;
+
+       g_slice_free (CursorTestMoveBy, move_by);
+}
+
+static void
+cursor_test_move_by_ready_cb (GObject      *source_object,
+                             GAsyncResult *result,
+                             gpointer      user_data)
+{
+       MoveByReadyData    *data = (MoveByReadyData *)user_data;
+       ETestServerFixture *server_fixture = (ETestServerFixture *)data->fixture;
+       GSList             *results = NULL;
+       GError             *error = NULL;
+
+       if (!e_book_client_cursor_move_by_finish (E_BOOK_CLIENT_CURSOR (source_object),
+                                                 result, &results, &error))
+               g_error ("Error calling e_book_client_cursor_move_by_finish(): %s",
+                        error->message);
+
+       cursor_test_assert_results (data->fixture, data->test, results);
+       g_slist_free_full (results, (GDestroyNotify)g_object_unref);
+
+       g_main_loop_quit (server_fixture->loop);
+}
+
+static void
+cursor_test_move_by (CursorFixture *fixture,
+                    CursorClosure *closure,
+                    CursorTest    *test)
+{
+       ETestServerFixture *server_fixture = (ETestServerFixture *)fixture;
+       CursorTestMoveBy   *move_by = (CursorTestMoveBy *)test;
+
+       if (closure->async) {
+               MoveByReadyData data = { fixture, move_by };
+
+               e_book_client_cursor_move_by (fixture->cursor,
+                                             move_by->origin,
+                                             move_by->count,
+                                             TRUE,
+                                             NULL,
+                                             cursor_test_move_by_ready_cb,
+                                             &data);
+
+               /* Wait for result with an error timeout */
+               cursor_fixture_timeout_start (fixture, "Timeout reached while moving the cursor");
+               g_main_loop_run (server_fixture->loop);
+               cursor_fixture_timeout_cancel (fixture);
+
+       } else {
+               GError *error = NULL;
+               GSList *results = NULL;
+
+               if (!e_book_client_cursor_move_by_sync (fixture->cursor,
+                                                       move_by->origin,
+                                                       move_by->count,
+                                                       &results,
+                                                       NULL, &error))
+                       g_error ("Error calling e_book_client_cursor_move_by_sync(): %s",
+                                error->message);
+
+
+               cursor_test_assert_results (fixture, move_by, results);
+               g_slist_free_full (results, (GDestroyNotify)g_object_unref);
+       }
+}
+
+static void
+cursor_closure_move_by (CursorClosure      *closure,
+                       EBookCursorOrigin   origin,
+                       gint                count,
+                       gint                expected,
+                       ...)
+{
+       CursorTestMoveBy *test = g_slice_new0 (CursorTestMoveBy);
+       va_list args;
+       gint i;
+
+       g_assert (expected <= N_SORTED_CONTACTS);
+       g_assert (ABS (count) <= N_SORTED_CONTACTS);
+
+       test->type     = CURSOR_TEST_MOVE_BY;
+       test->origin   = origin;
+       test->count    = count;
+       test->expected = expected;
+
+       va_start (args, expected);
+       for (i = 0; i < count; i++) {
+               gint expected = va_arg (args, gint);
+
+               /* Sanity check while building the test case */
+               g_assert_cmpint (expected, >, 0);
+
+               test->expected_order[i] = expected - 1;
+       }
+       va_end (args);
+
+       closure->tests = g_list_append (closure->tests, test);
+}
+
+/******************************************************
+ *                     Set Sexp Tests                 *
+ ******************************************************/
+typedef struct {
+       CursorTestType    type;
+
+       gchar            *sexp;
+       gboolean          expect_success;
+} CursorTestSetSexp;
+
+typedef struct {
+       CursorFixture     *fixture;
+       CursorTestSetSexp *test;
+} SetSexpReadyData;
+
+static void
+cursor_closure_set_sexp (CursorClosure      *closure,
+                        EBookQuery         *query,
+                        gboolean            expect_success)
+{
+       CursorTestSetSexp *test = g_slice_new0 (CursorTestSetSexp);
+
+       g_assert (query != NULL);
+
+       test->type           = CURSOR_TEST_SET_SEXP;
+       test->sexp           = e_book_query_to_string (query);
+       test->expect_success = expect_success;
+
+       e_book_query_unref (query);
+
+       closure->tests = g_list_append (closure->tests, test);
+}
+
+static void
+cursor_test_set_sexp_assert (CursorTestSetSexp  *test,
+                            gboolean            success,
+                            GError             *error)
+{
+       if (test->expect_success) {
+               if (!success)
+                       g_error ("Failed to set sexp '%s': %s",
+                                test->sexp, error->message);
+       } else {
+
+               if (success)
+                       g_error ("Unexpected success setting sexp '%s'",
+                                test->sexp);
+               else if (!g_error_matches (error,
+                                          E_CLIENT_ERROR,
+                                          E_CLIENT_ERROR_INVALID_QUERY))
+                       g_error ("Wrong error message when failing to set a search expression: "
+                                "Domain '%s' Code '%d' Message: %s",
+                                g_quark_to_string (error->domain),
+                                error->code,
+                                error->message);
+       }
+}
+
+static void
+cursor_test_set_sexp_ready_cb (GObject      *source_object,
+                              GAsyncResult *result,
+                              gpointer      user_data)
+{
+       SetSexpReadyData *data = (SetSexpReadyData *)user_data;
+       gboolean          success;
+       GError           *error = NULL;
+
+       success = e_book_client_cursor_set_sexp_finish (E_BOOK_CLIENT_CURSOR (source_object),
+                                                       result, &error);
+       cursor_test_set_sexp_assert (data->test, success, error);
+       g_clear_error (&error);
+}
+
+static void
+cursor_test_set_sexp (CursorFixture      *fixture,
+                     CursorClosure      *closure,
+                     CursorTest         *test)
+{
+       ETestServerFixture *server_fixture = (ETestServerFixture *)fixture;
+       CursorTestSetSexp  *set_sexp = (CursorTestSetSexp *)test;
+
+       if (closure->async) {
+               SetSexpReadyData data = { fixture, set_sexp };
+
+               e_book_client_cursor_set_sexp (fixture->cursor,
+                                              set_sexp->sexp,
+                                              NULL,
+                                              cursor_test_set_sexp_ready_cb,
+                                              &data);
+
+               /* Wait for result with an error timeout */
+               cursor_fixture_timeout_start (fixture, "Timeout reached while moving the cursor");
+               g_main_loop_run (server_fixture->loop);
+               cursor_fixture_timeout_cancel (fixture);
+       } else {
+               gboolean  success;
+               GError   *error = NULL;
+               
+               success = e_book_client_cursor_set_sexp_sync (fixture->cursor,
+                                                             set_sexp->sexp,
+                                                             NULL, 
+                                                             &error);
+               cursor_test_set_sexp_assert (set_sexp, success, error);
+               g_clear_error (&error);
+       }
+}
+
+static void
+cursor_test_set_sexp_free (CursorTest *test)
+{
+       CursorTestSetSexp *set_sexp = (CursorTestSetSexp *)test;
+
+       g_free (set_sexp->sexp);
+       g_slice_free (CursorTestSetSexp, set_sexp);
+}
+
+
+/******************************************************
+ *                   Position Tests                   *
+ ******************************************************/
+typedef struct {
+       CursorTestType    type;
+
+       gint              total;
+       gint              position;
+
+       gulong            total_id;
+       gulong            position_id;
+} CursorTestPosition;
+
+typedef struct {
+       CursorFixture      *fixture;
+       CursorTestPosition *test;
+} PositionData;
+
+static void
+cursor_closure_position (CursorClosure *closure,
+                        gint           total,
+                        gint           position)
+{
+       CursorTestPosition *test = g_slice_new0 (CursorTestPosition);
+
+       test->type     = CURSOR_TEST_POSITION;
+       test->total    = total;
+       test->position = position;
+
+       closure->tests = g_list_append (closure->tests, test);
+}
+
+static void
+position_or_total_changed (EBookClientCursor *cursor,
+                          GParamSpec        *pspec,
+                          PositionData      *data)
+{
+       ETestServerFixture *server_fixture = (ETestServerFixture *)data->fixture;
+
+       if (g_getenv ("TEST_DEBUG") != NULL) {
+               g_print ("Position changed, total: %d position: %d (expecting total: %d position: %d)\n",
+                        e_book_client_cursor_get_total (cursor),
+                        e_book_client_cursor_get_position (cursor),
+                        data->test->total,
+                        data->test->position);
+       }
+
+       if (data->test->total    == e_book_client_cursor_get_total (cursor) &&
+           data->test->position == e_book_client_cursor_get_position (cursor))
+               g_main_loop_quit (server_fixture->loop);
+}
+
+static void
+cursor_test_position (CursorFixture *fixture,
+                     CursorClosure *closure,
+                     CursorTest    *test)
+{
+       ETestServerFixture *server_fixture = (ETestServerFixture *)fixture;
+       CursorTestPosition *position = (CursorTestPosition *)test;
+       PositionData        data = { fixture, position };
+
+       if (g_getenv ("TEST_DEBUG") != NULL) {
+               g_print ("Actual total: %d position: %d, Waiting for total: %d position: %d\n",
+                        e_book_client_cursor_get_total (fixture->cursor),
+                        e_book_client_cursor_get_position (fixture->cursor),
+                        position->total,
+                        position->position);
+       }
+
+       /* Position is already correct */
+       if (position->total    == e_book_client_cursor_get_total (fixture->cursor) &&
+           position->position == e_book_client_cursor_get_position (fixture->cursor))
+               return;
+
+       /* Position & Total is notified asynchronously, connect to signals and
+        * timeout error if the correct position / total is never reached.
+        */
+       position->total_id = g_signal_connect (fixture->cursor, "notify::total",
+                                              G_CALLBACK (position_or_total_changed),
+                                              &data);
+       position->position_id = g_signal_connect (fixture->cursor, "notify::position",
+                                                 G_CALLBACK (position_or_total_changed),
+                                                 &data);
+
+       cursor_fixture_timeout_start (fixture, "Timeout waiting for expected position and total");
+       g_main_loop_run (server_fixture->loop);
+       cursor_fixture_timeout_cancel (fixture);
+
+       g_signal_handler_disconnect (fixture->cursor, position->total_id);
+       g_signal_handler_disconnect (fixture->cursor, position->position_id);
+}
+
+static void
+cursor_test_position_free (CursorTest *test)
+{
+       CursorTestPosition *position = (CursorTestPosition *)test;
+
+       g_slice_free (CursorTestPosition, position);
+}
+
+/******************************************************
+ *                 Main, tests defined here           *
+ ******************************************************/
+typedef struct {
+       const gchar *base_path;
+       gboolean     async;
+       gboolean     dra;
+} BaseParams;
+
+static BaseParams base_params[] = {
+       { "/EBookClientCursor/Sync",         FALSE, FALSE },
+       { "/EBookClientCursor/Async",        TRUE,  FALSE },
+       { "/EBookClientCursor/Direct/Sync",  FALSE, TRUE },
+       { "/EBookClientCursor/Direct/Async", TRUE,  TRUE },
+};
+
+gint
+main (gint argc,
+      gchar **argv)
+{
+       CursorClosure *closure;
+       gchar         *test_path;
+       gint           i;
+
+       g_test_init (&argc, &argv, NULL);
+       g_test_bug_base ("http://bugzilla.gnome.org/";);
+
+       for (i = 0; i < G_N_ELEMENTS (base_params); i++) {
+
+               /****************************************************
+                *             BASIC SORT ORDERING TESTS            *
+                ****************************************************
+                *
+                * Note that all sort ordering can be confirmed with the
+                * chart found in tests/libedata-book/data-test-utils.h
+                */
+
+               /* POSIX Order Test */
+               test_path = g_strconcat (base_params[i].base_path, "/Order/POSIX", NULL);
+               closure = cursor_closure_new (base_params[i].async, base_params[i].dra, "POSIX");
+
+               cursor_closure_position (closure, 20, 0);
+               cursor_closure_move_by (closure,
+                                       E_BOOK_CURSOR_ORIGIN_CURRENT,
+                                       10, /* Count */
+                                       10, /* Expected results */
+                                       11, 2,  6,  3,  8, 1,  5,  4,  7,  15);
+               cursor_closure_position (closure, 20, 10);
+               cursor_closure_move_by (closure,
+                                       E_BOOK_CURSOR_ORIGIN_CURRENT,
+                                       10, /* Count */
+                                       10, /* Expected results */
+                                       17, 16, 18, 10, 14, 12, 13, 9,  19, 20);
+               cursor_closure_position (closure, 20, 20);
+               cursor_closure_add (closure, test_path);
+               g_free (test_path);
+
+               /* en_US Order Test */
+               test_path = g_strconcat (base_params[i].base_path, "/Order/en_US", NULL);
+               closure = cursor_closure_new (base_params[i].async, base_params[i].dra, "en_US.utf8");
+
+               cursor_closure_position (closure, 20, 0);
+               cursor_closure_move_by (closure,
+                                       E_BOOK_CURSOR_ORIGIN_CURRENT,
+                                       10, /* Count */
+                                       10, /* Expected results */
+                                       11, 1,  2,  5,  6, 4,  3,  7,  8,  15);
+               cursor_closure_position (closure, 20, 10);
+               cursor_closure_move_by (closure,
+                                       E_BOOK_CURSOR_ORIGIN_CURRENT,
+                                       10, /* Count */
+                                       10, /* Expected results */
+                                       17, 16, 18, 10, 14, 12, 13, 9,  19, 20);
+               cursor_closure_position (closure, 20, 20);
+               cursor_closure_add (closure, test_path);
+               g_free (test_path);
+
+               /* fr_CA Order Test */
+               test_path = g_strconcat (base_params[i].base_path, "/Order/fr_CA", NULL);
+               closure = cursor_closure_new (base_params[i].async, base_params[i].dra, "fr_CA.utf8");
+
+               cursor_closure_position (closure, 20, 0);
+               cursor_closure_move_by (closure,
+                                       E_BOOK_CURSOR_ORIGIN_CURRENT,
+                                       10, /* Count */
+                                       10, /* Expected results */
+                                       11, 1,  2,  5,  6, 4,  3,  7,  8,  15);
+               cursor_closure_position (closure, 20, 10);
+               cursor_closure_move_by (closure,
+                                       E_BOOK_CURSOR_ORIGIN_CURRENT,
+                                       10, /* Count */
+                                       10, /* Expected results */
+                                       17, 16, 18, 10, 14, 13, 12, 9,  19, 20);
+               cursor_closure_position (closure, 20, 20);
+               cursor_closure_add (closure, test_path);
+               g_free (test_path);
+
+               /* de_DE Order Test */
+               test_path = g_strconcat (base_params[i].base_path, "/Order/de_DE", NULL);
+               closure = cursor_closure_new (base_params[i].async, base_params[i].dra, "de_DE.utf8");
+
+               cursor_closure_position (closure, 20, 0);
+               cursor_closure_move_by (closure,
+                                       E_BOOK_CURSOR_ORIGIN_CURRENT,
+                                       10, /* Count */
+                                       10, /* Expected results */
+                                       11, 1,  2,  5,  6, 7,  8,  4,  3,  15);
+
+               cursor_closure_position (closure, 20, 10);
+               cursor_closure_move_by (closure,
+                                       E_BOOK_CURSOR_ORIGIN_CURRENT,
+                                       10, /* Count */
+                                       10, /* Expected results */
+                                       17, 16, 18, 10, 14, 12, 13, 9,  20, 19);
+               cursor_closure_position (closure, 20, 20);
+
+               cursor_closure_add (closure, test_path);
+               g_free (test_path);
+
+       }
+
+       return e_test_server_utils_run ();
+}


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