summaryrefslogtreecommitdiff
path: root/gnome/tests/test-addressbook.c
blob: 7b2cef8c59b7484331b4b28dedebf4b8ed65deca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2006 Imendio AB
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * 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 General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Mikael Hallendal <micke@test-user.com>
 *         Martyn Russell <martyn@test-user.com>
 */

#include <string.h>

#include <glib.h>
#include <libebook/e-book.h>

#include "tests.h"

#define d(x)

#define CONTACT_FULL_NAME   "Dapi TestUser"
#define CONTACT_GIVEN_NAME  "Dapi"
#define CONTACT_FAMILY_NAME "TestUser"
#define CONTACT_FILE_AS     "TestUser, Dapi"
#define CONTACT_EMAIL_1     "Dapi@test-user.com"
#define CONTACT_EMAIL_2     "TestUser@test-user.com"
#define CONTACT_EMAIL_3     "DapiTestUser@test-user.com"

static gchar *new_contact_id = NULL;

/* FIXME: I have noticed that if a test fails that the post condition
 * is NEVER run, which means you end up with a lot of new contacts
 * which don't get deleted!  
 */

static void
remove_known_test_contacts (void)
{
	DBusGProxy  *proxy;
	gchar      **ids, **p;
	gint         count;
	gboolean     found;
	gboolean     success;
        EBook       *book;
	GError      *error = NULL;

        /* Remove the added test contact from the addressbook here */
        book = e_book_new_system_addressbook (NULL);
        g_return_if_fail (book != NULL);

        proxy = tests_get_dbus_proxy ();

        /* Expected successes */
        success = org_freedesktop_dapi_address_book_find_by_name (proxy, CONTACT_FULL_NAME, &ids, NULL);
	if (!success) {
		return;
	}
	
	/* Remove all found */
        success = e_book_open (book, FALSE, &error);
        if (!success) {
                g_warning ("Could not open address book, %s",
                           error ? error->message : "no error given");
                g_clear_error (&error);
                g_object_unref (book);
                return;
        }

        for (p = ids, found = FALSE, count = 0;
             *p && !found;
             p++, count++) {
        	success = e_book_remove_contact (book, *p, &error);
	        if (!success) {
        	        g_warning ("Could not remove new contact with ID:'%s', %s",
                	           *p,
                        	   error ? error->message : "no error given");
	                g_clear_error (&error);
        	} else {
                	d(g_print ("Removed new contact with ID is '%s'\n", *p));
	        }
        }
	
	g_object_unref (book);
}

