1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * This program is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU Lesser General Public License as published by
5  * the Free Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
10  * for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  */
16 
17 #include <stdlib.h>
18 #include <libebook/libebook.h>
19 
20 #include "ebook-test-utils.h"
21 #include "e-test-server-utils.h"
22 
23 static ETestServerClosure book_closure =
24 	{ E_TEST_SERVER_DEPRECATED_ADDRESS_BOOK, NULL, 0 };
25 
26 #define EMAIL_ADD "foo@bar.com"
27 
28 /* Global data */
29 static EBook *book = NULL;
30 static gchar *uid = NULL;
31 
32 static void
verify_precommit_and_prepare_contact(EContact * contact)33 verify_precommit_and_prepare_contact (EContact *contact)
34 {
35 	EVCardAttribute *attr;
36 
37 	/* ensure there is no email address to begin with, then add one */
38 	g_assert (!e_vcard_get_attribute (E_VCARD (contact), EVC_EMAIL));
39 	attr = e_vcard_attribute_new (NULL, EVC_EMAIL);
40 	e_vcard_add_attribute_with_value (E_VCARD (contact), attr, EMAIL_ADD);
41 }
42 
43 static void
verify_commit(EContact * contact)44 verify_commit (EContact *contact)
45 {
46 	EVCardAttribute *attr;
47 	gchar *email_value;
48 
49 	attr = e_vcard_get_attribute (E_VCARD (contact), EVC_EMAIL);
50 	g_assert (attr != NULL);
51 	g_assert (e_vcard_attribute_is_single_valued (attr));
52 	email_value = e_vcard_attribute_get_value (attr);
53 	g_assert (!g_strcmp0 (email_value, EMAIL_ADD));
54 }
55 
56 static gboolean
commit_verify_cb(EBookTestClosure * closure)57 commit_verify_cb (EBookTestClosure *closure)
58 {
59 	EContact *contact;
60 
61 	contact = ebook_test_utils_book_get_contact (book, uid);
62 	verify_commit (contact);
63 
64 	g_main_loop_quit ((GMainLoop *) (closure->user_data));
65 
66 	return FALSE;
67 }
68 
69 static void
test_commit_contact_sync(ETestServerFixture * fixture,gconstpointer user_data)70 test_commit_contact_sync (ETestServerFixture *fixture,
71                           gconstpointer user_data)
72 {
73 	EContact *contact;
74 
75 	book = E_TEST_SERVER_UTILS_SERVICE (fixture, EBook);
76 	uid = ebook_test_utils_book_add_contact_from_test_case_verify (book, "name-only", &contact);
77 
78 	verify_precommit_and_prepare_contact (contact);
79 	ebook_test_utils_book_commit_contact (book, contact);
80 	verify_commit (contact);
81 
82 	test_print ("successfully committed changes to contact contact '%s'\n", uid);
83 	g_object_unref (contact);
84 	g_free (uid);
85 
86 	contact = NULL;
87 	uid = NULL;
88 }
89 
90 static void
test_commit_contact_async(ETestServerFixture * fixture,gconstpointer user_data)91 test_commit_contact_async (ETestServerFixture *fixture,
92                            gconstpointer user_data)
93 {
94 	EContact *contact;
95 
96 	book = E_TEST_SERVER_UTILS_SERVICE (fixture, EBook);
97 	uid = ebook_test_utils_book_add_contact_from_test_case_verify (book, "name-only", &contact);
98 
99 	verify_precommit_and_prepare_contact (contact);
100 
101 	ebook_test_utils_book_async_commit_contact (
102 		book, contact, (GSourceFunc) commit_verify_cb, fixture->loop);
103 
104 	g_main_loop_run (fixture->loop);
105 
106 	g_object_unref (contact);
107 	g_free (uid);
108 	contact = NULL;
109 	uid = NULL;
110 }
111 
112 gint
main(gint argc,gchar ** argv)113 main (gint argc,
114       gchar **argv)
115 {
116 	g_test_init (&argc, &argv, NULL);
117 	g_test_bug_base ("https://gitlab.gnome.org/GNOME/evolution-data-server/");
118 
119 	ebook_test_utils_read_args (argc, argv);
120 
121 	g_test_add (
122 		"/EBook/CommitContact/Sync",
123 		ETestServerFixture,
124 		&book_closure,
125 		e_test_server_utils_setup,
126 		test_commit_contact_sync,
127 		e_test_server_utils_teardown);
128 	g_test_add (
129 		"/EBook/CommitContact/Async",
130 		ETestServerFixture,
131 		&book_closure,
132 		e_test_server_utils_setup,
133 		test_commit_contact_async,
134 		e_test_server_utils_teardown);
135 
136 	return e_test_server_utils_run (argc, argv);
137 }
138