[evolution-data-server/openismus-work] tests: Add shared new_custom_temp_client()



commit 03b4e98a4f0892db886430a67be27dc08151f6be
Author: Mathias Hasselmann <mathias openismus com>
Date:   Tue Dec 11 09:12:56 2012 +0100

    tests: Add shared new_custom_temp_client()
    
    Added a new version of new_custom_temp_client() to
    client-test-utils.[ch] that takes a setip callback for
    tuning the scratch source.

 tests/libebook/client/client-test-utils.c          |   19 +++-
 tests/libebook/client/client-test-utils.h          |    5 +-
 tests/libebook/client/test-client-custom-summary.c |  106 +-------------------
 3 files changed, 24 insertions(+), 106 deletions(-)
---
diff --git a/tests/libebook/client/client-test-utils.c b/tests/libebook/client/client-test-utils.c
index 89d81bb..a67a94b 100644
--- a/tests/libebook/client/client-test-utils.c
+++ b/tests/libebook/client/client-test-utils.c
@@ -253,7 +253,7 @@ get_main_loop_stop_result (void)
 
 void
 foreach_configured_source (ESourceRegistry *registry,
-                           void (*func) (ESource *source))
+                           SourceFunc func)
 {
 	gpointer foreach_async_data;
 	ESource *source = NULL;
@@ -335,6 +335,7 @@ typedef struct {
 	ESource         *scratch;
 	ESource         *source;
 	EBookClient     *book;
+	SourceFunc       on_setup;
 } CreateBookData;
 
 static gboolean
@@ -388,6 +389,9 @@ register_source_idle (CreateBookData *data)
 	config = e_source_get_extension (data->scratch, E_SOURCE_EXTENSION_ADDRESS_BOOK_CONFIG);
 	e_source_address_book_config_set_revision_guards_enabled (config, TRUE);
 
+	if (data->on_setup)
+		data->on_setup (data->scratch);
+
 	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);
 
@@ -400,13 +404,15 @@ register_source_idle (CreateBookData *data)
 }
 
 static EBookClient *
-ebook_test_utils_book_with_uid (const gchar *uid)
+ebook_test_utils_book_with_uid (const gchar *uid,
+                                SourceFunc   on_setup)
 {
 	CreateBookData data = { 0, };
 
 	data.uid = uid;
 
 	data.loop = g_main_loop_new (NULL, FALSE);
+	data.on_setup = on_setup;
 	g_idle_add ((GSourceFunc)register_source_idle, &data);
 	g_main_loop_run (data.loop);
 	g_main_loop_unref (data.loop);
@@ -421,12 +427,19 @@ ebook_test_utils_book_with_uid (const gchar *uid)
 EBookClient *
 new_temp_client (gchar **uri)
 {
+	return new_custom_temp_client (uri, NULL);
+}
+
+EBookClient *
+new_custom_temp_client (gchar      **uri,
+                        SourceFunc   on_setup)
+{
 	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);
+	book = ebook_test_utils_book_with_uid (uid, on_setup);
 
 	if (uri)
 		*uri = g_strdup (uid);
diff --git a/tests/libebook/client/client-test-utils.h b/tests/libebook/client/client-test-utils.h
index 2537735..59d9632 100644
--- a/tests/libebook/client/client-test-utils.h
+++ b/tests/libebook/client/client-test-utils.h
@@ -27,6 +27,8 @@
 
 #include <libebook/libebook.h>
 
+typedef void (*SourceFunc) (ESource *source);
+
 void report_error (const gchar *operation, GError **error);
 void print_email (EContact *contact);
 EBookClient *open_system_book (ESourceRegistry *registry, gboolean only_if_exists);
@@ -38,11 +40,12 @@ void start_in_idle_with_main_loop (GThreadFunc func, gpointer data);
 void stop_main_loop (gint stop_result);
 gint get_main_loop_stop_result (void);
 
-void foreach_configured_source (ESourceRegistry *registry, void (*func) (ESource *source));
+void foreach_configured_source (ESourceRegistry *registry, SourceFunc func);
 gpointer foreach_configured_source_async_start (ESourceRegistry *registry, ESource **source);
 gboolean foreach_configured_source_async_next (gpointer *foreach_async_data, ESource **source);
 
 EBookClient *new_temp_client (gchar **uri);
+EBookClient *new_custom_temp_client (gchar **uri, SourceFunc on_setup);
 gboolean add_contact_from_test_case_verify (EBookClient *book_client, const gchar *case_name, EContact **contact);
 gboolean add_contact_verify (EBookClient *book_client, EContact *contact);
 gchar *new_vcard_from_test_case (const gchar *case_name);
diff --git a/tests/libebook/client/test-client-custom-summary.c b/tests/libebook/client/test-client-custom-summary.c
index d5977f0..04b139d 100644
--- a/tests/libebook/client/test-client-custom-summary.c
+++ b/tests/libebook/client/test-client-custom-summary.c
@@ -32,63 +32,13 @@
 #define REGISTER_TYPE(type) \
 	(g_type_class_unref (g_type_class_ref (type)))
 
-/****************************** Custom Book Creation *****************************/
-
-
-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)
-{
-	GError *error = NULL;
-
-	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_client_new_direct (data->registry, 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)
+static void
+setup_custom_summary (ESource *scratch)
 {
-	GError *error = NULL;
-	ESourceBackend  *backend;
 	ESourceBackendSummarySetup *setup;
 
-	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");
-
 	REGISTER_TYPE (E_TYPE_SOURCE_BACKEND_SUMMARY_SETUP);
-	setup = e_source_get_extension (data->scratch, E_SOURCE_EXTENSION_BACKEND_SUMMARY_SETUP);
+	setup = e_source_get_extension (scratch, E_SOURCE_EXTENSION_BACKEND_SUMMARY_SETUP);
 	e_source_backend_summary_setup_set_summary_fields (setup,
 							   E_CONTACT_FULL_NAME,
 							   E_CONTACT_FAMILY_NAME,
@@ -103,54 +53,6 @@ register_source_idle (CreateBookData *data)
 							   E_CONTACT_FAMILY_NAME, E_BOOK_INDEX_PREFIX,
 							   E_CONTACT_FAMILY_NAME, E_BOOK_INDEX_SUFFIX,
 							   0);
-
-
-	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;
-}
-
-static EBookClient *
-new_custom_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 book;
 }
 
 typedef struct {
@@ -269,7 +171,7 @@ main (gint argc,
 	main_initialize ();
 
 	/* Setup */
-	book_client = new_custom_temp_client (NULL);
+	book_client = new_custom_temp_client (NULL, setup_custom_summary);
 	g_return_val_if_fail (book_client != NULL, 1);
 
 	if (!e_client_open_sync (E_CLIENT (book_client), FALSE, NULL, &error)) {



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