add empathy_connection_managers_call_when_ready
[empathy-mirror.git] / tests / empathy-chatroom-manager-test.c
blob3d6b6f6b7a8cd1b043cb2de23f01a36be800d390
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
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>
11 #include <libempathy/empathy-chatroom-manager.h>
13 #include "test-helper.h"
15 #define CHATROOM_SAMPLE "chatrooms-sample.xml"
16 #define CHATROOM_FILE "chatrooms.xml"
18 #if 0
19 static void
20 check_chatroom (EmpathyChatroom *chatroom,
21 const gchar *name,
22 const gchar *room,
23 gboolean auto_connect,
24 gboolean favorite)
26 gboolean _favorite;
28 fail_if (tp_strdiff (empathy_chatroom_get_name (chatroom), name));
29 fail_if (tp_strdiff (empathy_chatroom_get_room (chatroom), room));
30 fail_if (empathy_chatroom_get_auto_connect (chatroom) != auto_connect);
31 g_object_get (chatroom, "favorite", &_favorite, NULL);
32 fail_if (favorite != _favorite);
35 struct chatroom_t
37 gchar *name;
38 gchar *room;
39 gboolean auto_connect;
40 gboolean favorite;
43 static void
44 check_chatrooms_list (EmpathyChatroomManager *mgr,
45 EmpathyAccount *account,
46 struct chatroom_t *_chatrooms,
47 guint nb_chatrooms)
49 GList *chatrooms, *l;
50 guint i;
51 GHashTable *found;
53 fail_if (empathy_chatroom_manager_get_count (mgr, account) != nb_chatrooms);
55 found = g_hash_table_new (g_str_hash, g_str_equal);
56 for (i = 0; i < nb_chatrooms; i++)
58 g_hash_table_insert (found, _chatrooms[i].room, &_chatrooms[i]);
61 chatrooms = empathy_chatroom_manager_get_chatrooms (mgr, account);
62 fail_if (g_list_length (chatrooms) != nb_chatrooms);
64 for (l = chatrooms; l != NULL; l = g_list_next (l))
66 EmpathyChatroom *chatroom = l->data;
67 struct chatroom_t *tmp;
69 tmp = g_hash_table_lookup (found, empathy_chatroom_get_room (chatroom));
70 fail_if (tmp == NULL);
72 check_chatroom (chatroom, tmp->name, tmp->room, tmp->auto_connect,
73 tmp->favorite);
75 g_hash_table_remove (found, empathy_chatroom_get_room (chatroom));
78 fail_if (g_hash_table_size (found) != 0);
80 g_list_free (chatrooms);
81 g_hash_table_destroy (found);
84 static gboolean
85 change_account_name_in_file (EmpathyAccount *account,
86 const gchar *file)
88 gchar *cmd;
90 cmd = g_strdup_printf ("sed -i 's/CHANGE_ME/%s/' %s",
91 empathy_account_get_unique_name (account), file);
93 if (system (cmd) == -1)
95 g_print ("'%s' call failed\n", cmd);
96 g_free (cmd);
97 return FALSE;
100 g_free (cmd);
101 return TRUE;
104 START_TEST (test_empathy_chatroom_manager_dup_singleton)
106 EmpathyChatroomManager *mgr;
107 gchar *file;
108 EmpathyAccount *account;
109 EmpathyAccountManager *account_manager;
110 struct chatroom_t chatrooms[] = {
111 { "name1", "room1", TRUE, TRUE },
112 { "name2", "room2", FALSE, TRUE }};
114 account_manager = empathy_account_manager_dup_singleton ();
115 account = get_test_account ();
117 copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
119 file = get_user_xml_file (CHATROOM_FILE);
121 /* change the chatrooms XML file to use the account we just created */
122 if (!change_account_name_in_file (account, file))
123 return;
125 mgr = empathy_chatroom_manager_dup_singleton (file);
126 check_chatrooms_list (mgr, account, chatrooms, 2);
128 g_free (file);
129 g_object_unref (mgr);
130 g_object_unref (account_manager);
131 g_object_unref (account);
133 END_TEST
135 START_TEST (test_empathy_chatroom_manager_add)
137 EmpathyChatroomManager *mgr;
138 gchar *file;
139 EmpathyAccount *account;
140 EmpathyAccountManager *account_manager;
141 struct chatroom_t chatrooms[] = {
142 { "name1", "room1", TRUE, TRUE },
143 { "name2", "room2", FALSE, TRUE },
144 { "name3", "room3", FALSE, TRUE },
145 { "name4", "room4", FALSE, FALSE }};
146 EmpathyChatroom *chatroom;
148 account_manager = empathy_account_manager_dup_singleton ();
150 account = get_test_account ();
152 copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
154 file = get_user_xml_file (CHATROOM_FILE);
156 /* change the chatrooms XML file to use the account we just created */
157 fail_unless (change_account_name_in_file (account, file));
159 mgr = empathy_chatroom_manager_dup_singleton (file);
161 /* add a favorite chatroom */
162 chatroom = empathy_chatroom_new_full (account, "room3", "name3", FALSE);
163 g_object_set (chatroom, "favorite", TRUE, NULL);
164 empathy_chatroom_manager_add (mgr, chatroom);
165 g_object_unref (chatroom);
167 check_chatrooms_list (mgr, account, chatrooms, 3);
169 /* reload chatrooms file */
170 g_object_unref (mgr);
171 mgr = empathy_chatroom_manager_dup_singleton (file);
173 /* chatroom has been added to the XML file as it's a favorite */
174 check_chatrooms_list (mgr, account, chatrooms, 3);
176 /* add a non favorite chatroom */
177 chatroom = empathy_chatroom_new_full (account, "room4", "name4", FALSE);
178 g_object_set (chatroom, "favorite", FALSE, NULL);
179 empathy_chatroom_manager_add (mgr, chatroom);
180 g_object_unref (chatroom);
182 check_chatrooms_list (mgr, account, chatrooms, 4);
184 /* reload chatrooms file */
185 g_object_unref (mgr);
186 mgr = empathy_chatroom_manager_dup_singleton (file);
188 /* chatrooms has not been added to the XML file */
189 check_chatrooms_list (mgr, account, chatrooms, 3);
191 g_object_unref (mgr);
192 g_free (file);
193 g_object_unref (account_manager);
194 g_object_unref (account);
196 END_TEST
198 START_TEST (test_empathy_chatroom_manager_remove)
200 EmpathyChatroomManager *mgr;
201 gchar *file;
202 EmpathyAccount *account;
203 struct chatroom_t chatrooms[] = {
204 { "name2", "room2", FALSE, TRUE }};
205 EmpathyChatroom *chatroom;
206 EmpathyAccountManager *account_mgr;
208 account_mgr = empathy_account_manager_dup_singleton ();
209 account = get_test_account ();
211 copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
213 file = get_user_xml_file (CHATROOM_FILE);
215 /* change the chatrooms XML file to use the account we just created */
216 fail_unless (change_account_name_in_file (account, file));
218 mgr = empathy_chatroom_manager_dup_singleton (file);
220 /* remove room1 */
221 chatroom = empathy_chatroom_manager_find (mgr, account, "room1");
222 fail_if (chatroom == NULL);
223 empathy_chatroom_manager_remove (mgr, chatroom);
225 check_chatrooms_list (mgr, account, chatrooms, 1);
227 /* reload chatrooms file */
228 g_object_unref (mgr);
229 mgr = empathy_chatroom_manager_dup_singleton (file);
231 check_chatrooms_list (mgr, account, chatrooms, 1);
233 /* remove room1 */
234 chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
235 fail_if (chatroom == NULL);
237 empathy_chatroom_manager_remove (mgr, chatroom);
239 check_chatrooms_list (mgr, account, chatrooms, 0);
241 /* reload chatrooms file */
242 g_object_unref (mgr);
243 mgr = empathy_chatroom_manager_dup_singleton (file);
245 check_chatrooms_list (mgr, account, chatrooms, 0);
247 g_object_unref (mgr);
248 g_free (file);
249 g_object_unref (account);
250 g_object_unref (account_mgr);
252 END_TEST
254 START_TEST (test_empathy_chatroom_manager_change_favorite)
256 EmpathyChatroomManager *mgr;
257 gchar *file;
258 EmpathyAccount *account;
259 EmpathyAccountManager *account_manager;
260 struct chatroom_t chatrooms[] = {
261 { "name1", "room1", TRUE, TRUE },
262 { "name2", "room2", FALSE, FALSE }};
263 EmpathyChatroom *chatroom;
265 account_manager = empathy_account_manager_dup_singleton ();
266 account = get_test_account ();
268 copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
270 file = get_user_xml_file (CHATROOM_FILE);
272 /* change the chatrooms XML file to use the account we just created */
273 fail_unless (change_account_name_in_file (account, file));
275 mgr = empathy_chatroom_manager_dup_singleton (file);
277 /* room2 is not favorite anymore */
278 chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
279 fail_if (chatroom == NULL);
280 g_object_set (chatroom, "favorite", FALSE, NULL);
282 check_chatrooms_list (mgr, account, chatrooms, 2);
284 /* reload chatrooms file */
285 g_object_unref (mgr);
286 mgr = empathy_chatroom_manager_dup_singleton (file);
288 /* room2 is not present in the XML file anymore as it's not a favorite */
289 check_chatrooms_list (mgr, account, chatrooms, 1);
291 /* re-add room2 */
292 chatroom = empathy_chatroom_new_full (account, "room2", "name2", FALSE);
293 empathy_chatroom_manager_add (mgr, chatroom);
295 check_chatrooms_list (mgr, account, chatrooms, 2);
297 /* set room2 as favorite */
298 g_object_set (chatroom, "favorite", TRUE, NULL);
300 chatrooms[1].favorite = TRUE;
301 check_chatrooms_list (mgr, account, chatrooms, 2);
303 /* reload chatrooms file */
304 g_object_unref (mgr);
305 mgr = empathy_chatroom_manager_dup_singleton (file);
307 /* room2 is back in the XML file now */
308 check_chatrooms_list (mgr, account, chatrooms, 2);
310 g_object_unref (mgr);
311 g_object_unref (chatroom);
312 g_free (file);
313 g_object_unref (account_manager);
314 g_object_unref (account);
316 END_TEST
318 START_TEST (test_empathy_chatroom_manager_change_chatroom)
320 EmpathyChatroomManager *mgr;
321 gchar *file;
322 EmpathyAccount *account;
323 EmpathyAccountManager *account_manager;
324 struct chatroom_t chatrooms[] = {
325 { "name1", "room1", TRUE, TRUE },
326 { "name2", "room2", FALSE, TRUE }};
327 EmpathyChatroom *chatroom;
329 account_manager = empathy_account_manager_dup_singleton ();
330 account = get_test_account ();
332 copy_xml_file (CHATROOM_SAMPLE, "foo.xml");
334 file = get_user_xml_file ("foo.xml");
336 /* change the chatrooms XML file to use the account we just created */
337 fail_unless (change_account_name_in_file (account, file));
339 mgr = empathy_chatroom_manager_dup_singleton (file);
341 check_chatrooms_list (mgr, account, chatrooms, 2);
343 /* change room2 name */
344 chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
345 fail_if (chatroom == NULL);
346 empathy_chatroom_set_name (chatroom, "new_name");
348 /* reload chatrooms file */
349 g_object_unref (mgr);
350 mgr = empathy_chatroom_manager_dup_singleton (file);
352 chatrooms[1].name = "new_name";
353 check_chatrooms_list (mgr, account, chatrooms, 2);
355 /* change room2 auto-connect status */
356 chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
357 fail_if (chatroom == NULL);
358 empathy_chatroom_set_auto_connect (chatroom, TRUE);
360 /* reload chatrooms file */
361 g_object_unref (mgr);
362 mgr = empathy_chatroom_manager_dup_singleton (file);
364 chatrooms[1].auto_connect = TRUE;
365 check_chatrooms_list (mgr, account, chatrooms, 2);
367 /* change room2 room */
368 chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
369 fail_if (chatroom == NULL);
370 empathy_chatroom_set_room (chatroom, "new_room");
372 /* reload chatrooms file */
373 g_object_unref (mgr);
374 mgr = empathy_chatroom_manager_dup_singleton (file);
376 chatrooms[1].room = "new_room";
377 check_chatrooms_list (mgr, account, chatrooms, 2);
379 g_object_unref (mgr);
380 g_free (file);
381 g_object_unref (account);
382 g_object_unref (account_manager);
384 END_TEST
385 #endif
388 main (int argc,
389 char **argv)
391 int result;
393 test_init (argc, argv);
395 #if 0
396 g_test_add_func ("/chatroom-manager/dup-singleton",
397 test_empathy_chatroom_manager_dup_singleton);
398 g_test_add_func ("/chatroom-manager/add", test_empathy_chatroom_manager_add);
399 g_test_add_func ("/chatroom-manager/remove",
400 test_empathy_chatroom_manager_remove);
401 g_test_add_func ("/chatroom-manager/change-favorite",
402 test_empathy_chatroom_manager_change_favorite);
403 g_test_add_func ("/chatroom-manager/change-chatroom",
404 test_empathy_chatroom_manager_change_chatroom);
405 #endif
407 result = g_test_run ();
408 test_deinit ();
409 return result;