[evolution-data-server/openismus-work-master: 14/23] Adding test-server-utils
- From: Tristan Van Berkom <tvb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [evolution-data-server/openismus-work-master: 14/23] Adding test-server-utils
- Date: Thu, 13 Dec 2012 07:38:33 +0000 (UTC)
commit 97bb4ec27e4a4b78055b9625201329f32b57a113
Author: Tristan Van Berkom <tristanvb openismus com>
Date: Tue Dec 11 18:22:49 2012 +0900
Adding test-server-utils
A library with text fixtures for testing ESourceRegistry, EBookClient and
ECalClient in a self contained fashion.
tests/test-server-utils/e-test-server-utils.c | 219 +++++++++++++++++--------
tests/test-server-utils/e-test-server-utils.h | 76 +++++++---
tests/test-server-utils/test-fixture.c | 40 ++++-
3 files changed, 240 insertions(+), 95 deletions(-)
---
diff --git a/tests/test-server-utils/e-test-server-utils.c b/tests/test-server-utils/e-test-server-utils.c
index e170087..0a5de19 100644
--- a/tests/test-server-utils/e-test-server-utils.c
+++ b/tests/test-server-utils/e-test-server-utils.c
@@ -32,32 +32,90 @@ setup_environment (void)
g_assert (g_setenv ("XDG_CONFIG_HOME", EDS_TEST_WORK_DIR, TRUE));
}
+static void
+delete_work_directory (void)
+{
+ gchar *command_line;
+
+ /* XXX Instead of complex error checking here, we should ideally use
+ * a recursive GDir / g_unlink() function.
+ *
+ * We cannot use GFile and the recursive delete function without
+ * corrupting our contained D-Bus environment with service files
+ * from the OS.
+ */
+ command_line = g_strdup_printf ("/bin/rm -rf %s", EDS_TEST_WORK_DIR);
+ g_spawn_command_line_sync (command_line, NULL, NULL, NULL, NULL);
+ g_free (command_line);
+}
+
static gboolean
e_test_server_utils_bootstrap_timeout (FixturePair *pair)
{
ESource *source = NULL;
GError *error = NULL;
- if ((pair->closure->flags & E_TEST_SERVER_ADDRESS_BOOK) != 0) {
+ switch (pair->closure->type) {
+ case E_TEST_SERVER_ADDRESS_BOOK:
+ source = e_source_registry_ref_source (pair->fixture->registry, ADDRESS_BOOK_SOURCE_UID);
+ if (!source)
+ g_error ("Unable to fetch newly created addressbook source from the registry");
+
+ pair->fixture->service.book_client = e_book_client_new (source, &error);
+ if (!pair->fixture->service.book_client)
+ g_error ("Unable to create the test book: %s", error->message);
+
+ if (!e_client_open_sync (E_CLIENT (pair->fixture->service.book_client), FALSE, NULL, &error))
+ g_error ("Unable to open book client: %s", error->message);
+
+ break;
+ case E_TEST_SERVER_DEPRECATED_ADDRESS_BOOK:
source = e_source_registry_ref_source (pair->fixture->registry, ADDRESS_BOOK_SOURCE_UID);
if (!source)
g_error ("Unable to fetch newly created addressbook source from the registry");
- pair->fixture->book = e_book_client_new (source, &error);
- if (!pair->fixture->book)
+ pair->fixture->service.book = e_book_new (source, &error);
+ if (!pair->fixture->service.book)
g_error ("Unable to create the test book: %s", error->message);
- }
- if ((pair->closure->flags & E_TEST_SERVER_CALENDAR) != 0) {
+ if (!e_book_open (pair->fixture->service.book, FALSE, &error))
+ g_error ("Unable to open book: %s", error->message);
+
+ break;
+ case E_TEST_SERVER_CALENDAR:
source = e_source_registry_ref_source (pair->fixture->registry, CALENDAR_SOURCE_UID);
if (!source)
g_error ("Unable to fetch newly created addressbook source from the registry");
- pair->fixture->calendar = e_cal_client_new (source, E_CAL_CLIENT_SOURCE_TYPE_EVENTS, &error);
- if (!pair->fixture->calendar)
+ pair->fixture->service.calendar_client = e_cal_client_new (source,
+ pair->closure->calendar_source_type,
+ &error);
+ if (!pair->fixture->service.calendar_client)
g_error ("Unable to create the test calendar: %s", error->message);
+
+ if (!e_client_open_sync (E_CLIENT (pair->fixture->service.calendar_client), FALSE, NULL, &error))
+ g_error ("Unable to open calendar client: %s", error->message);
+
+ break;
+
+ case E_TEST_SERVER_DEPRECATED_CALENDAR:
+ source = e_source_registry_ref_source (pair->fixture->registry, CALENDAR_SOURCE_UID);
+ if (!source)
+ g_error ("Unable to fetch newly created addressbook source from the registry");
+
+ pair->fixture->service.calendar = e_cal_new (source, pair->closure->calendar_source_type);
+ if (!pair->fixture->service.calendar)
+ g_error ("Unable to create the test calendar");
+
+ if (!e_cal_open (pair->fixture->service.calendar, FALSE, &error))
+ g_error ("Unable to open calendar: %s", error->message);
+
+ break;
+
+ case E_TEST_SERVER_NONE:
+ break;
}
if (source)
@@ -71,6 +129,7 @@ e_test_server_utils_bootstrap_timeout (FixturePair *pair)
static gboolean
e_test_server_utils_bootstrap_idle (FixturePair *pair)
{
+ ESourceBackend *backend = NULL;
ESource *scratch = NULL;
GError *error = NULL;
@@ -80,46 +139,48 @@ e_test_server_utils_bootstrap_idle (FixturePair *pair)
g_error ("Unable to create the test registry: %s", error->message);
/* Create an address book */
- if ((pair->closure->flags & E_TEST_SERVER_ADDRESS_BOOK) != 0) {
- ESourceBackend *backend;
+ switch (pair->closure->type) {
+ case E_TEST_SERVER_ADDRESS_BOOK:
+ case E_TEST_SERVER_DEPRECATED_ADDRESS_BOOK:
scratch = e_source_new_with_uid (ADDRESS_BOOK_SOURCE_UID, NULL, &error);
if (!scratch)
g_error ("Failed to create scratch source for an addressbook: %s", error->message);
+ /* Ensure Book type */
backend = e_source_get_extension (scratch, E_SOURCE_EXTENSION_ADDRESS_BOOK);
e_source_backend_set_backend_name (backend, "local");
- /* Allow customization of the scratch esource */
- if (pair->closure->customize_book)
- pair->closure->customize_book (scratch, pair->closure);
-
- if (!e_source_registry_commit_source_sync (pair->fixture->registry, scratch, NULL, &error))
- g_error ("Unable to add new addressbook source to the registry: %s", error->message);
- }
-
- /* Create a calendar */
- if ((pair->closure->flags & E_TEST_SERVER_CALENDAR) != 0) {
+ break;
+ case E_TEST_SERVER_CALENDAR:
+ case E_TEST_SERVER_DEPRECATED_CALENDAR:
scratch = e_source_new_with_uid (CALENDAR_SOURCE_UID, NULL, &error);
if (!scratch)
- g_error ("Failed to create scratch source for an addressbook: %s", error->message);
+ g_error ("Failed to create scratch source for a calendar: %s", error->message);
/* Ensure Calendar type source (how to specify the backend here ?? */
- e_source_get_extension (scratch, E_SOURCE_EXTENSION_CALENDAR);
+ backend = e_source_get_extension (scratch, E_SOURCE_EXTENSION_CALENDAR);
+ e_source_backend_set_backend_name (backend, "local");
- /* Allow customization of the scratch esource */
- if (pair->closure->customize_calendar)
- pair->closure->customize_book (scratch, pair->closure);
+ break;
- if (!e_source_registry_commit_source_sync (pair->fixture->registry, scratch, NULL, &error))
- g_error ("Unable to add new calendar source to the registry: %s", error->message);
+ case E_TEST_SERVER_NONE:
+ break;
}
- if (scratch)
+ if (scratch) {
+ if (pair->closure->customize)
+ pair->closure->customize (scratch, pair->closure);
+
+ if (!e_source_registry_commit_source_sync (pair->fixture->registry, scratch, NULL, &error))
+ g_error ("Unable to add new addressbook source to the registry: %s", error->message);
+
+
g_object_unref (scratch);
+ }
- if (pair->closure->flags != 0)
+ if (pair->closure->type != E_TEST_SERVER_NONE)
g_timeout_add (20, (GSourceFunc)e_test_server_utils_bootstrap_timeout, pair);
else
g_main_loop_quit (pair->fixture->loop);
@@ -174,21 +235,47 @@ void
e_test_server_utils_teardown (ETestServerFixture *fixture,
gconstpointer user_data)
{
- GFile *file;
- GError *error = NULL;
-
- if (fixture->book) {
- if (!e_client_remove_sync (E_CLIENT (fixture->book), NULL, &error))
- g_error ("Failed to remove test book: %s", error->message);
- g_object_unref (fixture->book);
- fixture->book = NULL;
- }
-
- if (fixture->calendar) {
- if (!e_client_remove_sync (E_CLIENT (fixture->calendar), NULL, &error))
- g_error ("Failed to remove test calendar: %s", error->message);
- g_object_unref (fixture->calendar);
- fixture->calendar = NULL;
+ ETestServerClosure *closure = (ETestServerClosure *)user_data;
+ GError *error = NULL;
+
+ switch (closure->type) {
+ case E_TEST_SERVER_ADDRESS_BOOK:
+ if (!e_client_remove_sync (E_CLIENT (fixture->service.book_client), NULL, &error)) {
+ g_message ("Failed to remove test book: %s (ignoring)", error->message);
+ g_clear_error (&error);
+ }
+ g_object_unref (fixture->service.book_client);
+ fixture->service.book_client = NULL;
+ break;
+
+ case E_TEST_SERVER_DEPRECATED_ADDRESS_BOOK:
+ if (!e_book_remove (fixture->service.book, &error)) {
+ g_message ("Failed to remove test book: %s (ignoring)", error->message);
+ g_clear_error (&error);
+ }
+ g_object_unref (fixture->service.book);
+ fixture->service.book = NULL;
+ break;
+
+ case E_TEST_SERVER_CALENDAR:
+ if (!e_client_remove_sync (E_CLIENT (fixture->service.calendar_client), NULL, &error)) {
+ g_message ("Failed to remove test calendar: %s (ignoring)", error->message);
+ g_clear_error (&error);
+ }
+ g_object_unref (fixture->service.calendar_client);
+ fixture->service.calendar_client = NULL;
+ break;
+
+ case E_TEST_SERVER_DEPRECATED_CALENDAR:
+ if (!e_cal_remove (fixture->service.calendar, &error)) {
+ g_message ("Failed to remove test calendar: %s (ignoring)", error->message);
+ g_clear_error (&error);
+ }
+ g_object_unref (fixture->service.calendar);
+ fixture->service.calendar = NULL;
+
+ case E_TEST_SERVER_NONE:
+ break;
}
g_object_run_dispose (G_OBJECT (fixture->registry));
@@ -213,44 +300,44 @@ e_test_server_utils_teardown (ETestServerFixture *fixture,
#endif
/* Cleanup work directory */
- file = g_file_new_for_path (EDS_TEST_WORK_DIR);
- if (g_file_query_exists (file, NULL))
- e_file_recursive_delete_sync (file, NULL, NULL);
- g_object_unref (file);
+ delete_work_directory ();
}
gint
e_test_server_utils_run (void)
{
- gint tests_ret;
+ gint tests_ret;
+
+ /* Cleanup work directory */
+ delete_work_directory ();
- setup_environment ();
+ setup_environment ();
#if GLOBAL_DBUS_DAEMON
- /* Create the global dbus-daemon for this test suite */
- global_test_dbus = g_test_dbus_new (G_TEST_DBUS_NONE);
+ /* Create the global dbus-daemon for this test suite */
+ global_test_dbus = g_test_dbus_new (G_TEST_DBUS_NONE);
- /* Add the private directory with our in-tree service files */
- g_test_dbus_add_service_dir (global_test_dbus, EDS_TEST_DBUS_SERVICE_DIR);
+ /* Add the private directory with our in-tree service files */
+ g_test_dbus_add_service_dir (global_test_dbus, EDS_TEST_DBUS_SERVICE_DIR);
- /* Start the private D-Bus daemon */
- g_test_dbus_up (global_test_dbus);
+ /* Start the private D-Bus daemon */
+ g_test_dbus_up (global_test_dbus);
#endif
- /* Run the GTest suite */
- tests_ret = g_test_run ();
+ /* Run the GTest suite */
+ tests_ret = g_test_run ();
#if GLOBAL_DBUS_DAEMON
- /* Teardown the D-Bus Daemon
- *
- * Note that we intentionally leak the TestDBus daemon
- * in this case, presumably this is due to some leaked
- * GDBusConnection reference counting
- */
- g_test_dbus_stop (global_test_dbus);
- /* g_object_unref (global_test_dbus); */
- global_test_dbus = NULL;
+ /* Teardown the D-Bus Daemon
+ *
+ * Note that we intentionally leak the TestDBus daemon
+ * in this case, presumably this is due to some leaked
+ * GDBusConnection reference counting
+ */
+ g_test_dbus_stop (global_test_dbus);
+ /* g_object_unref (global_test_dbus); */
+ global_test_dbus = NULL;
#endif
return tests_ret;
diff --git a/tests/test-server-utils/e-test-server-utils.h b/tests/test-server-utils/e-test-server-utils.h
index 12d66cd..34c153b 100644
--- a/tests/test-server-utils/e-test-server-utils.h
+++ b/tests/test-server-utils/e-test-server-utils.h
@@ -8,6 +8,22 @@
typedef struct _ETestServerFixture ETestServerFixture;
typedef struct _ETestServerClosure ETestServerClosure;
+
+/**
+ * E_TEST_SERVER_UTILS_SERVICE:
+ * @fixture: An #ETestServerFixture
+ * @service_type: The type to cast for the service in use
+ *
+ * A convenience macro to fetch the service for a given test case:
+ *
+ * |[
+ * EBookClient *book = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);
+ * ]|
+ *
+ */
+#define E_TEST_SERVER_UTILS_SERVICE(fixture, service_type) \
+ ((service_type *)((ETestServerFixture *)fixture)->service.generic)
+
/**
* ETestSourceCustomizeFunc:
* @scratch: The scratch #ESource template being used to create an addressbook or calendar
@@ -20,43 +36,63 @@ typedef void (* ETestSourceCustomizeFunc) (ESource *scratch,
ETestServerClosure *closure);
/**
- * ETestServiceFlags:
- * @E_TEST_SERVER_ADDRESS_BOOK: An Address Book is required for this test
- * @E_TEST_SERVER_CALENDAR: A Calendar is required for this test
+ * ETestServiceType:
+ * @E_TEST_SERVER_NONE: Only the #ESourceRegistry will be created
+ * @E_TEST_SERVER_ADDRESS_BOOK: An #EBookCLient will be created and opened for the test
+ * @E_TEST_SERVER_CALENDAR: An #ECalClient will be created and opened for the test
+ * @E_TEST_SERVER_DEPRECATED_ADDRESS_BOOK: An #EBook will be created and opened for the test
*
- * Flags specifying which portions of the #ETestServerFixture should be
- * initialized in the fixture.
- *
- * The #ESourceRegistry is always created for every test.
+ * The type of service to test
*/
typedef enum {
- E_TEST_SERVER_ADDRESS_BOOK = (1 << 0),
- E_TEST_SERVER_CALENDAR = (1 << 1)
-} ETestServiceFlags;
+ E_TEST_SERVER_NONE = 0,
+ E_TEST_SERVER_ADDRESS_BOOK,
+ E_TEST_SERVER_CALENDAR,
+ E_TEST_SERVER_DEPRECATED_ADDRESS_BOOK,
+ E_TEST_SERVER_DEPRECATED_CALENDAR
+} ETestServiceType;
/**
* ETestServerClosure:
- * @flags: The #ETestServiceFlags to use for this test
- * @customize_book: An #ETestSourceCustomizeFunc to use to parameterize the created book, if any, or %NULL
- * @customize_calendar: An #ETestSourceCustomizeFunc to use to parameterize the created calendar, if any, or %NULL
+ * @flags: The #ETestServiceFlags to use for this test
+ * @customize: An #ETestSourceCustomizeFunc to use to parameterize the scratch #ESource, or %NULL
+ * @calendar_source_type: An #ECalClientSourceType or #ECalSourceType; for %E_TEST_SERVER_CALENDAR
+ * and %E_TEST_SERVER_DEPRECATED_CALENDAR tests
*
* This structure provides the parameters for the #ETestServerFixture tests,
* it can be included as the first member of a derived structure
* for any tests deriving from the #ETestServerFixture test type
*/
struct _ETestServerClosure {
- ETestServiceFlags flags;
- ETestSourceCustomizeFunc customize_book;
- ETestSourceCustomizeFunc customize_calendar;
+ ETestServiceType type;
+ ETestSourceCustomizeFunc customize;
+ gint calendar_source_type;
};
/**
+ * ETestService:
+ * @book_client: An #EBookClient, created for %E_TEST_SERVER_ADDRESS_BOOK tests
+ * @calendar_client: An #ECalClient, created for %E_TEST_SERVER_CALENDAR tests
+ * @book: An #EBook, created for %E_TEST_SERVER_DEPRECATED_ADDRESS_BOOK tests
+ * @calendar: An #ECal, created for %E_TEST_SERVER_DEPRECATED_CALENDAR tests
+ *
+ * A union of service types, holds the object to test in a #ETestServerFixture.
+ *
+ */
+typedef union {
+ gpointer generic;
+ EBookClient *book_client;
+ ECalClient *calendar_client;
+ EBook *book;
+ ECal *calendar;
+} ETestService;
+
+/**
* ETestServerFixture:
* @loop: A Main loop to run traffic in
* @dbus: The D-Bus test scaffold
* @registry: An #ESourceRegistry
- * @book: An #EBookClient, or %NULL
- * @calendar: An #ECalClient, or %NULL
+ * @service: The #ETestService
*
* A fixture for running tests on the Evolution Data Server
* components in an encapsulated D-Bus environment.
@@ -65,10 +101,10 @@ struct _ETestServerFixture {
GMainLoop *loop;
GTestDBus *dbus;
ESourceRegistry *registry;
- EBookClient *book;
- ECalClient *calendar;
+ ETestService service;
};
+
void e_test_server_utils_setup (ETestServerFixture *fixture,
gconstpointer user_data);
diff --git a/tests/test-server-utils/test-fixture.c b/tests/test-server-utils/test-fixture.c
index f476834..875e37d 100644
--- a/tests/test-server-utils/test-fixture.c
+++ b/tests/test-server-utils/test-fixture.c
@@ -1,20 +1,19 @@
#include "e-test-server-utils.h"
-static ETestServerClosure registry_closure = { 0, NULL, NULL };
-static ETestServerClosure book_closure = { E_TEST_SERVER_ADDRESS_BOOK, NULL, NULL };
+static ETestServerClosure registry_closure = { E_TEST_SERVER_NONE, NULL, 0 };
+static ETestServerClosure book_closure = { E_TEST_SERVER_ADDRESS_BOOK, NULL, 0 };
+static ETestServerClosure calendar_closure = { E_TEST_SERVER_CALENDAR, NULL, E_CAL_CLIENT_SOURCE_TYPE_EVENTS };
+static ETestServerClosure deprecated_book_closure = { E_TEST_SERVER_DEPRECATED_ADDRESS_BOOK, NULL, 0 };
+static ETestServerClosure deprecated_calendar_closure = { E_TEST_SERVER_DEPRECATED_CALENDAR, NULL, E_CAL_SOURCE_TYPE_EVENT };
static void
empty_test (ETestServerFixture *fixture,
gconstpointer user_data)
{
/* Basic Empty case just to run the fixture */
-
- /* Server running ? */
- //sleep (10);
}
-
int
main (int argc,
char *argv[])
@@ -33,8 +32,6 @@ main (int argc,
e_test_server_utils_setup, empty_test, e_test_server_utils_teardown);
g_test_add ("/Fixture/Registry4", ETestServerFixture, ®istry_closure,
e_test_server_utils_setup, empty_test, e_test_server_utils_teardown);
- g_test_add ("/Fixture/Registry5", ETestServerFixture, ®istry_closure,
- e_test_server_utils_setup, empty_test, e_test_server_utils_teardown);
g_test_add ("/Fixture/Book1", ETestServerFixture, &book_closure,
e_test_server_utils_setup, empty_test, e_test_server_utils_teardown);
@@ -44,7 +41,32 @@ main (int argc,
e_test_server_utils_setup, empty_test, e_test_server_utils_teardown);
g_test_add ("/Fixture/Book4", ETestServerFixture, &book_closure,
e_test_server_utils_setup, empty_test, e_test_server_utils_teardown);
- g_test_add ("/Fixture/Book5", ETestServerFixture, &book_closure,
+
+ g_test_add ("/Fixture/Calendar1", ETestServerFixture, &calendar_closure,
+ e_test_server_utils_setup, empty_test, e_test_server_utils_teardown);
+ g_test_add ("/Fixture/Calendar2", ETestServerFixture, &calendar_closure,
+ e_test_server_utils_setup, empty_test, e_test_server_utils_teardown);
+ g_test_add ("/Fixture/Calendar3", ETestServerFixture, &calendar_closure,
+ e_test_server_utils_setup, empty_test, e_test_server_utils_teardown);
+ g_test_add ("/Fixture/Calendar4", ETestServerFixture, &calendar_closure,
+ e_test_server_utils_setup, empty_test, e_test_server_utils_teardown);
+
+ g_test_add ("/Fixture/Deprecated/Book1", ETestServerFixture, &deprecated_book_closure,
+ e_test_server_utils_setup, empty_test, e_test_server_utils_teardown);
+ g_test_add ("/Fixture/Deprecated/Book2", ETestServerFixture, &deprecated_book_closure,
+ e_test_server_utils_setup, empty_test, e_test_server_utils_teardown);
+ g_test_add ("/Fixture/Deprecated/Book3", ETestServerFixture, &deprecated_book_closure,
+ e_test_server_utils_setup, empty_test, e_test_server_utils_teardown);
+ g_test_add ("/Fixture/Deprecated/Book4", ETestServerFixture, &deprecated_book_closure,
+ e_test_server_utils_setup, empty_test, e_test_server_utils_teardown);
+
+ g_test_add ("/Fixture/Deprecated/Calendar1", ETestServerFixture, &deprecated_calendar_closure,
+ e_test_server_utils_setup, empty_test, e_test_server_utils_teardown);
+ g_test_add ("/Fixture/Deprecated/Calendar2", ETestServerFixture, &deprecated_calendar_closure,
+ e_test_server_utils_setup, empty_test, e_test_server_utils_teardown);
+ g_test_add ("/Fixture/Deprecated/Calendar3", ETestServerFixture, &deprecated_calendar_closure,
+ e_test_server_utils_setup, empty_test, e_test_server_utils_teardown);
+ g_test_add ("/Fixture/Deprecated/Calendar4", ETestServerFixture, &deprecated_calendar_closure,
e_test_server_utils_setup, empty_test, e_test_server_utils_teardown);
return e_test_server_utils_run ();
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]