[evolution-data-server/openismus-work] tests: Fix test-client-change-country-code



commit 8051c2a1802d4b04cbea0409ff026f05598ce146
Author: Mathias Hasselmann <mathias openismus com>
Date:   Tue Mar 5 11:04:54 2013 +0100

    tests: Fix test-client-change-country-code
    
    Make it work again and make sure it is self-contained.

 tests/libebook/client/Makefile.am                  |    2 +-
 .../client/test-client-change-country-code.c       |  464 +++++++++++++-------
 2 files changed, 311 insertions(+), 155 deletions(-)
---
diff --git a/tests/libebook/client/Makefile.am b/tests/libebook/client/Makefile.am
index 33ad444..b62e14f 100644
--- a/tests/libebook/client/Makefile.am
+++ b/tests/libebook/client/Makefile.am
@@ -59,6 +59,7 @@ TESTS =                                                               \
        test-client-remove-contacts                             \
        test-client-photo-is-uri                                \
        test-client-async                                       \
+       test-client-change-country-code                         \
        $(NULL)
 
 # The noinst tests are functional tests, not unit tests.
@@ -68,7 +69,6 @@ TESTS =                                                               \
 noinst_PROGRAMS =                                              \
        $(TESTS)                                                \
        test-client-upgrade-addressbook                         \
-       test-client-change-country-code                         \
        test-client-examine                                     \
        test-client                                             \
        test-client-nonexistent-id                              \
diff --git a/tests/libebook/client/test-client-change-country-code.c 
b/tests/libebook/client/test-client-change-country-code.c
index b4c8c67..16b5867 100644
--- a/tests/libebook/client/test-client-change-country-code.c
+++ b/tests/libebook/client/test-client-change-country-code.c
@@ -24,32 +24,19 @@
 #include <errno.h>
 #include <locale.h>
 #include <langinfo.h>
+#include <string.h>
 
 #include <libebook/libebook.h>
 
 #include "client-test-utils.h"
+#include "e-test-server-utils.h"
 
+#define TEST_SOURCE_UID "change-country-code-test"
 
