[evolution-data-server/treitter-test-suites] Add a test for the e-book 'getBookView' method.



commit 577d6edfb9711ad646590fb28e28d5023f5cdbfe
Author: Travis Reitter <treitter gmail com>
Date:   Fri Dec 11 11:58:55 2009 -0800

    Add a test for the e-book 'getBookView' method.

 addressbook/tests/ebook/Makefile.am                |    6 +-
 addressbook/tests/ebook/ebook-test-utils.c         |   54 ++++++++
 addressbook/tests/ebook/ebook-test-utils.h         |   11 ++
 addressbook/tests/ebook/test-ebook-get-book-view.c |  129 ++++++++++++++++++++
 addressbook/tests/ebook/test-ebook-view.c          |   99 ---------------
 5 files changed, 197 insertions(+), 102 deletions(-)
---
diff --git a/addressbook/tests/ebook/Makefile.am b/addressbook/tests/ebook/Makefile.am
index 262b021..5a476de 100644
--- a/addressbook/tests/ebook/Makefile.am
+++ b/addressbook/tests/ebook/Makefile.am
@@ -30,6 +30,7 @@ TESTS = \
 	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		     \
@@ -43,7 +44,6 @@ noinst_PROGRAMS = \
 	test-date				     \
 	test-ebook				     \
 	test-ebook-async			     \
-	test-ebook-view				     \
 	test-nonexistent-id			     \
 	test-photo				     \
 	test-query				     \
@@ -71,6 +71,8 @@ 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_CPPFLAGS=$(TEST_CPPFLAGS)
+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_CPPFLAGS=$(TEST_CPPFLAGS)
 test_ebook_get_required_fields_LDADD=$(TEST_LIBS)
@@ -89,8 +91,6 @@ 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_CPPFLAGS=$(TEST_CPPFLAGS)
-test_ebook_view_LDADD=$(TEST_LIBS)
-test_ebook_view_CPPFLAGS=$(TEST_CPPFLAGS)
 test_changes_LDADD=$(TEST_LIBS)
 test_changes_CPPFLAGS=$(TEST_CPPFLAGS)
 test_categories_LDADD=$(TEST_LIBS)
diff --git a/addressbook/tests/ebook/ebook-test-utils.c b/addressbook/tests/ebook/ebook-test-utils.c
index 46dada9..dc8d571 100644
--- a/addressbook/tests/ebook/ebook-test-utils.c
+++ b/addressbook/tests/ebook/ebook-test-utils.c
@@ -697,3 +697,57 @@ ebook_test_utils_book_async_remove (EBook       *book,
                 exit(1);
         }
 }
+
+void
+ebook_test_utils_book_get_book_view (EBook       *book,
+                                     EBookQuery  *query,
+                                     EBookView  **view)
+{
+        GError *error = NULL;
+
+        if (!e_book_get_book_view (book, query, NULL, -1, view, &error)) {
+                const char *uri;
+
+                uri = e_book_get_uri (book);
+
+                g_warning ("failed to get view for addressbook: `%s': %s", uri,
+                                error->message);
+                exit(1);
+        }
+}
+
+static void
+get_book_view_cb (EBook            *book,
+                  EBookStatus       status,
+                  EBookView        *view,
+                  EBookTestClosure *closure)
+{
+        if (status != E_BOOK_ERROR_OK) {
+                g_warning ("failed to asynchronously get book view for the "
+                                "book: status %d", status);
+                exit (1);
+        }
+
+        closure->view = view;
+
+        g_print ("successfully asynchronously retrieved the book view\n");
+        if (closure)
+                (*closure->cb) (closure);
+}
+
+void
+ebook_test_utils_book_async_get_book_view (EBook       *book,
+                                           EBookQuery  *query,
+                                           GSourceFunc  callback,
+                                           gpointer     user_data)
+{
+        EBookTestClosure *closure;
+
+        closure = g_new0 (EBookTestClosure, 1);
+        closure->cb = callback;
+        closure->user_data = user_data;
+        if (e_book_async_get_book_view (book, query, NULL, -1, (EBookBookViewCallback) get_book_view_cb, closure)) {
+                g_warning ("failed to set up book view retrieval");
+                exit(1);
+        }
+}
diff --git a/addressbook/tests/ebook/ebook-test-utils.h b/addressbook/tests/ebook/ebook-test-utils.h
index c6d9d08..e1d12b3 100644
--- a/addressbook/tests/ebook/ebook-test-utils.h
+++ b/addressbook/tests/ebook/ebook-test-utils.h
@@ -31,6 +31,7 @@
 typedef struct {
         GSourceFunc  cb;
         gpointer     user_data;
+	EBookView   *view;
 	EList       *list;
 } EBookTestClosure;
 
