summaryrefslogtreecommitdiff
path: root/tests/dbus/connection-interests.c
blob: 3b96674e1112030f44bcca231b46876ada700748 (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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
/* Feature test for https://bugs.freedesktop.org/show_bug.cgi?id=27835
 *
 * Copyright © 2007-2010 Collabora Ltd. <http://www.collabora.co.uk/>
 * Copyright © 2007-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/cli-connection.h>
#include <telepathy-glib/connection.h>
#include <telepathy-glib/dbus.h>
#include <telepathy-glib/debug.h>
#include <telepathy-glib/interfaces.h>
#include <telepathy-glib/proxy-subclass.h>

#include "telepathy-glib/reentrants.h"

#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>

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

/* an almost-no-op subclass... */
typedef TpTestsContactsConnection InterestedConnection;
typedef TpTestsContactsConnectionClass InterestedConnectionClass;

static GType interested_connection_get_type (void);

G_DEFINE_TYPE_WITH_CODE (InterestedConnection,
    interested_connection,
    TP_TESTS_TYPE_CONTACTS_CONNECTION,
    G_STMT_START { } G_STMT_END)

/* Lord Pearson of Rannoch: My Lords, I beg leave to ask the Question
 * standing in my name on the Order Paper. In doing so, I declare an
 * interest as patron of the British Register of Chinese Herbal Medicine.
 * -- Hansard, 2010-02-01 */
#define SUPPORTED_TOKEN "com.example.rannoch/ChineseHerbalMedicine"

/* Lord Hoyle: My Lords, in thanking my noble friend for his Answer, I declare
 * an interest as the chairman and now president of Warrington Wolves Rugby
 * League Club. -- Hansard, 2010-01-11
 */
#define UNSUPPORTED_TOKEN "org.example.Warrington/Wolves"

static void
interested_connection_init (InterestedConnection *self G_GNUC_UNUSED)
{
}

static void
interested_connection_constructed (GObject *object)
{
  TpBaseConnection *base = (TpBaseConnection *) object;
  void (*chain_up) (GObject *) =
    ((GObjectClass *) interested_connection_parent_class)->constructed;

  if (chain_up != NULL)
    chain_up (object);

  tp_base_connection_add_possible_client_interest (base,
      TP_IFACE_QUARK_CONNECTION_INTERFACE_LOCATION1);
  tp_base_connection_add_possible_client_interest (base,
      g_quark_from_static_string (SUPPORTED_TOKEN));
}

static void
interested_connection_class_init (InterestedConnectionClass *cls)
{
  GObjectClass *object_class = (GObjectClass *) cls;

  object_class->constructed = interested_connection_constructed;
}

typedef struct {
    TpDBusDaemon *dbus;
    GDBusConnection *client_gdbus;
    TpDBusDaemon *client_bus;
    TpTestsSimpleConnection *service_conn;
    TpBaseConnection *service_conn_as_base;
    gchar *conn_name;
    gchar *conn_path;
    TpConnection *conn;

    gboolean cwr_ready;
    GError *cwr_error /* initialized in setup */;

    GAsyncResult *prepare_result;

    GPtrArray *log;
} Test;

static void
connection_prepared_cb (GObject *object,
    GAsyncResult *res,
    gpointer user_data)
{
  Test *test = user_data;

  g_message ("%p prepared", object);
  g_assert (test->prepare_result == NULL);
  test->prepare_result = g_object_ref (res);
}

static void
interested_cb (TpBaseConnection *unused G_GNUC_UNUSED,
    const gchar *iface,
    Test *test)
{
  g_ptr_array_add (test->log, g_strdup_printf ("interested in %s", iface));
}

static void
location_interested_cb (TpBaseConnection *unused G_GNUC_UNUSED,
    const gchar *iface,
    Test *test)
{
  g_assert_cmpstr (iface, ==, TP_IFACE_CONNECTION_INTERFACE_LOCATION1);

  g_ptr_array_add (test->log, g_strdup ("Location interested"));
}

static void
uninterested_cb (TpBaseConnection *unused G_GNUC_UNUSED,
    const gchar *iface,
    Test *test)
{
  g_ptr_array_add (test->log, g_strdup_printf ("uninterested in %s", iface));
}

static void
location_uninterested_cb (TpBaseConnection *unused G_GNUC_UNUSED,
    const gchar *iface,
    Test *test)
{
  g_assert_cmpstr (iface, ==, TP_IFACE_CONNECTION_INTERFACE_LOCATION1);

  g_ptr_array_add (test->log, g_strdup ("Location uninterested"));
}

static void
setup (Test *test,
    gconstpointer data)
{
  GError *error = NULL;
  GQuark features[] = { TP_CONNECTION_FEATURE_CONNECTED, 0 };

  tp_debug_set_flags ("all");
  test->dbus = tp_tests_dbus_daemon_dup_or_die ();

  test->client_gdbus = tp_tests_get_private_bus ();
  test->client_bus = tp_dbus_daemon_new (test->client_gdbus);
  g_assert (test->client_bus != NULL);

  test->service_conn = tp_tests_object_new_static_class (
        interested_connection_get_type (),
        "account", "me@example.com",
        "protocol", "simple_protocol",
        NULL);
  test->service_conn_as_base = TP_BASE_CONNECTION (test->service_conn);
  g_assert (test->service_conn != NULL);
  g_assert (test->service_conn_as_base != NULL);

  g_assert (tp_base_connection_register (test->service_conn_as_base, "simple",
        &test->conn_name, &test->conn_path, &error));
  g_assert_no_error (error);

  test->cwr_ready = FALSE;
  test->cwr_error = NULL;

  test->conn = tp_tests_connection_new (test->client_bus, test->conn_name,
      test->conn_path, &error);
  g_assert_no_error (error);
  g_assert (test->conn != NULL);

  tp_cli_connection_call_connect (test->conn, -1, NULL, NULL, NULL, NULL);

  g_assert (!tp_proxy_is_prepared (test->conn, TP_CONNECTION_FEATURE_CORE));
  g_assert (!tp_proxy_is_prepared (test->conn,
        TP_CONNECTION_FEATURE_CONNECTED));

  tp_proxy_prepare_async (test->conn, features, connection_prepared_cb, test);
  g_assert (test->prepare_result == NULL);

  while (test->prepare_result == NULL)
    g_main_context_iteration (NULL, TRUE);

  g_assert (tp_proxy_prepare_finish (test->conn, test->prepare_result,
        &error));
  g_assert_no_error (error);
  g_object_unref (test->prepare_result);
  test->prepare_result = NULL;

  test->log = g_ptr_array_new_with_free_func (g_free);

  g_signal_connect (test->service_conn,
      "clients-interested", G_CALLBACK (interested_cb), test);
  g_signal_connect (test->service_conn,
      "clients-interested::" TP_IFACE_CONNECTION_INTERFACE_LOCATION1,
      G_CALLBACK (location_interested_cb), test);

  g_signal_connect (test->service_conn,
      "clients-uninterested", G_CALLBACK (uninterested_cb), test);
  g_signal_connect (test->service_conn,
      "clients-uninterested::" TP_IFACE_CONNECTION_INTERFACE_LOCATION1,
      G_CALLBACK (location_uninterested_cb), test);
}

static void
teardown (Test *test,
    gconstpointer data)
{
  TpConnection *conn;
  GError *error = NULL;

  if (test->conn != NULL)
    {
      g_object_unref (test->conn);
      test->conn = NULL;
    }

  /* disconnect the connection so we don't leak it */
  conn = tp_tests_connection_new (test->dbus, test->conn_name, test->conn_path,
      &error);
  g_assert (conn != NULL);
  g_assert_no_error (error);

  tp_tests_connection_assert_disconnect_succeeds (conn);
  tp_tests_proxy_run_until_prepared_or_failed (conn, NULL, &error);
  g_assert_error (error, TP_ERROR, TP_ERROR_CANCELLED);
  g_clear_error (&error);

  test->service_conn_as_base = NULL;
  g_object_unref (test->service_conn);
  g_free (test->conn_name);
  g_free (test->conn_path);

  g_object_unref (test->dbus);
  test->dbus = NULL;
  g_object_unref (test->client_bus);
  test->client_bus = NULL;

  if (test->client_gdbus != NULL)
    {
      g_dbus_connection_close_sync (test->client_gdbus, NULL, NULL);
      g_object_unref (test->client_gdbus);
    }

  g_ptr_array_unref (test->log);
}

static void
test_interested_client (Test *test,
    gconstpointer nil G_GNUC_UNUSED)
{
  guint i;

  tp_connection_add_client_interest_by_id (test->conn,
      TP_IFACE_QUARK_CONNECTION_INTERFACE_LOCATION1);
  tp_connection_add_client_interest_by_id (test->conn,
      TP_IFACE_QUARK_CONNECTION_INTERFACE_AVATARS1);

  /* run until (after) the AddClientInterest calls have gone out */
  tp_tests_proxy_run_until_dbus_queue_processed (test->client_bus);

  /* we auto-release the Location client interest by disposing the client
   * connection */
  g_object_run_dispose ((GObject *) test->conn);
  g_object_unref (test->conn);
  test->conn = NULL;

  /* run until (after) the RemoveClientInterest call has gone out */
  tp_tests_proxy_run_until_dbus_queue_processed (test->client_bus);

  /* then, run until (after) the CM should have processed both ACI and RCI */
  tp_tests_proxy_run_until_dbus_queue_processed (test->dbus);

  i = 0;
  g_assert_cmpuint (test->log->len, >, i);
  g_assert_cmpstr (g_ptr_array_index (test->log, i), ==,
      "interested in " TP_IFACE_CONNECTION_INTERFACE_LOCATION1);

  i++;
  g_assert_cmpuint (test->log->len, >, i);
  g_assert_cmpstr (g_ptr_array_index (test->log, i), ==,
      "Location interested");

  i++;
  g_assert_cmpuint (test->log->len, >, i);
  g_assert_cmpstr (g_ptr_array_index (test->log, i), ==,
      "uninterested in " TP_IFACE_CONNECTION_INTERFACE_LOCATION1);

  i++;
  g_assert_cmpuint (test->log->len, >, i);
  g_assert_cmpstr (g_ptr_array_index (test->log, i), ==,
      "Location uninterested");

  i++;
  g_assert_cmpuint (test->log->len, ==, i);
}

static void
test_interest (Test *test,
    gconstpointer nil G_GNUC_UNUSED)
{
  static const gchar * telepathy[] = {
      TP_IFACE_CONNECTION_INTERFACE_LOCATION1,
      TP_IFACE_CONNECTION_INTERFACE_AVATARS1,
      NULL
  };
  static const gchar * hansard[] = {
      SUPPORTED_TOKEN,
      UNSUPPORTED_TOKEN,
      NULL
  };
  GError *error = NULL;
  guint i;

  tp_cli_connection_run_add_client_interest (test->conn, -1, telepathy, &error,
      NULL);
  g_assert_no_error (error);

  tp_cli_connection_run_add_client_interest (test->conn, -1, hansard, &error,
      NULL);
  g_assert_no_error (error);

  tp_cli_connection_run_add_client_interest (test->conn, -1, telepathy, &error,
      NULL);
  g_assert_no_error (error);

  tp_cli_connection_run_remove_client_interest (test->conn, -1, telepathy,
      &error, NULL);
  g_assert_no_error (error);

  tp_cli_connection_run_remove_client_interest (test->conn, -1, hansard,
      &error, NULL);
  g_assert_no_error (error);

  /* we auto-release the Location client interest by dropping the client
   * connection */
  g_dbus_connection_flush_sync (test->client_gdbus, NULL, NULL);
  g_dbus_connection_close_sync (test->client_gdbus, NULL, NULL);
  g_object_unref (test->client_gdbus);
  test->client_gdbus = NULL;

  while (test->log->len < 6)
    g_main_context_iteration (NULL, TRUE);

  i = 0;
  g_assert_cmpuint (test->log->len, >, i);
  g_assert_cmpstr (g_ptr_array_index (test->log, i), ==,
      "interested in " TP_IFACE_CONNECTION_INTERFACE_LOCATION1);

  i++;
  g_assert_cmpuint (test->log->len, >, i);
  g_assert_cmpstr (g_ptr_array_index (test->log, i), ==,
      "Location interested");

  i++;
  g_assert_cmpuint (test->log->len, >, i);
  g_assert_cmpstr (g_ptr_array_index (test->log, i), ==,
      "interested in " SUPPORTED_TOKEN);

  i++;
  g_assert_cmpuint (test->log->len, >, i);
  g_assert_cmpstr (g_ptr_array_index (test->log, i), ==,
      "uninterested in " SUPPORTED_TOKEN);

  i++;
  g_assert_cmpuint (test->log->len, >, i);
  g_assert_cmpstr (g_ptr_array_index (test->log, i), ==,
      "uninterested in " TP_IFACE_CONNECTION_INTERFACE_LOCATION1);

  i++;
  g_assert_cmpuint (test->log->len, >, i);
  g_assert_cmpstr (g_ptr_array_index (test->log, i), ==,
      "Location uninterested");

  i++;
  g_assert_cmpuint (test->log->len, ==, i);
}

int
main (int argc,
      char **argv)
{
  tp_tests_init (&argc, &argv);

  g_test_add ("/conn/interest", Test, NULL, setup, test_interest, teardown);
  g_test_add ("/conn/interested-client", Test, NULL, setup,
      test_interested_client, teardown);

  return tp_tests_run_with_bus ();
}