[evolution-data-server] Bug 686421 - Restore libebook tests to minimum working condition



commit 1f8a1be4974016858adbf0384c14823eaba52288
Author: Tristan Van Berkom <tristanvb openismus com>
Date:   Fri Oct 19 11:14:36 2012 -0400

    Bug 686421 - Restore libebook tests to minimum working condition

 tests/libebook/Makefile.am                |   62 +++++++---------
 tests/libebook/client/client-test-utils.c |  114 ++++++++++++++++++++++-------
 tests/libebook/ebook-test-utils.c         |  109 +++++++++++++++++++++++-----
 tests/libebook/test-ebook-remove.c        |    2 +
 4 files changed, 207 insertions(+), 80 deletions(-)
---
diff --git a/tests/libebook/Makefile.am b/tests/libebook/Makefile.am
index 543593e..ada931d 100644
--- a/tests/libebook/Makefile.am
+++ b/tests/libebook/Makefile.am
@@ -65,27 +65,21 @@ endif
 
 # Should be kept ordered approximately from least to most difficult/complex
 TESTS = \
-	$(NULL)
-
-# These tests are broken at the moment because the test fixture hacks
-# the old "file://absolute/path/to/source-uid" URIs for local ESources.
-# But local ESource URIs are now just "local:source-uid", so we need to
-# figure out a different way to set up the test fixture.
-
-#	test-ebook-remove
-#	test-ebook-get-required-fields
-#	test-ebook-get-static-capabilities
-#	test-ebook-get-supported-fields
-#	test-ebook-get-supported-auth-methods
-#	test-ebook-add-contact
-#	test-ebook-get-contact
-#	test-ebook-get-book-view
-#	test-ebook-commit-contact
-#	test-ebook-remove-contact
-#	test-ebook-remove-contact-by-id
-#	test-ebook-remove-contacts
-#	test-ebook-stress-factory--serial
-#	test-ebook-stress-factory--fifo
+	test-ebook-add-contact \
+	test-ebook-get-contact \
+	test-ebook-commit-contact \
+	test-ebook-remove-contact \
+	test-ebook-get-required-fields \
+	test-ebook-get-static-capabilities \
+	test-ebook-get-supported-fields \
+	test-ebook-get-supported-auth-methods \
+	test-ebook-remove-contact-by-id \
+	test-ebook-remove-contacts \
+	test-ebook-get-book-view \
+	test-ebook-remove
+
+#	test-ebook-stress-factory--serial \
+#	test-ebook-stress-factory--fifo \
 #	test-ebook-stress-factory--single-book
 
 noinst_PROGRAMS = \
@@ -135,29 +129,29 @@ test_ebook_LDADD=$(TEST_LIBS)
 test_ebook_CPPFLAGS=$(TEST_CPPFLAGS)
 test_ebook_async_LDADD=$(TEST_LIBS)
 test_ebook_async_CPPFLAGS=$(TEST_CPPFLAGS)
-#test_ebook_add_contact_LDADD=$(TEST_LIBS)
-test_ebook_add_contact_CPPFLAGS=$(TEST_CPPFLAGS)
-#test_ebook_commit_contact_LDADD=$(TEST_LIBS)
+test_ebook_commit_contact_LDADD=$(TEST_LIBS)
 test_ebook_commit_contact_CPPFLAGS=$(TEST_CPPFLAGS)
-#test_ebook_get_book_view_LDADD=$(TEST_LIBS)
+test_ebook_get_book_view_LDADD=$(TEST_LIBS)
 test_ebook_get_book_view_CPPFLAGS=$(TEST_CPPFLAGS)
-#test_ebook_get_contact_LDADD=$(TEST_LIBS)
+test_ebook_get_contact_LDADD=$(TEST_LIBS)
 test_ebook_get_contact_CPPFLAGS=$(TEST_CPPFLAGS)
-#test_ebook_get_required_fields_LDADD=$(TEST_LIBS)
+test_ebook_get_required_fields_LDADD=$(TEST_LIBS)
 test_ebook_get_required_fields_CPPFLAGS=$(TEST_CPPFLAGS)
