summaryrefslogtreecommitdiff
path: root/tests/dbus/callable-example.c
blob: b6999687efaece197515ca05b20bfcaa004a7ede (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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
/* Feature test for example StreamedMedia CM code.
 *
 * Copyright © 2009 Collabora Ltd. <http://www.collabora.co.uk/>
 * Copyright © 2009 Nokia Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <telepathy-glib/channel.h>
#include <telepathy-glib/connection.h>
#include <telepathy-glib/connection-manager.h>
#include <telepathy-glib/debug.h>
#include <telepathy-glib/gtypes.h>
#include <telepathy-glib/interfaces.h>

#include "examples/cm/callable/connection-manager.h"
#include "examples/cm/callable/conn.h"
#include "examples/cm/callable/media-channel.h"
#include "examples/cm/callable/media-stream.h"

#include "tests/lib/util.h"

#define CLEAR_OBJECT(o) \
  G_STMT_START { \
      if (*(o) != NULL) \
        { \
          g_object_unref (*(o)); \
          *(o) = NULL; \
        } \
  } G_STMT_END

#define CLEAR_BOXED(g, o) \
  G_STMT_START { \
      if (*(o) != NULL) \
        { \
          g_boxed_free ((g), *(o)); \
          *(o) = NULL; \
        } \
  } G_STMT_END

static void
test_assert_uu_hash_contains (GHashTable *hash,
                              guint key,
                              guint expected)
{
  gpointer v;

  if (!g_hash_table_lookup_extended (hash, GUINT_TO_POINTER (key), NULL, &v))
    g_error ("Expected %u => %u in hash table, but key was absent", key,
        expected);

  g_assert_cmpuint (GPOINTER_TO_UINT (v), ==, expected);
}

typedef struct
{
  TpIntSet *added;
  TpIntSet *removed;
  TpIntSet *local_pending;
  TpIntSet *remote_pending;
  GHashTable *details;
} GroupEvent;

static GroupEvent *
group_event_new (void)
{
  return g_slice_new0 (GroupEvent);
}

static void
group_event_destroy (GroupEvent *ge)
{
  if (ge->added != NULL)
    tp_intset_destroy (ge->added);

  if (ge->removed != NULL)
    tp_intset_destroy (ge->removed);

  if (ge->local_pending != NULL)
    tp_intset_destroy (ge->local_pending);

  if (ge->remote_pending != NULL)
    tp_intset_destroy (ge->remote_pending);

  if (ge->details != NULL)
    g_hash_table_destroy (ge->details);

  g_slice_free (GroupEvent, ge);
}

typedef enum
{
  STREAM_EVENT_ADDED,
  STREAM_EVENT_DIRECTION_CHANGED,
  STREAM_EVENT_ERROR,
  STREAM_EVENT_REMOVED,
  STREAM_EVENT_STATE_CHANGED
} StreamEventType;

typedef struct
{
  StreamEventType type;
  guint id;
  TpHandle contact;
  TpMediaStreamType media_type;
  TpMediaStreamDirection direction;
  TpMediaStreamPendingSend pending_send;
  TpMediaStreamError error;
  TpMediaStreamState state;
} StreamEvent;

static StreamEvent *
stream_event_new (void)
{
  return g_slice_new0 (StreamEvent);
}

static void
stream_event_destroy (StreamEvent *se)
{
  g_slice_free (StreamEvent, se);
}

typedef struct
{
  GMainLoop *mainloop;
  TpDBusDaemon *dbus;
  GError *error /* statically initialized to NULL */ ;

  ExampleCallableConnectionManager *service_cm;

  TpConnectionManager *cm;
  TpConnection *conn;
  TpChannel *chan;
  TpHandle self_handle;

  GArray *audio_request;
  GArray *video_request;
  GArray *invalid_request;

  GArray *stream_ids;
  GArray *contacts;
  GPtrArray *request_streams_return;
  GPtrArray *list_streams_return;

  GSList *group_events;
  gulong members_changed_detailed_id;

  GSList *stream_events;

  guint audio_stream_id;
  guint video_stream_id;
  GHashTable *stream_directions;
  GHashTable *stream_pending_sends;
  GHashTable *stream_states;
} Test;

static void
cm_ready_cb (TpConnectionManager *cm G_GNUC_UNUSED,
             const GError *error,
             gpointer user_data,
             GObject *weak_object G_GNUC_UNUSED)
{
  Test *test = user_data;

  test_assert_no_error (error);
  g_main_loop_quit (test->mainloop);
}

static void
conn_ready_cb (TpConnection *conn G_GNUC_UNUSED,
               const GError *error,
               gpointer user_data)
{
  Test *test = user_data;

  test_assert_no_error (error);
  g_main_loop_quit (test->mainloop);
}

