aboutsummaryrefslogtreecommitdiffstats
path: root/tests/check-empathy-chatroom-manager.c
blob: f8fa8127c56433da2b70a5999f367ae4fd701e63 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <glib/gstdio.h>

#include <gconf/gconf.h>
#include <gconf/gconf-client.h>
#include <telepathy-glib/util.h>
#include <check.h>

#include "check-helpers.h"
#include "check-libempathy.h"
#include "check-empathy-helpers.h"

#include <libempathy/empathy-chatroom-manager.h>

#define CHATROOM_SAMPLE "chatrooms-sample.xml"
#define CHATROOM_FILE "chatrooms.xml"

static void
check_chatroom (EmpathyChatroom *chatroom,
                const gchar *name,
                const gchar *room,
                gboolean auto_connect,
                gboolean favorite)
{
  gboolean _favorite;

  fail_if (tp_strdiff (empathy_chatroom_get_name (chatroom), name));
  fail_if (tp_strdiff (empathy_chatroom_get_room (chatroom), room));
  fail_if (empathy_chatroom_get_auto_connect (chatroom) != auto_connect);
  g_object_get (chatroom, "favorite", &_favorite, NULL);
  fail_if (favorite != _favorite);
}

struct chatroom_t
{
  gchar *name;
  gchar *room;
  gboolean auto_connect;
  gboolean favorite;
};

static void
check_chatrooms_list (EmpathyChatroomManager *mgr,
                      McAccount *account,
                      struct chatroom_t *_chatrooms,
                      guint nb_chatrooms)
{
  GList *chatrooms, *l;
  guint i;
  GHashTable *found;

  fail_if (empathy_chatroom_manager_get_count (mgr, account) != nb_chatrooms);

  found = g_hash_table_new (g_str_hash, g_str_equal);
  for (i = 0; i < nb_chatrooms; i++)
    {
      g_hash_table_insert (found, _chatrooms[i].room, &_chatrooms[i]);
    }

  chatrooms = empathy_chatroom_manager_get_chatrooms (mgr, account);
  fail_if (g_list_length (chatrooms) != nb_chatrooms);

  for (l = chatrooms; l != NULL; l = g_list_next (l))
    {
      EmpathyChatroom *chatroom = l->data;
      struct chatroom_t *tmp;

      tmp = g_hash_table_lookup (found, empathy_chatroom_get_room (chatroom));
      fail_if (tmp == NULL);

      check_chatroom (chatroom, tmp->name, tmp->room, tmp->auto_connect,
          tmp->favorite);

      g_hash_table_remove (found, empathy_chatroom_get_room (chatroom));
    }

  fail_if (g_hash_table_size (found) != 0);

  g_list_free (chatrooms);
  g_hash_table_destroy (found);
}

START_TEST (test_empathy_chatroom_manager_new)
{
  EmpathyChatroomManager *mgr;
  gchar *cmd;
  gchar *file;
  McAccount *account;
  struct chatroom_t chatrooms[] = {
        { "name1", "room1", TRUE, TRUE },
        { "name2", "room2", FALSE, TRUE }};

  account = get_test_account ();

  copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);

  file = get_user_xml_file (CHATROOM_FILE);
  /* change the chatrooms XML file to use the account we just created */
  cmd = g_strdup_printf ("sed -i 's/CHANGE_ME/%s/' %s",
      mc_account_get_unique_name (account), file);

  if (system (cmd) == -1)
    {
      g_print ("system call failed\n");
      return;
    }

  g_free (cmd);

  mgr = empathy_chatroom_manager_new (file);
  check_chatrooms_list (mgr, account, chatrooms, 2);

  g_free (file);
  g_object_unref (mgr);
  g_object_unref (account);
}
END_TEST

