[evolution-data-server/openismus-work-master: 35/50] tests/libedata-book: Added a testing API to set the locale dynamically



commit 70d8ca7388b8c692c94fae6ac085fbd10afeb893
Author: Tristan Van Berkom <tristanvb openismus com>
Date:   Fri May 17 18:31:50 2013 +0900

    tests/libedata-book: Added a testing API to set the locale dynamically
    
    Also, the 'move-by' testing API now has a locale argument.

 tests/libedata-book/Makefile.am       |    6 ++
 tests/libedata-book/data-test-utils.c |  107 +++++++++++++++++++++++++++++++--
 tests/libedata-book/data-test-utils.h |   11 +++-
 3 files changed, 117 insertions(+), 7 deletions(-)
---
diff --git a/tests/libedata-book/Makefile.am b/tests/libedata-book/Makefile.am
index 98726c6..2fadc32 100644
--- a/tests/libedata-book/Makefile.am
+++ b/tests/libedata-book/Makefile.am
@@ -1,3 +1,4 @@
+
 noinst_LTLIBRARIES = libdata-test-utils.la
 
 libdata_test_utils_la_SOURCES =                                        \
@@ -17,11 +18,14 @@ libdata_test_utils_la_CPPFLAGS =                            \
        -I$(top_builddir)/tests/libedataserver                  \
        -I$(top_srcdir)/tests/test-server-utils                 \
        -I$(top_builddir)/tests/test-server-utils               \
+       -I$(top_srcdir)/private                                 \
+       -I$(top_builddir)/private                               \
        -DBACKENDDIR=\"$(ebook_backenddir)\"                    \
        -DDATADIR=\"$(datadir)\"                                \
        -DSRCDIR=\""$(abs_srcdir)"\"                            \
        -DBUILDDIR=\""$(abs_topbuilddir)"\"                     \
        $(EVOLUTION_ADDRESSBOOK_CFLAGS)                         \
+       $(GIO_UNIX_CFLAGS)                                      \
        $(CAMEL_CFLAGS)                                         \
        $(NULL)
 
@@ -30,7 +34,9 @@ libdata_test_utils_la_LIBADD =                                \
        $(top_builddir)/addressbook/libedata-book/libedata-book-1.2.la  \
        $(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)                           \
+       $(GIO_UNIX_LIBS)                                        \
        $(CAMEL_LIBS)                                           \
        $(NULL)
 
diff --git a/tests/libedata-book/data-test-utils.c b/tests/libedata-book/data-test-utils.c
index 9e548ce..c95744a 100644
--- a/tests/libedata-book/data-test-utils.c
+++ b/tests/libedata-book/data-test-utils.c
@@ -202,7 +202,6 @@ e_sqlitedb_cursor_fixture_setup_book (ESource            *scratch,
                                      ETestServerClosure *closure)
 {
        ESourceBackendSummarySetup *setup;
-       EbSdbCursorClosure *data = (EbSdbCursorClosure *)closure;
 
        g_type_class_unref (g_type_class_ref (E_TYPE_SOURCE_BACKEND_SUMMARY_SETUP));
        setup = e_source_get_extension (scratch, E_SOURCE_EXTENSION_BACKEND_SUMMARY_SETUP);
@@ -223,6 +222,8 @@ e_sqlitedb_cursor_fixture_setup (EbSdbCursorFixture *fixture,
                                 gconstpointer       user_data)
 {
        ETestServerFixture *base_fixture = (ETestServerFixture *)fixture;
+       ESqliteDBFixture *ebsdb_fixture = (ESqliteDBFixture *)fixture;
+       EbSdbCursorClosure *data = (EbSdbCursorClosure *)user_data;
        EContactField sort_fields[] = { E_CONTACT_FAMILY_NAME, E_CONTACT_GIVEN_NAME };
        EBookSortType sort_types[] = { E_BOOK_SORT_ASCENDING, E_BOOK_SORT_ASCENDING };
        EBookClient *book_client;
@@ -237,10 +238,21 @@ e_sqlitedb_cursor_fixture_setup (EbSdbCursorFixture *fixture,
        if (source_name != NULL)
                base_fixture->source_name = g_strdup (source_name);
 
-       e_sqlitedb_fixture_setup ((ESqliteDBFixture *)fixture, user_data);
-
+       /* Setup the EBookClient, but don't open the EBookBackendSqliteDB until after
+        * we've specified the locale
+        */
+       e_test_server_utils_setup ((ETestServerFixture *)fixture, user_data);
        book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);
 
+       if (data->locale)
+               e_sqlitedb_cursor_fixture_set_locale (fixture, data->locale);
+       else
+               e_sqlitedb_cursor_fixture_set_locale (fixture, "en_US.UTF-8");
+
+       /* Now open the EBookBackendSqliteDB */
+       ebsdb_fixture->ebsdb = open_sqlitedb (((ETestServerFixture *)fixture)->registry,
+                                             e_client_get_source (E_CLIENT (book_client)));
+
        for (i = 0; i < N_SORTED_CONTACTS; i++) {
                gchar *case_name = g_strdup_printf ("sorted-%d", i + 1);
                gchar *vcard;
@@ -305,10 +317,91 @@ e_sqlitedb_cursor_fixture_teardown (EbSdbCursorFixture *fixture,
                        g_object_unref (fixture->contacts[i]);
        }
 
+       if (fixture->locale1)
+               g_object_unref (fixture->locale1);
+
+       if (fixture->own_id > 0)
+               g_bus_unown_name (fixture->own_id);
+
        e_book_backend_sqlitedb_cursor_free (((ESqliteDBFixture *) fixture)->ebsdb, fixture->cursor);
        e_sqlitedb_fixture_teardown ((ESqliteDBFixture *)fixture, user_data);
 }
 
+typedef struct {
+       EbSdbCursorFixture *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);
+}
+
+void
+e_sqlitedb_cursor_fixture_set_locale (EbSdbCursorFixture *fixture,
+                                     const gchar        *locale)
+{
+       ETestServerFixture *base_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);
+       g_main_loop_run (base_fixture->loop);
+       g_signal_handler_disconnect (book_client, handler_id);
+}
+
 static gint
 find_contact_data (EbSdbSearchData *data,
                   const gchar     *uid)
