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