-/* This forces the GType to be registered in a way that
- * avoids a "statement with no effect" compiler warning.
- * FIXME Use g_type_ensure() once we require GLib 2.34. */
-#define REGISTER_TYPE(type) \
-       (g_type_class_unref (g_type_class_ref (type)))
-
-static void
-setup_custom_summary (ESource *scratch)
-{
-       ESourceBackendSummarySetup *setup;
-
-       REGISTER_TYPE (E_TYPE_SOURCE_BACKEND_SUMMARY_SETUP);
-       setup = e_source_get_extension (scratch, E_SOURCE_EXTENSION_BACKEND_SUMMARY_SETUP);
-       e_source_backend_summary_setup_set_summary_fields (setup,
-                                                          E_CONTACT_TEL,
-                                                          0);
-       e_source_backend_summary_setup_set_indexed_fields (setup,
-                                                          E_CONTACT_TEL, E_BOOK_INDEX_PHONE,
-                                                          0);
-}
+typedef struct {
+       ETestServerClosure       closure;
+       const gchar             *exec_path;
+} TestData;
 
 static gboolean
 query_service_pid (const char  *name,
@@ -84,91 +71,124 @@ query_service_pid (const char  *name,
        return TRUE;
 }
 
-static gboolean
-spawn_addressbook_factory (GPid *pid, GError **error)
+static void
+run_forked_test (const TestData *data,
+                 const gchar *path)
 {
-       int i;
-       GPid child_pid = 0;
-       GPid service_pid = 0;
-
-       char *argv[] = {
-               g_build_filename (BUILDDIR, 
"services/evolution-addressbook-factory/evolution-addressbook-factory", NULL),
-               NULL
-       };
-
-       gboolean success = g_spawn_async (NULL, argv, NULL,
-                                          G_SPAWN_LEAVE_DESCRIPTORS_OPEN,
-                                          NULL, NULL, &child_pid, error);
+       GTestTrapFlags test_flags = 0;
+       GPid pid = 0;
 
-       g_free (argv[0]);
+       if (!g_test_verbose ())
+               test_flags |= G_TEST_TRAP_SILENCE_STDOUT;
 
-       /* Waiting a bit for the addressbook factory starting up so that it can claim its D-Bus name */
-       for (i = 0; success && service_pid == 0 && i < 10; ++i) {
-               sleep_in_main_loop (150);
-               success = query_service_pid (ADDRESS_BOOK_DBUS_SERVICE_NAME, &service_pid, error);
+       if (g_test_trap_fork (0, test_flags)) {
+               execl (data->exec_path, data->exec_path, "run-forked", path, g_test_verbose () ? "--verbose" 
: NULL, NULL);
+               g_error ("execl failed: %s", g_strerror (errno));
+               exit (1);
        }
 
-       if (success) {
-               g_assert_cmpuint (service_pid, ==, child_pid);
+       g_test_trap_assert_passed ();
 
-               if (pid)
-                       *pid = child_pid;
-       }
+       if (query_service_pid (ADDRESS_BOOK_DBUS_SERVICE_NAME, &pid, NULL))
+              kill (pid, SIGTERM);
+}
 
-       return success;
+static gchar *
+make_path (const TestData *data,
+           const gchar *suffix)
+{
+       return g_strdup_printf (
+               "/EBookClient/%s%s/PhoneNumber/ChangeCountryCode/%s",
+               data->closure.customize ? "Custom" : "Default",
+               data->closure.type == E_TEST_SERVER_DIRECT_ADDRESS_BOOK ? "/DirectAccess" : "",
+               suffix);
 }
 
-gint
-main (gint argc,
-      gchar **argv)
+static void
+test_change_country_code (gconstpointer user_data)
 {
-       EBookClient *client;
-       GError *error = NULL;
-       EContact *contact;
-       gchar *source_uid;
-       gchar *contact_uid;
-       EVCardAttribute *attr;
-       GList *e164_values;
-       gboolean success;
-       ESource *source;
-       GPid factory_pid = 0;
-       GSList *fetched_uids;
+       const TestData *data = user_data;
+       gchar *region;
+       gchar *path;
 
-       g_test_init (&argc, &argv, NULL);
+       /* Starting with U.S. locale */
+       g_setenv ("LC_ADDRESS", "en_US.UTF-8", TRUE);
+       setlocale (LC_ADDRESS, "");
 
-       /*** Setup ****/
+       region = e_phone_number_get_default_region ();
+       g_assert_cmpstr (region, ==, "US");
+       g_free (region);
 
-       /* Initializing fake D-Bus and event loop machinery */
-       main_initialize ();
-       sleep_in_main_loop (500);
+       /* Create the addressbook with U.S. locale */
+       path = make_path (data, "Setup");
+       run_forked_test (data, path);
+       g_free (path);
 
-       /* Setting with U.S. locale for addresses */
-       g_setenv ("LC_ADDRESS", "en_US.UTF-8", TRUE);
+       /* Test the addressbook with U.S. locale */
+       path = make_path (data, "UnitedStates");
+       run_forked_test (data, path);
+       g_free (path);
+
+       /* Switching to British locale */
+       g_setenv ("LC_ADDRESS", "en_GB.UTF-8", TRUE);
        setlocale (LC_ADDRESS, "");
-       g_assert_cmpstr (nl_langinfo (_NL_ADDRESS_COUNTRY_AB2), ==, "US");
 
-       /* Launching addressbook factory on fake-dbus */
-       success = spawn_addressbook_factory (&factory_pid, &error);
-       g_assert_cmpstr (error ? error->message : NULL, ==, NULL);
-       g_assert_cmpuint (factory_pid, !=, 0);
-       g_assert (success);
+       region = e_phone_number_get_default_region ();
+       g_assert_cmpstr (region, ==, "GB");
+       g_free (region);
 
-       /* Creating the test addressbook */
-       client = new_custom_temp_client (&source_uid, setup_custom_summary);
-       g_assert (client != NULL);
+       /* Test the addressbook with British locale */
+       path = make_path (data, "GreatBritain");
+       run_forked_test (data, path);
+       g_free (path);
+}
 
-       success = e_client_open_sync (E_CLIENT (client), FALSE, NULL, &error);
-       g_assert_cmpstr (error ? error->message : NULL, ==, NULL);
-       g_assert (success);
+static void
+setup_custom_book (ESource            *scratch,
+                  ETestServerClosure *closure)
+{
+       ESourceBackendSummarySetup *setup;
+
+       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);
+       e_source_backend_summary_setup_set_summary_fields (setup, E_CONTACT_TEL, 0);
+       e_source_backend_summary_setup_set_indexed_fields (setup, E_CONTACT_TEL, E_BOOK_INDEX_PHONE, 0);
+}
+
+static TestData *
+test_data_new (ETestSourceCustomizeFunc  custom_summary,
+               ETestServiceType          service_type,
+               const gchar              *exec_path)
+{
+       TestData *data = g_new0 (TestData, 1);
+
+       data->closure.keep_work_directory = TRUE;
+       data->closure.customize = custom_summary;
+       data->closure.type = service_type;
+       data->exec_path = exec_path;
+
+       return data;
+}
+
+static void
+save_test_contact (EBookClient *client,
+                   const gchar *vcard_string,
+                   const gchar *national_number)
+{
+       GError          *error = NULL;
+       gchar           *contact_uid;
+       EContact        *contact;
+       gboolean         success;
+       EVCardAttribute *attr;
+       GList           *params;
 
        /* Add test contact */
-       contact = e_contact_new_from_vcard ("BEGIN:VCARD\nTEL:221.542.3789\nEND:VCARD");
+       contact = e_contact_new_from_vcard (vcard_string);
        success = e_book_client_add_contact_sync (client, contact, &contact_uid, NULL, &error);
-       g_assert_cmpstr (error ? error->message : NULL, ==, NULL);
-       g_assert (success);
        g_object_unref (contact);
 
-       /*** Verification of U.S. context */
+       g_assert_cmpstr (error ? error->message : NULL, ==, NULL);
+       g_assert (success);
 
        /* Fetch the contact by UID and check EVC_TEL attribute and its EVC_X_E164 parameter */
        success = e_book_client_get_contact_sync (client, contact_uid, &contact, NULL, &error);
@@ -178,110 +198,246 @@ main (gint argc,
        attr = e_vcard_get_attribute (E_VCARD (contact), EVC_TEL);
 
        g_assert (attr != NULL);
-       g_assert_cmpstr (e_vcard_attribute_get_value (attr), ==, "221.542.3789");
+       g_assert_cmpstr (e_vcard_attribute_get_value (attr), ==, "123.456");
 
-       e164_values = e_vcard_attribute_get_param (attr, EVC_X_E164);
+       params = e_vcard_attribute_get_param (attr, EVC_X_E164);
 
-       g_assert (e164_values != NULL);
-       g_assert_cmpstr (e164_values->data, ==, "+12215423789");
+       g_assert (params != NULL);
+       g_assert (params->next == NULL);
+       g_assert_cmpstr (params->data, ==, national_number);
 
-       /* Now resolve the contact via its phone number, assuming indexes are used */
-       success = e_book_client_get_contacts_uids_sync (
-                       client, "(eqphone \"phone\" \"+1/221/5423789\")",
-                       &fetched_uids, NULL, &error);
-       g_assert_cmpstr (error ? error->message : NULL, ==, NULL);
-       g_assert (success);
+       g_object_unref (contact);
+}
 
-       g_assert_cmpuint (g_slist_length (fetched_uids), ==, 1);
-       g_assert_cmpstr (fetched_uids->data, ==, contact_uid);
-       g_slist_free_full (fetched_uids, g_free);
+static void
+find_contacts (EBookClient *client,
+               const gchar *query,
+               const gchar *national_number,
+               gint         n_names,
+               ...)
+{
+       GHashTable *expected_names = g_hash_table_new (g_str_hash, g_str_equal);
+       GError     *error = NULL;
+       GSList     *contacts, *l;
+       gboolean    success;
+       va_list     args;
+       gint        i;
 
-       success = e_book_client_get_contacts_uids_sync (
-                       client, "(eqphone \"phone\" \"+49 (221) 5423789\")",
-                       &fetched_uids, NULL, &error);
+       g_test_message ("the query is: %s", query);
+
+       va_start (args, n_names);
+
+       for (i = 0; i < n_names; ++i) {
+               gchar *name = va_arg (args, gchar *);
+               g_hash_table_add (expected_names, name);
+       }
+
+       va_end (args);
+
+       success = e_book_client_get_contacts_sync (client, query, &contacts, NULL, &error);
        g_assert_cmpstr (error ? error->message : NULL, ==, NULL);
        g_assert (success);
 
-       g_assert_cmpuint (g_slist_length (fetched_uids), ==, 0);
-       g_slist_free_full (fetched_uids, g_free);
+       for (l = contacts; l; l = l->next) {
+               const gchar *name;
+               gboolean name_found;
+               EVCardAttribute *attr;
+               GList *params;
 
-       /*** Switch to German locale */
+               name = e_contact_get_const (l->data, E_CONTACT_FULL_NAME);
+               name_found = g_hash_table_remove (expected_names, name);
+               g_assert_cmpstr (name_found ? name : NULL, ==, name);
 
-       /* Shutdown address book factory on fake D-Bus */
-       g_object_unref (client);
+               attr = e_vcard_get_attribute (l->data, EVC_TEL);
+               g_assert (attr != NULL);
+               g_assert_cmpstr (e_vcard_attribute_get_value (attr), ==, "123.456");
 
-       success = (kill (factory_pid, SIGTERM) == 0);
-       g_assert (success);
-       factory_pid = 0;
+               params = e_vcard_attribute_get_param (attr, EVC_X_E164);
 
-       /* Wait a bit to let GDBus notice what happened... */
-       sleep_in_main_loop (1500);
+               g_assert (params != NULL);
+               g_assert (params->next == NULL);
+               g_assert_cmpstr (params->data, ==, national_number);
+       }
 
-       /* Switch to German locale */
-       g_setenv ("LC_ADDRESS", "de_DE.UTF-8", TRUE);
-       setlocale (LC_ADDRESS, "");
+       g_assert_cmpint (g_slist_length (contacts), ==, n_names);
+       g_assert_cmpint (g_hash_table_size (expected_names), ==, 0);
+}
 
-       g_assert_cmpstr (nl_langinfo (_NL_ADDRESS_COUNTRY_AB2), ==, "DE");
+static void
+test_setup_addressbook (ETestServerFixture *fixture,
+                        gconstpointer       user_data)
+{
+}
 
-       /* Respawn the addressbook factory */
-       success = spawn_addressbook_factory (&factory_pid, &error);
-       g_assert_cmpstr (error ? error->message : NULL, ==, NULL);
-       g_assert_cmpuint (factory_pid, !=, 0);
-       g_assert (success);
+static void
+test_change_country_code_setup (ETestServerFixture *fixture,
+                                gconstpointer       user_data)
+{
+       fixture->source_name = g_strdup (TEST_SOURCE_UID);
+       e_test_server_utils_setup (fixture, user_data);
+       g_assert_cmpstr (fixture->source_name, ==, TEST_SOURCE_UID);
+}
 
-       /* Reopen the book */
-       source = e_source_new_with_uid (source_uid, NULL, &error);
-       g_assert_cmpstr (error ? error->message : NULL, ==, NULL);
-       g_assert (source != NULL);
+static void
+test_change_country_code_setup_reopen (ETestServerFixture *fixture,
+                                       gconstpointer       user_data)
+{
+       const ETestServerClosure *closure = user_data;
+       ESource *source = NULL;
+       GError *error = NULL;
+       gboolean success;
 
-       client = e_book_client_new (source, &error);
+       fixture->registry = e_source_registry_new_sync (NULL, &error);
        g_assert_cmpstr (error ? error->message : NULL, ==, NULL);
-       g_assert (client != NULL);
+       g_assert (E_IS_SOURCE_REGISTRY (fixture->registry));
 
-       success = e_client_open_sync (E_CLIENT (client), FALSE, NULL, &error);
+       source = e_source_new_with_uid (TEST_SOURCE_UID, NULL, &error);
        g_assert_cmpstr (error ? error->message : NULL, ==, NULL);
-       g_assert (success);
+       g_assert (E_IS_SOURCE (source));
 
-       /*** Verification of German context */
+       switch (closure->type) {
+       case E_TEST_SERVER_ADDRESS_BOOK:
+               fixture->service.book_client = e_book_client_new (source, &error);
+               break;
 
-       /* Fetch the contact by UID and check EVC_TEL attribute and its EVC_X_E164 parameter */
-       success = e_book_client_get_contact_sync (client, contact_uid, &contact, NULL, &error);
+       case E_TEST_SERVER_DIRECT_ADDRESS_BOOK:
+               fixture->service.book_client = e_book_client_new_direct (fixture->registry, source, &error);
+               break;
+
+       default:
+               g_assert_not_reached ();
+               break;
+       }
+
+       g_assert_cmpstr (error ? error->message : NULL, ==, NULL);
+       g_assert (E_IS_BOOK_CLIENT (fixture->service.book_client));
+
+       success = e_client_open_sync (E_CLIENT (fixture->service.book_client), TRUE, NULL, &error);
        g_assert_cmpstr (error ? error->message : NULL, ==, NULL);
        g_assert (success);
 
-       attr = e_vcard_get_attribute (E_VCARD (contact), EVC_TEL);
+       g_object_unref (source);
+}
 
-       g_assert (attr != NULL);
-       g_assert_cmpstr (e_vcard_attribute_get_value (attr), ==, "221.542.3789");
+static void
+test_change_country_code_teardown (ETestServerFixture *fixture,
+                                   gconstpointer       user_data)
+{
+       if (fixture->service.book_client)
+               g_object_unref (fixture->service.book_client);
+}
 
-       e164_values = e_vcard_attribute_get_param (attr, EVC_X_E164);
+static void
+test_united_states (ETestServerFixture *fixture,
+                    gconstpointer       user_data)
+{
+       EBookClient *const client = fixture->service.book_client;
+       gchar *region;
 
-       g_assert (e164_values != NULL);
-       g_assert_cmpstr (e164_values->data, ==, "+492215423789");
+       region = e_phone_number_get_default_region ();
+       g_assert_cmpstr (region, ==, "US");
+       g_free (region);
 
-       /* Now resolve the contact via its phone number, assuming indexes are used */
-       success = e_book_client_get_contacts_uids_sync (
-                       client, "(eqphone \"phone\" \"+1/221/5423789\")",
-                       &fetched_uids, NULL, &error);
-       g_assert_cmpstr (error ? error->message : NULL, ==, NULL);
-       g_assert (success);
+       save_test_contact (client, "BEGIN:VCARD\nFN:Joe Smith\nTEL:123.456\nEND:VCARD", "23456");
 
-       g_assert_cmpuint (g_slist_length (fetched_uids), ==, 0);
-       g_slist_free_full (fetched_uids, g_free);
+       find_contacts (client, "(eqphone_national \"phone\" \"123456\")", "23456", 1, "Joe Smith");
+       find_contacts (client, "(eqphone_national \"phone\" \"23456\")", "23456", 1, "Joe Smith");
+       find_contacts (client, "(eqphone_national \"phone\" \"+123456\")", "23456", 1, "Joe Smith");
+       find_contacts (client, "(eqphone_national \"phone\" \"+44123456\")", "23456", 0);
+}
 
-       success = e_book_client_get_contacts_uids_sync (
-                       client, "(eqphone \"phone\" \"+49 (221) 5423789\")",
-                       &fetched_uids, NULL, &error);
-       g_assert_cmpstr (error ? error->message : NULL, ==, NULL);
-       g_assert (success);
+static void
+test_great_britain (ETestServerFixture *fixture,
+                    gconstpointer       user_data)
+{
+       EBookClient *const client = fixture->service.book_client;
+       gchar *region;
+
+       /* FIXME: e_book_backend_file_create_unique_id() is broken:
+        * The generated id consists of a second-resolution timestamp and
+        * a process local static counter. It therefore will generate equal
+        * ids if the addressbook factory is respawning quickly. Let's
+        * work around for now by sleeping a bit. */
+       sleep (1);
+
+       region = e_phone_number_get_default_region ();
+       g_assert_cmpstr (region, ==, "GB");
+       g_free (region);
+
+       save_test_contact (client, "BEGIN:VCARD\nFN:Joe Black\nTEL:123.456\nEND:VCARD", "123456");
+
+       find_contacts (client, "(contains \"x-evolution-any-field\" \"\")", "123456", 2, "Joe Smith", "Joe 
Black");
+       find_contacts (client, "(eqphone_national \"phone\" \"123456\")", "123456", 2, "Joe Smith", "Joe 
Black");
+       find_contacts (client, "(eqphone_national \"phone\" \"23456\")", "123456", 0);
+       find_contacts (client, "(eqphone_national \"phone\" \"+123456\")", "123456", 0);
+       find_contacts (client, "(eqphone_national \"phone\" \"+44123456\")", "123456", 2, "Joe Smith", "Joe 
Black");
+}
 
-       g_assert_cmpuint (g_slist_length (fetched_uids), ==, 1);
-       g_assert_cmpstr (fetched_uids->data, ==, contact_uid);
-       g_slist_free_full (fetched_uids, g_free);
+gint
+main (gint    argc,
+      gchar **argv)
+{
+       setlocale (LC_ALL, "");
 
-       /* Cleanup */
-       g_object_unref (source);
-       g_object_unref (client);
+#if !GLIB_CHECK_VERSION (2, 35, 1)
+       g_type_init ();
+#endif
+       g_test_init (&argc, &argv, NULL);
+
+       if (argc > 2 && strcmp (argv[1], "run-forked") == 0) {
+               ETestServerFlags flags = E_TEST_SERVER_KEEP_WORK_DIRECTORY;
+               const gchar *const path = argv[2];
+
+               TestData *data = test_data_new (
+                       strstr (path, "/Custom/") ? setup_custom_book : NULL,
+                       strstr (path, "/DirectAccess/")
+                               ? E_TEST_SERVER_DIRECT_ADDRESS_BOOK
+                               : E_TEST_SERVER_ADDRESS_BOOK,
+                       NULL);
+
+               if (g_str_has_suffix (path, "/Setup")) {
+                       g_test_add (
+                               path, ETestServerFixture, data,
+                               test_change_country_code_setup, test_setup_addressbook,
+                               e_test_server_utils_teardown);
+
+                       flags &= ~E_TEST_SERVER_KEEP_WORK_DIRECTORY;
+               } else if (g_str_has_suffix (path, "/UnitedStates")) {
+                       g_test_add (
+                               path, ETestServerFixture, data,
+                               test_change_country_code_setup_reopen, test_united_states,
+                               test_change_country_code_teardown);
+               } else if (g_str_has_suffix (path, "/GreatBritain")) {
+                       g_test_add (
+                               path, ETestServerFixture, data,
+                               test_change_country_code_setup_reopen, test_great_britain,
+                               test_change_country_code_teardown);
+               }
+
+               return e_test_server_utils_run_full (flags);
+       }
 
-       return g_test_run ();
+#ifdef ENABLE_PHONENUMBER
+
+       g_test_add_data_func (
+               "/EBookClient/Default/PhoneNumber/ChangeCountryCode",
+               test_data_new (NULL, E_TEST_SERVER_ADDRESS_BOOK, argv[0]),
+               test_change_country_code);
+       g_test_add_data_func (
+               "/EBookClient/Default/DirectAccess/PhoneNumber/ChangeCountryCode",
+               test_data_new (NULL, E_TEST_SERVER_DIRECT_ADDRESS_BOOK, argv[0]),
+               test_change_country_code);
+       g_test_add_data_func (
+               "/EBookClient/Custom/PhoneNumber/ChangeCountryCode",
+               test_data_new (setup_custom_book, E_TEST_SERVER_ADDRESS_BOOK, argv[0]),
+               test_change_country_code);
+       g_test_add_data_func (
+               "/EBookClient/Custom/DirectAccess/PhoneNumber/ChangeCountryCode",
+               test_data_new (setup_custom_book, E_TEST_SERVER_DIRECT_ADDRESS_BOOK, argv[0]),
+               test_change_country_code);
+
+#endif /* ENABLE_PHONENUMBER */
+
+       /* Call without flags to get the working directory wiped. */
+       return e_test_server_utils_run ();
 }


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