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 <libebook/libebook.h>
18 
19 #include "client-test-utils.h"
20 #include "e-test-server-utils.h"
21 
22 static ETestServerClosure book_closure_sync = { E_TEST_SERVER_ADDRESS_BOOK, NULL, 0, FALSE, NULL, FALSE };
23 static ETestServerClosure book_closure_async = { E_TEST_SERVER_ADDRESS_BOOK, NULL, 0, FALSE, NULL, TRUE };
24 
25 static void
check_removed(EBookClient * book_client,const GSList * uids)26 check_removed (EBookClient *book_client,
27                const GSList *uids)
28 {
29 	g_return_if_fail (book_client != NULL);
30 	g_return_if_fail (uids != NULL);
31 
32 	while (uids) {
33 		GError *error = NULL;
34 		EContact *contact = NULL;
35 
36 		if (!e_book_client_get_contact_sync (book_client, uids->data, &contact, NULL, &error) &&
37 		    g_error_matches (error, E_BOOK_CLIENT_ERROR, E_BOOK_CLIENT_ERROR_CONTACT_NOT_FOUND)) {
38 			g_clear_error (&error);
39 		} else
40 			g_error ("fail with get contact on removed contact: %s", error->message);
41 
42 		uids = uids->next;
43 	}
44 }
45 
46 static gboolean
fill_book_client(EBookClient * book_client,GSList ** uids)47 fill_book_client (EBookClient *book_client,
48                   GSList **uids)
49 {
50 	EContact *contact;
51 
52 	g_return_val_if_fail (book_client != NULL, FALSE);
53 	g_return_val_if_fail (uids != NULL, FALSE);
54 
55 	*uids = NULL;
56 
57 	if (!add_contact_from_test_case_verify (book_client, "simple-1", &contact))
58 		return FALSE;
59 
60 	*uids = g_slist_append (*uids, e_contact_get (contact, E_CONTACT_UID));
61 	g_object_unref (contact);
62 
63 	if (!add_contact_from_test_case_verify (book_client, "simple-2", &contact))
64 		return FALSE;
65 
66 	*uids = g_slist_append (*uids, e_contact_get (contact, E_CONTACT_UID));
67 	g_object_unref (contact);
68 
69 	return TRUE;
70 }
71 
72 static void
test_remove_contacts_sync(ETestServerFixture * fixture,gconstpointer user_data)73 test_remove_contacts_sync (ETestServerFixture *fixture,
74                            gconstpointer user_data)
75 {
76 	EBookClient *book_client;
77 	GError *error = NULL;
78 	GSList *uids = NULL;
79 
80 	book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);
81 
82 	if (!fill_book_client (book_client, &uids))
83 		g_error ("Failed to add contacts");
84 
85 	if (!e_book_client_remove_contacts_sync (book_client, uids, E_BOOK_OPERATION_FLAG_NONE, NULL, &error))
86 		g_error ("remove contact sync: %s", error->message);
87 
88 	/* This will assert they are actually removed */
89 	check_removed (book_client, uids);
90 	g_slist_foreach (uids, (GFunc) g_free, NULL);
91 	g_slist_free (uids);
92 }
93 
94 typedef struct {
95 	GSList *uids;
96 	GMainLoop *loop;
97 } RemoveData;
98 
99 static void
remove_contacts_cb(GObject * source_object,GAsyncResult * result,gpointer user_data)100 remove_contacts_cb (GObject *source_object,
101                     GAsyncResult *result,
102                     gpointer user_data)
103 {
104 	GError *error = NULL;
105 	RemoveData *data = (RemoveData *) user_data;
106 
107 	if (!e_book_client_remove_contacts_finish (E_BOOK_CLIENT (source_object), result, &error))
108 		g_error ("remove contacts finish: %s", error->message);
109 
110 	check_removed (E_BOOK_CLIENT (source_object), data->uids);
111 	g_main_loop_quit (data->loop);
112 }
113 
114 static void
test_remove_contacts_async(ETestServerFixture * fixture,gconstpointer user_data)115 test_remove_contacts_async (ETestServerFixture *fixture,
116                             gconstpointer user_data)
117 {
118 	EBookClient *book_client;
119 	GSList *uids = NULL;
120 	RemoveData data;
121 
122 	book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);
123 
124 	if (!fill_book_client (book_client, &uids))
125 		g_error ("Failed to add contacts");
126 
127 	data.uids = uids;
128 	data.loop = fixture->loop;
129 	e_book_client_remove_contacts (book_client, uids, E_BOOK_OPERATION_FLAG_NONE, NULL, remove_contacts_cb, &data);
130 
131 	g_main_loop_run (fixture->loop);
132 
133 	g_slist_foreach (uids, (GFunc) g_free, NULL);
134 	g_slist_free (uids);
135 }
136 
137 gint
main(gint argc,gchar ** argv)138 main (gint argc,
139       gchar **argv)
140 {
141 	g_test_init (&argc, &argv, NULL);
142 	g_test_bug_base ("https://gitlab.gnome.org/GNOME/evolution-data-server/");
143 
144 	client_test_utils_read_args (argc, argv);
145 
146 	g_test_add (
147 		"/EBookClient/RemoveContacts/Sync",
148 		ETestServerFixture,
149 		&book_closure_sync,
150 		e_test_server_utils_setup,
151 		test_remove_contacts_sync,
152 		e_test_server_utils_teardown);
153 	g_test_add (
154 		"/EBookClient/RemoveContacts/Async",
155 		ETestServerFixture,
156 		&book_closure_async,
157 		e_test_server_utils_setup,
158 		test_remove_contacts_async,
159 		e_test_server_utils_teardown);
160 
161 	return e_test_server_utils_run (argc, argv);
162 }
163