-#test_ebook_get_static_capabilities_LDADD=$(TEST_LIBS)
+test_ebook_get_static_capabilities_LDADD=$(TEST_LIBS)
 test_ebook_get_static_capabilities_CPPFLAGS=$(TEST_CPPFLAGS)
-#test_ebook_get_supported_auth_methods_LDADD=$(TEST_LIBS)
+test_ebook_get_supported_auth_methods_LDADD=$(TEST_LIBS)
 test_ebook_get_supported_auth_methods_CPPFLAGS=$(TEST_CPPFLAGS)
-#test_ebook_get_supported_fields_LDADD=$(TEST_LIBS)
+test_ebook_get_supported_fields_LDADD=$(TEST_LIBS)
 test_ebook_get_supported_fields_CPPFLAGS=$(TEST_CPPFLAGS)
-#test_ebook_remove_LDADD=$(TEST_LIBS)
+test_ebook_add_contact_LDADD=$(TEST_LIBS)
+test_ebook_add_contact_CPPFLAGS=$(TEST_CPPFLAGS)
+test_ebook_remove_LDADD=$(TEST_LIBS)
 test_ebook_remove_CPPFLAGS=$(TEST_CPPFLAGS)
-#test_ebook_remove_contact_LDADD=$(TEST_LIBS)
+test_ebook_remove_contact_LDADD=$(TEST_LIBS)
 test_ebook_remove_contact_CPPFLAGS=$(TEST_CPPFLAGS)
-#test_ebook_remove_contact_by_id_LDADD=$(TEST_LIBS)
+test_ebook_remove_contact_by_id_LDADD=$(TEST_LIBS)
 test_ebook_remove_contact_by_id_CPPFLAGS=$(TEST_CPPFLAGS)
-#test_ebook_remove_contacts_LDADD=$(TEST_LIBS)
+test_ebook_remove_contacts_LDADD=$(TEST_LIBS)
 test_ebook_remove_contacts_CPPFLAGS=$(TEST_CPPFLAGS)
 #test_ebook_stress_factory__fifo_LDADD=$(TEST_LIBS)
 test_ebook_stress_factory__fifo_CPPFLAGS=$(TEST_CPPFLAGS)
diff --git a/tests/libebook/client/client-test-utils.c b/tests/libebook/client/client-test-utils.c
index ee6cafe..4fb45d7 100644
--- a/tests/libebook/client/client-test-utils.c
+++ b/tests/libebook/client/client-test-utils.c
@@ -288,45 +288,105 @@ foreach_configured_source_async_next (gpointer *foreach_async_data,
 	return FALSE;
 }
 
-EBookClient *
-new_temp_client (gchar **uri)
+
+
+typedef struct {
+	GMainLoop       *loop;
+	const gchar     *uid;
+	ESourceRegistry *registry;
+	ESource         *scratch;
+	ESource         *source;
+	EBookClient     *book;
+} CreateBookData;
+
+static gboolean
+quit_idle (CreateBookData *data)
+{
+	g_main_loop_quit (data->loop);
+	return FALSE;
+}
+
+static gboolean
+create_book_idle (CreateBookData *data)
 {
-#if 0  /* ACCOUNT_MGMT */
-	EBookClient *book_client;
-	ESource *source;
-	gchar *abs_uri, *filename;
-	gint handle;
 	GError *error = NULL;
 
-	filename = g_build_filename (g_get_tmp_dir (), "e-book-client-test-XXXXXX/", NULL);
-	handle = g_mkstemp (filename);
+	data->source = e_source_registry_ref_source (data->registry, data->uid);
+	if (!data->source)
+		g_error ("Unable to fetch newly created source uid '%s' from the registry", data->uid);
 
-	if (handle != -1)
-		close (handle);
+	data->book = e_book_client_new (data->source, &error);
+	if (!data->book)
+		g_error ("Unable to create the book: %s", error->message);
 
-	g_return_val_if_fail (g_mkdir_with_parents (filename, 0700) == 0, NULL);
+	g_idle_add ((GSourceFunc)quit_idle, data);
 
-	abs_uri = g_strconcat ("local://", filename, NULL);
-	g_free (filename);
+	return FALSE;
+}
 
