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

// This test may be flaky, so it's in its own file.
// The test belongs in RTCPeerConnection-addIceCandidate.

promise_test(async t => {
  const pc1 = new RTCPeerConnection();
  const pc2 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  t.add_cleanup(() => pc2.close());
  const transceiver = pc1.addTransceiver('video');

  exchangeIceCandidates(pc1, pc2);
  await exchangeOffer(pc1, pc2);
  const answer = await pc2.createAnswer();
  // Note that sequence of the following two calls is critical
  // for test stability.
  await pc1.setRemoteDescription(answer);
  await pc2.setLocalDescription(answer);
  await waitForState(transceiver.sender.transport, 'connected');
}, 'Candidates are added dynamically; connection should work');

promise_test(async t => {
  const pc1 = new RTCPeerConnection();
  const pc2 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  t.add_cleanup(() => pc2.close());
  const transceiver = pc1.addTransceiver('video');

  let candidates1to2 = [];
  let candidates2to1 = [];
  pc1.onicecandidate = e => candidates1to2.push(e.candidate);
  pc2.onicecandidate = e => candidates2to1.push(e.candidate);
  const pc2GatheredCandidates = new Promise((resolve) => {
    pc2.addEventListener('icegatheringstatechange', () => {
      if (pc2.iceGatheringState == 'complete') {
        resolve();
      }
    });
  });
  await exchangeOffer(pc1, pc2);
  let answer = await pc2.createAnswer();
  await pc1.setRemoteDescription(answer);
  await pc2.setLocalDescription(answer);
  await pc2GatheredCandidates;
  // Add candidates to pc1, ensuring that it goes to "connecting" state before "connected".
  // We do not iterate/await because repeatedly awaiting while we serially add
  // the candidates opens the opportunity to miss the 'connecting' transition.
  const addCandidatesDone = Promise.all(candidates2to1.map(c => pc1.addIceCandidate(c)));
  await waitForState(transceiver.sender.transport, 'connecting');
  await addCandidatesDone;
  await waitForState(transceiver.sender.transport, 'connected');
}, 'Candidates are added at PC1; connection should work');

promise_test(async t => {
  const pc1 = new RTCPeerConnection();
  const pc2 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  t.add_cleanup(() => pc2.close());
  const transceiver = pc1.addTransceiver('video');

  let candidates1to2 = [];
  let candidates2to1 = [];
  pc1.onicecandidate = e => candidates1to2.push(e.candidate);
  pc2.onicecandidate = e => candidates2to1.push(e.candidate);
  const pc1GatheredCandidates = new Promise((resolve) => {
    pc1.addEventListener('icegatheringstatechange', () => {
      if (pc1.iceGatheringState == 'complete') {
        resolve();
      }
    });
  });
  await exchangeOffer(pc1, pc2);
  let answer = await pc2.createAnswer();
  await pc1.setRemoteDescription(answer);
  await pc2.setLocalDescription(answer);
  await pc1GatheredCandidates;
  // Add candidates to pc2
  // We do not iterate/await because repeatedly awaiting while we serially add
  // the candidates opens the opportunity to miss the ICE state transitions.
  await Promise.all(candidates1to2.map(c => pc2.addIceCandidate(c)));
  await waitForState(transceiver.sender.transport, 'connected');
}, 'Candidates are added at PC2; connection should work');

</script>