START_TEST (test_empathy_chatroom_manager_add)
{
  EmpathyChatroomManager *mgr;
  gchar *cmd;
  gchar *file;
  McAccount *account;
  struct chatroom_t chatrooms[] = {
        { "name1", "room1", TRUE, TRUE },
        { "name2", "room2", FALSE, TRUE },
        { "name3", "room3", FALSE, TRUE },
        { "name4", "room4", FALSE, FALSE }};
  EmpathyChatroom *chatroom;

  account = get_test_account ();

  copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);

  file = get_user_xml_file (CHATROOM_FILE);
  /* change the chatrooms XML file to use the account we just created */
  cmd = g_strdup_printf ("sed -i 's/CHANGE_ME/%s/' %s",
      mc_account_get_unique_name (account), file);
  system (cmd);
  g_free (cmd);

  mgr = empathy_chatroom_manager_new (file);

  /* add a favorite chatroom */
  chatroom = empathy_chatroom_new_full (account, "room3", "name3", FALSE);
  g_object_set (chatroom, "favorite", TRUE, NULL);
  empathy_chatroom_manager_add (mgr, chatroom);
  g_object_unref (chatroom);

  check_chatrooms_list (mgr, account, chatrooms, 3);

  /* reload chatrooms file */
  g_object_unref (mgr);
  mgr = empathy_chatroom_manager_new (file);

  /* chatroom has been added to the XML file as it's a favorite */
  check_chatrooms_list (mgr, account, chatrooms, 3);

  /* add a non favorite chatroom */
  chatroom = empathy_chatroom_new_full (account, "room4", "name4", FALSE);
  g_object_set (chatroom, "favorite", FALSE, NULL);
  empathy_chatroom_manager_add (mgr, chatroom);
  g_object_unref (chatroom);

  check_chatrooms_list (mgr, account, chatrooms, 4);

  /* reload chatrooms file */
  g_object_unref (mgr);
  mgr = empathy_chatroom_manager_new (file);

  /* chatrooms has not been added to the XML file */
  check_chatrooms_list (mgr, account, chatrooms, 3);

  g_object_unref (mgr);
  g_free (file);
  g_object_unref (account);
}
END_TEST

START_TEST (test_empathy_chatroom_manager_remove)
{
  EmpathyChatroomManager *mgr;
  gchar *cmd;
  gchar *file;
  McAccount *account;
  struct chatroom_t chatrooms[] = {
        { "name2", "room2", FALSE, TRUE }};
  EmpathyChatroom *chatroom;

  account = get_test_account ();

  copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);

  file = get_user_xml_file (CHATROOM_FILE);
  /* change the chatrooms XML file to use the account we just created */
  cmd = g_strdup_printf ("sed -i 's/CHANGE_ME/%s/' %s",
      mc_account_get_unique_name (account), file);
  system (cmd);
  g_free (cmd);

  mgr = empathy_chatroom_manager_new (file);

  /* remove room1 */
  chatroom = empathy_chatroom_manager_find (mgr, account, "room1");
  fail_if (chatroom == NULL);
  empathy_chatroom_manager_remove (mgr, chatroom);

  check_chatrooms_list (mgr, account, chatrooms, 1);

  /* reload chatrooms file */
  g_object_unref (mgr);
  mgr = empathy_chatroom_manager_new (file);

  check_chatrooms_list (mgr, account, chatrooms, 1);

  /* remove room1 */
  chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
  fail_if (chatroom == NULL);

  empathy_chatroom_manager_remove (mgr, chatroom);

  check_chatrooms_list (mgr, account, chatrooms, 0);

  /* reload chatrooms file */
  g_object_unref (mgr);
  mgr = empathy_chatroom_manager_new (file);

  check_chatrooms_list (mgr, account, chatrooms, 0);

  g_object_unref (mgr);
  g_free (file);
  g_object_unref (account);
}
END_TEST

