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