static void
setup (Test *test,
       gconstpointer data G_GNUC_UNUSED)
{
  TpBaseConnectionManager *service_cm_as_base;
  gboolean ok;
  gchar *bus_name;
  gchar *object_path;
  GHashTable *parameters;
  guint audio = TP_MEDIA_STREAM_TYPE_AUDIO;
  guint video = TP_MEDIA_STREAM_TYPE_VIDEO;
  guint not_a_media_type = 31337;

  g_type_init ();
  tp_debug_set_flags ("all");

  test->audio_stream_id = G_MAXUINT;
  test->video_stream_id = G_MAXUINT;

  test->stream_directions = g_hash_table_new (NULL, NULL);
  test->stream_pending_sends = g_hash_table_new (NULL, NULL);
  test->stream_states = g_hash_table_new (NULL, NULL);

  test->mainloop = g_main_loop_new (NULL, FALSE);
  test->dbus = tp_dbus_daemon_dup (NULL);
  g_assert (test->dbus != NULL);

  test->service_cm = EXAMPLE_CALLABLE_CONNECTION_MANAGER (g_object_new (
        EXAMPLE_TYPE_CALLABLE_CONNECTION_MANAGER,
        NULL));
  g_assert (test->service_cm != NULL);
  service_cm_as_base = TP_BASE_CONNECTION_MANAGER (test->service_cm);
  g_assert (service_cm_as_base != NULL);

  ok = tp_base_connection_manager_register (service_cm_as_base);
  g_assert (ok);

  test->cm = tp_connection_manager_new (test->dbus, "example_callable",
      NULL, &test->error);
  g_assert (test->cm != NULL);
  tp_connection_manager_call_when_ready (test->cm, cm_ready_cb, test, NULL,
      NULL);
  g_main_loop_run (test->mainloop);

  parameters = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
      (GDestroyNotify) tp_g_value_slice_free);
  g_hash_table_insert (parameters, "account",
      tp_g_value_slice_new_static_string ("me"));
  g_hash_table_insert (parameters, "simulation-delay",
      tp_g_value_slice_new_uint (0));

  tp_cli_connection_manager_run_request_connection (test->cm, -1,
      "example", parameters, &bus_name, &object_path, &test->error, NULL);
  test_assert_no_error (test->error);

  test->conn = tp_connection_new (test->dbus, bus_name, object_path,
      &test->error);
  test_assert_no_error (test->error);
  g_assert (test->conn != NULL);
  tp_cli_connection_call_connect (test->conn, -1, NULL, NULL, NULL, NULL);
  tp_connection_call_when_ready (test->conn, conn_ready_cb, test);
  g_main_loop_run (test->mainloop);

  test->self_handle = tp_connection_get_self_handle (test->conn);
  g_assert (test->self_handle != 0);

  test->audio_request = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
  g_array_append_val (test->audio_request, audio);

  test->video_request = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
  g_array_append_val (test->video_request, video);

  test->invalid_request = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
  g_array_append_val (test->invalid_request, not_a_media_type);

  test->stream_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 2);
  test->contacts = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);

  g_free (bus_name);
  g_free (object_path);
}

static void
channel_created_cb (TpConnection *connection,
                    const gchar *object_path,
                    GHashTable *immutable_properties,
                    const GError *error,
                    gpointer user_data,
                    GObject *weak_object G_GNUC_UNUSED)
{
  Test *test = user_data;
  GError *new_error = NULL;

  test_assert_no_error (error);

  test->chan = tp_channel_new_from_properties (connection, object_path,
      immutable_properties, &new_error);
  test_assert_no_error (new_error);

  g_main_loop_quit (test->mainloop);
}

static void
channel_ready_cb (TpChannel *channel G_GNUC_UNUSED,
                  const GError *error,
                  gpointer user_data)
{
  Test *test = user_data;

  test_assert_no_error (error);
  g_main_loop_quit (test->mainloop);
}

static void
requested_streams_cb (TpChannel *chan G_GNUC_UNUSED,
                      const GPtrArray *stream_info,
                      const GError *error,
                      gpointer user_data,
                      GObject *weak_object G_GNUC_UNUSED)
{
  Test *test = user_data;

  CLEAR_BOXED (TP_ARRAY_TYPE_MEDIA_STREAM_INFO_LIST,
      &test->request_streams_return);

  if (error != NULL)
    {
      test->error = g_error_copy (error);
    }
  else
    {
      test->request_streams_return = g_boxed_copy (
          TP_ARRAY_TYPE_MEDIA_STREAM_INFO_LIST, stream_info);
    }

  g_main_loop_quit (test->mainloop);
}

static void
listed_streams_cb (TpChannel *chan G_GNUC_UNUSED,
                   const GPtrArray *stream_info,
                   const GError *error,
                   gpointer user_data,
                   GObject *weak_object G_GNUC_UNUSED)
{
  Test *test = user_data;

  /* ListStreams shouldn't fail in any of these tests */
  test_assert_no_error (error);

  CLEAR_BOXED (TP_ARRAY_TYPE_MEDIA_STREAM_INFO_LIST,
      &test->list_streams_return);

  test->list_streams_return = g_boxed_copy (
      TP_ARRAY_TYPE_MEDIA_STREAM_INFO_LIST, stream_info);

  g_main_loop_quit (test->mainloop);
}

static void
void_cb (TpChannel *chan G_GNUC_UNUSED,
         const GError *error,
         gpointer user_data,
         GObject *weak_object G_GNUC_UNUSED)
{
  Test *test = user_data;

  if (error != NULL)
    {
      test->error = g_error_copy (error);
    }

  g_main_loop_quit (test->mainloop);
}

static void
members_changed_detailed_cb (TpChannel *chan G_GNUC_UNUSED,
                             const GArray *added,
                             const GArray *removed,
                             const GArray *local_pending,
                             const GArray *remote_pending,
                             GHashTable *details,
                             gpointer user_data)
{
  Test *test = user_data;
  GroupEvent *ge = group_event_new ();

  /* just log the event */
  ge->added = tp_intset_from_array (added);
  ge->removed = tp_intset_from_array (removed);
  ge->local_pending = tp_intset_from_array (local_pending);
  ge->remote_pending = tp_intset_from_array (remote_pending);
  ge->details = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
      (GDestroyNotify) tp_g_value_slice_free);
  tp_g_hash_table_update (ge->details, details,
      (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);

  test->group_events = g_slist_prepend (test->group_events, ge);
}

