[evolution-data-server/openismus-work: 5/5] Added new test-ebook-get-book-view-uid-only.c test.



commit 7f0b83559bc3017b0328a3d87dbcf05773d2a4cb
Author: Tristan Van Berkom <tristan van berkom gmail com>
Date:   Wed Jun 15 20:46:12 2011 +0900

    Added new test-ebook-get-book-view-uid-only.c test.
    
    Added a test that ensures the file backend returns shallow vcards
    in the case that it's requested_fields is a single field that
    is the UID (in which case it should only send the UID).
    
    Test also includes some profiling which can be viewed
    when the test is launched with EDS_TEST_DEBUG=1

 addressbook/tests/ebook/Makefile.am                |    3 +
 addressbook/tests/ebook/ebook-test-utils.c         |    6 +-
 addressbook/tests/ebook/ebook-test-utils.h         |    2 +
 .../ebook/test-ebook-get-book-view-uid-only.c      |  259 ++++++++++++++++++++
 addressbook/tests/ebook/test-ebook-get-book-view.c |    4 +-
 5 files changed, 270 insertions(+), 4 deletions(-)
---
diff --git a/addressbook/tests/ebook/Makefile.am b/addressbook/tests/ebook/Makefile.am
index 32c8e40..c2dca52 100644
--- a/addressbook/tests/ebook/Makefile.am
+++ b/addressbook/tests/ebook/Makefile.am
@@ -32,6 +32,7 @@ TESTS = \
 	test-ebook-add-contact			     \
 	test-ebook-get-contact			     \
 	test-ebook-get-book-view		     \
+	test-ebook-get-book-view-uid-only	     \
 	test-ebook-commit-contact		     \
 	test-ebook-remove-contact		     \
 	test-ebook-remove-contact-by-id		     \