@@ -393,6 +486,7 @@ print_results (GSList      *results)
 
 static MoveByData *
 move_by_test_new_internal (const gchar *test_path,
+                          const gchar *locale,
                           gsize        struct_size)
 {
        MoveByData *data;
@@ -400,6 +494,7 @@ move_by_test_new_internal (const gchar *test_path,
        data = g_slice_alloc0 (struct_size);
        data->parent.parent.type = E_TEST_SERVER_ADDRESS_BOOK;
        data->parent.parent.customize = e_sqlitedb_cursor_fixture_setup_book;
+       data->parent.locale = g_strdup (locale);
        data->path = g_strdup (test_path);
        data->struct_size = struct_size;
 
@@ -414,13 +509,15 @@ static void
 move_by_test_free (MoveByData *data)
 {
        g_free (data->path);
+       g_free ((gchar *)data->parent.locale);
        g_slice_free1 (data->struct_size, data);
 }
 
 MoveByData *
-move_by_test_new (const gchar *test_path)
+move_by_test_new (const gchar *test_path,
+                 const gchar *locale)
 {
-       return move_by_test_new_internal (test_path, sizeof (MoveByData));
+       return move_by_test_new_internal (test_path, locale, sizeof (MoveByData));
 }
 
 static void
diff --git a/tests/libedata-book/data-test-utils.h b/tests/libedata-book/data-test-utils.h
index 3c413f8..5ec726a 100644
--- a/tests/libedata-book/data-test-utils.h
+++ b/tests/libedata-book/data-test-utils.h
@@ -25,7 +25,7 @@
 #include <libebook/libebook.h>
 #include <libedata-book/libedata-book.h>
 #include "e-test-server-utils.h"
-
+#include "e-dbus-localed.h"
 
 /* This legend shows the add order, and various sort order of the sorted
  * vcards. The UIDs of these contacts are formed as 'sorted-1', 'sorted-2' etc
@@ -82,11 +82,15 @@ typedef struct {
        EbSdbCursor     *cursor;
        EContact        *contacts[N_SORTED_CONTACTS];
        EBookQuery      *query;
+
+       EDBusLocale1    *locale1;
+       guint            own_id;
 } EbSdbCursorFixture;
 
 typedef struct {
        ETestServerClosure parent;
 
+       const gchar *locale;
 } EbSdbCursorClosure;
 
 typedef struct {
@@ -114,6 +118,8 @@ void     e_sqlitedb_cursor_fixture_setup    (EbSdbCursorFixture *fixture,
                                             gconstpointer       user_data);
 void     e_sqlitedb_cursor_fixture_teardown (EbSdbCursorFixture *fixture,
                                             gconstpointer       user_data);
+void     e_sqlitedb_cursor_fixture_set_locale (EbSdbCursorFixture *fixture,
+                                              const gchar        *locale);
 
 /* Filters contacts with E_CONTACT_EMAIL ending with '.com' */
 void     e_sqlitedb_cursor_fixture_filtered_setup (EbSdbCursorFixture *fixture,
@@ -139,7 +145,8 @@ void     print_results                     (GSList      *results);
 void        move_by_test_add_assertion     (MoveByData  *data,
                                            gint         count,
                                            ...);
-MoveByData *move_by_test_new               (const gchar *test_path);
+MoveByData *move_by_test_new               (const gchar *test_path,
+                                           const gchar *locale);
 void        move_by_test_add               (MoveByData  *data,
                                            gboolean     filtered);
 


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