]> git.0d.be Git - empathy.git/blob - tests/check-empathy-chatroom-manager.c
test empathy chatroom manager change favorite
[empathy.git] / tests / check-empathy-chatroom-manager.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/util.h>
9 #include <check.h>
10
11 #include "check-helpers.h"
12 #include "check-libempathy.h"
13 #include "check-empathy-helpers.h"
14
15 #include <libempathy/empathy-chatroom-manager.h>
16
17 #define CHATROOM_SAMPLE "chatrooms-sample.xml"
18 #define CHATROOM_FILE "chatrooms.xml"
19
20 static void
21 check_chatroom (EmpathyChatroom *chatroom,
22                 const gchar *name,
23                 const gchar *room,
24                 gboolean auto_connect,
25                 gboolean favorite)
26 {
27   gboolean _favorite;
28
29   fail_if (tp_strdiff (empathy_chatroom_get_name (chatroom), name));
30   fail_if (tp_strdiff (empathy_chatroom_get_room (chatroom), room));
31   fail_if (empathy_chatroom_get_auto_connect (chatroom) != auto_connect);
32   g_object_get (chatroom, "favorite", &_favorite, NULL);
33   fail_if (favorite != _favorite);
34 }
35
36 struct chatroom_t
37 {
38   gchar *name;
39   gchar *room;
40   gboolean auto_connect;
41   gboolean favorite;
42 };
43
44 static void
45 check_chatrooms_list (EmpathyChatroomManager *mgr,
46                       McAccount *account,
47                       struct chatroom_t *_chatrooms,
48                       guint nb_chatrooms)
49 {
50   GList *chatrooms, *l;
51   guint i;
52   GHashTable *found;
53
54   fail_if (empathy_chatroom_manager_get_count (mgr, account) != nb_chatrooms);
55
56   found = g_hash_table_new (g_str_hash, g_str_equal);
57   for (i = 0; i < nb_chatrooms; i++)
58     {
59       g_hash_table_insert (found, _chatrooms[i].room, &_chatrooms[i]);
60     }
61
62   chatrooms = empathy_chatroom_manager_get_chatrooms (mgr, account);
63   fail_if (g_list_length (chatrooms) != nb_chatrooms);
64
65   for (l = chatrooms; l != NULL; l = g_list_next (l))
66     {
67       EmpathyChatroom *chatroom = l->data;
68       struct chatroom_t *tmp;
69
70       tmp = g_hash_table_lookup (found, empathy_chatroom_get_room (chatroom));
71       fail_if (tmp == NULL);
72
73       check_chatroom (chatroom, tmp->name, tmp->room, tmp->auto_connect,
74           tmp->favorite);
75
76       g_hash_table_remove (found, empathy_chatroom_get_room (chatroom));
77     }
78
79   fail_if (g_hash_table_size (found) != 0);
80
81   g_list_free (chatrooms);
82   g_hash_table_destroy (found);
83 }
84
85 START_TEST (test_empathy_chatroom_manager_new)
86 {
87   EmpathyChatroomManager *mgr;
88   gchar *cmd;
89   gchar *file;
90   McAccount *account;
91   struct chatroom_t chatrooms[] = {
92         { "name1", "room1", TRUE, TRUE },
93         { "name2", "room2", FALSE, TRUE }};
94
95   account = create_test_account ();
96
97   copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
98
99   file = get_user_xml_file (CHATROOM_FILE);
100   /* change the chatrooms XML file to use the account we just created */
101   cmd = g_strdup_printf ("sed -i 's/CHANGE_ME/%s/' %s",
102       mc_account_get_unique_name (account), file);
103   system (cmd);
104   g_free (cmd);
105
106   mgr = empathy_chatroom_manager_new (file);
107   check_chatrooms_list (mgr, account, chatrooms, 2);
108
109   g_free (file);
110   g_object_unref (mgr);
111   destroy_test_account (account);
112 }
113 END_TEST
114
115 START_TEST (test_empathy_chatroom_manager_add)
116 {
117   EmpathyChatroomManager *mgr;
118   gchar *cmd;
119   gchar *file;
120   McAccount *account;
121   struct chatroom_t chatrooms[] = {
122         { "name1", "room1", TRUE, TRUE },
123         { "name2", "room2", FALSE, TRUE },
124         { "name3", "room3", FALSE, TRUE },
125         { "name4", "room4", FALSE, FALSE }};
126   EmpathyChatroom *chatroom;
127
128   account = create_test_account ();
129
130   copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
131
132   file = get_user_xml_file (CHATROOM_FILE);
133   /* change the chatrooms XML file to use the account we just created */
134   cmd = g_strdup_printf ("sed -i 's/CHANGE_ME/%s/' %s",
135       mc_account_get_unique_name (account), file);
136   system (cmd);
137   g_free (cmd);
138
139   mgr = empathy_chatroom_manager_new (file);
140
141   /* add a favorite chatroom */
142   chatroom = empathy_chatroom_new_full (account, "room3", "name3", FALSE);
143   g_object_set (chatroom, "favorite", TRUE, NULL);
144   empathy_chatroom_manager_add (mgr, chatroom);
145   g_object_unref (chatroom);
146
147   check_chatrooms_list (mgr, account, chatrooms, 3);
148
149   /* reload chatrooms file */
150   g_object_unref (mgr);
151   mgr = empathy_chatroom_manager_new (file);
152
153   /* chatroom has been added to the XML file as it's a favorite */
154   check_chatrooms_list (mgr, account, chatrooms, 3);
155
156   /* add a non favorite chatroom */
157   chatroom = empathy_chatroom_new_full (account, "room4", "name4", FALSE);
158   g_object_set (chatroom, "favorite", FALSE, NULL);
159   empathy_chatroom_manager_add (mgr, chatroom);
160   g_object_unref (chatroom);
161
162   check_chatrooms_list (mgr, account, chatrooms, 4);
163
164   /* reload chatrooms file */
165   g_object_unref (mgr);
166   mgr = empathy_chatroom_manager_new (file);
167
168   /* chatrooms has not been added to the XML file */
169   check_chatrooms_list (mgr, account, chatrooms, 3);
170
171   g_object_unref (mgr);
172   g_free (file);
173   destroy_test_account (account);
174 }
175 END_TEST
176
177 START_TEST (test_empathy_chatroom_manager_remove)
178 {
179   EmpathyChatroomManager *mgr;
180   gchar *cmd;
181   gchar *file;
182   McAccount *account;
183   struct chatroom_t chatrooms[] = {
184         { "name2", "room2", FALSE, TRUE }};
185   EmpathyChatroom *chatroom;
186
187   account = create_test_account ();
188
189   copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
190
191   file = get_user_xml_file (CHATROOM_FILE);
192   /* change the chatrooms XML file to use the account we just created */
193   cmd = g_strdup_printf ("sed -i 's/CHANGE_ME/%s/' %s",
194       mc_account_get_unique_name (account), file);
195   system (cmd);
196   g_free (cmd);
197
198   mgr = empathy_chatroom_manager_new (file);
199
200   /* remove room1 */
201   chatroom = empathy_chatroom_manager_find (mgr, account, "room1");
202   fail_if (chatroom == NULL);
203   empathy_chatroom_manager_remove (mgr, chatroom);
204
205   check_chatrooms_list (mgr, account, chatrooms, 1);
206
207   /* reload chatrooms file */
208   g_object_unref (mgr);
209   mgr = empathy_chatroom_manager_new (file);
210
211   check_chatrooms_list (mgr, account, chatrooms, 1);
212
213   /* remove room1 */
214   chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
215   fail_if (chatroom == NULL);
216
217   empathy_chatroom_manager_remove (mgr, chatroom);
218
219   check_chatrooms_list (mgr, account, chatrooms, 0);
220
221   /* reload chatrooms file */
222   g_object_unref (mgr);
223   mgr = empathy_chatroom_manager_new (file);
224
225   check_chatrooms_list (mgr, account, chatrooms, 0);
226
227   g_object_unref (mgr);
228   g_free (file);
229   destroy_test_account (account);
230 }
231 END_TEST
232
233 START_TEST (test_empathy_chatroom_manager_change_favorite)
234 {
235   EmpathyChatroomManager *mgr;
236   gchar *cmd;
237   gchar *file;
238   McAccount *account;
239   struct chatroom_t chatrooms[] = {
240         { "name1", "room1", TRUE, TRUE },
241         { "name2", "room2", FALSE, FALSE }};
242   EmpathyChatroom *chatroom;
243
244   account = create_test_account ();
245
246   copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);
247
248   file = get_user_xml_file (CHATROOM_FILE);
249   /* change the chatrooms XML file to use the account we just created */
250   cmd = g_strdup_printf ("sed -i 's/CHANGE_ME/%s/' %s",
251       mc_account_get_unique_name (account), file);
252   system (cmd);
253   g_free (cmd);
254
255   mgr = empathy_chatroom_manager_new (file);
256
257   /* room2 is not favorite anymore */
258   chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
259   fail_if (chatroom == NULL);
260   g_object_set (chatroom, "favorite", FALSE, NULL);
261
262   check_chatrooms_list (mgr, account, chatrooms, 2);
263
264   /* reload chatrooms file */
265   g_object_unref (mgr);
266   mgr = empathy_chatroom_manager_new (file);
267
268   /* room2 is not present in the XML file anymore as it's not a favorite */
269   check_chatrooms_list (mgr, account, chatrooms, 1);
270
271   /* re-add room2 */
272   chatroom = empathy_chatroom_new_full (account, "room2", "name2", FALSE);
273   empathy_chatroom_manager_add (mgr, chatroom);
274
275   check_chatrooms_list (mgr, account, chatrooms, 2);
276
277   /* set room2 as favorite */
278   g_object_set (chatroom, "favorite", TRUE, NULL);
279
280   chatrooms[1].favorite = TRUE;
281   check_chatrooms_list (mgr, account, chatrooms, 2);
282
283   /* reload chatrooms file */
284   g_object_unref (mgr);
285   mgr = empathy_chatroom_manager_new (file);
286
287   /* room2 is back in the XML file now */
288   check_chatrooms_list (mgr, account, chatrooms, 2);
289
290   g_object_unref (mgr);
291   g_object_unref (chatroom);
292   g_free (file);
293   destroy_test_account (account);
294 }
295 END_TEST
296
297 TCase *
298 make_empathy_chatroom_manager_tcase (void)
299 {
300     TCase *tc = tcase_create ("empathy-chatroom-manager");
301     tcase_add_test (tc, test_empathy_chatroom_manager_new);
302     tcase_add_test (tc, test_empathy_chatroom_manager_add);
303     tcase_add_test (tc, test_empathy_chatroom_manager_remove);
304     tcase_add_test (tc, test_empathy_chatroom_manager_change_favorite);
305     return tc;
306 }