4 #include <glib/gstdio.h>
6 #include <gconf/gconf.h>
7 #include <gconf/gconf-client.h>
8 #include <telepathy-glib/account-manager.h>
9 #include <telepathy-glib/util.h>
12 #include "check-helpers.h"
13 #include "check-libempathy.h"
14 #include "check-empathy-helpers.h"
16 #include <libempathy/empathy-chatroom-manager.h>
18 #define CHATROOM_SAMPLE "chatrooms-sample.xml"
19 #define CHATROOM_FILE "chatrooms.xml"
23 check_chatroom (EmpathyChatroom *chatroom,
26 gboolean auto_connect,
31 fail_if (tp_strdiff (empathy_chatroom_get_name (chatroom), name));
32 fail_if (tp_strdiff (empathy_chatroom_get_room (chatroom), room));
33 fail_if (empathy_chatroom_get_auto_connect (chatroom) != auto_connect);
34 g_object_get (chatroom, "favorite", &_favorite, NULL);
35 fail_if (favorite != _favorite);
42 gboolean auto_connect;
47 check_chatrooms_list (EmpathyChatroomManager *mgr,
48 EmpathyAccount *account,
49 struct chatroom_t *_chatrooms,
56 fail_if (empathy_chatroom_manager_get_count (mgr, account) != nb_chatrooms);
58 found = g_hash_table_new (g_str_hash, g_str_equal);
59 for (i = 0; i < nb_chatrooms; i++)
61 g_hash_table_insert (found, _chatrooms[i].room, &_chatrooms[i]);
64 chatrooms = empathy_chatroom_manager_get_chatrooms (mgr, account);
65 fail_if (g_list_length (chatrooms) != nb_chatrooms);
67 for (l = chatrooms; l != NULL; l = g_list_next (l))
69 EmpathyChatroom *chatroom = l->data;
70 struct chatroom_t *tmp;
72 tmp = g_hash_table_lookup (found, empathy_chatroom_get_room (chatroom));
73 fail_if (tmp == NULL);
75 check_chatroom (chatroom, tmp->name, tmp->room, tmp->auto_connect,
78 g_hash_table_remove (found, empathy_chatroom_get_room (chatroom));
81 fail_if (g_hash_table_size (found) != 0);
83 g_list_free (chatrooms);
84 g_hash_table_destroy (found);
88 change_account_name_in_file (EmpathyAccount *account,
93 cmd = g_strdup_printf ("sed -i 's/CHANGE_ME/%s/' %s",
94 empathy_account_get_unique_name (account), file);
96 if (system (cmd) == -1)
98 g_print ("'%s' call failed\n", cmd);
107 START_TEST (test_empathy_chatroom_manager_dup_singleton)
109 EmpathyChatroomManager *mgr;
111 EmpathyAccount *account;
112 EmpathyAccountManager *account_manager;
113 struct chatroom_t chatrooms[] = {
114 { "name1", "room1", TRUE, TRUE },
115 { "name2", "room2", FALSE, TRUE }};
117 account_manager = empathy_account_manager_dup_singleton ();
118 account = get_test_account ();
120 copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
122 file = get_user_xml_file (CHATROOM_FILE);
124 /* change the chatrooms XML file to use the account we just created */
125 if (!change_account_name_in_file (account, file))
128 mgr = empathy_chatroom_manager_dup_singleton (file);
129 check_chatrooms_list (mgr, account, chatrooms, 2);
132 g_object_unref (mgr);
133 g_object_unref (account_manager);
134 g_object_unref (account);
138 START_TEST (test_empathy_chatroom_manager_add)
140 EmpathyChatroomManager *mgr;
142 EmpathyAccount *account;
143 EmpathyAccountManager *account_manager;
144 struct chatroom_t chatrooms[] = {
145 { "name1", "room1", TRUE, TRUE },
146 { "name2", "room2", FALSE, TRUE },
147 { "name3", "room3", FALSE, TRUE },
148 { "name4", "room4", FALSE, FALSE }};
149 EmpathyChatroom *chatroom;
151 account_manager = empathy_account_manager_dup_singleton ();
153 account = get_test_account ();
155 copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
157 file = get_user_xml_file (CHATROOM_FILE);
159 /* change the chatrooms XML file to use the account we just created */
160 fail_unless (change_account_name_in_file (account, file));
162 mgr = empathy_chatroom_manager_dup_singleton (file);
164 /* add a favorite chatroom */
165 chatroom = empathy_chatroom_new_full (account, "room3", "name3", FALSE);
166 g_object_set (chatroom, "favorite", TRUE, NULL);
167 empathy_chatroom_manager_add (mgr, chatroom);
168 g_object_unref (chatroom);
170 check_chatrooms_list (mgr, account, chatrooms, 3);
172 /* reload chatrooms file */
173 g_object_unref (mgr);
174 mgr = empathy_chatroom_manager_dup_singleton (file);
176 /* chatroom has been added to the XML file as it's a favorite */
177 check_chatrooms_list (mgr, account, chatrooms, 3);
179 /* add a non favorite chatroom */
180 chatroom = empathy_chatroom_new_full (account, "room4", "name4", FALSE);
181 g_object_set (chatroom, "favorite", FALSE, NULL);
182 empathy_chatroom_manager_add (mgr, chatroom);
183 g_object_unref (chatroom);
185 check_chatrooms_list (mgr, account, chatrooms, 4);
187 /* reload chatrooms file */
188 g_object_unref (mgr);
189 mgr = empathy_chatroom_manager_dup_singleton (file);
191 /* chatrooms has not been added to the XML file */
192 check_chatrooms_list (mgr, account, chatrooms, 3);
194 g_object_unref (mgr);
196 g_object_unref (account_manager);
197 g_object_unref (account);
201 START_TEST (test_empathy_chatroom_manager_remove)
203 EmpathyChatroomManager *mgr;
205 EmpathyAccount *account;
206 struct chatroom_t chatrooms[] = {
207 { "name2", "room2", FALSE, TRUE }};
208 EmpathyChatroom *chatroom;
209 EmpathyAccountManager *account_mgr;
211 account_mgr = empathy_account_manager_dup_singleton ();
212 account = get_test_account ();
214 copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
216 file = get_user_xml_file (CHATROOM_FILE);
218 /* change the chatrooms XML file to use the account we just created */
219 fail_unless (change_account_name_in_file (account, file));
221 mgr = empathy_chatroom_manager_dup_singleton (file);
224 chatroom = empathy_chatroom_manager_find (mgr, account, "room1");
225 fail_if (chatroom == NULL);
226 empathy_chatroom_manager_remove (mgr, chatroom);
228 check_chatrooms_list (mgr, account, chatrooms, 1);
230 /* reload chatrooms file */
231 g_object_unref (mgr);
232 mgr = empathy_chatroom_manager_dup_singleton (file);
234 check_chatrooms_list (mgr, account, chatrooms, 1);
237 chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
238 fail_if (chatroom == NULL);
240 empathy_chatroom_manager_remove (mgr, chatroom);
242 check_chatrooms_list (mgr, account, chatrooms, 0);
244 /* reload chatrooms file */
245 g_object_unref (mgr);
246 mgr = empathy_chatroom_manager_dup_singleton (file);
248 check_chatrooms_list (mgr, account, chatrooms, 0);
250 g_object_unref (mgr);
252 g_object_unref (account);
253 g_object_unref (account_mgr);
257 START_TEST (test_empathy_chatroom_manager_change_favorite)
259 EmpathyChatroomManager *mgr;
261 EmpathyAccount *account;
262 EmpathyAccountManager *account_manager;
263 struct chatroom_t chatrooms[] = {
264 { "name1", "room1", TRUE, TRUE },
265 { "name2", "room2", FALSE, FALSE }};
266 EmpathyChatroom *chatroom;
268 account_manager = empathy_account_manager_dup_singleton ();
269 account = get_test_account ();
271 copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
273 file = get_user_xml_file (CHATROOM_FILE);
275 /* change the chatrooms XML file to use the account we just created */
276 fail_unless (change_account_name_in_file (account, file));
278 mgr = empathy_chatroom_manager_dup_singleton (file);
280 /* room2 is not favorite anymore */
281 chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
282 fail_if (chatroom == NULL);
283 g_object_set (chatroom, "favorite", FALSE, NULL);
285 check_chatrooms_list (mgr, account, chatrooms, 2);
287 /* reload chatrooms file */
288 g_object_unref (mgr);
289 mgr = empathy_chatroom_manager_dup_singleton (file);
291 /* room2 is not present in the XML file anymore as it's not a favorite */
292 check_chatrooms_list (mgr, account, chatrooms, 1);
295 chatroom = empathy_chatroom_new_full (account, "room2", "name2", FALSE);
296 empathy_chatroom_manager_add (mgr, chatroom);
298 check_chatrooms_list (mgr, account, chatrooms, 2);
300 /* set room2 as favorite */
301 g_object_set (chatroom, "favorite", TRUE, NULL);
303 chatrooms[1].favorite = TRUE;
304 check_chatrooms_list (mgr, account, chatrooms, 2);
306 /* reload chatrooms file */
307 g_object_unref (mgr);
308 mgr = empathy_chatroom_manager_dup_singleton (file);
310 /* room2 is back in the XML file now */
311 check_chatrooms_list (mgr, account, chatrooms, 2);
313 g_object_unref (mgr);
314 g_object_unref (chatroom);
316 g_object_unref (account_manager);
317 g_object_unref (account);
321 START_TEST (test_empathy_chatroom_manager_change_chatroom)
323 EmpathyChatroomManager *mgr;
325 EmpathyAccount *account;
326 EmpathyAccountManager *account_manager;
327 struct chatroom_t chatrooms[] = {
328 { "name1", "room1", TRUE, TRUE },
329 { "name2", "room2", FALSE, TRUE }};
330 EmpathyChatroom *chatroom;
332 account_manager = empathy_account_manager_dup_singleton ();
333 account = get_test_account ();
335 copy_xml_file (CHATROOM_SAMPLE, "foo.xml");
337 file = get_user_xml_file ("foo.xml");
339 /* change the chatrooms XML file to use the account we just created */
340 fail_unless (change_account_name_in_file (account, file));
342 mgr = empathy_chatroom_manager_dup_singleton (file);
344 check_chatrooms_list (mgr, account, chatrooms, 2);
346 /* change room2 name */
347 chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
348 fail_if (chatroom == NULL);
349 empathy_chatroom_set_name (chatroom, "new_name");
351 /* reload chatrooms file */
352 g_object_unref (mgr);
353 mgr = empathy_chatroom_manager_dup_singleton (file);
355 chatrooms[1].name = "new_name";
356 check_chatrooms_list (mgr, account, chatrooms, 2);
358 /* change room2 auto-connect status */
359 chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
360 fail_if (chatroom == NULL);
361 empathy_chatroom_set_auto_connect (chatroom, TRUE);
363 /* reload chatrooms file */
364 g_object_unref (mgr);
365 mgr = empathy_chatroom_manager_dup_singleton (file);
367 chatrooms[1].auto_connect = TRUE;
368 check_chatrooms_list (mgr, account, chatrooms, 2);
370 /* change room2 room */
371 chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
372 fail_if (chatroom == NULL);
373 empathy_chatroom_set_room (chatroom, "new_room");
375 /* reload chatrooms file */
376 g_object_unref (mgr);
377 mgr = empathy_chatroom_manager_dup_singleton (file);
379 chatrooms[1].room = "new_room";
380 check_chatrooms_list (mgr, account, chatrooms, 2);
382 g_object_unref (mgr);
384 g_object_unref (account);
385 g_object_unref (account_manager);
391 make_empathy_chatroom_manager_tcase (void)
393 TCase *tc = tcase_create ("empathy-chatroom-manager");
395 tcase_add_test (tc, test_empathy_chatroom_manager_dup_singleton);
396 tcase_add_test (tc, test_empathy_chatroom_manager_add);
397 tcase_add_test (tc, test_empathy_chatroom_manager_remove);
398 tcase_add_test (tc, test_empathy_chatroom_manager_change_favorite);
399 tcase_add_test (tc, test_empathy_chatroom_manager_change_chatroom);