]> git.0d.be Git - empathy.git/blob - tests/check-empathy-chatroom-manager.c
remove obsolete help file
[empathy.git] / tests / check-empathy-chatroom-manager.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <glib/gstdio.h>
5
6 #include <gconf/gconf.h>
7 #include <gconf/gconf-client.h>
8 #include <telepathy-glib/util.h>
9 #include <check.h>
10
11 #include "check-helpers.h"
12 #include "check-libempathy.h"
13 #include "check-empathy-helpers.h"
14
15 #include <libempathy/empathy-chatroom-manager.h>
16 #include <libempathy/empathy-account-manager.h>
17
18 #define CHATROOM_SAMPLE "chatrooms-sample.xml"
19 #define CHATROOM_FILE "chatrooms.xml"
20
21 static void
22 check_chatroom (EmpathyChatroom *chatroom,
23                 const gchar *name,
24                 const gchar *room,
25                 gboolean auto_connect,
26                 gboolean favorite)
27 {
28   gboolean _favorite;
29
30   fail_if (tp_strdiff (empathy_chatroom_get_name (chatroom), name));
31   fail_if (tp_strdiff (empathy_chatroom_get_room (chatroom), room));
32   fail_if (empathy_chatroom_get_auto_connect (chatroom) != auto_connect);
33   g_object_get (chatroom, "favorite", &_favorite, NULL);
34   fail_if (favorite != _favorite);
35 }
36
37 struct chatroom_t
38 {
39   gchar *name;
40   gchar *room;
41   gboolean auto_connect;
42   gboolean favorite;
43 };
44
45 static void
46 check_chatrooms_list (EmpathyChatroomManager *mgr,
47                       EmpathyAccount *account,
48                       struct chatroom_t *_chatrooms,
49                       guint nb_chatrooms)
50 {
51   GList *chatrooms, *l;
52   guint i;
53   GHashTable *found;
54
55   fail_if (empathy_chatroom_manager_get_count (mgr, account) != nb_chatrooms);
56
57   found = g_hash_table_new (g_str_hash, g_str_equal);
58   for (i = 0; i < nb_chatrooms; i++)
59     {
60       g_hash_table_insert (found, _chatrooms[i].room, &_chatrooms[i]);
61     }
62
63   chatrooms = empathy_chatroom_manager_get_chatrooms (mgr, account);
64   fail_if (g_list_length (chatrooms) != nb_chatrooms);
65
66   for (l = chatrooms; l != NULL; l = g_list_next (l))
67     {
68       EmpathyChatroom *chatroom = l->data;
69       struct chatroom_t *tmp;
70
71       tmp = g_hash_table_lookup (found, empathy_chatroom_get_room (chatroom));
72       fail_if (tmp == NULL);
73
74       check_chatroom (chatroom, tmp->name, tmp->room, tmp->auto_connect,
75           tmp->favorite);
76
77       g_hash_table_remove (found, empathy_chatroom_get_room (chatroom));
78     }
79
80   fail_if (g_hash_table_size (found) != 0);
81
82   g_list_free (chatrooms);
83   g_hash_table_destroy (found);
84 }
85
86 static gboolean
87 change_account_name_in_file (EmpathyAccount *account,
88                              const gchar *file)
89 {
90   gchar *cmd;
91
92   cmd = g_strdup_printf ("sed -i 's/CHANGE_ME/%s/' %s",
93       empathy_account_get_unique_name (account), file);
94
95   if (system (cmd) == -1)
96     {
97       g_print ("'%s' call failed\n", cmd);
98       g_free (cmd);
99       return FALSE;
100     }
101
102   g_free (cmd);
103   return TRUE;
104 }
105
106 START_TEST (test_empathy_chatroom_manager_dup_singleton)
107 {
108   EmpathyChatroomManager *mgr;
109   gchar *file;
110   EmpathyAccount *account;
111   EmpathyAccountManager *account_manager;
112   struct chatroom_t chatrooms[] = {
113         { "name1", "room1", TRUE, TRUE },
114         { "name2", "room2", FALSE, TRUE }};
115
116   account_manager = empathy_account_manager_dup_singleton ();
117   account = get_test_account ();
118
119   copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
120
121   file = get_user_xml_file (CHATROOM_FILE);
122
123   /* change the chatrooms XML file to use the account we just created */
124   if (!change_account_name_in_file (account, file))
125     return;
126
127   mgr = empathy_chatroom_manager_dup_singleton (file);
128   check_chatrooms_list (mgr, account, chatrooms, 2);
129
130   g_free (file);
131   g_object_unref (mgr);
132   g_object_unref (account_manager);
133   g_object_unref (account);
134 }
135 END_TEST
136
137 START_TEST (test_empathy_chatroom_manager_add)
138 {
139   EmpathyChatroomManager *mgr;
140   gchar *file;
141   EmpathyAccount *account;
142   EmpathyAccountManager *account_manager;
143   struct chatroom_t chatrooms[] = {
144         { "name1", "room1", TRUE, TRUE },
145         { "name2", "room2", FALSE, TRUE },
146         { "name3", "room3", FALSE, TRUE },
147         { "name4", "room4", FALSE, FALSE }};
148   EmpathyChatroom *chatroom;
149
150   account_manager = empathy_account_manager_dup_singleton ();
151
152   account = get_test_account ();
153
154   copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
155
156   file = get_user_xml_file (CHATROOM_FILE);
157
158   /* change the chatrooms XML file to use the account we just created */
159   fail_unless (change_account_name_in_file (account, file));
160
161   mgr = empathy_chatroom_manager_dup_singleton (file);
162
163   /* add a favorite chatroom */
164   chatroom = empathy_chatroom_new_full (account, "room3", "name3", FALSE);
165   g_object_set (chatroom, "favorite", TRUE, NULL);
166   empathy_chatroom_manager_add (mgr, chatroom);
167   g_object_unref (chatroom);
168
169   check_chatrooms_list (mgr, account, chatrooms, 3);
170
171   /* reload chatrooms file */
172   g_object_unref (mgr);
173   mgr = empathy_chatroom_manager_dup_singleton (file);
174
175   /* chatroom has been added to the XML file as it's a favorite */
176   check_chatrooms_list (mgr, account, chatrooms, 3);
177
178   /* add a non favorite chatroom */
179   chatroom = empathy_chatroom_new_full (account, "room4", "name4", FALSE);
180   g_object_set (chatroom, "favorite", FALSE, NULL);
181   empathy_chatroom_manager_add (mgr, chatroom);
182   g_object_unref (chatroom);
183
184   check_chatrooms_list (mgr, account, chatrooms, 4);
185
186   /* reload chatrooms file */
187   g_object_unref (mgr);
188   mgr = empathy_chatroom_manager_dup_singleton (file);
189
190   /* chatrooms has not been added to the XML file */
191   check_chatrooms_list (mgr, account, chatrooms, 3);
192
193   g_object_unref (mgr);
194   g_free (file);
195   g_object_unref (account_manager);
196   g_object_unref (account);
197 }
198 END_TEST
199
200 START_TEST (test_empathy_chatroom_manager_remove)
201 {
202   EmpathyChatroomManager *mgr;
203   gchar *file;
204   EmpathyAccount *account;
205   struct chatroom_t chatrooms[] = {
206         { "name2", "room2", FALSE, TRUE }};
207   EmpathyChatroom *chatroom;
208   EmpathyAccountManager *account_mgr;
209
210   account_mgr = empathy_account_manager_dup_singleton ();
211   account = get_test_account ();
212
213   copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
214
215   file = get_user_xml_file (CHATROOM_FILE);
216
217   /* change the chatrooms XML file to use the account we just created */
218   fail_unless (change_account_name_in_file (account, file));
219
220   mgr = empathy_chatroom_manager_dup_singleton (file);
221
222   /* remove room1 */
223   chatroom = empathy_chatroom_manager_find (mgr, account, "room1");
224   fail_if (chatroom == NULL);
225   empathy_chatroom_manager_remove (mgr, chatroom);
226
227   check_chatrooms_list (mgr, account, chatrooms, 1);
228
229   /* reload chatrooms file */
230   g_object_unref (mgr);
231   mgr = empathy_chatroom_manager_dup_singleton (file);
232
233   check_chatrooms_list (mgr, account, chatrooms, 1);
234
235   /* remove room1 */
236   chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
237   fail_if (chatroom == NULL);
238
239   empathy_chatroom_manager_remove (mgr, chatroom);
240
241   check_chatrooms_list (mgr, account, chatrooms, 0);
242
243   /* reload chatrooms file */
244   g_object_unref (mgr);
245   mgr = empathy_chatroom_manager_dup_singleton (file);
246
247   check_chatrooms_list (mgr, account, chatrooms, 0);
248
249   g_object_unref (mgr);
250   g_free (file);
251   g_object_unref (account);
252   g_object_unref (account_mgr);
253 }
254 END_TEST
255
256 START_TEST (test_empathy_chatroom_manager_change_favorite)
257 {
258   EmpathyChatroomManager *mgr;
259   gchar *file;
260   EmpathyAccount *account;
261   EmpathyAccountManager *account_manager;
262   struct chatroom_t chatrooms[] = {
263         { "name1", "room1", TRUE, TRUE },
264         { "name2", "room2", FALSE, FALSE }};
265   EmpathyChatroom *chatroom;
266
267   account_manager = empathy_account_manager_dup_singleton ();
268   account = get_test_account ();
269
270   copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
271
272   file = get_user_xml_file (CHATROOM_FILE);
273
274   /* change the chatrooms XML file to use the account we just created */
275   fail_unless (change_account_name_in_file (account, file));
276
277   mgr = empathy_chatroom_manager_dup_singleton (file);
278
279   /* room2 is not favorite anymore */
280   chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
281   fail_if (chatroom == NULL);
282   g_object_set (chatroom, "favorite", FALSE, NULL);
283
284   check_chatrooms_list (mgr, account, chatrooms, 2);
285
286   /* reload chatrooms file */
287   g_object_unref (mgr);
288   mgr = empathy_chatroom_manager_dup_singleton (file);
289
290   /* room2 is not present in the XML file anymore as it's not a favorite */
291   check_chatrooms_list (mgr, account, chatrooms, 1);
292
293   /* re-add room2 */
294   chatroom = empathy_chatroom_new_full (account, "room2", "name2", FALSE);
295   empathy_chatroom_manager_add (mgr, chatroom);
296
297   check_chatrooms_list (mgr, account, chatrooms, 2);
298
299   /* set room2 as favorite */
300   g_object_set (chatroom, "favorite", TRUE, NULL);
301
302   chatrooms[1].favorite = TRUE;
303   check_chatrooms_list (mgr, account, chatrooms, 2);
304
305   /* reload chatrooms file */
306   g_object_unref (mgr);
307   mgr = empathy_chatroom_manager_dup_singleton (file);
308
309   /* room2 is back in the XML file now */
310   check_chatrooms_list (mgr, account, chatrooms, 2);
311
312   g_object_unref (mgr);
313   g_object_unref (chatroom);
314   g_free (file);
315   g_object_unref (account_manager);
316   g_object_unref (account);
317 }
318 END_TEST
319
320 START_TEST (test_empathy_chatroom_manager_change_chatroom)
321 {
322   EmpathyChatroomManager *mgr;
323   gchar *file;
324   EmpathyAccount *account;
325   EmpathyAccountManager *account_manager;
326   struct chatroom_t chatrooms[] = {
327         { "name1", "room1", TRUE, TRUE },
328         { "name2", "room2", FALSE, TRUE }};
329   EmpathyChatroom *chatroom;
330
331   account_manager = empathy_account_manager_dup_singleton ();
332   account = get_test_account ();
333
334   copy_xml_file (CHATROOM_SAMPLE, "foo.xml");
335
336   file = get_user_xml_file ("foo.xml");
337
338   /* change the chatrooms XML file to use the account we just created */
339   fail_unless (change_account_name_in_file (account, file));
340
341   mgr = empathy_chatroom_manager_dup_singleton (file);
342
343   check_chatrooms_list (mgr, account, chatrooms, 2);
344
345   /* change room2 name */
346   chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
347   fail_if (chatroom == NULL);
348   empathy_chatroom_set_name (chatroom, "new_name");
349
350   /* reload chatrooms file */
351   g_object_unref (mgr);
352   mgr = empathy_chatroom_manager_dup_singleton (file);
353
354   chatrooms[1].name = "new_name";
355   check_chatrooms_list (mgr, account, chatrooms, 2);
356
357   /* change room2 auto-connect status */
358   chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
359   fail_if (chatroom == NULL);
360   empathy_chatroom_set_auto_connect (chatroom, TRUE);
361
362   /* reload chatrooms file */
363   g_object_unref (mgr);
364   mgr = empathy_chatroom_manager_dup_singleton (file);
365
366   chatrooms[1].auto_connect = TRUE;
367   check_chatrooms_list (mgr, account, chatrooms, 2);
368
369   /* change room2 room */
370   chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
371   fail_if (chatroom == NULL);
372   empathy_chatroom_set_room (chatroom, "new_room");
373
374   /* reload chatrooms file */
375   g_object_unref (mgr);
376   mgr = empathy_chatroom_manager_dup_singleton (file);
377
378   chatrooms[1].room = "new_room";
379   check_chatrooms_list (mgr, account, chatrooms, 2);
380
381   g_object_unref (mgr);
382   g_free (file);
383   g_object_unref (account);
384   g_object_unref (account_manager);
385 }
386 END_TEST
387
388 TCase *
389 make_empathy_chatroom_manager_tcase (void)
390 {
391     TCase *tc = tcase_create ("empathy-chatroom-manager");
392     tcase_add_test (tc, test_empathy_chatroom_manager_dup_singleton);
393     tcase_add_test (tc, test_empathy_chatroom_manager_add);
394     tcase_add_test (tc, test_empathy_chatroom_manager_remove);
395     tcase_add_test (tc, test_empathy_chatroom_manager_change_favorite);
396     tcase_add_test (tc, test_empathy_chatroom_manager_change_chatroom);
397     return tc;
398 }