static void
pre_test (void)
{
	EBook       *book;
	EContact    *contact;
	EBookChange *change;
	GList       *changes;
	GError      *error = NULL;
	const gchar *str;
	gboolean     success;

	remove_known_test_contacts ();

	if (new_contact_id) {
		g_warning ("Contact ID is already set, it looks like "
			   "the previous test didn't clean up");
		return;
	}

	d(g_print ("\n"));

	/* Add a contact to the address book here */
	book = e_book_new_system_addressbook (NULL);
	g_return_if_fail (book != NULL);

	success = e_book_open (book, FALSE, &error);
	if (!success) {
		g_warning ("Could not open address book, %s", 
			   error ? error->message : "no error given");
		g_clear_error (&error);
		g_object_unref (book); 
		return;
	}

	contact = e_contact_new ();
	if (!contact) {
		g_warning ("Could not create new contact");
		g_object_unref (book); 
		return;
	}

	/* Set details for contact */
	e_contact_set (contact, E_CONTACT_FILE_AS, CONTACT_FILE_AS);
	e_contact_set (contact, E_CONTACT_FULL_NAME, CONTACT_FULL_NAME);
	e_contact_set (contact, E_CONTACT_GIVEN_NAME, CONTACT_GIVEN_NAME);
	e_contact_set (contact, E_CONTACT_FAMILY_NAME, CONTACT_FAMILY_NAME);
	e_contact_set (contact, E_CONTACT_EMAIL_1, CONTACT_EMAIL_1);
	e_contact_set (contact, E_CONTACT_EMAIL_2, CONTACT_EMAIL_2);
	e_contact_set (contact, E_CONTACT_EMAIL_3, CONTACT_EMAIL_3);

	success = e_book_add_contact (book, contact, &error);
	if (!success) {
		g_warning ("Could not add new contact, %s", 
			   error ? error->message : "no error given");
		g_clear_error (&error);
		g_object_unref (book); 
		return;
	}

	if (!e_book_get_changes (book, "changeidtest", &changes, &error)) {
		g_warning ("Could not get book changes, %s", 
			   error ? error->message : "no error given");
		g_object_unref (contact); 
		g_object_unref (book); 
		return;
	}

	change = changes->data;
	if (change->change_type != E_BOOK_CHANGE_CARD_ADDED) {
		g_warning ("Expected change to be E_BOOK_CHANGE_CARD_ADDED, but didn't get it.\n");
		e_book_free_change_list (changes);
		g_object_unref (contact); 
		g_object_unref (book); 
		return;
	}	

	/* Remember pertinent information */
	str = e_contact_get_const (change->contact, E_CONTACT_UID);
	if (!str) {
		g_warning ("Could not get new contact's ID");
	} else {
		/* Remember this contact id */
		new_contact_id = g_strdup (str);

		d(g_print ("Added new contact with ID is '%s'\n", new_contact_id));
	}

	e_book_free_change_list (changes);
	g_object_unref (contact); 
	g_object_unref (book); 
}

static void
post_test (void)
{
	remove_known_test_contacts ();

	g_free (new_contact_id);
	new_contact_id = NULL;
}

START_TEST (test_addressbook_list)
{
	DBusGProxy  *proxy;
	gchar      **ids, **p;
	gint         count;
	gboolean     found;
	gboolean     success;

	/* Test the list call here */
	/* 1. Check that the command run successfully */
	/* 2. Check that our contact is among the listed */

	proxy = tests_get_dbus_proxy ();

	/* Expected successes */
	success = org_freedesktop_dapi_address_book_list (proxy, &ids, NULL);
	fail_if (success == FALSE);
	fail_if (ids == NULL);

	for (p = ids, found = FALSE, count = 0; 
	     *p && !found; 
	     p++, count++) {
		if (strcmp (*p, new_contact_id) == 0) {
			found = TRUE;
		}
	}
	
	d(g_print ("Address book has %d contacts and our new contact was %sfound\n", 
		   count, found ? "" : "not "));

	fail_if (found == FALSE);
}
END_TEST

START_TEST (test_addressbook_owner)
{
	DBusGProxy *proxy;
	gchar      *contact_id;
	gboolean    success;

	/* Test the owner call here */
	/* 1. Check that the command run successfully */
	/* 2. Unsure if we can really do any data checking here? */

	proxy = tests_get_dbus_proxy ();

	/* Expected successes */
	success = org_freedesktop_dapi_address_book_owner (proxy, &contact_id, NULL);
	fail_if (success == FALSE);
	fail_if (contact_id == NULL);

	d(g_print ("Own contact is '%s'\n", contact_id));
}
END_TEST

START_TEST (test_addressbook_find_by_name)
{
	DBusGProxy  *proxy;
	gchar      **ids, **p;
	gint         count;
	gboolean     found;
	gboolean     success;

	/* Test the owner call here */
	/* 1. Check that the command run successfully */
	/* 2. Check that the ID we got back was the one we added above */

	proxy = tests_get_dbus_proxy ();

	/* Expected successes */
	success = org_freedesktop_dapi_address_book_find_by_name (proxy, CONTACT_FULL_NAME, &ids, NULL);
	fail_if (success == FALSE);
	fail_if (ids == NULL);

	for (p = ids, found = FALSE, count = 0; 
	     *p && !found; 
	     p++, count++) {
		if (strcmp (*p, new_contact_id) == 0) {
			found = TRUE;
		}
	}
	
	d(g_print ("Address book found %d contacts matching and our new contact was %sfound\n", 
		   count, found ? "" : "not "));

	fail_if (found == FALSE);
}
END_TEST

