[evolution-data-server/account-mgmt: 30/30] Add unit tests for ESource.



commit 11f3eb981c0a5b6efccffe62ab310b8bc1b2bc53
Author: Matthew Barnes <mbarnes redhat com>
Date:   Thu Mar 17 21:26:44 2011 -0400

    Add unit tests for ESource.

 Makefile.am           |    9 ++-
 configure.ac          |   15 ++--
 tests/Makefile.am     |   21 +++++
 tests/e-source-test.c |  238 +++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 275 insertions(+), 8 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 0e3d27b..d2a887a 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -6,8 +6,13 @@ if ENABLE_CALENDAR
 CALENDAR_DIR = calendar
 endif
 
-SUBDIRS = data libedataserver libebackend servers camel addressbook $(CALENDAR_DIR) libedataserverui docs art po
-DIST_SUBDIRS = data libedataserver libebackend servers camel addressbook calendar libedataserverui docs art po
+if OS_WIN32
+else
+TESTS_DIR = tests
+endif
+
+SUBDIRS = data libedataserver libebackend servers camel addressbook $(CALENDAR_DIR) libedataserverui $(TESTS_DIR) docs art po
+DIST_SUBDIRS = data libedataserver libebackend servers camel addressbook calendar libedataserverui tests docs art po
 
 %-$(API_VERSION).pc: %.pc
 	 cp $< $@
diff --git a/configure.ac b/configure.ac
index 6d18cc9..883554e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -319,15 +319,17 @@ PKG_CHECK_MODULES(GNOME_PLATFORM,
 	libsoup-2.4 >= libsoup_minimum_version
 	libgdata >= libgdata_minimum_version])
 
-
 if test x$os_win32 = xno; then
-  dnl ***********************************
-  dnl Check for GNOME Keyring.
-  dnl ***********************************
-  PKG_CHECK_MODULES(GNOME_KEYRING,
-	[gnome-keyring-1 >= gnome_keyring_minimum_version])
+	PKG_CHECK_MODULES(GIO_UNIX, [gio-unix-2.0])
 fi
 
+dnl ***********************************
+dnl Check for GNOME Keyring.
+dnl ***********************************
+if test x$os_win32 = xno; then
+	PKG_CHECK_MODULES(GNOME_KEYRING,
+		[gnome-keyring-1 >= gnome_keyring_minimum_version])
+fi
 
 LIBICAL_REQUIRED=libical_minimum_version
 AC_SUBST(LIBICAL_REQUIRED)
@@ -1487,6 +1489,7 @@ libedataserverui/libedataserverui.pc
 servers/Makefile
 servers/groupwise/Makefile
 servers/groupwise/libegroupwise.pc
+tests/Makefile
 docs/Makefile
 docs/reference/Makefile
 docs/reference/addressbook/Makefile
diff --git a/tests/Makefile.am b/tests/Makefile.am
new file mode 100644
index 0000000..585cfbd
--- /dev/null
+++ b/tests/Makefile.am
@@ -0,0 +1,21 @@
+
+TEST_CPPFLAGS = \
+	$(AM_CPPFLAGS)			\
+	-I$(top_srcdir)			\
+	$(E_DATA_SERVER_CFLAGS)		\
+	$(GIO_UNIX_CFLAGS)
+
+TEST_LDADD = \
+	$(top_builddir)/libedataserver/libedataserver-1.2.la	\
+	$(E_DATA_SERVER_LIBS)					\
+	$(GIO_UNIX_LIBS)
+
+TESTS = \
+	e-source-test
+
+check_PROGRAMS = $(TESTS)
+
+e_source_test_CPPFLAGS=$(TEST_CPPFLAGS)
+e_source_test_LDADD=$(TEST_LDADD)
+
+-include $(top_srcdir)/git.mk
diff --git a/tests/e-source-test.c b/tests/e-source-test.c
new file mode 100644
index 0000000..e37b7a7
--- /dev/null
+++ b/tests/e-source-test.c
@@ -0,0 +1,238 @@
+/*
+ * 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. */
+	test->source = e_source_new (test->file, &error);
+	g_assert_no_error (error);
+
+	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)
+{
+	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);
+}
+
+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)
+{
+	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");
+}
+
+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]