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