static void
stream_added_cb (TpChannel *chan G_GNUC_UNUSED,
                 guint id,
                 guint contact,
                 guint media_type,
                 gpointer user_data,
                 GObject *weak_object G_GNUC_UNUSED)
{
  Test *test = user_data;
  StreamEvent *se = stream_event_new ();

  se->type = STREAM_EVENT_ADDED;
  se->id = id;
  se->contact = contact;
  se->media_type = media_type;

  test->stream_events = g_slist_prepend (test->stream_events, se);

  /* use G_MAXUINT to represent "we have no idea" until telepathy-spec
   * specifies what the initial direction is */
  g_hash_table_insert (test->stream_directions, GUINT_TO_POINTER (id),
      GUINT_TO_POINTER (G_MAXUINT));
  g_hash_table_insert (test->stream_pending_sends, GUINT_TO_POINTER (id),
      GUINT_TO_POINTER (G_MAXUINT));

  /* this one does seem reasonable, though */
  g_hash_table_insert (test->stream_states, GUINT_TO_POINTER (id),
      GUINT_TO_POINTER (TP_MEDIA_STREAM_STATE_DISCONNECTED));
}

static void
stream_direction_changed_cb (TpChannel *chan G_GNUC_UNUSED,
                             guint id,
                             guint direction,
                             guint pending_flags,
                             gpointer user_data,
                             GObject *weak_object G_GNUC_UNUSED)
{
  Test *test = user_data;
  StreamEvent *se = stream_event_new ();

  se->type = STREAM_EVENT_DIRECTION_CHANGED;
  se->id = id;
  se->direction = direction;
  se->pending_send = pending_flags;

  test->stream_events = g_slist_prepend (test->stream_events, se);

  g_hash_table_insert (test->stream_directions, GUINT_TO_POINTER (id),
      GUINT_TO_POINTER (direction));
  g_hash_table_insert (test->stream_pending_sends, GUINT_TO_POINTER (id),
      GUINT_TO_POINTER (pending_flags));
}

static void
stream_error_cb (TpChannel *chan G_GNUC_UNUSED,
                 guint id,
                 guint error,
                 const gchar *message,
                 gpointer user_data,
                 GObject *weak_object G_GNUC_UNUSED)
{
  Test *test = user_data;
  StreamEvent *se = stream_event_new ();

  se->type = STREAM_EVENT_ERROR;
  se->id = id;
  se->error = error;

  test->stream_events = g_slist_prepend (test->stream_events, se);
}

static void
stream_removed_cb (TpChannel *chan G_GNUC_UNUSED,
                   guint id,
                   gpointer user_data,
                   GObject *weak_object G_GNUC_UNUSED)
{
  Test *test = user_data;
  StreamEvent *se = stream_event_new ();

  se->type = STREAM_EVENT_REMOVED;
  se->id = id;

  test->stream_events = g_slist_prepend (test->stream_events, se);

  g_hash_table_remove (test->stream_directions, GUINT_TO_POINTER (id));
  g_hash_table_remove (test->stream_pending_sends, GUINT_TO_POINTER (id));
  g_hash_table_remove (test->stream_states, GUINT_TO_POINTER (id));
}

static void
stream_state_changed_cb (TpChannel *chan G_GNUC_UNUSED,
                         guint id,
                         guint state,
                         gpointer user_data,
                         GObject *weak_object G_GNUC_UNUSED)
{
  Test *test = user_data;
  StreamEvent *se = stream_event_new ();

  se->type = STREAM_EVENT_STATE_CHANGED;
  se->id = id;
  se->state = state;

  test->stream_events = g_slist_prepend (test->stream_events, se);

  g_hash_table_insert (test->stream_states, GUINT_TO_POINTER (id),
      GUINT_TO_POINTER (state));
}

static void
test_connect_channel_signals (Test *test)
{
  test->members_changed_detailed_id = g_signal_connect (test->chan,
      "group-members-changed-detailed",
      G_CALLBACK (members_changed_detailed_cb), test);

  tp_cli_channel_type_streamed_media_connect_to_stream_added (test->chan,
      stream_added_cb, test, NULL, NULL, NULL);
  tp_cli_channel_type_streamed_media_connect_to_stream_removed (test->chan,
      stream_removed_cb, test, NULL, NULL, NULL);
  tp_cli_channel_type_streamed_media_connect_to_stream_error (test->chan,
      stream_error_cb, test, NULL, NULL, NULL);
  tp_cli_channel_type_streamed_media_connect_to_stream_direction_changed (
      test->chan, stream_direction_changed_cb, test, NULL, NULL, NULL);
  tp_cli_channel_type_streamed_media_connect_to_stream_state_changed (
      test->chan, stream_state_changed_cb, test, NULL, NULL, NULL);
}

static void
outgoing_call (Test *test,
               const gchar *id)
{
  GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal,
      NULL, (GDestroyNotify) tp_g_value_slice_free);

  g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType",
      tp_g_value_slice_new_static_string (
        TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA));
  g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType",
      tp_g_value_slice_new_uint (TP_HANDLE_TYPE_CONTACT));
  g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetID",
      tp_g_value_slice_new_string (id));

  tp_cli_connection_interface_requests_call_create_channel (test->conn, -1,
      request, channel_created_cb, test, NULL, NULL);
  g_hash_table_destroy (request);
  request = NULL;
  g_main_loop_run (test->mainloop);

  tp_channel_call_when_ready (test->chan, channel_ready_cb, test);
  g_main_loop_run (test->mainloop);

  test_connect_channel_signals (test);
}

static void
maybe_pop_stream_direction (Test *test)
{
  while (test->stream_events != NULL)
    {
      StreamEvent *se = test->stream_events->data;

      if (se->type == STREAM_EVENT_DIRECTION_CHANGED)
        {
          stream_event_destroy (se);
          test->stream_events = g_slist_delete_link (test->stream_events,
              test->stream_events);
        }
      else
        {
          break;
        }
    }
}

