summaryrefslogtreecommitdiff
path: root/tests/dbus/contacts-mixin.c
blob: 5e286c3dffceffcec756b54b6dbced7c58258276 (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
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
/* Feature test for TpContactsMixin
 *
 * Copyright (C) 2008 Collabora Ltd. <http://www.collabora.co.uk/>
 * Copyright (C) 2008 Nokia Corporation
 *
 * Copying and distribution of this file, with or without modification,
 * are permitted in any medium without royalty provided the copyright
 * notice and this notice are preserved.
 */

#include "config.h"

#include <telepathy-glib/connection.h>
#include <telepathy-glib/dbus.h>
#include <telepathy-glib/debug.h>
#include <telepathy-glib/interfaces.h>

#include "tests/lib/contacts-conn.h"
#include "tests/lib/debug.h"
#include "tests/lib/myassert.h"
#include "tests/lib/util.h"

static void
test_no_features (TpTestsContactsConnection *service_conn,
                  TpConnection *client_conn,
                  GArray *handles)
{
  GError *error = NULL;
  GHashTable *contacts;
  GHashTable *attrs;

  g_message (G_STRFUNC);

  MYASSERT (tp_cli_connection_interface_contacts_run_get_contact_attributes (
        client_conn, -1, handles, NULL, FALSE, &contacts, &error, NULL), "");
  g_assert_no_error (error);
  g_assert_cmpuint (g_hash_table_size (contacts), ==, 3);

  attrs = g_hash_table_lookup (contacts,
      GUINT_TO_POINTER (g_array_index (handles, guint, 0)));
  MYASSERT (attrs != NULL, "");
  g_assert_cmpstr (
      tp_asv_get_string (attrs, TP_IFACE_CONNECTION "/contact-id"), ==,
      "alice");

  attrs = g_hash_table_lookup (contacts,
      GUINT_TO_POINTER (g_array_index (handles, guint, 1)));
  MYASSERT (attrs != NULL, "");
  g_assert_cmpstr (
      tp_asv_get_string (attrs, TP_IFACE_CONNECTION "/contact-id"), ==,
      "bob");

  attrs = g_hash_table_lookup (contacts,
      GUINT_TO_POINTER (g_array_index (handles, guint, 2)));
  MYASSERT (attrs != NULL, "");
  g_assert_cmpstr (
      tp_asv_get_string (attrs, TP_IFACE_CONNECTION "/contact-id"), ==,
      "chris");

  g_hash_table_unref (contacts);
}

static void
test_features (TpTestsContactsConnection *service_conn,
               TpConnection *client_conn,
               GArray *handles)
{
  const gchar *interfaces[] = { TP_IFACE_CONNECTION,
      TP_IFACE_CONNECTION_INTERFACE_ALIASING,
      TP_IFACE_CONNECTION_INTERFACE_AVATARS,
      TP_IFACE_CONNECTION_INTERFACE_SIMPLE_PRESENCE,
      NULL };
  GError *error = NULL;
  GHashTable *contacts;
  GHashTable *attrs;

  g_message (G_STRFUNC);

  MYASSERT (tp_cli_connection_interface_contacts_run_get_contact_attributes (
        client_conn, -1, handles, interfaces, FALSE, &contacts, &error, NULL),
      "");
  g_assert_no_error (error);
  g_assert_cmpuint (g_hash_table_size (contacts), ==, 3);

  attrs = g_hash_table_lookup (contacts,
      GUINT_TO_POINTER (g_array_index (handles, guint, 0)));
  MYASSERT (attrs != NULL, "");
  g_assert_cmpstr (
      tp_asv_get_string (attrs, TP_IFACE_CONNECTION "/contact-id"), ==,
      "alice");
  g_assert_cmpstr (
      tp_asv_get_string (attrs,
          TP_IFACE_CONNECTION_INTERFACE_ALIASING "/alias"), ==,
      "Alice in Wonderland");
  g_assert_cmpstr (
      tp_asv_get_string (attrs,
          TP_IFACE_CONNECTION_INTERFACE_AVATARS "/token"), ==,
      "aaaaa");

  attrs = g_hash_table_lookup (contacts,
      GUINT_TO_POINTER (g_array_index (handles, guint, 1)));
  MYASSERT (attrs != NULL, "");
  g_assert_cmpstr (
      tp_asv_get_string (attrs, TP_IFACE_CONNECTION "/contact-id"), ==,
      "bob");
  g_assert_cmpstr (
      tp_asv_get_string (attrs,
          TP_IFACE_CONNECTION_INTERFACE_ALIASING "/alias"), ==,
      "Bob the Builder");
  g_assert_cmpstr (
      tp_asv_get_string (attrs,
          TP_IFACE_CONNECTION_INTERFACE_AVATARS "/token"), ==,
      "bbbbb");

  attrs = g_hash_table_lookup (contacts,
      GUINT_TO_POINTER (g_array_index (handles, guint, 2)));
  MYASSERT (attrs != NULL, "");
  g_assert_cmpstr (
      tp_asv_get_string (attrs, TP_IFACE_CONNECTION "/contact-id"), ==,
      "chris");
  g_assert_cmpstr (
      tp_asv_get_string (attrs,
          TP_IFACE_CONNECTION_INTERFACE_ALIASING "/alias"), ==,
      "Christopher Robin");
  g_assert_cmpstr (
      tp_asv_get_string (attrs,
          TP_IFACE_CONNECTION_INTERFACE_AVATARS "/token"), ==,
      "ccccc");

  g_hash_table_unref (contacts);
}

int
main (int argc,
      char **argv)
{
  TpDBusDaemon *dbus;
  TpTestsContactsConnection *service_conn;
  TpBaseConnection *service_conn_as_base;
  gchar *name;
  gchar *conn_path;
  GError *error = NULL;
  TpConnection *client_conn;
  GArray *handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), 3);
  static const gchar * const ids[] = { "alice", "bob", "chris" };
  static const gchar * const aliases[] = { "Alice in Wonderland",
      "Bob the Builder", "Christopher Robin" };
  static const gchar * const tokens[] = { "aaaaa", "bbbbb", "ccccc" };
  static TpTestsContactsConnectionPresenceStatusIndex statuses[] = {
      TP_TESTS_CONTACTS_CONNECTION_STATUS_AVAILABLE,
      TP_TESTS_CONTACTS_CONNECTION_STATUS_BUSY,
      TP_TESTS_CONTACTS_CONNECTION_STATUS_AWAY };
  static const gchar * const messages[] = { "", "Fixing it",
      "GON OUT BACKSON" };
  TpHandleRepoIface *service_repo;
  guint i;

  /* Setup */

  tp_tests_abort_after (10);
  g_type_init ();
  tp_debug_set_flags ("all");
  dbus = tp_tests_dbus_daemon_dup_or_die ();

  service_conn = TP_TESTS_CONTACTS_CONNECTION (
    tp_tests_object_new_static_class (
        TP_TESTS_TYPE_CONTACTS_CONNECTION,
        "account", "me@example.com",
        "protocol", "simple",
        NULL));
  service_conn_as_base = TP_BASE_CONNECTION (service_conn);
  MYASSERT (service_conn != NULL, "");
  MYASSERT (service_conn_as_base != NULL, "");
  service_repo = tp_base_connection_get_handles (
      (TpBaseConnection *) service_conn, TP_HANDLE_TYPE_CONTACT);

  MYASSERT (tp_base_connection_register (service_conn_as_base, "simple",
        &name, &conn_path, &error), "");
  g_assert_no_error (error);

  client_conn = tp_connection_new (dbus, name, conn_path, &error);
  MYASSERT (client_conn != NULL, "");
  g_assert_no_error (error);
  MYASSERT (tp_connection_run_until_ready (client_conn, TRUE, &error, NULL),
      "");
  g_assert_no_error (error);

  /* Set up some contacts */

  for (i = 0; i < 3; i++)
    {
      TpHandle handle = tp_handle_ensure (service_repo, ids[i], NULL, NULL);

      g_array_append_val (handles, handle);
    }

  tp_tests_contacts_connection_change_aliases (service_conn, 3,
      (const TpHandle *) handles->data, aliases);
  tp_tests_contacts_connection_change_presences (service_conn, 3,
      (const TpHandle *) handles->data, statuses, messages);
  tp_tests_contacts_connection_change_avatar_tokens (service_conn, 3,
      (const TpHandle *) handles->data, tokens);

  /* Tests */

  test_no_features (service_conn, client_conn, handles);
  test_features (service_conn, client_conn, handles);

  /* Teardown */

  tp_tests_connection_assert_disconnect_succeeds (client_conn);

  service_conn_as_base = NULL;
  g_object_unref (service_conn);
  g_free (name);
  g_free (conn_path);

  g_object_unref (dbus);
  g_array_unref (handles);

  return 0;
}