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