summaryrefslogtreecommitdiff
path: root/tests/wocky-contact-factory-test.c
blob: 99dd7b1b6675de4ec9697428627689f97034ee92 (plain)
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
#include <stdio.h>
#include <unistd.h>
#include <string.h>

#include <glib.h>

#include <wocky/wocky.h>

#include "wocky-test-helper.h"

static void
test_instantiation (void)
{
  WockyContactFactory *factory;

  factory = wocky_contact_factory_new ();

  g_object_unref (factory);
}

gboolean add_signal_received = FALSE;

static void
bare_contact_added (WockyContactFactory *factory,
    WockyBareContact *contact,
    gpointer data)
{
  add_signal_received = TRUE;
}

static void
test_ensure_bare_contact (void)
{
  WockyContactFactory *factory;
  WockyBareContact *juliet, *a, *b;

  factory = wocky_contact_factory_new ();
  juliet = wocky_bare_contact_new ("juliet@example.org");

  add_signal_received = FALSE;
  g_signal_connect (factory, "bare-contact-added",
      G_CALLBACK (bare_contact_added), NULL);

  a = wocky_contact_factory_ensure_bare_contact (factory, "juliet@example.org");
  g_assert (wocky_bare_contact_equal (a, juliet));
  g_assert (add_signal_received);

  b = wocky_contact_factory_ensure_bare_contact (factory, "juliet@example.org");
  g_assert (a == b);

  g_object_unref (factory);
  g_object_unref (juliet);
  g_object_unref (a);
  g_object_unref (b);
}

static void
test_lookup_bare_contact (void)
{
  WockyContactFactory *factory;
  WockyBareContact *contact;

  factory = wocky_contact_factory_new ();

  g_assert (wocky_contact_factory_lookup_bare_contact (factory,
        "juliet@example.org") == NULL);

  contact = wocky_contact_factory_ensure_bare_contact (factory,
      "juliet@example.org");

  g_assert (wocky_contact_factory_lookup_bare_contact (factory,
        "juliet@example.org") != NULL);

  g_object_unref (contact);

  /* contact has been disposed and so is not in the factory anymore */
  g_assert (wocky_contact_factory_lookup_bare_contact (factory,
        "juliet@example.org") == NULL);

  g_object_unref (factory);
}

static void
resource_contact_added (WockyContactFactory *factory,
    WockyBareContact *contact,
    gpointer data)
{
  add_signal_received = TRUE;
}

static void
test_ensure_resource_contact (void)
{
  WockyContactFactory *factory;
  WockyBareContact *juliet, *bare;
  WockyResourceContact *juliet_balcony, *juliet_pub, *a, *b;
  GSList *resources;

  factory = wocky_contact_factory_new ();
  juliet = wocky_bare_contact_new ("juliet@example.org");
  juliet_balcony = wocky_resource_contact_new (juliet, "Balcony");
  juliet_pub = wocky_resource_contact_new (juliet, "Pub");

  add_signal_received = FALSE;
  g_signal_connect (factory, "resource-contact-added",
      G_CALLBACK (resource_contact_added), NULL);

  /* Bare contact isn't in the factory yet */
  a = wocky_contact_factory_ensure_resource_contact (factory,
      "juliet@example.org/Balcony");
  g_assert (wocky_resource_contact_equal (a, juliet_balcony));
  g_assert (add_signal_received);

  bare = wocky_contact_factory_lookup_bare_contact (factory,
        "juliet@example.org");
  g_assert (bare != NULL);

  /* Resource has been added to the bare contact */
  resources = wocky_bare_contact_get_resources (bare);
  g_assert_cmpuint (g_slist_length (resources), ==, 1);
  g_assert (g_slist_find (resources, a) != NULL);
  g_slist_free (resources);

  /* Bare contact is already in the factory */
  b = wocky_contact_factory_ensure_resource_contact (factory,
      "juliet@example.org/Pub");
  g_assert (wocky_resource_contact_equal (b, juliet_pub));

  g_object_unref (factory);
  g_object_unref (juliet);
  g_object_unref (juliet_balcony);
  g_object_unref (juliet_pub);
  g_object_unref (a);
  g_object_unref (b);
}

static void
test_lookup_resource_contact (void)
{
  WockyContactFactory *factory;
  WockyResourceContact *contact;

  factory = wocky_contact_factory_new ();

  g_assert (wocky_contact_factory_lookup_resource_contact (factory,
        "juliet@example.org/Balcony") == NULL);
  contact  = wocky_contact_factory_ensure_resource_contact (factory,
      "juliet@example.org/Balcony");
  g_assert (wocky_contact_factory_lookup_resource_contact (factory,
        "juliet@example.org/Balcony") != NULL);

  g_object_unref (factory);
  g_object_unref (contact);
}

int
main (int argc, char **argv)
{
  int result;

  test_init (argc, argv);

  g_test_add_func ("/contact-factory/instantiation", test_instantiation);
  g_test_add_func ("/contact-factory/ensure-bare-contact",
      test_ensure_bare_contact);
  g_test_add_func ("/contact-factory/lookup-bare-contact",
      test_lookup_bare_contact);
  g_test_add_func ("/contact-factory/ensure-resource-contact",
      test_ensure_resource_contact);
  g_test_add_func ("/contact-factory/lookup-resource-contact",
      test_lookup_resource_contact);

  result = g_test_run ();
  test_deinit ();
  return result;
}