]> git.0d.be Git - empathy.git/blob - tests/check-empathy-chatroom-manager.c
check-empathy-chatroom-manager.c: check the result of system()
[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 START_TEST (test_empathy_chatroom_manager_new)
86 {
87   EmpathyChatroomManager *mgr;
88   gchar *cmd;
89   gchar *file;
90   McAccount *account;
91   struct chatroom_t chatrooms[] = {
92         { "name1", "room1", TRUE, TRUE },
93         { "name2", "room2", FALSE, TRUE }};
94
95   account = get_test_account ();
96
97   copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
98
99   file = get_user_xml_file (CHATROOM_FILE);
100   /* change the chatrooms XML file to use the account we just created */
101   cmd = g_strdup_printf ("sed -i 's/CHANGE_ME/%s/' %s",
102       mc_account_get_unique_name (account), file);
103
104   if (system (cmd) == -1)
105     {
106       g_print ("system call failed\n");
107       return;
108     }
109
110   g_free (cmd);
111
112   mgr = empathy_chatroom_manager_new (file);
113   check_chatrooms_list (mgr, account, chatrooms, 2);
114
115   g_free (file);
116   g_object_unref (mgr);
117   g_object_unref (account);
118 }
119 END_TEST
120
121 START_TEST (test_empathy_chatroom_manager_add)
122 {
123   EmpathyChatroomManager *mgr;
124   gchar *cmd;
125   gchar *file;
126   McAccount *account;
127   struct chatroom_t chatrooms[] = {
128         { "name1", "room1", TRUE, TRUE },
129         { "name2", "room2", FALSE, TRUE },
130         { "name3", "room3", FALSE, TRUE },
131         { "name4", "room4", FALSE, FALSE }};
132   EmpathyChatroom *chatroom;
133
134   account = get_test_account ();
135
136   copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
137
138   file = get_user_xml_file (CHATROOM_FILE);
139   /* change the chatrooms XML file to use the account we just created */
140   cmd = g_strdup_printf ("sed -i 's/CHANGE_ME/%s/' %s",
141       mc_account_get_unique_name (account), file);
142   system (cmd);
143   g_free (cmd);
144
145   mgr = empathy_chatroom_manager_new (file);
146
147   /* add a favorite chatroom */
148   chatroom = empathy_chatroom_new_full (account, "room3", "name3", FALSE);
149   g_object_set (chatroom, "favorite", TRUE, NULL);
150   empathy_chatroom_manager_add (mgr, chatroom);
151   g_object_unref (chatroom);
152
153   check_chatrooms_list (mgr, account, chatrooms, 3);
154
155   /* reload chatrooms file */
156   g_object_unref (mgr);
157   mgr = empathy_chatroom_manager_new (file);
158
159   /* chatroom has been added to the XML file as it's a favorite */
160   check_chatrooms_list (mgr, account, chatrooms, 3);
161
162   /* add a non favorite chatroom */
163   chatroom = empathy_chatroom_new_full (account, "room4", "name4", FALSE);
164   g_object_set (chatroom, "favorite", FALSE, NULL);
165   empathy_chatroom_manager_add (mgr, chatroom);
166   g_object_unref (chatroom);
167
168   check_chatrooms_list (mgr, account, chatrooms, 4);
169
170   /* reload chatrooms file */
171   g_object_unref (mgr);
172   mgr = empathy_chatroom_manager_new (file);
173
174   /* chatrooms has not been added to the XML file */
175   check_chatrooms_list (mgr, account, chatrooms, 3);
176
177   g_object_unref (mgr);
178   g_free (file);
179   g_object_unref (account);
180 }
181 END_TEST
182
183 START_TEST (test_empathy_chatroom_manager_remove)
184 {
185   EmpathyChatroomManager *mgr;
186   gchar *cmd;
187   gchar *file;
188   McAccount *account;
189   struct chatroom_t chatrooms[] = {
190         { "name2", "room2", FALSE, TRUE }};
191   EmpathyChatroom *chatroom;
192
193   account = get_test_account ();
194
195   copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
196
197   file = get_user_xml_file (CHATROOM_FILE);
198   /* change the chatrooms XML file to use the account we just created */
199   cmd = g_strdup_printf ("sed -i 's/CHANGE_ME/%s/' %s",
200       mc_account_get_unique_name (account), file);
201   system (cmd);
202   g_free (cmd);
203
204   mgr = empathy_chatroom_manager_new (file);
205
206   /* remove room1 */
207   chatroom = empathy_chatroom_manager_find (mgr, account, "room1");
208   fail_if (chatroom == NULL);
209   empathy_chatroom_manager_remove (mgr, chatroom);
210
211   check_chatrooms_list (mgr, account, chatrooms, 1);
212
213   /* reload chatrooms file */
214   g_object_unref (mgr);
215   mgr = empathy_chatroom_manager_new (file);
216
217   check_chatrooms_list (mgr, account, chatrooms, 1);
218
219   /* remove room1 */
220   chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
221   fail_if (chatroom == NULL);
222
223   empathy_chatroom_manager_remove (mgr, chatroom);
224
225   check_chatrooms_list (mgr, account, chatrooms, 0);
226
227   /* reload chatrooms file */
228   g_object_unref (mgr);
229   mgr = empathy_chatroom_manager_new (file);
230
231   check_chatrooms_list (mgr, account, chatrooms, 0);
232
233   g_object_unref (mgr);
234   g_free (file);
235   g_object_unref (account);
236 }
237 END_TEST
238
239 START_TEST (test_empathy_chatroom_manager_change_favorite)
240 {
241   EmpathyChatroomManager *mgr;
242   gchar *cmd;
243   gchar *file;
244   McAccount *account;
245   struct chatroom_t chatrooms[] = {
246         { "name1", "room1", TRUE, TRUE },
247         { "name2", "room2", FALSE, FALSE }};
248   EmpathyChatroom *chatroom;
249
250   account = get_test_account ();
251
252   copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
253
254   file = get_user_xml_file (CHATROOM_FILE);
255   /* change the chatrooms XML file to use the account we just created */
256   cmd = g_strdup_printf ("sed -i 's/CHANGE_ME/%s/' %s",
257       mc_account_get_unique_name (account), file);
258   system (cmd);
259   g_free (cmd);
260
261   mgr = empathy_chatroom_manager_new (file);
262
263   /* room2 is not favorite anymore */
264   chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
265   fail_if (chatroom == NULL);
266   g_object_set (chatroom, "favorite", FALSE, NULL);
267
268   check_chatrooms_list (mgr, account, chatrooms, 2);
269
270   /* reload chatrooms file */
271   g_object_unref (mgr);
272   mgr = empathy_chatroom_manager_new (file);
273
274   /* room2 is not present in the XML file anymore as it's not a favorite */
275   check_chatrooms_list (mgr, account, chatrooms, 1);
276
277   /* re-add room2 */
278   chatroom = empathy_chatroom_new_full (account, "room2", "name2", FALSE);
279   empathy_chatroom_manager_add (mgr, chatroom);
280
281   check_chatrooms_list (mgr, account, chatrooms, 2);
282
283   /* set room2 as favorite */
284   g_object_set (chatroom, "favorite", TRUE, NULL);
285
286   chatrooms[1].favorite = TRUE;
287   check_chatrooms_list (mgr, account, chatrooms, 2);
288
289   /* reload chatrooms file */
290   g_object_unref (mgr);
291   mgr = empathy_chatroom_manager_new (file);
292
293   /* room2 is back in the XML file now */
294   check_chatrooms_list (mgr, account, chatrooms, 2);
295
296   g_object_unref (mgr);
297   g_object_unref (chatroom);
298   g_free (file);
299   g_object_unref (account);
300 }
301 END_TEST
302
303 START_TEST (test_empathy_chatroom_manager_change_chatroom)
304 {
305   EmpathyChatroomManager *mgr;
306   gchar *cmd;
307   gchar *file;
308   McAccount *account;
309   struct chatroom_t chatrooms[] = {
310         { "name1", "room1", TRUE, TRUE },
311         { "name2", "room2", FALSE, TRUE }};
312   EmpathyChatroom *chatroom;
313
314   account = get_test_account ();
315
316   /*
317   copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
318
319   file = get_user_xml_file (CHATROOM_FILE);
320   */
321   copy_xml_file (CHATROOM_SAMPLE, "foo.xml");
322
323   file = get_user_xml_file ("foo.xml");
324   /* change the chatrooms XML file to use the account we just created */
325   cmd = g_strdup_printf ("sed -i 's/CHANGE_ME/%s/' %s",
326       mc_account_get_unique_name (account), file);
327   system (cmd);
328   g_free (cmd);
329
330   mgr = empathy_chatroom_manager_new (file);
331
332   check_chatrooms_list (mgr, account, chatrooms, 2);
333
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");
338
339   /* reload chatrooms file */
340   g_object_unref (mgr);
341   mgr = empathy_chatroom_manager_new (file);
342
343   chatrooms[1].name = "new_name";
344   check_chatrooms_list (mgr, account, chatrooms, 2);
345
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);
350
351   /* reload chatrooms file */
352   g_object_unref (mgr);
353   mgr = empathy_chatroom_manager_new (file);
354
355   chatrooms[1].auto_connect = TRUE;
356   check_chatrooms_list (mgr, account, chatrooms, 2);
357
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");
362
363   /* reload chatrooms file */
364   g_object_unref (mgr);
365   mgr = empathy_chatroom_manager_new (file);
366
367   chatrooms[1].room = "new_room";
368   check_chatrooms_list (mgr, account, chatrooms, 2);
369
370   g_object_unref (mgr);
371   g_free (file);
372   g_object_unref (account);
373 }
374 END_TEST
375
376 TCase *
377 make_empathy_chatroom_manager_tcase (void)
378 {
379     TCase *tc = tcase_create ("empathy-chatroom-manager");
380     tcase_add_test (tc, test_empathy_chatroom_manager_new);
381     tcase_add_test (tc, test_empathy_chatroom_manager_add);
382     tcase_add_test (tc, test_empathy_chatroom_manager_remove);
383     tcase_add_test (tc, test_empathy_chatroom_manager_change_favorite);
384     tcase_add_test (tc, test_empathy_chatroom_manager_change_chatroom);
385     return tc;
386 }