START_TEST (test_empathy_chatroom_manager_change_favorite)
{
  EmpathyChatroomManager *mgr;
  gchar *cmd;
  gchar *file;
  McAccount *account;
  struct chatroom_t chatrooms[] = {
        { "name1", "room1", TRUE, TRUE },
        { "name2", "room2", FALSE, FALSE }};
  EmpathyChatroom *chatroom;

  account = get_test_account ();

  copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);

  file = get_user_xml_file (CHATROOM_FILE);
  /* change the chatrooms XML file to use the account we just created */
  cmd = g_strdup_printf ("sed -i 's/CHANGE_ME/%s/' %s",
      mc_account_get_unique_name (account), file);
  system (cmd);
  g_free (cmd);

  mgr = empathy_chatroom_manager_new (file);

  /* room2 is not favorite anymore */
  chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
  fail_if (chatroom == NULL);
  g_object_set (chatroom, "favorite", FALSE, NULL);

  check_chatrooms_list (mgr, account, chatrooms, 2);

  /* reload chatrooms file */
  g_object_unref (mgr);
  mgr = empathy_chatroom_manager_new (file);

  /* room2 is not present in the XML file anymore as it's not a favorite */
  check_chatrooms_list (mgr, account, chatrooms, 1);

  /* re-add room2 */
  chatroom = empathy_chatroom_new_full (account, "room2", "name2", FALSE);
  empathy_chatroom_manager_add (mgr, chatroom);

  check_chatrooms_list (mgr, account, chatrooms, 2);

  /* set room2 as favorite */
  g_object_set (chatroom, "favorite", TRUE, NULL);

  chatrooms[1].favorite = TRUE;
  check_chatrooms_list (mgr, account, chatrooms, 2);

  /* reload chatrooms file */
  g_object_unref (mgr);
  mgr = empathy_chatroom_manager_new (file);

  /* room2 is back in the XML file now */
  check_chatrooms_list (mgr, account, chatrooms, 2);

  g_object_unref (mgr);
  g_object_unref (chatroom);
  g_free (file);
  g_object_unref (account);
}
END_TEST

START_TEST (test_empathy_chatroom_manager_change_chatroom)
{
  EmpathyChatroomManager *mgr;
  gchar *cmd;
  gchar *file;
  McAccount *account;
  struct chatroom_t chatrooms[] = {
        { "name1", "room1", TRUE, TRUE },
        { "name2", "room2", FALSE, TRUE }};
  EmpathyChatroom *chatroom;

  account = get_test_account ();

  /*
  copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);

  file = get_user_xml_file (CHATROOM_FILE);
  */
  copy_xml_file (CHATROOM_SAMPLE, "foo.xml");

  file = get_user_xml_file ("foo.xml");
  /* change the chatrooms XML file to use the account we just created */
  cmd = g_strdup_printf ("sed -i 's/CHANGE_ME/%s/' %s",
      mc_account_get_unique_name (account), file);
  system (cmd);
  g_free (cmd);

  mgr = empathy_chatroom_manager_new (file);

  check_chatrooms_list (mgr, account, chatrooms, 2);

  /* change room2 name */
  chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
  fail_if (chatroom == NULL);
  empathy_chatroom_set_name (chatroom, "new_name");

  /* reload chatrooms file */
  g_object_unref (mgr);
  mgr = empathy_chatroom_manager_new (file);

  chatrooms[1].name = "new_name";
  check_chatrooms_list (mgr, account, chatrooms, 2);

  /* change room2 auto-connect status */
  chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
  fail_if (chatroom == NULL);
  empathy_chatroom_set_auto_connect (chatroom, TRUE);

  /* reload chatrooms file */
  g_object_unref (mgr);
  mgr = empathy_chatroom_manager_new (file);

  chatrooms[1].auto_connect = TRUE;
  check_chatrooms_list (mgr, account, chatrooms, 2);

  /* change room2 room */
  chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
  fail_if (chatroom == NULL);
  empathy_chatroom_set_room (chatroom, "new_room");

  /* reload chatrooms file */
  g_object_unref (mgr);
  mgr = empathy_chatroom_manager_new (file);

  chatrooms[1].room = "new_room";
  check_chatrooms_list (mgr, account, chatrooms, 2);

  g_object_unref (mgr);
  g_free (file);
  g_object_unref (account);
}
END_TEST

TCase *
make_empathy_chatroom_manager_tcase (void)
{
    TCase *tc = tcase_create ("empathy-chatroom-manager");
    tcase_add_test (tc, test_empathy_chatroom_manager_new);
    tcase_add_test (tc, test_empathy_chatroom_manager_add);
    tcase_add_test (tc, test_empathy_chatroom_manager_remove);
    tcase_add_test (tc, test_empathy_chatroom_manager_change_favorite);
    tcase_add_test (tc, test_empathy_chatroom_manager_change_chatroom);
    return tc;
}