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