~ubuntu-branches/ubuntu/maverick/telepathy-glib/maverick

« back to all changes in this revision

Viewing changes to tests/dbus/account.c

  • Committer: Bazaar Package Importer
  • Author(s): Simon McVittie
  • Date: 2010-05-10 17:59:54 UTC
  • mfrom: (1.6.1 upstream) (27.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20100510175954-bxvqq3xx0sy4itmp
Tags: 0.11.5-1
New upstream version with new API/ABI

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
#include <telepathy-glib/account.h>
12
12
#include <telepathy-glib/debug.h>
13
13
#include <telepathy-glib/defs.h>
 
14
#include <telepathy-glib/svc-account.h>
 
15
 
 
16
#include "tests/lib/util.h"
 
17
#include "tests/lib/simple-account.h"
 
18
#include "tests/lib/util.h"
 
19
 
 
20
#define ACCOUNT_PATH TP_ACCOUNT_OBJECT_PATH_BASE "what/ev/er"
 
21
#define CONN1_PATH TP_CONN_OBJECT_PATH_BASE "what/ev/er"
 
22
#define CONN2_PATH TP_CONN_OBJECT_PATH_BASE "what/ev/s"
 
23
#define CONN1_BUS_NAME TP_CONN_BUS_NAME_BASE "what.ev.er"
 
24
#define CONN2_BUS_NAME TP_CONN_BUS_NAME_BASE "what.ev.s"
14
25
 
15
26
static void
16
27
test_parse_failure (gconstpointer test_data)
72
83
    TpDBusDaemon *dbus;
73
84
 
74
85
    TpAccount *account;
 
86
    gulong notify_id;
 
87
    /* g_strdup (property name) => GUINT_TO_POINTER (counter) */
 
88
    GHashTable *times_notified;
75
89
    GError *error /* initialized where needed */;
 
90
 
 
91
    SimpleAccount *account_service /* initialized in prepare_service */;
76
92
} Test;
77
93
 
78
94
static void
80
96
       gconstpointer data)
81
97
{
82
98
  test->mainloop = g_main_loop_new (NULL, FALSE);
83
 
  test->dbus = tp_dbus_daemon_dup (NULL);
 
99
  test->dbus = test_dbus_daemon_dup_or_die ();
84
100
  g_assert (test->dbus != NULL);
85
101
 
86
102
  test->account = NULL;
 
103
 
 
104
  test->times_notified = g_hash_table_new_full (g_str_hash, g_str_equal,
 
105
      g_free, NULL);
 
106
}
 
107
 
 
108
static void
 
109
setup_service (Test *test,
 
110
    gconstpointer data)
 
111
{
 
112
  setup (test, data);
 
113
 
 
114
  tp_dbus_daemon_request_name (test->dbus,
 
115
      TP_ACCOUNT_MANAGER_BUS_NAME, FALSE, &test->error);
 
116
  g_assert_no_error (test->error);
 
117
 
 
118
  tp_dbus_daemon_request_name (test->dbus,
 
119
      CONN1_BUS_NAME, FALSE, &test->error);
 
120
  g_assert_no_error (test->error);
 
121
 
 
122
  tp_dbus_daemon_request_name (test->dbus,
 
123
      CONN2_BUS_NAME, FALSE, &test->error);
 
124
  g_assert_no_error (test->error);
 
125
 
 
126
  test->account_service = g_object_new (SIMPLE_TYPE_ACCOUNT, NULL);
 
127
  tp_dbus_daemon_register_object (test->dbus, ACCOUNT_PATH,
 
128
      test->account_service);
 
129
}
 
130
 
 
131
static guint
 
132
test_get_times_notified (Test *test,
 
133
    const gchar *property)
 
134
{
 
135
  return GPOINTER_TO_UINT (g_hash_table_lookup (test->times_notified,
 
136
        property));
 
137
}
 
138
 
 
139
static void
 
140
test_notify_cb (Test *test,
 
141
    GParamSpec *pspec,
 
142
    TpAccount *account)
 
143
{
 
144
  guint counter = test_get_times_notified (test, pspec->name);
 
145
 
 
146
  g_hash_table_insert (test->times_notified, g_strdup (pspec->name),
 
147
      GUINT_TO_POINTER (++counter));
 
148
}
 
149
 
 
150
static void
 
151
test_set_up_account_notify (Test *test)
 
