ESourceRegistry: Do not mandate builtin sources.
[platform/upstream/evolution-data-server.git] / tests / libebook / client / client-test-utils.c
index 760217b..bb41e82 100644 (file)
@@ -1,8 +1,31 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2011, 2012 Red Hat, Inc. (www.redhat.com)
+ * Copyright (C) 2012 Intel Corporation
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU Lesser General Public
+ * License as published by the Free Software Foundation.
+ *
+ * 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ * Authors: Milan Crha <mcrha@redhat.com>
+ *          Matthew Barnes <mbarnes@redhat.com>
+ *          Tristan Van Berkom <tristanvb@openismus.com>
+ */
 
 #include <stdio.h>
+#include <stdlib.h>
 
-#include <libedataserver/e-gdbus-templates.h>
+#include <libedataserver/libedataserver.h>
 
 #include "client-test-utils.h"
 
@@ -29,7 +52,7 @@ print_email (EContact *contact)
        g_print ("   Email addresses:\n");
        emails = e_contact_get (contact, E_CONTACT_EMAIL);
        for (e = emails; e; e = e->next) {
-               g_print ("\t%s\n",  (gchar *)e->data);
+               g_print ("\t%s\n",  (gchar *) e->data);
        }
        g_list_foreach (emails, (GFunc) g_free, NULL);
        g_list_free (emails);
@@ -38,14 +61,20 @@ print_email (EContact *contact)
 }
 
 EBookClient *
-open_system_book (gboolean only_if_exists)
+open_system_book (ESourceRegistry *registry,
+                  gboolean only_if_exists)
 {
+       ESource *source;
        EBookClient *book_client;
        GError *error = NULL;
 
        main_initialize ();
 
-       book_client = e_book_client_new_system (&error);
+       source = e_source_registry_ref_builtin_address_book (registry);
+       g_return_val_if_fail (source != NULL, NULL);
+       book_client = e_book_client_new (source, &error);
+       g_object_unref (source);
+
        if (error) {
                report_error ("create system addressbook", &error);
                return NULL;
@@ -89,14 +118,10 @@ idle_cb (gpointer data)
        g_return_val_if_fail (idle->func != NULL, FALSE);
 
        if (idle->run_in_thread) {
-               GError *error = NULL;
+               GThread *thread;
 
-               g_thread_create (idle->func, idle->data, FALSE, &error);
-
-               if (error) {
-                       report_error ("create thread", &error);
-                       stop_main_loop (1);
-               }
+               thread = g_thread_new (NULL, idle->func, idle->data);
+               g_thread_unref (thread);
        } else {
                idle->func (idle->data);
        }
@@ -208,7 +233,8 @@ get_main_loop_stop_result (void)
 }
 
 void
-foreach_configured_source (void (*func) (ESource *source))
+foreach_configured_source (ESourceRegistry *registry,
+                           void (*func) (ESource *source))
 {
        gpointer foreach_async_data;
        ESource *source = NULL;
@@ -217,7 +243,7 @@ foreach_configured_source (void (*func) (ESource *source))
 
        main_initialize ();
 
-       foreach_async_data = foreach_configured_source_async_start (&source);
+       foreach_async_data = foreach_configured_source_async_start (registry, &source);
        if (!foreach_async_data)
                return;
 
@@ -226,52 +252,29 @@ foreach_configured_source (void (*func) (ESource *source))
        } while (foreach_configured_source_async_next (&foreach_async_data, &source));
 }
 
-struct ForeachConfiguredData
-{
-       ESourceList *source_list;
-       GSList *current_group;
-       GSList *current_source;
+struct ForeachConfiguredData {
+       GList *list;
 };
 
 gpointer
-foreach_configured_source_async_start (ESource **source)
+foreach_configured_source_async_start (ESourceRegistry *registry,
+                                       ESource **source)
 {
        struct ForeachConfiguredData *async_data;
-       ESourceList *source_list = NULL;
-       GError *error = NULL;
+       const gchar *extension_name;
+       GList *list;
 
        g_return_val_if_fail (source != NULL, NULL);
 
        main_initialize ();
 
-       if (!e_book_client_get_sources (&source_list, &error)) {
-               report_error ("get addressbooks", &error);
-               return NULL;
-       }
-
-       g_return_val_if_fail (source_list != NULL, NULL);
+       extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK;
+       list = e_source_registry_list_sources (registry, extension_name);
 
        async_data = g_new0 (struct ForeachConfiguredData, 1);
-       async_data->source_list = source_list;
-       async_data->current_group = e_source_list_peek_groups (source_list);
-       if (!async_data->current_group) {
-               gpointer ad = async_data;
-
-               foreach_configured_source_async_next (&ad, source);
-               return ad;
-       }
+       async_data->list = list;
 
-       async_data->current_source = e_source_group_peek_sources (async_data->current_group->data);
-       if (!async_data->current_source) {
-               gpointer ad = async_data;
-
-               if (foreach_configured_source_async_next (&ad, source))
-                       return ad;
-
-               return NULL;
-       }
-
-       *source = async_data->current_source->data;
+       *source = async_data->list->data;
 
        return async_data;
 }
@@ -287,28 +290,16 @@ foreach_configured_source_async_next (gpointer *foreach_async_data,
 
        async_data = *foreach_async_data;
        g_return_val_if_fail (async_data != NULL, FALSE);
-       g_return_val_if_fail (async_data->source_list != NULL, FALSE);
-       g_return_val_if_fail (async_data->current_group != NULL, FALSE);
 
-       if (async_data->current_source)
-               async_data->current_source = async_data->current_source->next;
-       if (async_data->current_source) {
-               *source = async_data->current_source->data;
-               return TRUE;
+       if (async_data->list) {
+               g_object_unref (async_data->list->data);
+               async_data->list = async_data->list->next;
        }
-
-       do {
-               async_data->current_group = async_data->current_group->next;
-               if (async_data->current_group)
-                       async_data->current_source = e_source_group_peek_sources (async_data->current_group->data);
-       } while (async_data->current_group && !async_data->current_source);
-
-       if (async_data->current_source) {
-               *source = async_data->current_source->data;
+       if (async_data->list) {
+               *source = async_data->list->data;
                return TRUE;
        }
 
-       g_object_unref (async_data->source_list);
        g_free (async_data);
 
        *foreach_async_data = NULL;
@@ -316,41 +307,103 @@ 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)
 {
-       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;
+       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 book;
 }
 
 gchar *
@@ -362,11 +415,12 @@ new_vcard_from_test_case (const gchar *case_name)
        GError *error = NULL;
        gchar *vcard;
 
-        case_filename = g_strdup_printf ("%s.vcf", case_name);
+       case_filename = g_strdup_printf ("%s.vcf", case_name);
        filename = g_build_filename (SRCDIR, "..", "data", "vcards", case_filename, NULL);
        file = g_file_new_for_path (filename);
        if (!g_file_load_contents (file, NULL, &vcard, NULL, NULL, &error)) {
-                g_warning ("failed to read test contact file '%s': %s",
+               g_warning (
+                       "failed to read test contact file '%s': %s",
                                filename, error->message);
                exit (1);
        }