START_TEST (test_addressbook_get_name)
{
	DBusGProxy *proxy;
	gchar      *given_name;
	gchar      *family_name;
	gchar      *full_name;
	gboolean    success;

	/* Test the owner call here */
	/* 1. Check that the command run successfully */
	/* 2. Check that the name is correctly retrieved */

	proxy = tests_get_dbus_proxy ();

	/* Expected successes */
	success = org_freedesktop_dapi_address_book_get_name (proxy, new_contact_id, 
							      &given_name, 
							      &family_name, 
							      &full_name,
							      NULL);
	fail_if (success == FALSE);
	fail_if (strcmp (given_name, CONTACT_GIVEN_NAME) != 0);
	fail_if (strcmp (family_name, CONTACT_FAMILY_NAME) != 0);
	fail_if (strcmp (full_name, CONTACT_FULL_NAME) != 0);

	d(g_print ("New contact's names are, given:'%s', family:'%s', full:'%s'\n", 
		   given_name, family_name, full_name));

}
END_TEST

START_TEST (test_addressbook_get_emails)
{
	DBusGProxy  *proxy;
	gchar      **addresses, **p;
	gint         count, found;
	gboolean     success;

	/* Test the owner call here */
	/* 1. Check that the command run successfully */
	/* 2. Check that all the emails (we should set more than one) are
	 *    retrieved correctly */

	proxy = tests_get_dbus_proxy ();

	/* Expected successes */
	success = org_freedesktop_dapi_address_book_get_emails (proxy, new_contact_id, &addresses, NULL);
	fail_if (success == FALSE);
	fail_if (addresses == NULL);

	for (p = addresses, found = 0, count = 0; 
	     *p && found < 3; 
	     p++, count++) {
		if (strcmp (*p, CONTACT_EMAIL_1) == 0 ||
		    strcmp (*p, CONTACT_EMAIL_2) == 0 ||
		    strcmp (*p, CONTACT_EMAIL_3) == 0) {
			found++;
		}
	}
	
	d(g_print ("Address book found %d email addresses\n", found));

	fail_if (found < 3);
}
END_TEST

START_TEST (test_addressbook_get_vcard30)
{
	DBusGProxy *proxy;
	gchar      *vcard;
	gboolean    success;

	/* Test the owner call here */
	/* 1. Check that the command run successfully */
	/* 2. Check that the vcard looks good */

	proxy = tests_get_dbus_proxy ();

	/* Expected successes */
	success = org_freedesktop_dapi_address_book_get_vcard30 (proxy, new_contact_id, &vcard, NULL);
	fail_if (success == FALSE);
	fail_if (vcard == NULL);
	fail_if (strstr (vcard, CONTACT_FULL_NAME) == NULL);

	d(g_print ("VCard for contact ID:'%s' is\n%s\n", new_contact_id, vcard));

	/* Expected failures */
	success = org_freedesktop_dapi_address_book_get_vcard30 (proxy, NULL, &vcard, NULL);
	fail_if (success == TRUE);
}
END_TEST

Suite *
tests_create_addressbook_test_suite (void)
{
	Suite *suite;
	TCase *t_case;

	suite = suite_create ("Addressbook");

	t_case = tcase_create ("Api");
	tcase_add_checked_fixture (t_case, pre_test, post_test);
	tcase_add_test (t_case, test_addressbook_list);
	tcase_add_test (t_case, test_addressbook_owner);
	tcase_add_test (t_case, test_addressbook_find_by_name);
	tcase_add_test (t_case, test_addressbook_get_name);
	tcase_add_test (t_case, test_addressbook_get_emails);
	tcase_add_test (t_case, test_addressbook_get_vcard30);

	suite_add_tcase (suite, t_case);

	return suite;
}