3.12.12
[nijm-empathy.git] / tests / empathy-chatroom-manager-test.c
blob056986a8018a3eb0649a0bcfd3c3df7df8308914
1 #include "config.h"
3 #include "empathy-chatroom-manager.h"
4 #include "test-helper.h"
6 #define CHATROOM_SAMPLE "chatrooms-sample.xml"
7 #define CHATROOM_FILE "chatrooms.xml"
9 #if 0
10 static void
11 check_chatroom (EmpathyChatroom *chatroom,
12 const gchar *name,
13 const gchar *room,
14 gboolean auto_connect,
15 gboolean favorite)
17 gboolean _favorite;
19 fail_if (tp_strdiff (empathy_chatroom_get_name (chatroom), name));
20 fail_if (tp_strdiff (empathy_chatroom_get_room (chatroom), room));
21 fail_if (empathy_chatroom_get_auto_connect (chatroom) != auto_connect);
22 g_object_get (chatroom, "favorite", &_favorite, NULL);
23 fail_if (favorite != _favorite);
26 struct chatroom_t
28 gchar *name;
29 gchar *room;
30 gboolean auto_connect;
31 gboolean favorite;
34 static void
35 check_chatrooms_list (EmpathyChatroomManager *mgr,
36 EmpathyAccount *account,
37 struct chatroom_t *_chatrooms,
38 guint nb_chatrooms)
40 GList *chatrooms, *l;
41 guint i;
42 GHashTable *found;
44 fail_if (empathy_chatroom_manager_get_count (mgr, account) != nb_chatrooms);
46 found = g_hash_table_new (g_str_hash, g_str_equal);
47 for (i = 0; i < nb_chatrooms; i++)
49 g_hash_table_insert (found, _chatrooms[i].room, &_chatrooms[i]);
52 chatrooms = empathy_chatroom_manager_get_chatrooms (mgr, account);
53 fail_if (g_list_length (chatrooms) != nb_chatrooms);
55 for (l = chatrooms; l != NULL; l = g_list_next (l))
57 EmpathyChatroom *chatroom = l->data;
58 struct chatroom_t *tmp;
60 tmp = g_hash_table_lookup (found, empathy_chatroom_get_room (chatroom));
61 fail_if (tmp == NULL);
63 check_chatroom (chatroom, tmp->name, tmp->room, tmp->auto_connect,
64 tmp->favorite);
66 g_hash_table_remove (found, empathy_chatroom_get_room (chatroom));
69 fail_if (g_hash_table_size (found) != 0);
71 g_list_free (chatrooms);
72 g_hash_table_unref (found);
75 static gboolean
76 change_account_name_in_file (EmpathyAccount *account,
77 const gchar *file)
79 gchar *cmd;
81 cmd = g_strdup_printf ("sed -i 's/CHANGE_ME/%s/' %s",
82 empathy_account_get_unique_name (account), file);
84 if (system (cmd) == -1)
86 g_print ("'%s' call failed\n", cmd);
87 g_free (cmd);
88 return FALSE;
91 g_free (cmd);
92 return TRUE;
95 START_TEST (test_empathy_chatroom_manager_dup_singleton)
97 EmpathyChatroomManager *mgr;
98 gchar *file;
99 EmpathyAccount *account;
100 EmpathyAccountManager *account_manager;
101 struct chatroom_t chatrooms[] = {
102 { "name1", "room1", TRUE, TRUE },
103 { "name2", "room2", FALSE, TRUE }};
105 account_manager = tp_account_manager_dup ();
106 account = get_test_account ();
108 copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
110 file = get_user_xml_file (CHATROOM_FILE);
112 /* change the chatrooms XML file to use the account we just created */
113 if (!change_account_name_in_file (account, file))
114 return;
116 mgr = empathy_chatroom_manager_dup_singleton (file);
117 check_chatrooms_list (mgr, account, chatrooms, 2);
119 g_free (file);
120 g_object_unref (mgr);
121 g_object_unref (account_manager);
122 g_object_unref (account);
124 END_TEST
126 START_TEST (test_empathy_chatroom_manager_add)
128 EmpathyChatroomManager *mgr;
129 gchar *file;
130 EmpathyAccount *account;
131 EmpathyAccountManager *account_manager;
132 struct chatroom_t chatrooms[] = {
133 { "name1", "room1", TRUE, TRUE },
134 { "name2", "room2", FALSE, TRUE },
135 { "name3", "room3", FALSE, TRUE },
136 { "name4", "room4", FALSE, FALSE }};
137 EmpathyChatroom *chatroom;
139 account_manager = tp_account_manager_dup ();
141 account = get_test_account ();
143 copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
145 file = get_user_xml_file (CHATROOM_FILE);
147 /* change the chatrooms XML file to use the account we just created */
148 fail_unless (change_account_name_in_file (account, file));
150 mgr = empathy_chatroom_manager_dup_singleton (file);
152 /* add a favorite chatroom */
153 chatroom = empathy_chatroom_new_full (account, "room3", "name3", FALSE);
154 g_object_set (chatroom, "favorite", TRUE, NULL);
155 empathy_chatroom_manager_add (mgr, chatroom);
156 g_object_unref (chatroom);
158 check_chatrooms_list (mgr, account, chatrooms, 3);
160 /* reload chatrooms file */
161 g_object_unref (mgr);
162 mgr = empathy_chatroom_manager_dup_singleton (file);
164 /* chatroom has been added to the XML file as it's a favorite */
165 check_chatrooms_list (mgr, account, chatrooms, 3);
167 /* add a non favorite chatroom */
168 chatroom = empathy_chatroom_new_full (account, "room4", "name4", FALSE);
169 g_object_set (chatroom, "favorite", FALSE, NULL);
170 empathy_chatroom_manager_add (mgr, chatroom);
171 g_object_unref (chatroom);
173 check_chatrooms_list (mgr, account, chatrooms, 4);
175 /* reload chatrooms file */
176 g_object_unref (mgr);
177 mgr = empathy_chatroom_manager_dup_singleton (file);
179 /* chatrooms has not been added to the XML file */
180 check_chatrooms_list (mgr, account, chatrooms, 3);
182 g_object_unref (mgr);
183 g_free (file);
184 g_object_unref (account_manager);
185 g_object_unref (account);
187 END_TEST
189 START_TEST (test_empathy_chatroom_manager_remove)
191 EmpathyChatroomManager *mgr;
192 gchar *file;
193 EmpathyAccount *account;
194 struct chatroom_t chatrooms[] = {
195 { "name2", "room2", FALSE, TRUE }};
196 EmpathyChatroom *chatroom;
197 EmpathyAccountManager *account_mgr;
199 account_mgr = tp_account_manager_dup ();
200 account = get_test_account ();
202 copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
204 file = get_user_xml_file (CHATROOM_FILE);
206 /* change the chatrooms XML file to use the account we just created */
207 fail_unless (change_account_name_in_file (account, file));
209 mgr = empathy_chatroom_manager_dup_singleton (file);
211 /* remove room1 */
212 chatroom = empathy_chatroom_manager_find (mgr, account, "room1");
213 fail_if (chatroom == NULL);
214 empathy_chatroom_manager_remove (mgr, chatroom);
216 check_chatrooms_list (mgr, account, chatrooms, 1);
218 /* reload chatrooms file */
219 g_object_unref (mgr);
220 mgr = empathy_chatroom_manager_dup_singleton (file);
222 check_chatrooms_list (mgr, account, chatrooms, 1);
224 /* remove room1 */
225 chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
226 fail_if (chatroom == NULL);
228 empathy_chatroom_manager_remove (mgr, chatroom);
230 check_chatrooms_list (mgr, account, chatrooms, 0);
232 /* reload chatrooms file */
233 g_object_unref (mgr);
234 mgr = empathy_chatroom_manager_dup_singleton (file);
236 check_chatrooms_list (mgr, account, chatrooms, 0);
238 g_object_unref (mgr);
239 g_free (file);
240 g_object_unref (account);
241 g_object_unref (account_mgr);
243 END_TEST
245 START_TEST (test_empathy_chatroom_manager_change_favorite)
247 EmpathyChatroomManager *mgr;
248 gchar *file;
249 EmpathyAccount *account;
250 EmpathyAccountManager *account_manager;
251 struct chatroom_t chatrooms[] = {
252 { "name1", "room1", TRUE, TRUE },
253 { "name2", "room2", FALSE, FALSE }};
254 EmpathyChatroom *chatroom;
256 account_manager = tp_account_manager_dup ();
257 account = get_test_account ();
259 copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
261 file = get_user_xml_file (CHATROOM_FILE);
263 /* change the chatrooms XML file to use the account we just created */
264 fail_unless (change_account_name_in_file (account, file));
266 mgr = empathy_chatroom_manager_dup_singleton (file);
268 /* room2 is not favorite anymore */
269 chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
270 fail_if (chatroom == NULL);
271 g_object_set (chatroom, "favorite", FALSE, NULL);
273 check_chatrooms_list (mgr, account, chatrooms, 2);
275 /* reload chatrooms file */
276 g_object_unref (mgr);
277 mgr = empathy_chatroom_manager_dup_singleton (file);
279 /* room2 is not present in the XML file anymore as it's not a favorite */
280 check_chatrooms_list (mgr, account, chatrooms, 1);
282 /* re-add room2 */
283 chatroom = empathy_chatroom_new_full (account, "room2", "name2", FALSE);
284 empathy_chatroom_manager_add (mgr, chatroom);
286 check_chatrooms_list (mgr, account, chatrooms, 2);
288 /* set room2 as favorite */
289 g_object_set (chatroom, "favorite", TRUE, NULL);
291 chatrooms[1].favorite = TRUE;
292 check_chatrooms_list (mgr, account, chatrooms, 2);
294 /* reload chatrooms file */
295 g_object_unref (mgr);
296 mgr = empathy_chatroom_manager_dup_singleton (file);
298 /* room2 is back in the XML file now */
299 check_chatrooms_list (mgr, account, chatrooms, 2);
301 g_object_unref (mgr);
302 g_object_unref (chatroom);
303 g_free (file);
304 g_object_unref (account_manager);
305 g_object_unref (account);
307 END_TEST
309 START_TEST (test_empathy_chatroom_manager_change_chatroom)
311 EmpathyChatroomManager *mgr;
312 gchar *file;
313 EmpathyAccount *account;
314 EmpathyAccountManager *account_manager;
315 struct chatroom_t chatrooms[] = {
316 { "name1", "room1", TRUE, TRUE },
317 { "name2", "room2", FALSE, TRUE }};
318 EmpathyChatroom *chatroom;
320 account_manager = tp_account_manager_dup ();
321 account = get_test_account ();
323 copy_xml_file (CHATROOM_SAMPLE, "foo.xml");
325 file = get_user_xml_file ("foo.xml");
327 /* change the chatrooms XML file to use the account we just created */
328 fail_unless (change_account_name_in_file (account, file));
330 mgr = empathy_chatroom_manager_dup_singleton (file);
332 check_chatrooms_list (mgr, account, chatrooms, 2);
334 /* change room2 name */
335 chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
336 fail_if (chatroom == NULL);
337 empathy_chatroom_set_name (chatroom, "new_name");
339 /* reload chatrooms file */
340 g_object_unref (mgr);
341 mgr = empathy_chatroom_manager_dup_singleton (file);
343 chatrooms[1].name = "new_name";
344 check_chatrooms_list (mgr, account, chatrooms, 2);
346 /* change room2 auto-connect status */
347 chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
348 fail_if (chatroom == NULL);
349 empathy_chatroom_set_auto_connect (chatroom, TRUE);
351 /* reload chatrooms file */
352 g_object_unref (mgr);
353 mgr = empathy_chatroom_manager_dup_singleton (file);
355 chatrooms[1].auto_connect = TRUE;
356 check_chatrooms_list (mgr, account, chatrooms, 2);
358 /* change room2 room */
359 chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
360 fail_if (chatroom == NULL);
361 empathy_chatroom_set_room (chatroom, "new_room");
363 /* reload chatrooms file */
364 g_object_unref (mgr);
365 mgr = empathy_chatroom_manager_dup_singleton (file);
367 chatrooms[1].room = "new_room";
368 check_chatrooms_list (mgr, account, chatrooms, 2);
370 g_object_unref (mgr);
371 g_free (file);
372 g_object_unref (account);
373 g_object_unref (account_manager);
375 END_TEST
376 #endif
379 main (int argc,
380 char **argv)
382 int result;
384 test_init (argc, argv);
386 #if 0
387 g_test_add_func ("/chatroom-manager/dup-singleton",
388 test_empathy_chatroom_manager_dup_singleton);
389 g_test_add_func ("/chatroom-manager/add", test_empathy_chatroom_manager_add);
390 g_test_add_func ("/chatroom-manager/remove",
391 test_empathy_chatroom_manager_remove);
392 g_test_add_func ("/chatroom-manager/change-favorite",
393 test_empathy_chatroom_manager_change_favorite);
394 g_test_add_func ("/chatroom-manager/change-chatroom",
395 test_empathy_chatroom_manager_change_chatroom);
396 #endif
398 result = g_test_run ();
399 test_deinit ();
400 return result;