-	source = e_source_new_with_absolute_uri ("Test book", abs_uri);
-	if (uri)
-		*uri = abs_uri;
-	else
-		g_free (abs_uri);
+static gboolean
+register_source_idle (CreateBookData *data)
+{
+	GError *error = NULL;
+	ESourceBackend  *backend;
 
-	g_return_val_if_fail (source != NULL, NULL);
+	data->registry = e_source_registry_new_sync (NULL, &error);
+	if (!data->registry)
+		g_error ("Unable to create the registry: %s", error->message);
 
-	book_client = e_book_client_new (source, &error);
-	g_object_unref (source);
+	data->scratch = e_source_new_with_uid (data->uid, NULL, &error);
+	if (!data->scratch)
+		g_error ("Failed to create source with uid '%s': %s", data->uid, error->message);
 
-	if (error)
-		report_error ("new temp client", &error);
+	backend = e_source_get_extension (data->scratch, E_SOURCE_EXTENSION_ADDRESS_BOOK);
+	e_source_backend_set_backend_name (backend, "local");
 
-	return book_client;
-#endif /* ACCOUNT_MGMT */
+	if (!e_source_registry_commit_source_sync (data->registry, data->scratch, NULL, &error))
+		g_error ("Unable to add new source to the registry for uid %s: %s", data->uid, error->message);
+
+	/* XXX e_source_registry_commit_source_sync isnt really sync... or else
+	 * we could call e_source_registry_ref_source() immediately
+	 */
+	g_timeout_add (20, (GSourceFunc)create_book_idle, data);
+
+	return FALSE;
+}
+
+static EBookClient *
+ebook_test_utils_book_with_uid (const gchar *uid)
+{
+	CreateBookData data = { 0, };
+
+	data.uid = uid;
+
+	data.loop = g_main_loop_new (NULL, FALSE);
+	g_idle_add ((GSourceFunc)register_source_idle, &data);
+	g_main_loop_run (data.loop);
+	g_main_loop_unref (data.loop);
+
+	g_object_unref (data.scratch);
+	g_object_unref (data.source);
+	g_object_unref (data.registry);
+
+	return data.book;
+}
+
+EBookClient *
+new_temp_client (gchar **uri)
+{
+	EBookClient     *book;
+	gchar           *uid;
+	guint64          real_time = g_get_real_time ();
+
+	uid  = g_strdup_printf ("test-book-%" G_GINT64_FORMAT, real_time);
+	book = ebook_test_utils_book_with_uid (uid);
+
+	if (uri)
+		*uri = g_strdup (uid);
+
+	g_free (uid);
 
-	return NULL;
+	return book;
 }
 
 gchar *
diff --git a/tests/libebook/ebook-test-utils.c b/tests/libebook/ebook-test-utils.c
index c96692a..23b7ae4 100644
--- a/tests/libebook/ebook-test-utils.c
+++ b/tests/libebook/ebook-test-utils.c
@@ -638,32 +638,103 @@ ebook_test_utils_book_async_remove_contacts (EBook *book,
 	}
 }
 
-EBook *
-ebook_test_utils_book_new_temp (gchar **uri)
+
+
+typedef struct {
+	GMainLoop       *loop;
+	const gchar     *uid;
+	ESourceRegistry *registry;
+	ESource         *scratch;
+	ESource         *source;
+	EBook           *book;
+} CreateBookData;
+
+static gboolean
+quit_idle (CreateBookData *data)
 {
-	EBook *book;
-	gchar *file_template;
-	gchar *uri_result;
+	g_main_loop_quit (data->loop);
+	return FALSE;
+}
 