152
{
 
153
  g_assert (test->account != NULL);
 
154
 
 
155
  g_hash_table_remove_all (test->times_notified);
 
156
 
 
157
  if (test->notify_id != 0)
 
158
    {
 
159
      g_signal_handler_disconnect (test->account, test->notify_id);
 
160
    }
 
161
 
 
162
  test->notify_id = g_signal_connect_swapped (test->account, "notify",
 
163
      G_CALLBACK (test_notify_cb), test);
87
164
}
88
165
 
89
166
static void
92
169
{
93
170
  if (test->account != NULL)
94
171
    {
 
172
      test_proxy_run_until_dbus_queue_processed (test->account);
 
173
 
 
174
      if (test->notify_id != 0)
 
175
        {
 
176
          g_signal_handler_disconnect (test->account, test->notify_id);
 
177
        }
 
178
 
95
179
      g_object_unref (test->account);
96
180
      test->account = NULL;
97
181
    }
98
182
 
 
183
  g_hash_table_destroy (test->times_notified);
 
184
  test->times_notified = NULL;
 
185
 
 
186
  /* make sure any pending calls on the account have happened, so it can die */
 
187
  test_proxy_run_until_dbus_queue_processed (test->dbus);
 
188
 
99
189
  g_object_unref (test->dbus);
100
190
  test->dbus = NULL;
101
191
  g_main_loop_unref (test->mainloop);
103
193
}
104
194
 
105
195
static void
 
196
teardown_service (Test *test,
 
197
    gconstpointer data)
 
198
{
 
199
  tp_dbus_daemon_release_name (test->dbus, TP_ACCOUNT_MANAGER_BUS_NAME,
 
200
      &test->error);
 
201
  g_assert_no_error (test->error);
 
202
  tp_dbus_daemon_release_name (test->dbus, CONN1_BUS_NAME,
 
203
      &test->error);
 
204
  g_assert_no_error (test->error);
 
205
  tp_dbus_daemon_release_name (test->dbus, CONN2_BUS_NAME,
 
206
      &test->error);
 
207
  g_assert_no_error (test->error);
 
208
 
 
209
  tp_dbus_daemon_unregister_object (test->dbus, test->account_service);
 
210
 
 
211
  g_object_unref (test->account_service);
 
212
  test->account_service = NULL;
 
213
 
 
214
  teardown (test, data);
 
215
}
 
216
 
 
217
static void
106
218
test_new (Test *test,
107
219
          gconstpointer data G_GNUC_UNUSED)
108
220
{
119
231
  g_assert (test->account != NULL);
120
232
}
121
233
 
 
234
static void
 
235
account_prepare_cb (GObject *source,
 
236
    GAsyncResult *result,
 
237
    gpointer user_data)
 
238
{
 
239
  Test *test = user_data;
 
240
  GError *error = NULL;
 
241
 
 
242
  tp_proxy_prepare_finish (source, result, &error);
 
243
  g_assert_no_error (error);
 
244
 
 
245
  g_main_loop_quit (test->mainloop);
 
246
}
 
247
 
 
248
static void
 
249
test_prepare_success (Test *test,
 
250
    gconstpointer data G_GNUC_UNUSED)
 
