4 #include <glib/gstdio.h>
6 #include <telepathy-glib/account-manager.h>
7 #include <telepathy-glib/util.h>
9 #include <libempathy/empathy-chatroom-manager.h>
11 #include "test-helper.h"
13 #define CHATROOM_SAMPLE "chatrooms-sample.xml"
14 #define CHATROOM_FILE "chatrooms.xml"
18 check_chatroom (EmpathyChatroom
*chatroom
,
21 gboolean auto_connect
,
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
);
37 gboolean auto_connect
;
42 check_chatrooms_list (EmpathyChatroomManager
*mgr
,
43 EmpathyAccount
*account
,
44 struct chatroom_t
*_chatrooms
,
51 fail_if (empathy_chatroom_manager_get_count (mgr
, account
) != nb_chatrooms
);
53 found
= g_hash_table_new (g_str_hash
, g_str_equal
);
54 for (i
= 0; i
< nb_chatrooms
; i
++)
56 g_hash_table_insert (found
, _chatrooms
[i
].room
, &_chatrooms
[i
]);
59 chatrooms
= empathy_chatroom_manager_get_chatrooms (mgr
, account
);
60 fail_if (g_list_length (chatrooms
) != nb_chatrooms
);
62 for (l
= chatrooms
; l
!= NULL
; l
= g_list_next (l
))
64 EmpathyChatroom
*chatroom
= l
->data
;
65 struct chatroom_t
*tmp
;
67 tmp
= g_hash_table_lookup (found
, empathy_chatroom_get_room (chatroom
));
68 fail_if (tmp
== NULL
);
70 check_chatroom (chatroom
, tmp
->name
, tmp
->room
, tmp
->auto_connect
,
73 g_hash_table_remove (found
, empathy_chatroom_get_room (chatroom
));
76 fail_if (g_hash_table_size (found
) != 0);
78 g_list_free (chatrooms
);
79 g_hash_table_unref (found
);
83 change_account_name_in_file (EmpathyAccount
*account
,
88 cmd
= g_strdup_printf ("sed -i 's/CHANGE_ME/%s/' %s",
89 empathy_account_get_unique_name (account
), file
);
91 if (system (cmd
) == -1)
93 g_print ("'%s' call failed\n", cmd
);
102 START_TEST (test_empathy_chatroom_manager_dup_singleton
)
104 EmpathyChatroomManager
*mgr
;
106 EmpathyAccount
*account
;
107 EmpathyAccountManager
*account_manager
;
108 struct chatroom_t chatrooms
[] = {
109 { "name1", "room1", TRUE
, TRUE
},
110 { "name2", "room2", FALSE
, TRUE
}};
112 account_manager
= tp_account_manager_dup ();
113 account
= get_test_account ();
115 copy_xml_file (CHATROOM_SAMPLE
, CHATROOM_FILE
);
117 file
= get_user_xml_file (CHATROOM_FILE
);
119 /* change the chatrooms XML file to use the account we just created */
120 if (!change_account_name_in_file (account
, file
))
123 mgr
= empathy_chatroom_manager_dup_singleton (file
);
124 check_chatrooms_list (mgr
, account
, chatrooms
, 2);
127 g_object_unref (mgr
);
128 g_object_unref (account_manager
);
129 g_object_unref (account
);
133 START_TEST (test_empathy_chatroom_manager_add
)
135 EmpathyChatroomManager
*mgr
;
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
;
146 account_manager
= tp_account_manager_dup ();
148 account
= get_test_account ();
150 copy_xml_file (CHATROOM_SAMPLE
, CHATROOM_FILE
);
152 file
= get_user_xml_file (CHATROOM_FILE
);
154 /* change the chatrooms XML file to use the account we just created */
155 fail_unless (change_account_name_in_file (account
, file
));
157 mgr
= empathy_chatroom_manager_dup_singleton (file
);
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
);
165 check_chatrooms_list (mgr
, account
, chatrooms
, 3);
167 /* reload chatrooms file */
168 g_object_unref (mgr
);
169 mgr
= empathy_chatroom_manager_dup_singleton (file
);
171 /* chatroom has been added to the XML file as it's a favorite */
172 check_chatrooms_list (mgr
, account
, chatrooms
, 3);
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
);
180 check_chatrooms_list (mgr
, account
, chatrooms
, 4);
182 /* reload chatrooms file */
183 g_object_unref (mgr
);
184 mgr
= empathy_chatroom_manager_dup_singleton (file
);
186 /* chatrooms has not been added to the XML file */
187 check_chatrooms_list (mgr
, account
, chatrooms
, 3);
189 g_object_unref (mgr
);
191 g_object_unref (account_manager
);
192 g_object_unref (account
);
196 START_TEST (test_empathy_chatroom_manager_remove
)
198 EmpathyChatroomManager
*mgr
;
200 EmpathyAccount
*account
;
201 struct chatroom_t chatrooms
[] = {
202 { "name2", "room2", FALSE
, TRUE
}};
203 EmpathyChatroom
*chatroom
;
204 EmpathyAccountManager
*account_mgr
;
206 account_mgr
= tp_account_manager_dup ();
207 account
= get_test_account ();
209 copy_xml_file (CHATROOM_SAMPLE
, CHATROOM_FILE
);
211 file
= get_user_xml_file (CHATROOM_FILE
);
213 /* change the chatrooms XML file to use the account we just created */
214 fail_unless (change_account_name_in_file (account
, file
));
216 mgr
= empathy_chatroom_manager_dup_singleton (file
);
219 chatroom
= empathy_chatroom_manager_find (mgr
, account
, "room1");
220 fail_if (chatroom
== NULL
);
221 empathy_chatroom_manager_remove (mgr
, chatroom
);
223 check_chatrooms_list (mgr
, account
, chatrooms
, 1);
225 /* reload chatrooms file */
226 g_object_unref (mgr
);
227 mgr
= empathy_chatroom_manager_dup_singleton (file
);
229 check_chatrooms_list (mgr
, account
, chatrooms
, 1);
232 chatroom
= empathy_chatroom_manager_find (mgr
, account
, "room2");
233 fail_if (chatroom
== NULL
);
235 empathy_chatroom_manager_remove (mgr
, chatroom
);
237 check_chatrooms_list (mgr
, account
, chatrooms
, 0);
239 /* reload chatrooms file */
240 g_object_unref (mgr
);
241 mgr
= empathy_chatroom_manager_dup_singleton (file
);
243 check_chatrooms_list (mgr
, account
, chatrooms
, 0);
245 g_object_unref (mgr
);
247 g_object_unref (account
);
248 g_object_unref (account_mgr
);
252 START_TEST (test_empathy_chatroom_manager_change_favorite
)
254 EmpathyChatroomManager
*mgr
;
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
;
263 account_manager
= tp_account_manager_dup ();
264 account
= get_test_account ();
266 copy_xml_file (CHATROOM_SAMPLE
, CHATROOM_FILE
);
268 file
= get_user_xml_file (CHATROOM_FILE
);
270 /* change the chatrooms XML file to use the account we just created */
271 fail_unless (change_account_name_in_file (account
, file
));
273 mgr
= empathy_chatroom_manager_dup_singleton (file
);
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
);
280 check_chatrooms_list (mgr
, account
, chatrooms
, 2);
282 /* reload chatrooms file */
283 g_object_unref (mgr
);
284 mgr
= empathy_chatroom_manager_dup_singleton (file
);
286 /* room2 is not present in the XML file anymore as it's not a favorite */
287 check_chatrooms_list (mgr
, account
, chatrooms
, 1);
290 chatroom
= empathy_chatroom_new_full (account
, "room2", "name2", FALSE
);
291 empathy_chatroom_manager_add (mgr
, chatroom
);
293 check_chatrooms_list (mgr
, account
, chatrooms
, 2);
295 /* set room2 as favorite */
296 g_object_set (chatroom
, "favorite", TRUE
, NULL
);
298 chatrooms
[1].favorite
= TRUE
;
299 check_chatrooms_list (mgr
, account
, chatrooms
, 2);
301 /* reload chatrooms file */
302 g_object_unref (mgr
);
303 mgr
= empathy_chatroom_manager_dup_singleton (file
);
305 /* room2 is back in the XML file now */
306 check_chatrooms_list (mgr
, account
, chatrooms
, 2);
308 g_object_unref (mgr
);
309 g_object_unref (chatroom
);
311 g_object_unref (account_manager
);
312 g_object_unref (account
);
316 START_TEST (test_empathy_chatroom_manager_change_chatroom
)
318 EmpathyChatroomManager
*mgr
;
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
;
327 account_manager
= tp_account_manager_dup ();
328 account
= get_test_account ();
330 copy_xml_file (CHATROOM_SAMPLE
, "foo.xml");
332 file
= get_user_xml_file ("foo.xml");
334 /* change the chatrooms XML file to use the account we just created */
335 fail_unless (change_account_name_in_file (account
, file
));
337 mgr
= empathy_chatroom_manager_dup_singleton (file
);
339 check_chatrooms_list (mgr
, account
, chatrooms
, 2);
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");
346 /* reload chatrooms file */
347 g_object_unref (mgr
);
348 mgr
= empathy_chatroom_manager_dup_singleton (file
);
350 chatrooms
[1].name
= "new_name";
351 check_chatrooms_list (mgr
, account
, chatrooms
, 2);
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
);
358 /* reload chatrooms file */
359 g_object_unref (mgr
);
360 mgr
= empathy_chatroom_manager_dup_singleton (file
);
362 chatrooms
[1].auto_connect
= TRUE
;
363 check_chatrooms_list (mgr
, account
, chatrooms
, 2);
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");
370 /* reload chatrooms file */
371 g_object_unref (mgr
);
372 mgr
= empathy_chatroom_manager_dup_singleton (file
);
374 chatrooms
[1].room
= "new_room";
375 check_chatrooms_list (mgr
, account
, chatrooms
, 2);
377 g_object_unref (mgr
);
379 g_object_unref (account
);
380 g_object_unref (account_manager
);
391 test_init (argc
, argv
);
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
);
405 result
= g_test_run ();