-	file_template = g_build_filename (g_get_tmp_dir (),
-			"ebook-test-XXXXXX/", NULL);
-	g_mkstemp (file_template);
+static gboolean
+create_book_idle (CreateBookData *data)
+{
+	GError *error = NULL;
 
-	uri_result = g_strconcat ("local:", file_template, NULL);
-	if (!uri_result) {
-		g_warning ("failed to convert %s to a 'local:' URI", file_template);
-		exit (1);
-	}
-	g_free (file_template);
+	data->source = e_source_registry_ref_source (data->registry, data->uid);
+	if (!data->source)
+		g_error ("Unable to fetch newly created source uid '%s' from the registry", data->uid);
+
+	data->book = e_book_new (data->source, &error);
+	if (!data->book)
+		g_error ("Unable to create the book: %s", error->message);
+
+	g_idle_add ((GSourceFunc)quit_idle, data);
+
+	return FALSE;
+}
+
+static gboolean
+register_source_idle (CreateBookData *data)
+{
+	GError *error = NULL;
+	ESourceBackend  *backend;
+
+	data->registry = e_source_registry_new_sync (NULL, &error);
+	if (!data->registry)
+		g_error ("Unable to create the registry: %s", error->message);
+
+	data->scratch = e_source_new_with_uid (data->uid, NULL, &error);
+	if (!data->scratch)
+		g_error ("Failed to create source with uid '%s': %s", data->uid, error->message);
+
+	backend = e_source_get_extension (data->scratch, E_SOURCE_EXTENSION_ADDRESS_BOOK);
+	e_source_backend_set_backend_name (backend, "local");
+
+	if (!e_source_registry_commit_source_sync (data->registry, data->scratch, NULL, &error))
+		g_error ("Unable to add new source to the registry for uid %s: %s", data->uid, error->message);
+
+	/* XXX e_source_registry_commit_source_sync isnt really sync... or else
+	 * we could call e_source_registry_ref_source() immediately
+	 */
+	g_timeout_add (20, (GSourceFunc)create_book_idle, data);
+
+	return FALSE;
+}
+
+static EBook *
+ebook_test_utils_book_with_uid (const gchar *uid)
+{
+	CreateBookData data = { 0, };
+
+	data.uid = uid;
+
+	data.loop = g_main_loop_new (NULL, FALSE);
+	g_idle_add ((GSourceFunc)register_source_idle, &data);
+	g_main_loop_run (data.loop);
+	g_main_loop_unref (data.loop);
+
+	g_object_unref (data.scratch);
+	g_object_unref (data.source);
+	g_object_unref (data.registry);
+
+	return data.book;
+}
+
+EBook *
+ebook_test_utils_book_new_temp (gchar **uri)
+{
+	EBook           *book;
+	gchar           *uid;
+	guint64          real_time = g_get_real_time ();
 
-	/* FIXME We don't build EBooks from URIs anymore. */
-	/* book = ebook_test_utils_book_new_from_uri (uri_result); */
-	book = NULL;
+	uid  = g_strdup_printf ("test-book-%" G_GINT64_FORMAT, real_time);
+	book = ebook_test_utils_book_with_uid (uid);
 
 	if (uri)
-                *uri = g_strdup (uri_result);
+		*uri = g_strdup (uid);
 
-	g_free (uri_result);
+	g_free (uid);
 
 	return book;
 }
diff --git a/tests/libebook/test-ebook-remove.c b/tests/libebook/test-ebook-remove.c
index 42ec25c..f85b517 100644
--- a/tests/libebook/test-ebook-remove.c
+++ b/tests/libebook/test-ebook-remove.c
@@ -19,6 +19,7 @@ main (gint argc,
 	book = ebook_test_utils_book_new_temp (&uri);
 	ebook_test_utils_book_open (book, FALSE);
 
+#if 0 /* ebook_test_utils_book_async_remove() Not implemented */
 	/* Async version */
 	book = ebook_test_utils_book_new_temp (&uri);
 	ebook_test_utils_book_open (book, FALSE);
@@ -28,6 +29,7 @@ main (gint argc,
 			ebook_test_utils_callback_quit, loop);
 
 	g_main_loop_run (loop);
+#endif
 
 	return 0;
 }



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