@@ -83,6 +84,8 @@ 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_book_view_uid_only_LDADD=$(TEST_LIBS)
+test_ebook_get_book_view_uid_only_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)
diff --git a/addressbook/tests/ebook/ebook-test-utils.c b/addressbook/tests/ebook/ebook-test-utils.c
index 12045fe..76e2195 100644
--- a/addressbook/tests/ebook/ebook-test-utils.c
+++ b/addressbook/tests/ebook/ebook-test-utils.c
@@ -732,11 +732,12 @@ ebook_test_utils_book_async_remove (EBook       *book,
 void
 ebook_test_utils_book_get_book_view (EBook       *book,
                                      EBookQuery  *query,
+				     GList       *requested_fields,
                                      EBookView  **view)
 {
         GError *error = NULL;
 
-        if (!e_book_get_book_view (book, query, NULL, -1, view, &error)) {
+        if (!e_book_get_book_view (book, query, requested_fields, -1, view, &error)) {
                 const gchar *uri;
 
                 uri = e_book_get_uri (book);
@@ -769,6 +770,7 @@ get_book_view_cb (EBook            *book,
 void
 ebook_test_utils_book_async_get_book_view (EBook       *book,
                                            EBookQuery  *query,
+					   GList       *requested_fields,
                                            GSourceFunc  callback,
                                            gpointer     user_data)
 {
@@ -777,7 +779,7 @@ ebook_test_utils_book_async_get_book_view (EBook       *book,
         closure = g_new0 (EBookTestClosure, 1);
         closure->cb = callback;
         closure->user_data = user_data;
-        if (!e_book_get_book_view_async (book, query, NULL, -1, (EBookBookViewAsyncCallback) get_book_view_cb, closure)) {
+        if (!e_book_get_book_view_async (book, query, requested_fields, -1, (EBookBookViewAsyncCallback) 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 789e21d..13e128b 100644
--- a/addressbook/tests/ebook/ebook-test-utils.h
+++ b/addressbook/tests/ebook/ebook-test-utils.h
@@ -148,10 +148,12 @@ ebook_test_utils_book_async_remove (EBook          *book,
 void
 ebook_test_utils_book_get_book_view (EBook       *book,
                                      EBookQuery  *query,
+				     GList       *requested_fields,
                                      EBookView  **view);
 void
 ebook_test_utils_book_async_get_book_view (EBook       *book,
                                            EBookQuery  *query,
+					   GList       *requested_fields,
                                            GSourceFunc  callback,
                                            gpointer     user_data);
 
diff --git a/addressbook/tests/ebook/test-ebook-get-book-view-uid-only.c b/addressbook/tests/ebook/test-ebook-get-book-view-uid-only.c
new file mode 100644
index 0000000..cedb7c5
--- /dev/null
+++ b/addressbook/tests/ebook/test-ebook-get-book-view-uid-only.c
@@ -0,0 +1,259 @@
+/* -*- 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"
+
+#define N_TEST_CONTACTS 1000
+
+/* If COMPARE_PERFORMANCE is set, only print a performance comparison, otherwise print contacts */
+#define COMPARE_PERFORMANCE 1
+#define BEEFY_VCARDS        1
+
+
+#if COMPARE_PERFORMANCE
+#  define SETUP_TIMER(timer)  GTimer *timer = g_timer_new ();
+#  define START_TIMER(timer)  g_timer_start (timer);
+#  define STOP_TIMER(timer)   g_timer_stop (timer);
+#  define PRINT_TIMER(timer, activity)  \
+	test_print ("%s finished in %02.6f seconds\n", activity, g_timer_elapsed (timer, NULL));
+#else
+#  define SETUP_TIMER(timer)
+#  define START_TIMER(timer)
+#  define STOP_TIMER(timer)
+#  define PRINT_TIMER(timer, activity)
+#endif
+
+
+static GMainLoop *loop;
+
+#if !COMPARE_PERFORMANCE
+static void
+print_contact (EContact *contact)
+{
+	GList *emails, *e;
+
+	test_print ("Contact: %s\n", (gchar *)e_contact_get_const (contact, E_CONTACT_FULL_NAME));
+	test_print ("UID: %s\n", (gchar *)e_contact_get_const (contact, E_CONTACT_UID));
+	test_print ("Email addresses:\n");
+
+	emails = e_contact_get (contact, E_CONTACT_EMAIL);
+	for (e = emails; e; e = e->next) {
+		test_print ("\t%s\n",  (gchar *)e->data);
+	}
+	g_list_foreach (emails, (GFunc)g_free, NULL);
+	g_list_free (emails);
+
+	test_print ("\n");
+}
+#endif
+
+static void
+contacts_added (EBookView *book_view, const GList *contacts)
+{
+	GList *l;
+	gboolean uid_only;
+
+	uid_only = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (book_view), "uid-only"));
+
+	for (l = (GList*)contacts; l; l = l->next) {
+		EContact *contact = l->data;
+
+#if !COMPARE_PERFORMANCE
+		print_contact (contact);
+#endif
+
+		if (uid_only && e_contact_get_const (contact, E_CONTACT_FULL_NAME) != NULL)
+			g_error ("received contact name `%s' when only the uid was requested",
+				 (gchar *)e_contact_get_const (contact, E_CONTACT_FULL_NAME));
+		else if (!uid_only && e_contact_get_const (contact, E_CONTACT_FULL_NAME) == NULL)
+			g_error ("expected contact name missing");
+	}
+}
+
+static void
+contacts_removed (EBookView *book_view, const GList *ids)
+{
+	GList *l;
+
+	for (l = (GList*)ids; l; l = l->next) {
+		test_print ("Removed contact: %s\n", (gchar *)l->data);
+	}
+}
+
+static void
+view_complete (EBookView *book_view, EBookViewStatus status, const gchar *error_msg)
+{
+	e_book_view_stop (book_view);
+	g_object_unref (book_view);
+	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, "view_complete", G_CALLBACK (view_complete), NULL);
+
+	e_book_view_start (view);
+}
+
+static void
+get_book_view_cb (EBookTestClosure *closure)
+{
+	g_assert (closure->view);
+
+	g_object_set_data (G_OBJECT (closure->view), "uid-only", closure->user_data);
+
+	setup_and_start_view (closure->view);
+}
+
+static void
+setup_book (EBook     **book_out)
+{
+	EBook *book;
+	gint   i, j;
+
+	book = ebook_test_utils_book_new_temp (NULL);
+	ebook_test_utils_book_open (book, FALSE);
+
+	for (i = 0; i < N_TEST_CONTACTS; i++)
+	{
+		EContact *contact = e_contact_new ();
+		EContact    *final;
+		const gchar *uid;
+		gchar       *name      = g_strdup_printf ("Contact #%d", i + 1);
+		gchar       *emails[5] = {
+			g_strdup_printf ("contact%d first email com", i),
+			g_strdup_printf ("contact%d second email com", i),
+			g_strdup_printf ("contact%d third email com", i),
+			g_strdup_printf ("contact%d fourth email com", i),
+			NULL
+		};
+
+		e_contact_set (contact, E_CONTACT_FULL_NAME, name);
+		e_contact_set (contact, E_CONTACT_NICKNAME, name);
+
+		/* Fill some emails */
+		for (j = E_CONTACT_EMAIL_1; j < (E_CONTACT_EMAIL_4 + 1); j++)
+			e_contact_set (contact, j, emails[j - E_CONTACT_EMAIL_1]);
+
+#if BEEFY_VCARDS
+		/* Fill some other random stuff */
+		for (j = E_CONTACT_IM_AIM_HOME_1; j < (E_CONTACT_IM_AIM_HOME_3 + 1); j++)
+			e_contact_set (contact, j, emails[j - E_CONTACT_IM_AIM_HOME_1]);
+		for (j = E_CONTACT_IM_AIM_WORK_1; j < (E_CONTACT_IM_AIM_WORK_3 + 1); j++)
+			e_contact_set (contact, j, emails[j - E_CONTACT_IM_AIM_WORK_1]);
+		for (j = E_CONTACT_IM_GROUPWISE_HOME_1; j < (E_CONTACT_IM_GROUPWISE_HOME_3 + 1); j++)
+			e_contact_set (contact, j, emails[j - E_CONTACT_IM_GROUPWISE_HOME_1]);
+		for (j = E_CONTACT_IM_GROUPWISE_WORK_1; j < (E_CONTACT_IM_GROUPWISE_WORK_3 + 1); j++)
+			e_contact_set (contact, j, emails[j - E_CONTACT_IM_GROUPWISE_WORK_1]);
+		for (j = E_CONTACT_IM_JABBER_HOME_1; j < (E_CONTACT_IM_JABBER_HOME_3 + 1); j++)
+			e_contact_set (contact, j, emails[j - E_CONTACT_IM_JABBER_HOME_1]);
+		for (j = E_CONTACT_IM_JABBER_WORK_1; j < (E_CONTACT_IM_JABBER_WORK_3 + 1); j++)
+			e_contact_set (contact, j, emails[j - E_CONTACT_IM_JABBER_WORK_1]);
+		for (j = E_CONTACT_IM_YAHOO_HOME_1; j < (E_CONTACT_IM_YAHOO_HOME_3 + 1); j++)
+			e_contact_set (contact, j, emails[j - E_CONTACT_IM_YAHOO_HOME_1]);
+		for (j = E_CONTACT_IM_YAHOO_WORK_1; j < (E_CONTACT_IM_YAHOO_WORK_3 + 1); j++)
+			e_contact_set (contact, j, emails[j - E_CONTACT_IM_YAHOO_WORK_1]);
+#endif
+
+		uid   = ebook_test_utils_book_add_contact (book, contact);
+		final = ebook_test_utils_book_get_contact (book, uid);
+
+		/* verify the contact was added "successfully" (not thorough) */
+		g_assert (ebook_test_utils_contacts_are_equal_shallow (contact, contact));
+
+		g_free (name);
+		for (j = E_CONTACT_EMAIL_1; j < (E_CONTACT_EMAIL_4 + 1); j++)
+			g_free (emails[j - E_CONTACT_EMAIL_1]);
+
+		g_object_unref (contact);
+	}
+
+	*book_out = book;
+}
+
+gint
+main (gint argc, gchar **argv)
+{
+	EBook *book;
+	EBookQuery *query;
+	EBookView *view;
+	GList requested_field = { 0, };
+	SETUP_TIMER (timer);
+
+	requested_field.data = (gpointer)e_contact_field_name (E_CONTACT_UID);
+
+	g_type_init ();
+	setup_book (&book);
+
+	/*
+	 * Sync version with all data
+	 */
+	query = e_book_query_any_field_contains ("");
+	ebook_test_utils_book_get_book_view (book, query, NULL, &view);
+	g_object_set_data (G_OBJECT (view), "uid-only", GINT_TO_POINTER (FALSE));
+        e_book_query_unref (query);
+
+	START_TIMER (timer);
+	setup_and_start_view (view);
+	loop = g_main_loop_new (NULL, TRUE);
+	g_main_loop_run (loop);
+	STOP_TIMER (timer);
+	PRINT_TIMER (timer, "Loading all data from book view synchronously");
+
+	/*
+	 * Sync version with uids only
+	 */
+	//setup_book (&book);
+	query = e_book_query_any_field_contains ("");
+	ebook_test_utils_book_get_book_view (book, query, &requested_field, &view);
+	g_object_set_data (G_OBJECT (view), "uid-only", GINT_TO_POINTER (TRUE));
+        e_book_query_unref (query);
+
+	START_TIMER (timer);
+	setup_and_start_view (view);
+	loop = g_main_loop_new (NULL, TRUE);
+	g_main_loop_run (loop);
+	STOP_TIMER (timer);
+	PRINT_TIMER (timer, "Loading only uids from book view synchronously");
+
+
+	/*
+	 * Async version all data
+	 */
+	query = e_book_query_any_field_contains ("");
+
+	loop = g_main_loop_new (NULL, TRUE);
+	START_TIMER (timer);
+	ebook_test_utils_book_async_get_book_view (book, query, NULL,
+			   (GSourceFunc) get_book_view_cb, GINT_TO_POINTER (FALSE));
+
+	g_main_loop_run (loop);
+	STOP_TIMER (timer);
+	PRINT_TIMER (timer, "Loading all data from book view asynchronously");
+        e_book_query_unref (query);
+
+	/*
+	 * Async version only uids
+	 */
+	query = e_book_query_any_field_contains ("");
+
+	loop = g_main_loop_new (NULL, TRUE);
+	START_TIMER (timer);
+	ebook_test_utils_book_async_get_book_view (book, query, &requested_field,
+			   (GSourceFunc) get_book_view_cb, GINT_TO_POINTER (TRUE));
+
+	g_main_loop_run (loop);
+	STOP_TIMER (timer);
+	PRINT_TIMER (timer, "Loading only uids from book view asynchronously");
+        e_book_query_unref (query);
+
+
+	ebook_test_utils_book_remove (book);
+
+	return 0;
+}
diff --git a/addressbook/tests/ebook/test-ebook-get-book-view.c b/addressbook/tests/ebook/test-ebook-get-book-view.c
index e4a3277..f8e6a20 100644
--- a/addressbook/tests/ebook/test-ebook-get-book-view.c
+++ b/addressbook/tests/ebook/test-ebook-get-book-view.c
@@ -101,7 +101,7 @@ main (gint argc, gchar **argv)
 	 */
 	setup_book (&book);
 	query = e_book_query_any_field_contains ("");
-	ebook_test_utils_book_get_book_view (book, query, &view);
+	ebook_test_utils_book_get_book_view (book, query, NULL, &view);
 	setup_and_start_view (view);
 
 	test_print ("successfully set up the book view\n");
@@ -119,7 +119,7 @@ main (gint argc, gchar **argv)
 	query = e_book_query_any_field_contains ("");
 
 	loop = g_main_loop_new (NULL, TRUE);
-	ebook_test_utils_book_async_get_book_view (book, query,
+	ebook_test_utils_book_async_get_book_view (book, query, NULL,
 			(GSourceFunc) get_book_view_cb, loop);
 
 	g_main_loop_run (loop);



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