[evolution-data-server/account-mgmt: 35/39] Add unit tests for ESource.
- From: Matthew Barnes <mbarnes src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [evolution-data-server/account-mgmt: 35/39] Add unit tests for ESource.
- Date: Thu, 27 Oct 2011 02:51:46 +0000 (UTC)
commit 7349e23a64b364a05b374933f8ae1bd47469bf13
Author: Matthew Barnes <mbarnes redhat com>
Date: Thu Mar 17 21:26:44 2011 -0400
Add unit tests for ESource.
configure.ac | 4 +
tests/libedataserver/Makefile.am | 16 ++-
tests/libedataserver/e-source-test.c | 244 ++++++++++++++++++++++++++++++++++
3 files changed, 257 insertions(+), 7 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 515a8b6..45f0680 100644
--- a/configure.ac
+++ b/configure.ac
@@ -387,6 +387,10 @@ if test "x$enable_goa" = xyes; then
fi
AM_CONDITIONAL(HAVE_GOA, [test x$enable_goa = xyes])
+if test x$os_win32 = xno; then
+ PKG_CHECK_MODULES(GIO_UNIX, [gio-unix-2.0])
+fi
+
dnl ***********************************
dnl Check for GNOME Keyring.
dnl ***********************************
diff --git a/tests/libedataserver/Makefile.am b/tests/libedataserver/Makefile.am
index 6d714a6..2019aa8 100644
--- a/tests/libedataserver/Makefile.am
+++ b/tests/libedataserver/Makefile.am
@@ -1,15 +1,17 @@
-noinst_PROGRAMS = test-source-list
+TESTS = e-source-test
-test_source_list_CPPFLAGS = \
+noinst_PROGRAMS = $(TESTS)
+
+e_source_test_CPPFLAGS = \
$(AM_CPPFLAGS) \
-I$(top_srcdir) \
-DG_LOG_DOMAIN=\"e-data-server\" \
- $(E_DATA_SERVER_CFLAGS)
-
-test_source_list_SOURCES = test-source-list.c
+ $(E_DATA_SERVER_CFLAGS) \
+ $(GIO_UNIX_CFLAGS)
-test_source_list_LDADD = \
+e_source_test_LDADD = \
$(top_builddir)/libedataserver/libedataserver-1.2.la \
- $(E_DATA_SERVER_LIBS)
+ $(E_DATA_SERVER_LIBS) \
+ $(GIO_UNIX_LIBS)
-include $(top_srcdir)/git.mk
diff --git a/tests/libedataserver/e-source-test.c b/tests/libedataserver/e-source-test.c
new file mode 100644
index 0000000..7b0c495
--- /dev/null
+++ b/tests/libedataserver/e-source-test.c
@@ -0,0 +1,244 @@
+/*
+ * e-source-test.c
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) version 3.
+ *
+ * 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with the program; if not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+#include <string.h>
+#include <gio/gunixoutputstream.h>
+
+#include <libedataserver/e-source.h>
+#include <libedataserver/e-source-authentication.h>
+#include <libedataserver/e-uid.h>
+
+#define SIMPLE_KEY_FILE \
+ "[Data Source]\n" \
+ "DisplayName=Simple Test Case\n" \
+ "Parent=local\n"
+
+typedef struct _TestESource TestESource;
+typedef struct _TestFixture TestFixture;
+
+struct _TestESource {
+ GFile *file;
+ ESource *source;
+ gboolean changed;
+};
+
+struct _TestFixture {
+ TestESource test;
+ TestESource same_file;
+ TestESource same_content;
+ gboolean changed;
+};
+
+static void
+source_changed_cb (ESource *source,
+ TestESource *test)
+{
+ test->changed = TRUE;
+}
+
+static void
+setup_test_source (TestESource *test,
+ const gchar *content)
+{
+ GOutputStream *stream;
+ gchar *filename;
+ gint fd;
+ GError *error = NULL;
+
+ /* Create a new temporary key file. */
+ fd = g_file_open_tmp ("test-source-XXXXXX", &filename, &error);
+ g_assert_no_error (error);
+
+ /* Write the given content to the temporary key file. */
+ stream = g_unix_output_stream_new (fd, TRUE);
+ g_output_stream_write_all (
+ stream, content, strlen (content), NULL, NULL, &error);
+ g_object_unref (stream);
+ g_assert_no_error (error);
+
+ /* Create a GFile that points to the temporary key file. */
+ test->file = g_file_new_for_path (filename);
+
+ /* Create an ESource from the GFile and load the key file. */
+#if 0 /* ACCOUNT_MGMT */
+ test->source = e_source_new (test->file, &error);
+ g_assert_no_error (error);
+#endif /* ACCOUNT_MGMT */
+
+ g_signal_connect (
+ test->source, "changed",
+ G_CALLBACK (source_changed_cb), test);
+
+ g_free (filename);
+}
+
+static void
+teardown_test_source (TestESource *test)
+{
+ GError *error = NULL;
+
+ /* Delete the temporary key file. */
+ g_file_delete (test->file, NULL, &error);
+ g_assert_no_error (error);
+
+ g_object_unref (test->file);
+ g_object_unref (test->source);
+}
+
+static void
+test_fixture_setup_key_file (TestFixture *fixture,
+ gconstpointer test_data)
+{
+#if 0 /* ACCOUNT_MGMT */
+ GError *error = NULL;
+
+ /* The primary ESource for testing. */
+ setup_test_source (&fixture->test, SIMPLE_KEY_FILE);
+
+ /* Secondary ESource: different file with identical content. */
+ setup_test_source (&fixture->same_content, SIMPLE_KEY_FILE);
+
+ /* Secondary ESource: a clone of the primary ESource. */
+ fixture->same_file.file =
+ g_file_dup (fixture->test.file);
+ fixture->same_file.source =
+ e_source_new (fixture->same_file.file, &error);
+ g_assert_no_error (error);
+#endif /* ACCOUNT_MGMT */
+}
+
+static void
+test_fixture_teardown_key_file (TestFixture *fixture,
+ gconstpointer test_data)
+{
+ teardown_test_source (&fixture->test);
+}
+
+static void
+test_single_source (TestFixture *fixture,
+ gconstpointer test_data)
+{
+#if 0 /* ACCOUNT_MGMT */
+ GFile *file;
+ ESourceExtension *extension;
+ const gchar *extension_name;
+ const gchar *backend_name;
+ const gchar *display_name;
+ const gchar *parent;
+ const gchar *uid;
+ gchar *basename;
+ guint hash;
+ GError *error = NULL;
+
+ /* Extract a bunch of data from the primary ESource. */
+ backend_name = e_source_get_backend_name (fixture->test.source);
+ display_name = e_source_get_display_name (fixture->test.source);
+ parent = e_source_get_parent (fixture->test.source);
+ file = e_source_get_file (fixture->test.source);
+ uid = e_source_get_uid (fixture->test.source);
+ hash = e_source_hash (fixture->test.source);
+
+ /* An ESource's GFile should compare equal to the GFile
+ * used to create it. */
+ g_assert (g_file_equal (file, fixture->test.file));
+
+ /* An ESource's UID should match the key file's basename. */
+ basename = g_file_get_basename (fixture->test.file);
+ g_assert_cmpstr (uid, ==, basename);
+ g_free (basename);
+
+ /* An ESource's hash value is based on its UID. */
+ g_assert_cmpuint (hash, ==, g_str_hash (uid));
+
+ /* Check the other fields specified in this key file. */
+ g_assert_cmpstr (display_name, ==, "Simple Test Case");
+ g_assert_cmpstr (parent, ==, "local");
+
+ /* The backend name was omitted, so it should be NULL. */
+ g_assert_cmpstr (backend_name, ==, NULL);
+
+ /* ESource equality is based solely on UIDs. */
+ g_assert (e_source_equal (
+ fixture->test.source, fixture->test.source));
+ g_assert (e_source_equal (
+ fixture->test.source, fixture->same_file.source));
+ g_assert (!e_source_equal (
+ fixture->test.source, fixture->same_content.source));
+
+ /* Nothing done so far should have triggered ESource::changed. */
+ g_assert (!fixture->test.changed);
+
+ /* Check on-demand extension loading. */
+ E_TYPE_SOURCE_AUTHENTICATION; /* register the type */
+ extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
+ g_assert (!e_source_has_extension (
+ fixture->test.source, extension_name));
+ extension = e_source_get_extension (
+ fixture->test.source, extension_name);
+ g_assert (e_source_has_extension (
+ fixture->test.source, extension_name));
+ g_assert (E_IS_SOURCE_AUTHENTICATION (extension));
+
+ /* Loading an extension should trigger ESource::changed. */
+ g_assert (fixture->test.changed);
+
+ fixture->test.changed = FALSE;
+
+ /* The ESource::changed signal is wired to its own "notify" and
+ * the "notify" signals of its extensions. Passing any property
+ * to g_object_notify() should trigger it. */
+ g_object_notify (G_OBJECT (fixture->test.source), "display-name");
+ g_assert (fixture->test.changed);
+
+ fixture->test.changed = FALSE;
+
+ /* Now try the extension we loaded. */
+ g_object_notify (G_OBJECT (extension), "host");
+ g_assert (fixture->test.changed);
+
+ /* The primary ESource and cloned ESource both handle the same
+ * key file. If we change a property in one ESource and sync it
+ * to disk, then reload the other ESource and examine the same
+ * property, the values should match. */
+ display_name = "Modified Test Case";
+ e_source_set_display_name (fixture->test.source, display_name);
+ e_source_sync (fixture->test.source, &error);
+ g_assert_no_error (error);
+ e_source_reload (fixture->same_file.source, &error);
+ g_assert_no_error (error);
+ display_name = e_source_get_display_name (fixture->same_file.source);
+ g_assert_cmpstr (display_name, ==, "Modified Test Case");
+#endif /* ACOCUNT_MGMT */
+}
+
+gint
+main (gint argc, gchar **argv)
+{
+ g_type_init ();
+
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add (
+ "/e-source-test/SingleSource",
+ TestFixture, NULL,
+ test_fixture_setup_key_file,
+ test_single_source,
+ test_fixture_teardown_key_file);
+
+ return g_test_run ();
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]