@@ -137,4 +138,14 @@ ebook_test_utils_book_async_remove (EBook          *book,
                                     GSourceFunc     callback,
                                     gpointer        user_data);
 
+void
+ebook_test_utils_book_get_book_view (EBook       *book,
+                                     EBookQuery  *query,
+                                     EBookView  **view);
+void
+ebook_test_utils_book_async_get_book_view (EBook       *book,
+                                           EBookQuery  *query,
+                                           GSourceFunc  callback,
+                                           gpointer     user_data);
+
 #endif /* _EBOOK_TEST_UTILS_H */
diff --git a/addressbook/tests/ebook/test-ebook-get-book-view.c b/addressbook/tests/ebook/test-ebook-get-book-view.c
new file mode 100644
index 0000000..8fa1ba7
--- /dev/null
+++ b/addressbook/tests/ebook/test-ebook-get-book-view.c
@@ -0,0 +1,129 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+#include <stdlib.h>
+#include <libebook/e-book.h>
+
+#include "ebook-test-utils.h"
+
+static GMainLoop *loop;
+
+static void
+print_contact (EContact *contact)
+{
+	GList *emails, *e;
+
+	g_print ("Contact: %s\n", (gchar *)e_contact_get_const (contact, E_CONTACT_FULL_NAME));
+	g_print ("UID: %s\n", (gchar *)e_contact_get_const (contact, E_CONTACT_UID));
+	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_list_foreach (emails, (GFunc)g_free, NULL);
+	g_list_free (emails);
+
+	g_print ("\n");
+}
+
+static void
+contacts_added (EBookView *book_view, const GList *contacts)
+{
+	GList *l;
+
+	for (l = (GList*)contacts; l; l = l->next) {
+		print_contact (l->data);
+	}
+}
+
+static void
+contacts_removed (EBookView *book_view, const GList *ids)
+{
+	GList *l;
+
+	for (l = (GList*)ids; l; l = l->next) {
+		g_print ("Removed contact: %s\n", (gchar *)l->data);
+	}
+}
+
+static void
+sequence_complete (EBookView *book_view, EBookViewStatus status)
+{
+	g_main_loop_quit (loop);
+}
+
+static void
+setup_and_start_view (EBookView *view)
+{
+	g_signal_connect (view, "contacts_added", G_CALLBACK (contacts_added), NULL);   
+	g_signal_connect (view, "contacts_removed", G_CALLBACK (contacts_removed), NULL);
+	g_signal_connect (view, "sequence_complete", G_CALLBACK (sequence_complete), NULL);
+
+	e_book_view_start (view);
+}
+
+static void
+get_book_view_cb (EBookTestClosure *closure)
+{
+	g_assert (closure->view);
+
+	setup_and_start_view (closure->view);
+}
+
+static void
+setup_book (EBook     **book_out)
+{
+	EBook *book;
+
+	book = ebook_test_utils_book_new_temp (NULL);
+	ebook_test_utils_book_open (book, FALSE);
+
+	ebook_test_utils_book_add_contact_from_test_case_verify (book, "simple-1", NULL);
+	ebook_test_utils_book_add_contact_from_test_case_verify (book, "simple-2", NULL);
+	ebook_test_utils_book_add_contact_from_test_case_verify (book, "name-only", NULL);
+
+	*book_out = book;
+}
+
+gint
+main (gint argc, gchar **argv)
+{
+	EBook *book;
+	EBookQuery *query;
+	EBookView *view;
+
+	g_type_init ();
+
+	/*
+	 * Sync version
+	 */
+	setup_book (&book);
+	query = e_book_query_any_field_contains ("");
+	ebook_test_utils_book_get_book_view (book, query, &view);
+	setup_and_start_view (view);
+
+	g_print ("successfully set up the book view\n");
+
+	loop = g_main_loop_new (NULL, TRUE);
+	g_main_loop_run (loop);
+
+        e_book_query_unref (query);
+	ebook_test_utils_book_remove (book);
+
+	/*
+	 * Async version
+	 */
+	setup_book (&book);
+	query = e_book_query_any_field_contains ("");
+
+	loop = g_main_loop_new (NULL, TRUE);
+	ebook_test_utils_book_async_get_book_view (book, query,
+			(GSourceFunc) get_book_view_cb, loop);
+
+	g_main_loop_run (loop);
+
+        e_book_query_unref (query);
+	ebook_test_utils_book_remove (book);
+
+	return 0;
+}



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