static void
test_basics (Test *test,
             gconstpointer data G_GNUC_UNUSED)
{
  GValueArray *audio_info, *video_info;
  guint not_a_stream_id = 31337;
  GroupEvent *ge;
  StreamEvent *se;

  outgoing_call (test, "basic-test");

  /* At this point in the channel's lifetime, we should be the channel's
   * only member */
  g_assert_cmpuint (tp_channel_group_get_self_handle (test->chan), ==,
      test->self_handle);
  g_assert_cmpuint (tp_channel_group_get_handle_owner (test->chan,
        test->self_handle), ==, test->self_handle);
  g_assert_cmpuint (tp_intset_size (tp_channel_group_get_members (test->chan)),
      ==, 1);
  g_assert_cmpuint (tp_intset_size (
        tp_channel_group_get_local_pending (test->chan)), ==, 0);
  g_assert_cmpuint (tp_intset_size (
        tp_channel_group_get_remote_pending (test->chan)), ==, 0);
  g_assert (tp_intset_is_member (tp_channel_group_get_members (test->chan),
        test->self_handle));

  /* ListStreams: we have no streams yet */

  tp_cli_channel_type_streamed_media_call_list_streams (test->chan, -1,
      listed_streams_cb, test, NULL, NULL);
  g_main_loop_run (test->mainloop);
  test_assert_no_error (test->error);

  g_assert_cmpuint (test->list_streams_return->len, ==, 0);

  /* RequestStreams with bad handle must fail */

  tp_cli_channel_type_streamed_media_call_request_streams (test->chan, -1,
      test->self_handle,
      test->audio_request, requested_streams_cb,
      test, NULL, NULL);
  g_main_loop_run (test->mainloop);
  g_assert (test->error != NULL);
  g_clear_error (&test->error);

  /* RequestStreams with bad request must fail */

  tp_cli_channel_type_streamed_media_call_request_streams (test->chan, -1,
      tp_channel_get_handle (test->chan, NULL),
      test->invalid_request, requested_streams_cb,
      test, NULL, NULL);
  g_main_loop_run (test->mainloop);
  g_assert (test->error != NULL);
  g_clear_error (&test->error);

  /* RequestStreams */

  tp_cli_channel_type_streamed_media_call_request_streams (test->chan, -1,
      tp_channel_get_handle (test->chan, NULL),
      test->audio_request, requested_streams_cb,
      test, NULL, NULL);
  g_main_loop_run (test->mainloop);
  test_assert_no_error (test->error);

  g_assert_cmpuint (test->request_streams_return->len, ==, 1);
  audio_info = g_ptr_array_index (test->request_streams_return, 0);

  g_assert (G_VALUE_HOLDS_UINT (audio_info->values + 0));
  g_assert (G_VALUE_HOLDS_UINT (audio_info->values + 1));
  g_assert (G_VALUE_HOLDS_UINT (audio_info->values + 2));
  g_assert (G_VALUE_HOLDS_UINT (audio_info->values + 3));
  g_assert (G_VALUE_HOLDS_UINT (audio_info->values + 4));
  g_assert (G_VALUE_HOLDS_UINT (audio_info->values + 5));

  test->audio_stream_id = g_value_get_uint (audio_info->values + 0);

  g_assert_cmpuint (g_value_get_uint (audio_info->values + 1), ==,
      tp_channel_get_handle (test->chan, NULL));
  g_assert_cmpuint (g_value_get_uint (audio_info->values + 2), ==,
      TP_MEDIA_STREAM_TYPE_AUDIO);
  /* Initially, the stream is disconnected, we're willing to send to the peer,
   * and we've asked the peer whether they will send to us too */
  g_assert_cmpuint (g_value_get_uint (audio_info->values + 3), ==,
      TP_MEDIA_STREAM_STATE_DISCONNECTED);
  g_assert_cmpuint (g_value_get_uint (audio_info->values + 4), ==,
      TP_MEDIA_STREAM_DIRECTION_SEND);
  g_assert_cmpuint (g_value_get_uint (audio_info->values + 5), ==,
      TP_MEDIA_STREAM_PENDING_REMOTE_SEND);

  /* ListStreams again: now we have the audio stream */

  tp_cli_channel_type_streamed_media_call_list_streams (test->chan, -1,
      listed_streams_cb, test, NULL, NULL);
  g_main_loop_run (test->mainloop);
  test_assert_no_error (test->error);

  g_assert_cmpuint (test->list_streams_return->len, ==, 1);
  audio_info = g_ptr_array_index (test->list_streams_return, 0);

  g_assert (G_VALUE_HOLDS_UINT (audio_info->values + 0));
  g_assert (G_VALUE_HOLDS_UINT (audio_info->values + 1));
  g_assert (G_VALUE_HOLDS_UINT (audio_info->values + 2));
  g_assert (G_VALUE_HOLDS_UINT (audio_info->values + 3));
  g_assert (G_VALUE_HOLDS_UINT (audio_info->values + 4));
  g_assert (G_VALUE_HOLDS_UINT (audio_info->values + 5));

  g_assert_cmpuint (g_value_get_uint (audio_info->values + 0), ==,
      test->audio_stream_id);
  g_assert_cmpuint (g_value_get_uint (audio_info->values + 1), ==,
      tp_channel_get_handle (test->chan, NULL));
  g_assert_cmpuint (g_value_get_uint (audio_info->values + 1), ==,
      tp_channel_get_handle (test->chan, NULL));
  g_assert_cmpuint (g_value_get_uint (audio_info->values + 2), ==,
      TP_MEDIA_STREAM_TYPE_AUDIO);
  /* Don't assert about the state or the direction here - it might already have
   * changed to connected or bidirectional, respectively. */

  /* The two oldest stream events should be the addition of the audio stream,
   * and the change to the appropriate direction (StreamAdded does not signal
   * stream directionality) */

  g_assert_cmpuint (g_slist_length (test->stream_events), >=, 2);

  se = g_slist_nth_data (test->stream_events,
      g_slist_length (test->stream_events) - 1);
  g_assert_cmpuint (se->type, ==, STREAM_EVENT_ADDED);
  g_assert_cmpuint (se->id, ==, test->audio_stream_id);
  g_assert_cmpuint (se->contact, ==, tp_channel_get_handle (test->chan, NULL));
  g_assert_cmpuint (se->media_type, ==, TP_MEDIA_STREAM_TYPE_AUDIO);

  se = g_slist_nth_data (test->stream_events,
      g_slist_length (test->stream_events) - 2);
  g_assert_cmpuint (se->type, ==, STREAM_EVENT_DIRECTION_CHANGED);
  g_assert_cmpuint (se->id, ==, test->audio_stream_id);
  g_assert_cmpuint (se->direction, ==, TP_MEDIA_STREAM_DIRECTION_SEND);
  g_assert_cmpuint (se->pending_send, ==, TP_MEDIA_STREAM_PENDING_REMOTE_SEND);

  /* Wait for the remote contact to answer, if they haven't already */

  while (!tp_intset_is_member (tp_channel_group_get_members (test->chan),
        tp_channel_get_handle (test->chan, NULL)))
    g_main_context_iteration (NULL, TRUE);

  /* The self-handle and the peer are now the channel's members */
  g_assert_cmpuint (tp_channel_group_get_handle_owner (test->chan,
        test->self_handle), ==, test->self_handle);
  g_assert_cmpuint (tp_channel_group_get_handle_owner (test->chan,
        tp_channel_get_handle (test->chan, NULL)),
      ==, tp_channel_get_handle (test->chan, NULL));
  g_assert_cmpuint (tp_intset_size (tp_channel_group_get_members (test->chan)),
      ==, 2);
  g_assert_cmpuint (tp_intset_size (
        tp_channel_group_get_local_pending (test->chan)), ==, 0);
  g_assert_cmpuint (tp_intset_size (
        tp_channel_group_get_remote_pending (test->chan)), ==, 0);
  g_assert (tp_intset_is_member (tp_channel_group_get_members (test->chan),
        test->self_handle));
  g_assert (tp_intset_is_member (tp_channel_group_get_members (test->chan),
        tp_channel_get_handle (test->chan, NULL)));

  /* Look at the event log: what should have happened is that the remote
   * peer was added first to remote-pending, then to members. (The event
   * log is in reverse chronological order.) */

  ge = g_slist_nth_data (test->group_events, 1);

  g_assert_cmpuint (tp_intset_size (ge->added), ==, 0);
  g_assert_cmpuint (tp_intset_size (ge->removed), ==, 0);
  g_assert_cmpuint (tp_intset_size (ge->local_pending), ==, 0);
  g_assert_cmpuint (tp_intset_size (ge->remote_pending), ==, 1);
  g_assert (tp_intset_is_member (ge->remote_pending,
        tp_channel_get_handle (test->chan, NULL)));
  g_assert_cmpuint (tp_asv_get_uint32 (ge->details, "actor", NULL), ==,
      test->self_handle);
  g_assert_cmpuint (tp_asv_get_uint32 (ge->details, "change-reason", NULL), ==,
      TP_CHANNEL_GROUP_CHANGE_REASON_NONE);

  ge = g_slist_nth_data (test->group_events, 0);

  g_assert_cmpuint (tp_intset_size (ge->added), ==, 1);
  g_assert (tp_intset_is_member (ge->added,
        tp_channel_get_handle (test->chan, NULL)));
  g_assert_cmpuint (tp_intset_size (ge->removed), ==, 0);
  g_assert_cmpuint (tp_intset_size (ge->local_pending), ==, 0);
  g_assert_cmpuint (tp_intset_size (ge->remote_pending), ==, 0);
  g_assert_cmpuint (tp_asv_get_uint32 (ge->details, "actor", NULL), ==,
      tp_channel_get_handle (test->chan, NULL));
  g_assert_cmpuint (tp_asv_get_uint32 (ge->details, "change-reason", NULL), ==,
      TP_CHANNEL_GROUP_CHANGE_REASON_NONE);

  /* Immediately the call is accepted, the remote peer accepts our proposed
   * stream direction */
  test_connection_run_until_dbus_queue_processed (test->conn);

  se = g_slist_nth_data (test->stream_events, 0);
  g_assert_cmpuint (se->type, ==, STREAM_EVENT_DIRECTION_CHANGED);
  g_assert_cmpuint (se->id, ==, test->audio_stream_id);
  g_assert_cmpuint (se->direction, ==,
      TP_MEDIA_STREAM_DIRECTION_BIDIRECTIONAL);
  g_assert_cmpuint (se->pending_send, ==, 0);

  test_assert_uu_hash_contains (test->stream_states, test->audio_stream_id,
      TP_MEDIA_STREAM_STATE_DISCONNECTED);
  test_assert_uu_hash_contains (test->stream_directions, test->audio_stream_id,
      TP_MEDIA_STREAM_DIRECTION_BIDIRECTIONAL);
  test_assert_uu_hash_contains (test->stream_pending_sends,
      test->audio_stream_id, 0);

  /* The stream should either already be connected, or become connected after
   * a while */

  while (GPOINTER_TO_UINT (g_hash_table_lookup (test->stream_states,
        GUINT_TO_POINTER (test->audio_stream_id))) !=
        TP_MEDIA_STREAM_STATE_CONNECTED)
    {
      g_main_context_iteration (NULL, TRUE);
    }

  /* RequestStreams again, to add a video stream */

  tp_cli_channel_type_streamed_media_call_request_streams (test->chan, -1,
      tp_channel_get_handle (test->chan, NULL),
      test->video_request, requested_streams_cb,
      test, NULL, NULL);
  g_main_loop_run (test->mainloop);
  test_assert_no_error (test->error);

  g_assert_cmpuint (test->request_streams_return->len, ==, 1);

  video_info = g_ptr_array_index (test->request_streams_return, 0);

  g_assert (G_VALUE_HOLDS_UINT (video_info->values + 0));
  g_assert (G_VALUE_HOLDS_UINT (video_info->values + 1));
  g_assert (G_VALUE_HOLDS_UINT (video_info->values + 2));
  g_assert (G_VALUE_HOLDS_UINT (video_info->values + 3));
  g_assert (G_VALUE_HOLDS_UINT (video_info->values + 4));
  g_assert (G_VALUE_HOLDS_UINT (video_info->values + 5));

  test->video_stream_id = g_value_get_uint (video_info->values + 0);

  g_assert_cmpuint (g_value_get_uint (video_info->values + 1), ==,
      tp_channel_get_handle (test->chan, NULL));
  g_assert_cmpuint (g_value_get_uint (video_info->values + 1), ==,
      tp_channel_get_handle (test->chan, NULL));
  g_assert_cmpuint (g_value_get_uint (video_info->values + 2), ==,
      TP_MEDIA_STREAM_TYPE_VIDEO);
  g_assert_cmpuint (g_value_get_uint (video_info->values + 3), ==,
      TP_MEDIA_STREAM_STATE_DISCONNECTED);
  g_assert_cmpuint (g_value_get_uint (video_info->values + 4), ==,
      TP_MEDIA_STREAM_DIRECTION_SEND);
  g_assert_cmpuint (g_value_get_uint (video_info->values + 5), ==,
      TP_MEDIA_STREAM_PENDING_REMOTE_SEND);

  /* ListStreams again: now we have the video stream too */

  tp_cli_channel_type_streamed_media_call_list_streams (test->chan, -1,
      listed_streams_cb, test, NULL, NULL);
  g_main_loop_run (test->mainloop);
  test_assert_no_error (test->error);

  g_assert_cmpuint (test->list_streams_return->len, ==, 2);

  /* this might be the video or the audio - we'll have to find out */
  audio_info = g_ptr_array_index (test->list_streams_return, 0);

  if (g_value_get_uint (audio_info->values + 0) == test->audio_stream_id)
    {
      /* our guess was right, so the other one must be the video */
      video_info = g_ptr_array_index (test->list_streams_return, 1);
    }
  else
    {
      /* we guessed wrong, compensate for that */
      video_info = audio_info;
      audio_info = g_ptr_array_index (test->list_streams_return, 1);
    }

  g_assert_cmpuint (g_value_get_uint (audio_info->values + 0), ==,
      test->audio_stream_id);
  g_assert_cmpuint (g_value_get_uint (audio_info->values + 2), ==,
      TP_MEDIA_STREAM_TYPE_AUDIO);
  g_assert_cmpuint (g_value_get_uint (video_info->values + 0), ==,
      test->video_stream_id);
  g_assert_cmpuint (g_value_get_uint (video_info->values + 2), ==,
      TP_MEDIA_STREAM_TYPE_VIDEO);

  /* The last two stream events should be the addition of the video stream,
   * and the change to the appropriate direction (StreamAdded does not signal
   * stream directionality) */

  se = g_slist_nth_data (test->stream_events, 1);
  g_assert_cmpuint (se->type, ==, STREAM_EVENT_ADDED);
  g_assert_cmpuint (se->id, ==, test->video_stream_id);
  g_assert_cmpuint (se->contact, ==, tp_channel_get_handle (test->chan, NULL));
  g_assert_cmpuint (se->media_type, ==, TP_MEDIA_STREAM_TYPE_VIDEO);

  se = g_slist_nth_data (test->stream_events, 0);
  g_assert_cmpuint (se->type, ==, STREAM_EVENT_DIRECTION_CHANGED);
  g_assert_cmpuint (se->id, ==, test->video_stream_id);
  g_assert_cmpuint (se->direction, ==, TP_MEDIA_STREAM_DIRECTION_SEND);
  g_assert_cmpuint (se->pending_send, ==, TP_MEDIA_STREAM_PENDING_REMOTE_SEND);

  /* After a moment, the video stream becomes connected, and the remote user
   * accepts our proposed direction change. These might happen in either
   * order, at least in this implementation. We already tested the stream
   * becoming connected, so let's test the direction change here instead. */

  while (GPOINTER_TO_UINT (g_hash_table_lookup (test->stream_directions,
        GUINT_TO_POINTER (test->video_stream_id))) !=
        TP_MEDIA_STREAM_DIRECTION_BIDIRECTIONAL)
    {
      g_main_context_iteration (NULL, TRUE);
    }

  /* RemoveStreams with a bad stream ID must fail */

  g_array_set_size (test->stream_ids, 0);
  g_array_append_val (test->stream_ids, not_a_stream_id);
  tp_cli_channel_type_streamed_media_call_remove_streams (test->chan, -1,
      test->stream_ids,
      void_cb, test, NULL, NULL);
  g_main_loop_run (test->mainloop);
  g_assert (test->error != NULL);
  g_clear_error (&test->error);

  /* Drop the video stream with RemoveStreams */

  g_array_set_size (test->stream_ids, 0);
  g_array_append_val (test->stream_ids, test->video_stream_id);
  tp_cli_channel_type_streamed_media_call_remove_streams (test->chan, -1,
      test->stream_ids,
      void_cb, test, NULL, NULL);
  g_main_loop_run (test->mainloop);
  test_assert_no_error (test->error);

  /* List streams again: now there's only the audio */

  tp_cli_channel_type_streamed_media_call_list_streams (test->chan, -1,
      listed_streams_cb, test, NULL, NULL);
  g_main_loop_run (test->mainloop);
  test_assert_no_error (test->error);

  g_assert_cmpuint (test->list_streams_return->len, ==, 1);
  audio_info = g_ptr_array_index (test->list_streams_return, 0);

  g_assert (G_VALUE_HOLDS_UINT (audio_info->values + 0));
  g_assert (G_VALUE_HOLDS_UINT (audio_info->values + 1));
  g_assert (G_VALUE_HOLDS_UINT (audio_info->values + 2));
  g_assert (G_VALUE_HOLDS_UINT (audio_info->values + 3));
  g_assert (G_VALUE_HOLDS_UINT (audio_info->values + 4));
  g_assert (G_VALUE_HOLDS_UINT (audio_info->values + 5));

  g_assert_cmpuint (g_value_get_uint (audio_info->values + 0), ==,
      test->audio_stream_id);
  g_assert_cmpuint (g_value_get_uint (audio_info->values + 2), ==,
      TP_MEDIA_STREAM_TYPE_AUDIO);

  /* The last event should be the removal of the video stream */

  se = g_slist_nth_data (test->stream_events, 0);

  g_assert_cmpuint (se->type, ==, STREAM_EVENT_REMOVED);
  g_assert_cmpuint (se->id, ==, test->video_stream_id);

  /* Hang up the call in the recommended way */

  g_array_set_size (test->contacts, 0);
  g_array_append_val (test->contacts, test->self_handle);
  tp_cli_channel_interface_group_call_remove_members_with_reason (test->chan,
      -1, test->contacts, "", TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
      void_cb, test, NULL, NULL);
  g_main_loop_run (test->mainloop);
  test_assert_no_error (test->error);

  /* In response to hanging up, the channel closes */
  test_connection_run_until_dbus_queue_processed (test->conn);
  g_assert (tp_proxy_get_invalidated (test->chan) != NULL);

  /* The last event should be that the peer and the self-handle were both
   * removed */
  ge = g_slist_nth_data (test->group_events, 0);

  g_assert_cmpuint (tp_intset_size (ge->added), ==, 0);
  g_assert_cmpuint (tp_intset_size (ge->removed), ==, 2);
  g_assert (tp_intset_is_member (ge->removed,
        test->self_handle));
  g_assert (tp_intset_is_member (ge->removed,
        tp_channel_get_handle (test->chan, NULL)));
  g_assert_cmpuint (tp_intset_size (ge->local_pending), ==, 0);
  g_assert_cmpuint (tp_intset_size (ge->remote_pending), ==, 0);
  g_assert_cmpuint (tp_asv_get_uint32 (ge->details, "actor", NULL), ==,
      test->self_handle);
  g_assert_cmpuint (tp_asv_get_uint32 (ge->details, "change-reason", NULL), ==,
      TP_CHANNEL_GROUP_CHANGE_REASON_NONE);

  /* The last stream event should be the removal of the audio stream */

  se = g_slist_nth_data (test->stream_events, 0);

  g_assert_cmpuint (se->type, ==, STREAM_EVENT_REMOVED);
  g_assert_cmpuint (se->id, ==, test->audio_stream_id);

  /* FIXME: untested things include:
   *
   * RequestStreamDirection
   * StreamDirectionChanged being emitted correctly (part of RSD)
   * RequestStreamDirection failing (invalid direction, stream ID)
   */
}

