1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2013 Intel Corporation
4  *
5  * This program is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  *
17  * Authors: Tristan Van Berkom <tristanvb@openismus.com>
18  */
19 
20 #include <libebook/libebook.h>
21 
22 #include "e-test-server-utils.h"
23 #include "client-test-utils.h"
24 
25 static ETestServerClosure book_closure_sync = { E_TEST_SERVER_ADDRESS_BOOK, NULL, 0, FALSE, NULL, FALSE };
26 static ETestServerClosure book_closure_async = { E_TEST_SERVER_ADDRESS_BOOK, NULL, 0, FALSE, NULL, TRUE };
27 static ETestServerClosure book_closure_direct_sync = { E_TEST_SERVER_DIRECT_ADDRESS_BOOK, NULL, 0, FALSE, NULL, FALSE };
28 static ETestServerClosure book_closure_direct_async = { E_TEST_SERVER_DIRECT_ADDRESS_BOOK, NULL, 0, FALSE, NULL, TRUE };
29 
30 #define N_VALID_SORT_FIELDS 2
31 static EContactField valid_sort_fields[] = { E_CONTACT_FAMILY_NAME, E_CONTACT_GIVEN_NAME };
32 static EBookCursorSortType valid_sort_types[] = { E_BOOK_CURSOR_SORT_ASCENDING, E_BOOK_CURSOR_SORT_ASCENDING };
33 
34 #define N_INVALID_SORT_FIELDS 1
35 static EContactField invalid_sort_fields[] = { E_CONTACT_TEL };
36 static EBookCursorSortType invalid_sort_types[] = { E_BOOK_CURSOR_SORT_ASCENDING };
37 
38 static void
test_cursor_create_empty_query_sync(ETestServerFixture * fixture,gconstpointer user_data)39 test_cursor_create_empty_query_sync (ETestServerFixture *fixture,
40                                      gconstpointer user_data)
41 {
42 	EBookClient *book_client;
43 	EBookClientCursor *cursor = NULL;
44 	GError *error = NULL;
45 
46 	book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);
47 
48 	if (!e_book_client_get_cursor_sync (book_client,
49 					    NULL,
50 					    valid_sort_fields,
51 					    valid_sort_types,
52 					    N_VALID_SORT_FIELDS,
53 					    &cursor,
54 					    NULL, &error))
55 	  g_error ("Failed to create a cursor with an empty query: %s", error->message);
56 
57 	g_object_unref (cursor);
58 }
59 
60 static void
test_cursor_create_with_query_sync(ETestServerFixture * fixture,gconstpointer user_data)61 test_cursor_create_with_query_sync (ETestServerFixture *fixture,
62                                      gconstpointer user_data)
63 {
64 	EBookClient *book_client;
65 	EBookClientCursor *cursor = NULL;
66 	GError *error = NULL;
67 	EBookQuery *query;
68 	gchar *sexp;
69 
70 	query = e_book_query_field_test (E_CONTACT_FULL_NAME, E_BOOK_QUERY_IS, "James Brown");
71 	sexp = e_book_query_to_string (query);
72 
73 	book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);
74 
75 	if (!e_book_client_get_cursor_sync (book_client,
76 					    sexp,
77 					    valid_sort_fields,
78 					    valid_sort_types,
79 					    N_VALID_SORT_FIELDS,
80 					    &cursor,
81 					    NULL, &error))
82 	  g_error ("Failed to create a cursor with an empty query: %s", error->message);
83 
84 	g_object_unref (cursor);
85 	g_free (sexp);
86 	e_book_query_unref (query);
87 }
88 
89 static void
test_cursor_create_invalid_sort_sync(ETestServerFixture * fixture,gconstpointer user_data)90 test_cursor_create_invalid_sort_sync (ETestServerFixture *fixture,
91                                       gconstpointer user_data)
92 {
93 	EBookClient *book_client;
94 	EBookClientCursor *cursor = NULL;
95 	GError *error = NULL;
96 
97 	book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);
98 
99 	if (e_book_client_get_cursor_sync (book_client,
100 					   NULL,
101 					   invalid_sort_fields,
102 					   invalid_sort_types,
103 					   N_INVALID_SORT_FIELDS,
104 					   &cursor,
105 					   NULL, &error))
106 		g_error ("Expected invalid query but successfully created cursor");
107 	else if (!g_error_matches (error,
108 				   E_CLIENT_ERROR,
109 				   E_CLIENT_ERROR_INVALID_QUERY)) {
110 		g_error (
111 			"Unexpected error: Domain '%s' Code '%d' Message: %s\n",
112 			g_quark_to_string (error->domain), error->code,
113 			error->message);
114 	}
115 
116 	g_error_free (error);
117 }
118 
119 static void
cursor_create_success_ready_cb(GObject * source_object,GAsyncResult * result,gpointer user_data)120 cursor_create_success_ready_cb (GObject *source_object,
121                                 GAsyncResult *result,
122                                 gpointer user_data)
123 {
124 	GMainLoop *loop = (GMainLoop *) user_data;
125 	EBookClientCursor *cursor = NULL;
126 	GError *error = NULL;
127 
128 	if (!e_book_client_get_cursor_finish (E_BOOK_CLIENT (source_object),
129 					      result, &cursor, &error))
130 		g_error ("Failed to create a cursor: %s", error->message);
131 
132 	g_object_unref (cursor);
133 	g_main_loop_quit (loop);
134 }
135 
136 static void
cursor_create_invalid_query_ready_cb(GObject * source_object,GAsyncResult * result,gpointer user_data)137 cursor_create_invalid_query_ready_cb (GObject *source_object,
138                                       GAsyncResult *result,
139                                       gpointer user_data)
140 {
141 	GMainLoop *loop = (GMainLoop *) user_data;
142 	EBookClientCursor *cursor = NULL;
143 	GError *error = NULL;
144 
145 	if (e_book_client_get_cursor_finish (E_BOOK_CLIENT (source_object),
146 					     result, &cursor, &error))
147 		g_error ("Expected invalid query but successfully created cursor");
148 	else if (!g_error_matches (error,
149 				   E_CLIENT_ERROR,
150 				   E_CLIENT_ERROR_INVALID_QUERY)) {
151 		g_error (
152 			"Unexpected error: Domain '%s' Code '%d' Message: %s\n",
153 			g_quark_to_string (error->domain), error->code,
154 			error->message);
155 	}
156 
157 	g_error_free (error);
158 	g_main_loop_quit (loop);
159 }
160 
161 static void
test_cursor_create_empty_query_async(ETestServerFixture * fixture,gconstpointer user_data)162 test_cursor_create_empty_query_async (ETestServerFixture *fixture,
163                                       gconstpointer user_data)
164 {
165 	EBookClient *book_client;
166 
167 	book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);
168 	e_book_client_get_cursor (
169 		book_client,
170 		NULL,
171 		valid_sort_fields,
172 		valid_sort_types,
173 		N_VALID_SORT_FIELDS,
174 		NULL,
175 		cursor_create_success_ready_cb,
176 		fixture->loop);
177 	g_main_loop_run (fixture->loop);
178 }
179 
180 static void
test_cursor_create_with_query_async(ETestServerFixture * fixture,gconstpointer user_data)181 test_cursor_create_with_query_async (ETestServerFixture *fixture,
182                                       gconstpointer user_data)
183 {
184 	EBookClient *book_client;
185 	EBookQuery *query;
186 	gchar *sexp;
187 
188 	query = e_book_query_field_test (E_CONTACT_FULL_NAME, E_BOOK_QUERY_IS, "James Brown");
189 	sexp = e_book_query_to_string (query);
190 
191 	book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);
192 	e_book_client_get_cursor (
193 		book_client,
194 		sexp,
195 		valid_sort_fields,
196 		valid_sort_types,
197 		N_VALID_SORT_FIELDS,
198 		NULL,
199 		cursor_create_success_ready_cb,
200 		fixture->loop);
201 
202 	g_free (sexp);
203 	e_book_query_unref (query);
204 
205 	g_main_loop_run (fixture->loop);
206 }
207 
208 static void
test_cursor_create_invalid_sort_async(ETestServerFixture * fixture,gconstpointer user_data)209 test_cursor_create_invalid_sort_async (ETestServerFixture *fixture,
210                                        gconstpointer user_data)
211 {
212 	EBookClient *book_client;
213 
214 	book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);
215 	e_book_client_get_cursor (
216 		book_client,
217 		NULL,
218 		invalid_sort_fields,
219 		invalid_sort_types,
220 		N_INVALID_SORT_FIELDS,
221 		NULL,
222 		cursor_create_invalid_query_ready_cb,
223 		fixture->loop);
224 
225 	g_main_loop_run (fixture->loop);
226 }
227 
228 typedef void (*TestFunc) (ETestServerFixture *fixture, gconstpointer user_data);
229 
230 typedef struct {
231 	const gchar        *test_path;
232 	gboolean            sync_test;
233 	TestFunc            func;
234 } TestClosure;
235 
236 static const TestClosure test_closures[] = {
237 	{ "/EBookClientCursor/Create/EmptyQuery/Sync", TRUE,
238 	  test_cursor_create_empty_query_sync
239 	},
240 	{ "/EBookClientCursor/Create/EmptyQuery/Async", FALSE,
241 	  test_cursor_create_empty_query_async
242 	},
243 	{ "/EBookClientCursor/Create/WithQuery/Sync", TRUE,
244 	  test_cursor_create_with_query_sync
245 	},
246 	{ "/EBookClientCursor/Create/WithQuery/Async", FALSE,
247 	  test_cursor_create_with_query_async
248 	},
249 	{ "/EBookClientCursor/Create/InvalidSort/Sync", TRUE,
250 	  test_cursor_create_invalid_sort_sync
251 	},
252 	{ "/EBookClientCursor/Create/InvalidSort/Async", FALSE,
253 	  test_cursor_create_invalid_sort_async
254 	}
255 };
256 
257 gint
main(gint argc,gchar ** argv)258 main (gint argc,
259       gchar **argv)
260 {
261 	gint i, j;
262 
263 	g_test_init (&argc, &argv, NULL);
264 	g_test_bug_base ("https://gitlab.gnome.org/GNOME/evolution-data-server/");
265 
266 	client_test_utils_read_args (argc, argv);
267 
268 	for (i = 0; i < 2; i++) {
269 
270 		for (j = 0; j < G_N_ELEMENTS (test_closures); j++) {
271 			ETestServerClosure *closure;
272 			gchar *test_path;
273 
274 			/* Regular tests */
275 			if (i == 0) {
276 				closure = test_closures[j].sync_test ?
277 					&book_closure_sync : &book_closure_async;
278 
279 				test_path = g_strdup (test_closures[j].test_path);
280 
281 			} else /* DRA tests */ {
282 				closure = test_closures[j].sync_test ?
283 					&book_closure_direct_sync : &book_closure_direct_async;
284 
285 				test_path = g_strdup_printf ("/DRA/%s", test_closures[j].test_path);
286 			}
287 
288 			g_test_add (
289 				test_path,
290 				ETestServerFixture,
291 				closure,
292 				e_test_server_utils_setup,
293 				test_closures[j].func,
294 				e_test_server_utils_teardown);
295 
296 			g_free (test_path);
297 		}
298 	}
299 
300 	return e_test_server_utils_run (argc, argv);
301 }
302