summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webrtc/RTCPeerConnection-iceGatheringState.html
blob: 32a68953bc59311967c8e59c82cf70cecae3f32e (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
<!doctype html>
<meta charset=utf-8>
<meta name="timeout" content="long">
<title>RTCPeerConnection.prototype.iceGatheringState</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="RTCPeerConnection-helper.js"></script>
<script>
  'use strict';

  // Test is based on the following editor draft:
  // https://w3c.github.io/webrtc-pc/archives/20170605/webrtc.html

  // The following helper functions are called from RTCPeerConnection-helper.js:
  // exchangeAnswer
  // exchangeIceCandidates
  // generateAudioReceiveOnlyOffer

  /*
    4.3.2.  Interface Definition
      interface RTCPeerConnection : EventTarget {
        ...
        readonly  attribute RTCIceGatheringState   iceGatheringState;
                  attribute EventHandler           onicegatheringstatechange;
      };

    4.4.2.  RTCIceGatheringState Enum
      enum RTCIceGatheringState {
        "new",
        "gathering",
        "complete"
      };

    5.6.  RTCIceTransport Interface
      interface RTCIceTransport {
        readonly attribute RTCIceGathererState  gatheringState;
        ...
      };

      enum RTCIceGathererState {
        "new",
        "gathering",
        "complete"
      };
   */

  /*
    4.4.2.  RTCIceGatheringState Enum
      new
        Any of the RTCIceTransport s are in the new gathering state and
        none of the transports are in the gathering state, or there are
        no transports.
   */
  test(t => {
    const pc = new RTCPeerConnection();
    t.add_cleanup(() => pc.close());

    assert_equals(pc.iceGatheringState, 'new');
  }, 'Initial iceGatheringState should be new');

  async_test(t => {
    const pc = new RTCPeerConnection();

    t.add_cleanup(() => pc.close());

    let reachedGathering = false;
    const onIceGatheringStateChange = t.step_func(() => {
      const { iceGatheringState } = pc;

      if(iceGatheringState === 'gathering') {
        reachedGathering = true;
      } else if(iceGatheringState === 'complete') {
        assert_true(reachedGathering, 'iceGatheringState should reach gathering before complete');
        t.done();
      }
    });

    assert_equals(pc.onicegatheringstatechange, null,
      'Expect connection to have icegatheringstatechange event');
    assert_equals(pc.iceGatheringState, 'new');

    pc.addEventListener('icegatheringstatechange', onIceGatheringStateChange);

    generateAudioReceiveOnlyOffer(pc)
    .then(offer => pc.setLocalDescription(offer))
    .then(err => t.step_func(err =>
      assert_unreached(`Unhandled rejection ${err.name}: ${err.message}`)));
  }, 'iceGatheringState should eventually become complete after setLocalDescription');

  promise_test(async t => {
    const pc1 = new RTCPeerConnection();
    t.add_cleanup(() => pc1.close());
    const pc2 = new RTCPeerConnection();
    t.add_cleanup(() => pc2.close());
    pc1.addTransceiver('audio', { direction: 'recvonly' });
    await initialOfferAnswerWithIceGatheringStateTransitions(
        pc1, pc2);

    expectNoMoreGatheringStateChanges(t, pc1);
    expectNoMoreGatheringStateChanges(t, pc2);

    await pc1.setLocalDescription(await pc1.createOffer());
    await pc2.setLocalDescription(await pc2.createOffer());

    await new Promise(r => t.step_timeout(r, 500));
  }, 'setLocalDescription(reoffer) with no new transports should not cause iceGatheringState to change');

  promise_test(async t => {
    const pc1 = new RTCPeerConnection();
    t.add_cleanup(() => pc1.close());

    expectNoMoreGatheringStateChanges(t, pc1);

    await pc1.setLocalDescription(await pc1.createOffer());

    await new Promise(r => t.step_timeout(r, 500));
  }, 'setLocalDescription() with no transports should not cause iceGatheringState to change');

  promise_test(async t => {
    const pc1 = new RTCPeerConnection();
    t.add_cleanup(() => pc1.close());
    pc1.addTransceiver('audio', { direction: 'recvonly' });
    await pc1.setLocalDescription();
    await iceGatheringStateTransitions(pc1, 'gathering', 'complete');
    assert_true(pc1.localDescription.sdp.includes('a=end-of-candidates'));
  }, 'local description should have a=end-of-candidates when gathering completes');

  promise_test(async t => {
    const pc1 = new RTCPeerConnection();
    t.add_cleanup(() => pc1.close());
    const transceiver = pc1.addTransceiver('audio', { direction: 'recvonly' });
    await pc1.setLocalDescription();
    const iceTransport = transceiver.sender.transport.iceTransport;

    // This test code assumes that https://github.com/w3c/webrtc-pc/pull/2894
    // will be merged. The spec will say to dispatch two tasks; one that fires
    // the empty candidate, and another that fires
    // RTCIceTransport.gatheringstatechange, then
    // RTCPeerConnection.icegatheringstatechange, then the global null
    // candidate.
    while (true) {
      const {candidate} = await new Promise(r => pc1.onicecandidate = r);
      assert_not_equals(candidate, null, 'Global null candidate event should not fire yet');
      if (candidate.candidate == '') {
        break;
      }
    }
    assert_equals(iceTransport.gatheringState, 'gathering');
    assert_equals(pc1.iceGatheringState, 'gathering');

    // Now, we test the stuff that happens in the second queued task.
    const events = [];
    await new Promise(r => {
      iceTransport.ongatheringstatechange = () => {
        assert_equals(iceTransport.gatheringState, 'complete');
        assert_equals(pc1.iceGatheringState, 'complete');
        events.push('gatheringstatechange');
      };
      pc1.onicegatheringstatechange = () => {
        assert_equals(iceTransport.gatheringState, 'complete');
        assert_equals(pc1.iceGatheringState, 'complete');
        events.push('icegatheringstatechange');
      }
      pc1.onicecandidate = e => {
        assert_equals(e.candidate, null);
        assert_equals(iceTransport.gatheringState, 'complete');
        assert_equals(pc1.iceGatheringState, 'complete');
        events.push('icecandidate');
        r();
      };
    });

    assert_array_equals(events, [
      'gatheringstatechange',
      'icegatheringstatechange',
      'icecandidate'
    ], 'events must be fired on the same task in this order');
}, 'gathering state and candidate callbacks should fire in the correct order');

  promise_test(async t => {
    const pc1 = new RTCPeerConnection();
    t.add_cleanup(() => pc1.close());
    const pc2 = new RTCPeerConnection();
    t.add_cleanup(() => pc2.close());
    pc1.addTransceiver('audio', { direction: 'recvonly' });
    await initialOfferAnswerWithIceGatheringStateTransitions(
        pc1, pc2);
    await pc1.setLocalDescription(await pc1.createOffer({iceRestart: true}));
    await iceGatheringStateTransitions(pc1, 'gathering', 'complete');
  }, 'setLocalDescription(reoffer) with a restarted transport should cause iceGatheringState to go to "gathering" and then "complete"');

  promise_test(async t => {
    const pc1 = new RTCPeerConnection();
    t.add_cleanup(() => pc1.close());
    const pc2 = new RTCPeerConnection();
    t.add_cleanup(() => pc2.close());
    pc1.addTransceiver('audio', { direction: 'recvonly' });
    pc1.addTransceiver('video', { direction: 'recvonly' });
    exchangeIceCandidates(pc1, pc2);
    await pc1.setLocalDescription();
    const firstGather = Promise.all([
      iceGatheringStateTransitions(pc1, 'gathering', 'complete'),
      iceGatheringStateTransitions(pc2, 'gathering', 'complete')]);
    const mungedOffer = {type: 'offer', sdp: pc1.localDescription.sdp.replace('BUNDLE', 'BUNGLE')};
    await pc2.setRemoteDescription(mungedOffer);
    await pc2.setLocalDescription();
    await pc1.setRemoteDescription(pc2.localDescription);
    // Let gathering finish, so we don't have two generations gathering at once
    // This can cause errors depending on timing.
    await firstGather;
    await pc1.setLocalDescription(await pc1.createOffer({iceRestart: true}));
    // We only do this so we don't get errors in addCandidate. We don't want
    // to wait for it, because we might miss the gathering transitions.
    pc2.setRemoteDescription(pc1.localDescription);
    await iceGatheringStateTransitions(pc1, 'gathering', 'complete');
  }, 'setLocalDescription(reoffer) with two restarted transports should cause iceGatheringState to go to "gathering" and then "complete"');

  promise_test(async t => {
    const pc1 = new RTCPeerConnection();
    t.add_cleanup(() => pc1.close());
    const pc2 = new RTCPeerConnection();
    t.add_cleanup(() => pc2.close());
    expectNoMoreGatheringStateChanges(t, pc2);
    pc1.addTransceiver('audio', { direction: 'recvonly' });
    const offer = await pc1.createOffer();
    await pc2.setRemoteDescription(offer);
    await pc2.setRemoteDescription(offer);
    await pc2.setRemoteDescription({type: 'rollback'});
    await pc2.setRemoteDescription(offer);
  }, 'sRD does not cause ICE gathering state changes');

  promise_test(async t => {
    const pc1 = new RTCPeerConnection();
    t.add_cleanup(() => pc1.close());
    const pc2 = new RTCPeerConnection();
    t.add_cleanup(() => pc2.close());
    pc1.addTransceiver('audio', { direction: 'recvonly' });
    await initialOfferAnswerWithIceGatheringStateTransitions(
        pc1, pc2);

    const pc1waiter = iceGatheringStateTransitions(pc1, 'new');
    const pc2waiter = iceGatheringStateTransitions(pc2, 'new');
    pc1.getTransceivers()[0].stop();
    await pc1.setLocalDescription(await pc1.createOffer());
    await pc2.setRemoteDescription(pc1.localDescription);
    await pc2.setLocalDescription(await pc2.createAnswer());
    assert_equals(pc2.getTransceivers().length, 0,
                 'PC2 transceivers should be invisible after negotiation');
    assert_equals(pc2.iceGatheringState, 'new');
    await pc2waiter;
    await pc1.setRemoteDescription(pc2.localDescription);
    assert_equals(pc1.getTransceivers().length, 0,
                  'PC1 transceivers should be invisible after negotiation');
    assert_equals(pc1.iceGatheringState, 'new');
    await pc1waiter;
  }, 'renegotiation that closes all transports should result in ICE gathering state "new"');

  /*
    4.3.2.  RTCIceGatheringState Enum
      new
        Any of the RTCIceTransports are in the "new" gathering state and none
        of the transports are in the "gathering" state, or there are no
        transports.

      gathering
        Any of the RTCIceTransport s are in the gathering state.

      complete
        At least one RTCIceTransport exists, and all RTCIceTransports are
        in the completed gathering state.

    5.6.  RTCIceGathererState
      gathering
        The RTCIceTransport is in the process of gathering candidates.

      complete
        The RTCIceTransport has completed gathering and the end-of-candidates
        indication for this transport has been sent. It will not gather candidates
        again until an ICE restart causes it to restart.
   */
  promise_test(async t => {
    const pc1 = new RTCPeerConnection();
    t.add_cleanup(() => pc1.close());
    const pc2 = new RTCPeerConnection();

    t.add_cleanup(() => pc2.close());

    const onIceGatheringStateChange = t.step_func(() => {
      const { iceGatheringState } = pc2;

      if(iceGatheringState === 'gathering') {
        const iceTransport = pc2.sctp.transport.iceTransport;

        assert_equals(iceTransport.gatheringState, 'gathering',
          'Expect ICE transport to be in gathering gatheringState when iceGatheringState is gathering');

      } else if(iceGatheringState === 'complete') {
        const iceTransport = pc2.sctp.transport.iceTransport;

        assert_equals(iceTransport.gatheringState, 'complete',
          'Expect ICE transport to be in complete gatheringState when iceGatheringState is complete');

        t.done();
      }
    });

    pc1.createDataChannel('test');

    // Spec bug w3c/webrtc-pc#1382
    // Because sctp is only defined when answer is set, we listen
    // to pc2 so that we can be confident that sctp is defined
    // when icegatheringstatechange event is fired.
    pc2.addEventListener('icegatheringstatechange', onIceGatheringStateChange);


    exchangeIceCandidates(pc1, pc2);

    await pc1.setLocalDescription();
    assert_equals(pc1.sctp.transport.iceTransport.gatheringState, 'new');
    await pc2.setRemoteDescription(pc1.localDescription);

    await exchangeAnswer(pc1, pc2);
  }, 'connection with one data channel should eventually have connected connection state');

  /*
    TODO
    5.6.  RTCIceTransport Interface
      new
        The RTCIceTransport was just created, and has not started gathering
        candidates yet.
   */
</script>