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