]> git.0d.be Git - empathy.git/blob - tests/check-empathy-chatroom-manager.c
f1165fe671ad5fd35b480db8481a090ef7d29c4c
[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
178 TCase *
179 make_empathy_chatroom_manager_tcase (void)
180 {
181     TCase *tc = tcase_create ("empathy-chatroom-manager");
182     tcase_add_test (tc, test_empathy_chatroom_manager_new);
183     tcase_add_test (tc, test_empathy_chatroom_manager_add);
184     return tc;
185 }