static void
test_terminate_via_close (Test *test,
                          gconstpointer data G_GNUC_UNUSED)
{
  GroupEvent *ge;
  StreamEvent *se;

  outgoing_call (test, "basic-test");

  /* request an audio stream */
  tp_cli_channel_type_streamed_media_call_request_streams (test->chan, -1,
      tp_channel_get_handle (test->chan, NULL),
      test->audio_request, requested_streams_cb,
      test, NULL, NULL);
  g_main_loop_run (test->mainloop);
  test_assert_no_error (test->error);

  test_connection_run_until_dbus_queue_processed (test->conn);

  maybe_pop_stream_direction (test);
  g_assert_cmpuint (g_slist_length (test->stream_events), ==, 1);
  se = g_slist_nth_data (test->stream_events, 0);
  g_assert_cmpuint (se->type, ==, STREAM_EVENT_ADDED);
  test->audio_stream_id = se->id;

  /* Wait for the remote contact to answer, if they haven't already */

  while (!tp_intset_is_member (tp_channel_group_get_members (test->chan),
        tp_channel_get_handle (test->chan, NULL)))
    g_main_context_iteration (NULL, TRUE);

  /* Hang up the call unceremoniously, by calling Close */

  tp_cli_channel_call_close (test->chan, -1, void_cb, test, NULL, NULL);
  g_main_loop_run (test->mainloop);
  test_assert_no_error (test->error);

  /* In response to hanging up, the channel closes */
  test_connection_run_until_dbus_queue_processed (test->conn);
  g_assert (tp_proxy_get_invalidated (test->chan) != NULL);

  /* The last event should be that the peer and the self-handle were both
   * removed */
  ge = g_slist_nth_data (test->group_events, 0);

  g_assert_cmpuint (tp_intset_size (ge->added), ==, 0);
  g_assert_cmpuint (tp_intset_size (ge->removed), ==, 2);
  g_assert (tp_intset_is_member (ge->removed,
        test->self_handle));
  g_assert (tp_intset_is_member (ge->removed,
        tp_channel_get_handle (test->chan, NULL)));
  g_assert_cmpuint (tp_intset_size (ge->local_pending), ==, 0);
  g_assert_cmpuint (tp_intset_size (ge->remote_pending), ==, 0);
  g_assert_cmpuint (tp_asv_get_uint32 (ge->details, "actor", NULL), ==,
      test->self_handle);
  g_assert_cmpuint (tp_asv_get_uint32 (ge->details, "change-reason", NULL), ==,
      TP_CHANNEL_GROUP_CHANGE_REASON_NONE);

  /* The last stream event should be the removal of the audio stream */

  se = g_slist_nth_data (test->stream_events, 0);

  g_assert_cmpuint (se->type, ==, STREAM_EVENT_REMOVED);
  g_assert_cmpuint (se->id, ==, test->audio_stream_id);
}

