summaryrefslogtreecommitdiff
path: root/tests/twisted/jingle/call-basics.py
blob: 58020a53a163d9853f6c151fb7253ad4da569f02 (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
"""
Test basic outgoing and incoming call handling
"""

import config

if not config.CHANNEL_TYPE_CALL_ENABLED:
    print "NOTE: built with --disable-channel-type-call"
    raise SystemExit(77)

import dbus
from dbus.exceptions import DBusException

from functools import partial
from servicetest import EventPattern, assertEquals, assertContains, assertLength
from call_helper import CallTest, run_call_test
from jingletest2 import test_all_dialects
import constants as cs
import ns

class CallBasicsTest(CallTest):

    def test_connect_disconnect_endpoint(self):
        endpoints = self.audio_stream.Get(cs.CALL_STREAM_IFACE_MEDIA,
                "Endpoints", dbus_interface=dbus.PROPERTIES_IFACE)
        assertLength(1, endpoints)

        endpoint = self.bus.get_object(self.conn.bus_name, endpoints[0])

        # Lets try disconnecting one
        endpoint.SetEndpointState(1, cs.CALL_STREAM_ENDPOINT_STATE_CONNECTING,
                dbus_interface=cs.CALL_STREAM_ENDPOINT)
        ret = self.q.expect_many(
                EventPattern('dbus-signal', signal='EndpointStateChanged'),
                EventPattern('dbus-signal', signal='CallStateChanged'))
        assertEquals(1, ret[0].args[0])
        assertEquals(cs.CALL_STREAM_ENDPOINT_STATE_CONNECTING, ret[0].args[1])
        assertEquals(cs.CALL_STATE_ACCEPTED, ret[1].args[0])

        state = endpoint.Get(cs.CALL_STREAM_ENDPOINT,
                "EndpointState",  dbus_interface=dbus.PROPERTIES_IFACE)
        assertEquals(cs.CALL_STREAM_ENDPOINT_STATE_CONNECTING, state[1])

        # And reconnecting it
        endpoint.SetEndpointState(1,
                cs.CALL_STREAM_ENDPOINT_STATE_FULLY_CONNECTED,
                dbus_interface=cs.CALL_STREAM_ENDPOINT)

        ret = self.q.expect_many(
                EventPattern('dbus-signal', signal='EndpointStateChanged'),
                EventPattern('dbus-signal', signal='CallStateChanged'))
        assertEquals(1,ret[0].args[0])
        assertEquals(cs.CALL_STREAM_ENDPOINT_STATE_FULLY_CONNECTED,
                ret[0].args[1])
        assertEquals(cs.CALL_STATE_ACTIVE, ret[1].args[0])

        state = endpoint.Get(cs.CALL_STREAM_ENDPOINT,
                "EndpointState",  dbus_interface=dbus.PROPERTIES_IFACE)
        assertEquals(cs.CALL_STREAM_ENDPOINT_STATE_FULLY_CONNECTED, state[1])

        # All Direction should still be both now
        stream_props = self.audio_stream.GetAll(cs.CALL_STREAM,
                dbus_interface = dbus.PROPERTIES_IFACE)
        assertEquals({self.peer_handle: cs.CALL_SENDING_STATE_SENDING},
                stream_props["RemoteMembers"])
        assertEquals(cs.CALL_SENDING_STATE_SENDING,
                stream_props["LocalSendingState"])


    def test_content_addition(self):
        path = self.chan.AddContent("Webcam", cs.CALL_MEDIA_TYPE_VIDEO,
                dbus_interface=cs.CHANNEL_TYPE_CALL)
        content = self.bus.get_object(self.conn.bus_name, path)
        content_properties = content.GetAll(cs.CALL_CONTENT,
                dbus_interface=dbus.PROPERTIES_IFACE)

        assertEquals(cs.CALL_DISPOSITION_NONE,
                content_properties["Disposition"])
        #assertEquals(self_handle, content_properties["Creator"])
        assertContains("Webcam", content_properties["Name"])

        md = self.jt2.get_call_video_md_dbus()
        self.check_and_accept_offer(content, md)

        cstream = self.bus.get_object(self.conn.bus_name,
                content_properties["Streams"][0])
        candidates = self.jt2.get_call_remote_transports_dbus()
        cstream.AddCandidates(candidates,
                dbus_interface=cs.CALL_STREAM_IFACE_MEDIA)

        self.q.expect('stream-iq',
                predicate=self.jp.action_predicate('content-add'))

        content.Remove(dbus_interface=cs.CALL_CONTENT)
        self.q.expect('stream-iq',
                predicate=self.jp.action_predicate('content-remove'))


    def pickup(self):
        jt2 = self.jt2
        jp = self.jp
        q = self.q
        cstream = self.audio_stream
        remote_handle = self.peer_handle
        can_change_direction = self.can_change_direction
        incoming = self.incoming

        # We pickup the call as we need active state to run this test
        CallTest.pickup(self)

        self.test_connect_disconnect_endpoint()

        # FIXME: This should eventually be break down in smaller test methods

        # Turn sending off and on again

        # but first, let's try direction changes requested by the other side

        if can_change_direction:
            content_name = jt2.audio_names[0]
            if incoming:
                jt2.content_modify(content_name, "initiator", "initiator")
            else:
                jt2.content_modify(content_name, "initiator", "responder")
            o = q.expect('dbus-signal', signal='LocalSendingStateChanged')
            assertEquals(cs.CALL_SENDING_STATE_PENDING_STOP_SENDING, o.args[0])

            cstream.SetSending(False,
                    dbus_interface = cs.CALL_STREAM)
            o = q.expect('dbus-signal', signal='SendingStateChanged')
            assertEquals(cs.CALL_STREAM_FLOW_STATE_PENDING_STOP, o.args[0])

            cstream.CompleteSendingStateChange(
                    cs.CALL_STREAM_FLOW_STATE_STOPPED,
                    dbus_interface = cs.CALL_STREAM_IFACE_MEDIA)
            o = q.expect('dbus-signal', signal='SendingStateChanged',
                    interface = cs.CALL_STREAM_IFACE_MEDIA)
            assertEquals(cs.CALL_STREAM_FLOW_STATE_STOPPED, o.args[0])

            jt2.content_modify(content_name, "initiator", "both")
            o = q.expect('dbus-signal', signal='LocalSendingStateChanged')
            assertEquals(cs.CALL_SENDING_STATE_PENDING_SEND, o.args[0])

            cstream.SetSending(True,
                    dbus_interface = cs.CALL_STREAM)

            ret = q.expect_many(
                    EventPattern('dbus-signal', signal='SendingStateChanged'),
                    EventPattern('dbus-signal',
                        signal='LocalSendingStateChanged'))
            assertEquals(cs.CALL_STREAM_FLOW_STATE_PENDING_START,
                    ret[0].args[0])
            assertEquals(cs.CALL_SENDING_STATE_SENDING, ret[1].args[0])

            cstream.CompleteSendingStateChange(
                    cs.CALL_STREAM_FLOW_STATE_STARTED,
                    dbus_interface = cs.CALL_STREAM_IFACE_MEDIA)
            o = q.expect('dbus-signal', signal='SendingStateChanged',
                    interface = cs.CALL_STREAM_IFACE_MEDIA)
            assertEquals(cs.CALL_STREAM_FLOW_STATE_STARTED, o.args[0])

            stream_props = cstream.GetAll(cs.CALL_STREAM,
                    dbus_interface = dbus.PROPERTIES_IFACE)
            assertEquals({remote_handle: cs.CALL_SENDING_STATE_SENDING},
                    stream_props["RemoteMembers"])
            assertEquals(cs.CALL_SENDING_STATE_SENDING,
                    stream_props["LocalSendingState"])

        cstream.SetSending(False,
                dbus_interface = cs.CALL_STREAM)
        ret = q.expect_many(
                EventPattern('dbus-signal', signal='SendingStateChanged'),
                EventPattern('dbus-signal', signal='LocalSendingStateChanged'))
        assertEquals(cs.CALL_STREAM_FLOW_STATE_PENDING_STOP, ret[0].args[0])
        assertEquals(cs.CALL_SENDING_STATE_NONE, ret[1].args[0])

        cstream.CompleteSendingStateChange(
                cs.CALL_STREAM_FLOW_STATE_STOPPED,
                dbus_interface = cs.CALL_STREAM_IFACE_MEDIA)
        o = q.expect('dbus-signal', signal='SendingStateChanged',
                interface = cs.CALL_STREAM_IFACE_MEDIA)
        assertEquals(cs.CALL_STREAM_FLOW_STATE_STOPPED, o.args[0])

        stream_props = cstream.GetAll(cs.CALL_STREAM,
                dbus_interface = dbus.PROPERTIES_IFACE)
        assertEquals({remote_handle: cs.CALL_SENDING_STATE_SENDING},
                stream_props["RemoteMembers"])
        assertEquals(cs.CALL_SENDING_STATE_NONE,
                stream_props["LocalSendingState"])

        # If possible, test the other side asking us to start then stop sending

        if can_change_direction:
            jt2.content_modify(content_name, "initiator", "both")
            o = q.expect('dbus-signal', signal='LocalSendingStateChanged')
            assertEquals(cs.CALL_SENDING_STATE_PENDING_SEND, o.args[0])

            if incoming:
                jt2.content_modify(content_name, "initiator", "initiator")
            else:
                jt2.content_modify(content_name, "initiator", "responder")
            o = q.expect('dbus-signal', signal='LocalSendingStateChanged')
            assertEquals(cs.CALL_SENDING_STATE_NONE, o.args[0])

            jt2.content_modify(content_name, "initiator", "both")
            o = q.expect('dbus-signal', signal='LocalSendingStateChanged')
            assertEquals(cs.CALL_SENDING_STATE_PENDING_SEND, o.args[0])


        cstream.SetSending(True, dbus_interface = cs.CALL_STREAM)

        ret = q.expect_many(
                EventPattern('dbus-signal', signal='SendingStateChanged'),
                EventPattern('dbus-signal', signal='LocalSendingStateChanged'))
        assertEquals(cs.CALL_STREAM_FLOW_STATE_PENDING_START, ret[0].args[0])
        assertEquals(cs.CALL_SENDING_STATE_SENDING, ret[1].args[0])

        cstream.CompleteSendingStateChange(
                cs.CALL_STREAM_FLOW_STATE_STARTED,
                dbus_interface = cs.CALL_STREAM_IFACE_MEDIA)

        stream_props = cstream.GetAll(cs.CALL_STREAM,
                dbus_interface = dbus.PROPERTIES_IFACE)
        assertEquals({remote_handle: cs.CALL_SENDING_STATE_SENDING},
                stream_props["RemoteMembers"])
        assertEquals(cs.CALL_SENDING_STATE_SENDING,
                stream_props["LocalSendingState"])

        # Turn receiving off and on again

        try:
            cstream.RequestReceiving(remote_handle, False,
                    dbus_interface = cs.CALL_STREAM)
            assert can_change_direction
        except dbus.DBusException, e:
            assertEquals(cs.NOT_CAPABLE, e.get_dbus_name())
            assert not can_change_direction

        if can_change_direction:
            ret = q.expect_many(
                    EventPattern('dbus-signal', signal='ReceivingStateChanged'),
                    EventPattern('dbus-signal', signal='RemoteMembersChanged'),
                    EventPattern('stream-iq',
                        predicate=jp.action_predicate('content-modify')))
            assertEquals(cs.CALL_STREAM_FLOW_STATE_PENDING_STOP, ret[0].args[0])
            assert ret[1].args[0].has_key(remote_handle)

            cstream.CompleteReceivingStateChange(
                    cs.CALL_STREAM_FLOW_STATE_STOPPED,
                    dbus_interface = cs.CALL_STREAM_IFACE_MEDIA)
            o = q.expect('dbus-signal', signal='ReceivingStateChanged',
                    interface = cs.CALL_STREAM_IFACE_MEDIA)
            assertEquals(cs.CALL_STREAM_FLOW_STATE_STOPPED, o.args[0])

            stream_props = cstream.GetAll(cs.CALL_STREAM,
                    dbus_interface = dbus.PROPERTIES_IFACE)
            assertEquals({remote_handle: cs.CALL_SENDING_STATE_NONE},
                    stream_props["RemoteMembers"])
            assertEquals(cs.CALL_SENDING_STATE_SENDING,
                    stream_props["LocalSendingState"])
        else:
            stream_props = cstream.GetAll(cs.CALL_STREAM,
                    dbus_interface = dbus.PROPERTIES_IFACE)
            assertEquals({remote_handle: cs.CALL_SENDING_STATE_SENDING},
                    stream_props["RemoteMembers"])
            assertEquals(cs.CALL_SENDING_STATE_SENDING,
                    stream_props["LocalSendingState"])

        try:
            cstream.RequestReceiving(remote_handle, True,
                    dbus_interface = cs.CALL_STREAM)
            assert can_change_direction
        except dbus.DBusException, e:
            assertEquals(cs.NOT_CAPABLE, e.get_dbus_name())
            assert not can_change_direction

        if can_change_direction:
            ret = q.expect_many(
                    EventPattern('dbus-signal', signal='ReceivingStateChanged'),
                    EventPattern('dbus-signal', signal='RemoteMembersChanged'),
                    EventPattern('stream-iq',
                        predicate=jp.action_predicate('content-modify')))
            assertEquals(cs.CALL_STREAM_FLOW_STATE_PENDING_START,
                    ret[0].args[0])
            assert ret[1].args[0].has_key(remote_handle)
            assertEquals(cs.CALL_SENDING_STATE_PENDING_SEND,
                    ret[1].args[0][remote_handle])

            cstream.CompleteReceivingStateChange(
                    cs.CALL_STREAM_FLOW_STATE_STARTED,
                    dbus_interface = cs.CALL_STREAM_IFACE_MEDIA)
            ret = q.expect_many(
                    EventPattern('dbus-signal', signal='ReceivingStateChanged'),
                    EventPattern('dbus-signal', signal='RemoteMembersChanged'))
            assertEquals(cs.CALL_STREAM_FLOW_STATE_STARTED, ret[0].args[0])
            assert ret[1].args[0].has_key(remote_handle)
            assertEquals(cs.CALL_SENDING_STATE_SENDING,
                    ret[1].args[0][remote_handle])

        stream_props = cstream.GetAll(cs.CALL_STREAM,
                dbus_interface = dbus.PROPERTIES_IFACE)
        assertEquals({remote_handle: cs.CALL_SENDING_STATE_SENDING},
                stream_props["RemoteMembers"])
        assertEquals(cs.CALL_SENDING_STATE_SENDING,
                stream_props["LocalSendingState"])

        if can_change_direction:
            if incoming:
                jt2.content_modify(content_name, "initiator", "responder")
            else:
                jt2.content_modify(content_name, "initiator", "initiator")

            ret = q.expect_many(
                    EventPattern('dbus-signal', signal='ReceivingStateChanged'),
                    EventPattern('dbus-signal', signal='RemoteMembersChanged'))
            assertEquals(cs.CALL_STREAM_FLOW_STATE_PENDING_STOP, ret[0].args[0])
            assert ret[1].args[0].has_key(remote_handle)
            assertEquals(cs.CALL_SENDING_STATE_NONE,
                    ret[1].args[0][remote_handle])

            cstream.CompleteReceivingStateChange(
                    cs.CALL_STREAM_FLOW_STATE_STOPPED,
                    dbus_interface = cs.CALL_STREAM_IFACE_MEDIA)
            o = q.expect('dbus-signal', signal='ReceivingStateChanged')
            assertEquals(cs.CALL_STREAM_FLOW_STATE_STOPPED, o.args[0])

            jt2.content_modify(content_name, "initiator", "both")
            ret = q.expect_many(
                    EventPattern('dbus-signal', signal='ReceivingStateChanged'),
                    EventPattern('dbus-signal', signal='RemoteMembersChanged'))
            assertEquals(cs.CALL_STREAM_FLOW_STATE_PENDING_START,
                    ret[0].args[0])
            assert ret[1].args[0].has_key(remote_handle)
            assertEquals(cs.CALL_SENDING_STATE_SENDING,
                    ret[1].args[0][remote_handle])

            cstream.CompleteReceivingStateChange(
                    cs.CALL_STREAM_FLOW_STATE_STARTED,
                    dbus_interface = cs.CALL_STREAM_IFACE_MEDIA)
            o = q.expect('dbus-signal', signal='ReceivingStateChanged')
            assertEquals(cs.CALL_STREAM_FLOW_STATE_STARTED, o.args[0])

        try:
            self.test_content_addition()
        except DBusException, e:
            assertEquals(cs.NOT_AVAILABLE, e.get_dbus_name())
            assert not jp.can_do_video()


if __name__ == '__main__':
    test_all_dialects(
            partial(run_call_test, klass=CallBasicsTest, incoming=True))
    test_all_dialects(
            partial(run_call_test, klass=CallBasicsTest, incoming=False))