251
{
 
252
  GQuark account_features[] = { TP_ACCOUNT_FEATURE_CORE, 0 };
 
253
  TpConnectionStatusReason reason;
 
254
  gchar *status = NULL;
 
255
  gchar *message = NULL;
 
256
 
 
257
  test->account = tp_account_new (test->dbus, ACCOUNT_PATH, NULL);
 
258
  g_assert (test->account != NULL);
 
259
 
 
260
  tp_proxy_prepare_async (test->account, account_features,
 
261
      account_prepare_cb, test);
 
262
  g_main_loop_run (test->mainloop);
 
263
 
 
264
  /* the obvious accessors */
 
265
  g_assert (tp_account_is_prepared (test->account, TP_ACCOUNT_FEATURE_CORE));
 
266
  g_assert (tp_account_is_enabled (test->account));
 
267
  g_assert (tp_account_is_valid (test->account));
 
268
  g_assert_cmpstr (tp_account_get_display_name (test->account), ==,
 
269
      "Fake Account");
 
270
  g_assert_cmpstr (tp_account_get_nickname (test->account), ==, "badger");
 
271
  g_assert_cmpuint (tp_asv_size (tp_account_get_parameters (test->account)),
 
272
      ==, 0);
 
273
  g_assert (!tp_account_get_connect_automatically (test->account));
 
274
  g_assert (tp_account_get_has_been_online (test->account));
 
275
  g_assert_cmpint (tp_account_get_connection_status (test->account, NULL),
 
276
      ==, TP_CONNECTION_STATUS_CONNECTED);
 
277
  g_assert_cmpint (tp_account_get_connection_status (test->account, &reason),
 
278
      ==, TP_CONNECTION_STATUS_CONNECTED);
 
279
  g_assert_cmpint (reason, ==, TP_CONNECTION_STATUS_REASON_REQUESTED);
 
280
 
 
281
  /* the CM and protocol come from the object path */
 
282
  g_assert_cmpstr (tp_account_get_connection_manager (test->account),
 
283
      ==, "what");
 
284
  g_assert_cmpstr (tp_account_get_protocol (test->account), ==, "ev");
 
285
 
 
286
  /* the icon name in SimpleAccount is "", so we guess based on the protocol */
 
287
  g_assert_cmpstr (tp_account_get_icon_name (test->account), ==, "im-ev");
 
288
 
 
289
  /* RequestedPresence is (Available, "available", "") */
 
290
  g_assert_cmpint (tp_account_get_requested_presence (test->account, NULL,
 
291
        NULL), ==, TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
 
292
  g_assert_cmpint (tp_account_get_requested_presence (test->account, &status,
 
293
        NULL), ==, TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
 
294
  g_assert_cmpstr (status, ==, "available");
 
295
  g_free (status);
 
296
  g_assert_cmpint (tp_account_get_requested_presence (test->account, NULL,
 
297
        &message), ==, TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
 
298
  g_assert_cmpstr (message, ==, "");
 
299
  g_free (message);
 
300
 
 
301
  /* CurrentPresence is the same as RequestedPresence */
 
302
  g_assert_cmpint (tp_account_get_current_presence (test->account, NULL,
 
303
        NULL), ==, TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
 
304
  g_assert_cmpint (tp_account_get_current_presence (test->account, &status,
 
305
        NULL), ==, TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
 
306
  g_assert_cmpstr (status, ==, "available");
 
307
  g_free (status);
 
308
  g_assert_cmpint (tp_account_get_current_presence (test->account, NULL,
 
309
        &message), ==, TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
 
310
  g_assert_cmpstr (message, ==, "");
 
311
  g_free (message);
 
312
 
 
313
  /* NormalizedName and AutomaticPresence aren't available yet */
 
314
}
 
315
 
 
316
static void
 
317
test_connection (Test *test,
 
318
    gconstpointer data G_GNUC_UNUSED)
 
319
{
 
320
  GQuark account_features[] = { TP_ACCOUNT_FEATURE_CORE, 0 };
 
321
  GHashTable *change = tp_asv_new (NULL, NULL);
 
322
  TpConnection *conn;
 
323
 
 
324
  test->account = tp_account_new (test->dbus, ACCOUNT_PATH, NULL);
 
325
  g_assert (test->account != NULL);
 
326
 
 
327
  tp_proxy_prepare_async (test->account, account_features,
 
328
      account_prepare_cb, test);
 
329
  g_main_loop_run (test->mainloop);
 
330
 
 
331
  g_assert (tp_account_is_prepared (test->account, TP_ACCOUNT_FEATURE_CORE));
 
332
 
 
333
  /* a connection turns up */
 
334
 
 
335
  test_set_up_account_notify (test);
 
336
  tp_asv_set_object_path (change, "Connection", CONN1_PATH);
 
337
  tp_asv_set_uint32 (change, "ConnectionStatus",
 
338
      TP_CONNECTION_STATUS_CONNECTING);
 
339
  tp_asv_set_uint32 (change, "ConnectionStatusReason",
 
340
      TP_CONNECTION_STATUS_REASON_REQUESTED);
 
341
  tp_svc_account_emit_account_property_changed (test->account_service, change);
 
342
  g_hash_table_remove_all (change);
 
343
 
 
344
  while (test_get_times_notified (test, "connection") < 1)
 
345
    g_main_context_iteration (NULL, TRUE);
 
346
 
 
347
  g_assert_cmpuint (test_get_times_notified (test, "connection"), ==, 1);
 
348
  conn = tp_account_get_connection (test->account);
 
349
  g_assert_cmpstr (tp_proxy_get_object_path (conn), ==, CONN1_PATH);
 
350
  g_assert_cmpuint (test_get_times_notified (test, "connection"), ==, 1);
 
351
 
 
352
  /* ensure the same connection - no change notification */
 
353
 
 
354
  test_set_up_account_notify (test);
 
355
  conn = tp_account_ensure_connection (test->account, CONN1_PATH);
 
356
  g_assert_cmpstr (tp_proxy_get_object_path (conn), ==, CONN1_PATH);
 
357
  g_assert_cmpuint (test_get_times_notified (test, "connection"), ==, 0);
 
358
 
 
359
  /* a no-op "change" */
 
360
 
 
361
  test_set_up_account_notify (test);
 
362
  tp_asv_set_object_path (change, "Connection", CONN1_PATH);
 
363
  tp_asv_set_uint32 (change, "ConnectionStatus",
 
364
      TP_CONNECTION_STATUS_CONNECTING);
 
365
  tp_asv_set_uint32 (change, "ConnectionStatusReason",
 
366
      TP_CONNECTION_STATUS_REASON_REQUESTED);
 
367
  tp_svc_account_emit_account_property_changed (test->account_service, change);
 
368
  g_hash_table_remove_all (change);
 
369
 
 
370
  test_proxy_run_until_dbus_queue_processed (test->account);
 
371
 
 
372
  g_assert_cmpuint (test_get_times_notified (test, "connection"), ==, 0);
 
373
  conn = tp_account_get_connection (test->account);
 
374
  g_assert_cmpstr (tp_proxy_get_object_path (conn), ==, CONN1_PATH);
 
375
  g_assert_cmpuint (test_get_times_notified (test, "connection"), ==, 0);
 
376
 
 
377
  /* atomically flip from one connection to another (unlikely) */
 
378
 
 
379
  test_set_up_account_notify (test);
 
380
  tp_asv_set_object_path (change, "Connection", CONN2_PATH);
 
381
  tp_asv_set_uint32 (change, "ConnectionStatus",
 
382
      TP_CONNECTION_STATUS_CONNECTED);
 
383
  tp_asv_set_uint32 (change, "ConnectionStatusReason",
 
384
      TP_CONNECTION_STATUS_REASON_REQUESTED);
 
385
  tp_svc_account_emit_account_property_changed (test->account_service, change);
 
386
  g_hash_table_remove_all (change);
 
387
 
 
388
  while (test_get_times_notified (test, "connection") < 1)
 
389
    g_main_context_iteration (NULL, TRUE);
 
390
 
 
391
  g_assert_cmpuint (test_get_times_notified (test, "connection"), ==, 1);
 
392
  conn = tp_account_get_connection (test->account);
 
393
  g_assert_cmpstr (tp_proxy_get_object_path (conn), ==, CONN2_PATH);
 
394
  g_assert_cmpuint (test_get_times_notified (test, "connection"), ==, 1);
 
395
 
 
396
  /* no more connection for you */
 
397
 
 
398
  test_set_up_account_notify (test);
 
399
  tp_asv_set_object_path (change, "Connection", "/");
 
400
  tp_asv_set_uint32 (change, "ConnectionStatus",
 
401
      TP_CONNECTION_STATUS_DISCONNECTED);
 
402
  tp_asv_set_uint32 (change, "ConnectionStatusReason",
 
403
      TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR);
 
404
  tp_svc_account_emit_account_property_changed (test->account_service, change);
 
405
  g_hash_table_remove_all (change);
 
406
 
 
407
  while (test_get_times_notified (test, "connection") < 1)
 
408
    g_main_context_iteration (NULL, TRUE);
 
409
 
 
410
  g_assert_cmpuint (test_get_times_notified (test, "connection"), ==, 1);
 
411
  conn = tp_account_get_connection (test->account);
 
412
  g_assert (conn == NULL);
 
413
 
 
414
  /* staple on a Connection (this is intended for use in e.g. observers,
 
415
   * if they're told about a Connection that the Account hasn't told them
 
416
   * about yet) */
 
417
 
 
418
  test_set_up_account_notify (test);
 
419
  conn = tp_account_ensure_connection (test->account, CONN1_PATH);
 
420
  g_assert_cmpstr (tp_proxy_get_object_path (conn), ==, CONN1_PATH);
 
421
  g_assert_cmpuint (test_get_times_notified (test, "connection"), ==, 1);
 
422
 
 
423
  g_hash_table_destroy (change);
 
424
}
 
425
 
122
426
int
123
427
main (int argc,
124
428
      char **argv)
168
472
 
169
473
  g_test_add ("/account/new", Test, NULL, setup, test_new, teardown);
170
474
 
 
475
  g_test_add ("/account/prepare/success", Test, NULL, setup_service,
 
476
              test_prepare_success, teardown_service);
 
477
 
 
478
  g_test_add ("/account/connection", Test, NULL, setup_service,
 
479
              test_connection, teardown_service);
 
480
 
171
481
  return g_test_run ();
172
482
}