static void
test_terminate_via_no_streams (Test *test,
                               gconstpointer data G_GNUC_UNUSED)
{
  GroupEvent *ge;
  StreamEvent *se;

  outgoing_call (test, "basic-test");

  /* request an audio stream */
  tp_cli_channel_type_streamed_media_call_request_streams (test->chan, -1,
      tp_channel_get_handle (test->chan, NULL),
      test->audio_request, requested_streams_cb,
      test, NULL, NULL);
  g_main_loop_run (test->mainloop);
  test_assert_no_error (test->error);

  test_connection_run_until_dbus_queue_processed (test->conn);

  maybe_pop_stream_direction (test);
  g_assert_cmpuint (g_slist_length (test->stream_events), ==, 1);
  se = g_slist_nth_data (test->stream_events, 0);
  g_assert_cmpuint (se->type, ==, STREAM_EVENT_ADDED);
  test->audio_stream_id = se->id;

  /* Wait for the remote contact to answer, if they haven't already */

  while (!tp_intset_is_member (tp_channel_group_get_members (test->chan),
        tp_channel_get_handle (test->chan, NULL)))
    g_main_context_iteration (NULL, TRUE);

  /* Close the audio stream */

  g_array_set_size (test->stream_ids, 0);
  g_array_append_val (test->stream_ids, test->audio_stream_id);
  tp_cli_channel_type_streamed_media_call_remove_streams (test->chan, -1,
      test->stream_ids,
      void_cb, test, NULL, NULL);
  g_main_loop_run (test->mainloop);
  test_assert_no_error (test->error);

  /* In response to hanging up, the channel closes */
  test_connection_run_until_dbus_queue_processed (test->conn);
  g_assert (tp_proxy_get_invalidated (test->chan) != NULL);

  /* The last event should be that the peer and the self-handle were both
   * removed */
  ge = g_slist_nth_data (test->group_events, 0);

  g_assert_cmpuint (tp_intset_size (ge->added), ==, 0);
  g_assert_cmpuint (tp_intset_size (ge->removed), ==, 2);
  g_assert (tp_intset_is_member (ge->removed,
        test->self_handle));
  g_assert (tp_intset_is_member (ge->removed,
        tp_channel_get_handle (test->chan, NULL)));
  g_assert_cmpuint (tp_intset_size (ge->local_pending), ==, 0);
  g_assert_cmpuint (tp_intset_size (ge->remote_pending), ==, 0);

  /* The last stream event should be the removal of the audio stream */

  se = g_slist_nth_data (test->stream_events, 0);

  g_assert_cmpuint (se->type, ==, STREAM_EVENT_REMOVED);
  g_assert_cmpuint (se->id, ==, test->audio_stream_id);
}

