[evolution-data-server/openismus-work-master: 13/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: 13/23] Adding test-server-utils
- Date: Thu, 13 Dec 2012 07:38:28 +0000 (UTC)
commit d0047bb630f510dfc3ac4bff1e05c846ebf38f61
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.
configure.ac | 5 +
tests/Makefile.am | 2 +-
tests/test-server-utils/Makefile.am | 51 ++++
tests/test-server-utils/e-test-server-utils.c | 257 ++++++++++++++++++++
tests/test-server-utils/e-test-server-utils.h | 81 ++++++
tests/test-server-utils/services/Makefile.am | 4 +
...ome.evolution.dataserver.AddressBook.service.in | 3 +
....gnome.evolution.dataserver.Calendar.service.in | 3 +
...g.gnome.evolution.dataserver.Sources.service.in | 3 +
tests/test-server-utils/test-fixture.c | 51 ++++
10 files changed, 459 insertions(+), 1 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index eed24c1..8c57745 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1637,6 +1637,11 @@ tests/libecal/Makefile
tests/libecal/client/Makefile
tests/libedata-cal/Makefile
tests/libedataserver/Makefile
+tests/test-server-utils/Makefile
+tests/test-server-utils/services/Makefile
+tests/test-server-utils/services/org.gnome.evolution.dataserver.AddressBook.service
+tests/test-server-utils/services/org.gnome.evolution.dataserver.Calendar.service
+tests/test-server-utils/services/org.gnome.evolution.dataserver.Sources.service
tests/tools/Makefile
docs/Makefile
docs/reference/Makefile
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 6c5f457..388ea42 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,4 +1,4 @@
-SUBDIRS = libedataserver libebook libecal libedata-cal tools
+SUBDIRS = test-server-utils libedataserver libebook libecal libedata-cal tools
@GNOME_CODE_COVERAGE_RULES@
diff --git a/tests/test-server-utils/Makefile.am b/tests/test-server-utils/Makefile.am
new file mode 100644
index 0000000..d382a4d
--- /dev/null
+++ b/tests/test-server-utils/Makefile.am
@@ -0,0 +1,51 @@
+NULL =
+
+ GNOME_CODE_COVERAGE_RULES@
+
+common_cflags = \
+ $(AM_CPPFLAGS) \
+ -I$(top_srcdir) \
+ -I$(top_builddir) \
+ -I$(top_srcdir)/addressbook \
+ -I$(top_builddir)/addressbook \
+ -I$(top_srcdir)/calendar \
+ -I$(top_builddir)/calendar \
+ -DG_LOG_DOMAIN=\"e-test-server-utils\" \
+ -DEDS_TEST_DBUS_SERVICE_DIR=\""$(abs_top_builddir)/tests/test-server-utils/services"\" \
+ -DEDS_TEST_WORK_DIR=\""$(abs_top_builddir)/tests/test-server-utils/cache"\" \
+ $(E_DATA_SERVER_CFLAGS) \
+ $(GIO_UNIX_CFLAGS) \
+ $(NULL)
+
+common_libs = \
+ $(top_builddir)/libedataserver/libedataserver-1.2.la \
+ $(top_builddir)/addressbook/libebook/libebook-1.2.la \
+ $(top_builddir)/calendar/libecal/libecal-1.2.la \
+ $(E_DATA_SERVER_LIBS) \
+ $(GIO_UNIX_LIBS) \
+ $(NULL)
+
+######################################
+# Utility Library #
+######################################
+noinst_LTLIBRARIES = libetestserverutils.la
+
+libetestserverutils_la_CPPFLAGS = $(common_cflags)
+libetestserverutils_la_LIBADD = $(common_libs)
+libetestserverutils_la_SOURCES = \
+ e-test-server-utils.c \
+ e-test-server-utils.h \
+ $(NULL)
+
+######################################
+# Local Unit Tests #
+######################################
+noinst_PROGRAMS = $(TESTS)
+
+TESTS = test-fixture
+
+test_fixture_CPPFLAGS = $(common_cflags)
+test_fixture_LDADD = $(common_libs) libetestserverutils.la
+test_fixture_SOURCES = test-fixture.c
+
+-include $(top_srcdir)/git.mk
diff --git a/tests/test-server-utils/e-test-server-utils.c b/tests/test-server-utils/e-test-server-utils.c
new file mode 100644
index 0000000..e170087
--- /dev/null
+++ b/tests/test-server-utils/e-test-server-utils.c
@@ -0,0 +1,257 @@
+#include "e-test-server-utils.h"
+
+#define ADDRESS_BOOK_SOURCE_UID "test-address-book"
+#define CALENDAR_SOURCE_UID "test-calendar"
+
+/* FIXME, currently we are unable to achieve server activation
+ * twice in a single test case, so we're using one D-Bus server
+ * throughout an entire test suite.
+ *
+ * When this is fixed we can migrate the D-Bus initialization
+ * and teardown from e_test_server_utils_run() to
+ * e_test_server_utils_setup() and e_test_server_utils_teardown()
+ * and this will transparantly change the way tests run using
+ * this test framework.
+ */
+#define GLOBAL_DBUS_DAEMON 1
+
+#if GLOBAL_DBUS_DAEMON
+static GTestDBus *global_test_dbus = NULL;
+#endif
+
+typedef struct {
+ ETestServerFixture *fixture;
+ ETestServerClosure *closure;
+} FixturePair;
+
+static void
+setup_environment (void)
+{
+ g_assert (g_setenv ("XDG_DATA_HOME", EDS_TEST_WORK_DIR, TRUE));
+ g_assert (g_setenv ("XDG_CACHE_HOME", EDS_TEST_WORK_DIR, TRUE));
+ g_assert (g_setenv ("XDG_CONFIG_HOME", EDS_TEST_WORK_DIR, TRUE));
+}
+
+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) {
+
+ 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)
+ g_error ("Unable to create the test book: %s", error->message);
+ }
+
+ if ((pair->closure->flags & E_TEST_SERVER_CALENDAR) != 0) {
+
+ 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)
+ g_error ("Unable to create the test calendar: %s", error->message);
+ }
+
+ if (source)
+ g_object_unref (source);
+
+ g_main_loop_quit (pair->fixture->loop);
+
+ return FALSE;
+}
+
+static gboolean
+e_test_server_utils_bootstrap_idle (FixturePair *pair)
+{
+ ESource *scratch = NULL;
+ GError *error = NULL;
+
+ pair->fixture->registry = e_source_registry_new_sync (NULL, &error);
+
+ if (!pair->fixture->registry)
+ 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;
+
+ 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);
+
+ 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) {
+
+ 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);
+
+ /* Ensure Calendar type source (how to specify the backend here ?? */
+ e_source_get_extension (scratch, E_SOURCE_EXTENSION_CALENDAR);
+
+ /* Allow customization of the scratch esource */
+ if (pair->closure->customize_calendar)
+ 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 calendar source to the registry: %s", error->message);
+ }
+
+ if (scratch)
+ g_object_unref (scratch);
+
+ if (pair->closure->flags != 0)
+ g_timeout_add (20, (GSourceFunc)e_test_server_utils_bootstrap_timeout, pair);
+ else
+ g_main_loop_quit (pair->fixture->loop);
+
+ return FALSE;
+}
+
+/**
+ * e_test_server_utils_setup:
+ * @fixture: A #ETestServerFixture
+ * @user_data: A #ETestServerClosure or derived structure provided by the test.
+ *
+ * A setup function for the #ETestServerFixture fixture
+ */
+void
+e_test_server_utils_setup (ETestServerFixture *fixture,
+ gconstpointer user_data)
+{
+ ETestServerClosure *closure = (ETestServerClosure *)user_data;
+ FixturePair pair = { fixture, closure };
+
+ /* Create work directory */
+ g_assert (g_mkdir_with_parents (EDS_TEST_WORK_DIR, 0755) == 0);
+
+ fixture->loop = g_main_loop_new (NULL, FALSE);
+
+#if !GLOBAL_DBUS_DAEMON
+ /* Create the global dbus-daemon for this test suite */
+ fixture->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 (fixture->dbus, EDS_TEST_DBUS_SERVICE_DIR);
+
+ /* Start the private D-Bus daemon */
+ g_test_dbus_up (fixture->dbus);
+#else
+ fixture->dbus = global_test_dbus;
+#endif
+
+ g_idle_add ((GSourceFunc)e_test_server_utils_bootstrap_idle, &pair);
+ g_main_loop_run (fixture->loop);
+}
+
+/**
+ * e_test_server_utils_teardown:
+ * @fixture: A #ETestServerFixture
+ * @user_data: A #ETestServerClosure or derived structure provided by the test.
+ *
+ * A teardown function for the #ETestServerFixture fixture
+ */
+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;
+ }
+
+ g_object_run_dispose (G_OBJECT (fixture->registry));
+ g_object_unref (fixture->registry);
+ fixture->registry = NULL;
+
+ g_main_loop_unref (fixture->loop);
+ fixture->loop = NULL;
+
+#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_down (fixture->dbus);
+ g_object_unref (fixture->dbus);
+ fixture->dbus = NULL;
+#else
+ fixture->dbus = NULL;
+#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);
+}
+
+gint
+e_test_server_utils_run (void)
+{
+ gint tests_ret;
+
+ 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);
+
+ /* 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);
+#endif
+
+ /* 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;
+#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
new file mode 100644
index 0000000..12d66cd
--- /dev/null
+++ b/tests/test-server-utils/e-test-server-utils.h
@@ -0,0 +1,81 @@
+#ifndef E_TEST_UTILS_H
+#define E_TEST_UTILS_H
+
+#include <libedataserver/libedataserver.h>
+#include <libebook/libebook.h>
+#include <libecal/libecal.h>
+
+typedef struct _ETestServerFixture ETestServerFixture;
+typedef struct _ETestServerClosure ETestServerClosure;
+
+/**
+ * ETestSourceCustomizeFunc:
+ * @scratch: The scratch #ESource template being used to create an addressbook or calendar
+ * @closure: The #ETestServerClosure for this test case
+ *
+ * This can be used to parameterize the addressbook or calendar @scratch #ESource
+ * before creating/committing it.
+ */
+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
+ *
+ * Flags specifying which portions of the #ETestServerFixture should be
+ * initialized in the fixture.
+ *
+ * The #ESourceRegistry is always created for every test.
+ */
+typedef enum {
+ E_TEST_SERVER_ADDRESS_BOOK = (1 << 0),
+ E_TEST_SERVER_CALENDAR = (1 << 1)
+} ETestServiceFlags;
+
+/**
+ * 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
+ *
+ * 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;
+};
+
+/**
+ * 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
+ *
+ * A fixture for running tests on the Evolution Data Server
+ * components in an encapsulated D-Bus environment.
+ */
+struct _ETestServerFixture {
+ GMainLoop *loop;
+ GTestDBus *dbus;
+ ESourceRegistry *registry;
+ EBookClient *book;
+ ECalClient *calendar;
+};
+
+void e_test_server_utils_setup (ETestServerFixture *fixture,
+ gconstpointer user_data);
+
+void e_test_server_utils_teardown (ETestServerFixture *fixture,
+ gconstpointer user_data);
+
+gint e_test_server_utils_run (void);
+
+
+#endif /* E_TEST_UTILS_H */
diff --git a/tests/test-server-utils/services/Makefile.am b/tests/test-server-utils/services/Makefile.am
new file mode 100644
index 0000000..0ffe1db
--- /dev/null
+++ b/tests/test-server-utils/services/Makefile.am
@@ -0,0 +1,4 @@
+EXTRA_DIST = \
+ org.gnome.evolution.dataserver.AddressBook.service.in \
+ org.gnome.evolution.dataserver.Calendar.service.in \
+ org.gnome.evolution.dataserver.Sources.service.in
diff --git a/tests/test-server-utils/services/org.gnome.evolution.dataserver.AddressBook.service.in b/tests/test-server-utils/services/org.gnome.evolution.dataserver.AddressBook.service.in
new file mode 100644
index 0000000..b32af72
--- /dev/null
+++ b/tests/test-server-utils/services/org.gnome.evolution.dataserver.AddressBook.service.in
@@ -0,0 +1,3 @@
+[D-BUS Service]
+Name= ADDRESS_BOOK_DBUS_SERVICE_NAME@
+Exec= abs_top_builddir@/services/evolution-addressbook-factory/evolution-addressbook-factory
diff --git a/tests/test-server-utils/services/org.gnome.evolution.dataserver.Calendar.service.in b/tests/test-server-utils/services/org.gnome.evolution.dataserver.Calendar.service.in
new file mode 100644
index 0000000..aa148e3
--- /dev/null
+++ b/tests/test-server-utils/services/org.gnome.evolution.dataserver.Calendar.service.in
@@ -0,0 +1,3 @@
+[D-BUS Service]
+Name= CALENDAR_DBUS_SERVICE_NAME@
+Exec= abs_top_builddir@/services/evolution-calendar-factory/evolution-calendar-factory
diff --git a/tests/test-server-utils/services/org.gnome.evolution.dataserver.Sources.service.in b/tests/test-server-utils/services/org.gnome.evolution.dataserver.Sources.service.in
new file mode 100644
index 0000000..cd88998
--- /dev/null
+++ b/tests/test-server-utils/services/org.gnome.evolution.dataserver.Sources.service.in
@@ -0,0 +1,3 @@
+[D-BUS Service]
+Name= SOURCES_DBUS_SERVICE_NAME@
+Exec= abs_top_builddir@/services/evolution-source-registry/evolution-source-registry
diff --git a/tests/test-server-utils/test-fixture.c b/tests/test-server-utils/test-fixture.c
new file mode 100644
index 0000000..f476834
--- /dev/null
+++ b/tests/test-server-utils/test-fixture.c
@@ -0,0 +1,51 @@
+#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 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[])
+{
+#if !GLIB_CHECK_VERSION (2, 35, 1)
+ g_type_init ();
+#endif
+ g_test_init (&argc, &argv, NULL);
+
+ /* Test that internal implementations can return all kinds of type through its api */
+ g_test_add ("/Fixture/Registry1", ETestServerFixture, ®istry_closure,
+ e_test_server_utils_setup, empty_test, e_test_server_utils_teardown);
+ g_test_add ("/Fixture/Registry2", ETestServerFixture, ®istry_closure,
+ e_test_server_utils_setup, empty_test, e_test_server_utils_teardown);
+ g_test_add ("/Fixture/Registry3", ETestServerFixture, ®istry_closure,
+ 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);
+ g_test_add ("/Fixture/Book2", ETestServerFixture, &book_closure,
+ e_test_server_utils_setup, empty_test, e_test_server_utils_teardown);
+ g_test_add ("/Fixture/Book3", ETestServerFixture, &book_closure,
+ 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,
+ 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]