]> git.0d.be Git - empathy.git/blob - tests/empathy-chatroom-manager-test.c
remove released flag
[empathy.git] / tests / empathy-chatroom-manager-test.c
1 #include "config.h"
2
3 #include "empathy-chatroom-manager.h"
4 #include "test-helper.h"
5
6 #define CHATROOM_SAMPLE "chatrooms-sample.xml"
7 #define CHATROOM_FILE "chatrooms.xml"
8
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)
16 {
17   gboolean _favorite;
18
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);
24 }
25
26 struct chatroom_t
27 {
28   gchar *name;
29   gchar *room;
30   gboolean auto_connect;
31   gboolean favorite;
32 };
33
34 static void
35 check_chatrooms_list (EmpathyChatroomManager *mgr,
36                       EmpathyAccount *account,
37                       struct chatroom_t *_chatrooms,
38                       guint nb_chatrooms)
39 {
40   GList *chatrooms, *l;
41   guint i;
42   GHashTable *found;
43
44   fail_if (empathy_chatroom_manager_get_count (mgr, account) != nb_chatrooms);
45
46   found = g_hash_table_new (g_str_hash, g_str_equal);
47   for (i = 0; i < nb_chatrooms; i++)
48     {
49       g_hash_table_insert (found, _chatrooms[i].room, &_chatrooms[i]);
50     }
51
52   chatrooms = empathy_chatroom_manager_get_chatrooms (mgr, account);
53   fail_if (g_list_length (chatrooms) != nb_chatrooms);
54
55   for (l = chatrooms; l != NULL; l = g_list_next (l))
56     {
57       EmpathyChatroom *chatroom = l->data;
58       struct chatroom_t *tmp;
59
60       tmp = g_hash_table_lookup (found, empathy_chatroom_get_room (chatroom));
61       fail_if (tmp == NULL);
62
63       check_chatroom (chatroom, tmp->name, tmp->room, tmp->auto_connect,
64           tmp->favorite);
65
66       g_hash_table_remove (found, empathy_chatroom_get_room (chatroom));
67     }
68
69   fail_if (g_hash_table_size (found) != 0);
70
71   g_list_free (chatrooms);
72   g_hash_table_unref (found);
73 }
74
75 static gboolean
76 change_account_name_in_file (EmpathyAccount *account,
77                              const gchar *file)
78 {
79   gchar *cmd;
80
81   cmd = g_strdup_printf ("sed -i 's/CHANGE_ME/%s/' %s",
82       empathy_account_get_unique_name (account), file);
83
84   if (system (cmd) == -1)
85     {
86       g_print ("'%s' call failed\n", cmd);
87       g_free (cmd);
88       return FALSE;
89     }
90
91   g_free (cmd);
92   return TRUE;
93 }
94
95 START_TEST (test_empathy_chatroom_manager_dup_singleton)
96 {
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 }};
104
105   account_manager = tp_account_manager_dup ();
106   account = get_test_account ();
107
108   copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
109
110   file = get_user_xml_file (CHATROOM_FILE);
111
112   /* change the chatrooms XML file to use the account we just created */
113   if (!change_account_name_in_file (account, file))
114     return;
115
116   mgr = empathy_chatroom_manager_dup_singleton (file);
117   check_chatrooms_list (mgr, account, chatrooms, 2);
118
119   g_free (file);
120   g_object_unref (mgr);
121   g_object_unref (account_manager);
122   g_object_unref (account);
123 }
124 END_TEST
125
126 START_TEST (test_empathy_chatroom_manager_add)
127 {
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;
138
139   account_manager = tp_account_manager_dup ();
140
141   account = get_test_account ();
142
143   copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
144
145   file = get_user_xml_file (CHATROOM_FILE);
146
147   /* change the chatrooms XML file to use the account we just created */
148   fail_unless (change_account_name_in_file (account, file));
149
150   mgr = empathy_chatroom_manager_dup_singleton (file);
151
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);
157
158   check_chatrooms_list (mgr, account, chatrooms, 3);
159
160   /* reload chatrooms file */
161   g_object_unref (mgr);
162   mgr = empathy_chatroom_manager_dup_singleton (file);
163
164   /* chatroom has been added to the XML file as it's a favorite */
165   check_chatrooms_list (mgr, account, chatrooms, 3);
166
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);
172
173   check_chatrooms_list (mgr, account, chatrooms, 4);
174
175   /* reload chatrooms file */
176   g_object_unref (mgr);
177   mgr = empathy_chatroom_manager_dup_singleton (file);
178
179   /* chatrooms has not been added to the XML file */
180   check_chatrooms_list (mgr, account, chatrooms, 3);
181
182   g_object_unref (mgr);
183   g_free (file);
184   g_object_unref (account_manager);
185   g_object_unref (account);
186 }
187 END_TEST
188
189 START_TEST (test_empathy_chatroom_manager_remove)
190 {
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;
198
199   account_mgr = tp_account_manager_dup ();
200   account = get_test_account ();
201
202   copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
203
204   file = get_user_xml_file (CHATROOM_FILE);
205
206   /* change the chatrooms XML file to use the account we just created */
207   fail_unless (change_account_name_in_file (account, file));
208
209   mgr = empathy_chatroom_manager_dup_singleton (file);
210
211   /* remove room1 */
212   chatroom = empathy_chatroom_manager_find (mgr, account, "room1");
213   fail_if (chatroom == NULL);
214   empathy_chatroom_manager_remove (mgr, chatroom);
215
216   check_chatrooms_list (mgr, account, chatrooms, 1);
217
218   /* reload chatrooms file */
219   g_object_unref (mgr);
220   mgr = empathy_chatroom_manager_dup_singleton (file);
221
222   check_chatrooms_list (mgr, account, chatrooms, 1);
223
224   /* remove room1 */
225   chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
226   fail_if (chatroom == NULL);
227
228   empathy_chatroom_manager_remove (mgr, chatroom);
229
230   check_chatrooms_list (mgr, account, chatrooms, 0);
231
232   /* reload chatrooms file */
233   g_object_unref (mgr);
234   mgr = empathy_chatroom_manager_dup_singleton (file);
235
236   check_chatrooms_list (mgr, account, chatrooms, 0);
237
238   g_object_unref (mgr);
239   g_free (file);
240   g_object_unref (account);
241   g_object_unref (account_mgr);
242 }
243 END_TEST
244
245 START_TEST (test_empathy_chatroom_manager_change_favorite)
246 {
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;
255
256   account_manager = tp_account_manager_dup ();
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   fail_unless (change_account_name_in_file (account, file));
265
266   mgr = empathy_chatroom_manager_dup_singleton (file);
267
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);
272
273   check_chatrooms_list (mgr, account, chatrooms, 2);
274
275   /* reload chatrooms file */
276   g_object_unref (mgr);
277   mgr = empathy_chatroom_manager_dup_singleton (file);
278
279   /* room2 is not present in the XML file anymore as it's not a favorite */
280   check_chatrooms_list (mgr, account, chatrooms, 1);
281
282   /* re-add room2 */
283   chatroom = empathy_chatroom_new_full (account, "room2", "name2", FALSE);
284   empathy_chatroom_manager_add (mgr, chatroom);
285
286   check_chatrooms_list (mgr, account, chatrooms, 2);
287
288   /* set room2 as favorite */
289   g_object_set (chatroom, "favorite", TRUE, NULL);
290
291   chatrooms[1].favorite = TRUE;
292   check_chatrooms_list (mgr, account, chatrooms, 2);
293
294   /* reload chatrooms file */
295   g_object_unref (mgr);
296   mgr = empathy_chatroom_manager_dup_singleton (file);
297
298   /* room2 is back in the XML file now */
299   check_chatrooms_list (mgr, account, chatrooms, 2);
300
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);
306 }
307 END_TEST
308
309 START_TEST (test_empathy_chatroom_manager_change_chatroom)
310 {
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;
319
320   account_manager = tp_account_manager_dup ();
321   account = get_test_account ();
322
323   copy_xml_file (CHATROOM_SAMPLE, "foo.xml");
324
325   file = get_user_xml_file ("foo.xml");
326
327   /* change the chatrooms XML file to use the account we just created */
328   fail_unless (change_account_name_in_file (account, file));
329
330   mgr = empathy_chatroom_manager_dup_singleton (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_dup_singleton (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_dup_singleton (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_dup_singleton (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   g_object_unref (account_manager);
374 }
375 END_TEST
376 #endif
377
378 int
379 main (int argc,
380     char **argv)
381 {
382   int result;
383
384   test_init (argc, argv);
385
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
397
398   result = g_test_run ();
399   test_deinit ();
400   return result;
401 }