/* FIXME: add a special contact who never accepts the call, so it rings
 * forever, and test that */

/* FIXME: add a special contact who accepts the call, then terminates it */

/* FIXME: add a special contact who rejects the call with BUSY */

/* FIXME: add a special contact who refuses to have video */

/* FIXME: add a special contact who asks us for video */

/* FIXME: add a special contact whose stream errors */

static void
teardown (Test *test,
          gconstpointer data G_GNUC_UNUSED)
{
  tp_cli_connection_run_disconnect (test->conn, -1, &test->error, NULL);
  test_assert_no_error (test->error);

  if (test->members_changed_detailed_id != 0)
    {
      g_signal_handler_disconnect (test->chan,
          test->members_changed_detailed_id);
    }

  g_array_free (test->audio_request, TRUE);
  g_array_free (test->video_request, TRUE);
  g_array_free (test->stream_ids, TRUE);
  g_array_free (test->contacts, TRUE);

  g_slist_foreach (test->group_events, (GFunc) group_event_destroy, NULL);
  g_slist_free (test->group_events);

  g_slist_foreach (test->stream_events, (GFunc) stream_event_destroy, NULL);
  g_slist_free (test->stream_events);

  CLEAR_BOXED (TP_ARRAY_TYPE_MEDIA_STREAM_INFO_LIST,
      &test->list_streams_return);
  CLEAR_BOXED (TP_ARRAY_TYPE_MEDIA_STREAM_INFO_LIST,
      &test->request_streams_return);

  g_hash_table_destroy (test->stream_directions);
  g_hash_table_destroy (test->stream_pending_sends);
  g_hash_table_destroy (test->stream_states);

  CLEAR_OBJECT (&test->chan);
  CLEAR_OBJECT (&test->conn);
  CLEAR_OBJECT (&test->cm);

  CLEAR_OBJECT (&test->service_cm);

  CLEAR_OBJECT (&test->dbus);
  g_main_loop_unref (test->mainloop);
  test->mainloop = NULL;
}

int
main (int argc,
      char **argv)
{
  g_test_init (&argc, &argv, NULL);
  g_test_bug_base ("http://bugs.freedesktop.org/show_bug.cgi?id=");

  g_test_add ("/callable/basics", Test, NULL, setup, test_basics, teardown);
  g_test_add ("/callable/terminate-via-close", Test, NULL, setup,
      test_terminate_via_close, teardown);
  g_test_add ("/callable/terminate-via-no-streams", Test, NULL, setup,
      test_terminate_via_no_streams, teardown);

  return g_test_run ();
}