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