| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 1 | <!doctype html> |
| 2 | <meta charset=utf-8> |
| 3 | <title>RTCPeerConnection.prototype.setLocalDescription</title> |
| 4 | <script src="/resources/testharness.js"></script> |
| 5 | <script src="/resources/testharnessreport.js"></script> |
| 6 | <script src="RTCPeerConnection-helper.js"></script> |
| 7 | <script> |
| 8 | 'use strict'; |
| 9 | |
| 10 | // Test is based on the following editor draft: |
| Soares Chen | a01b1e5 | 2017-07-07 09:52:03 | [diff] [blame] | 11 | // https://w3c.github.io/webrtc-pc/archives/20170605/webrtc.html |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 12 | |
| Rick Waldron | 1c5c05d | 2017-06-07 17:54:42 | [diff] [blame] | 13 | // The following helper functions are called from RTCPeerConnection-helper.js: |
| youennf | cd17e3b | 2018-11-04 08:03:59 | [diff] [blame] | 14 | // generateDataChannelOffer |
| Jan-Ivar Bruaroey | 6d3fda5 | 2018-07-04 13:49:09 | [diff] [blame] | 15 | // assert_session_desc_not_similar |
| 16 | // assert_session_desc_similar |
| Rick Waldron | 1c5c05d | 2017-06-07 17:54:42 | [diff] [blame] | 17 | |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 18 | /* |
| Soares Chen | a01b1e5 | 2017-07-07 09:52:03 | [diff] [blame] | 19 | 4.3.2. Interface Definition |
| 20 | [Constructor(optional RTCConfiguration configuration)] |
| 21 | interface RTCPeerConnection : EventTarget { |
| 22 | Promise<void> setRemoteDescription( |
| 23 | RTCSessionDescriptionInit description); |
| 24 | |
| 25 | readonly attribute RTCSessionDescription? remoteDescription; |
| 26 | readonly attribute RTCSessionDescription? currentRemoteDescription; |
| 27 | readonly attribute RTCSessionDescription? pendingRemoteDescription; |
| 28 | ... |
| 29 | }; |
| 30 | |
| 31 | 4.6.2. RTCSessionDescription Class |
| 32 | dictionary RTCSessionDescriptionInit { |
| 33 | required RTCSdpType type; |
| 34 | DOMString sdp = ""; |
| 35 | }; |
| 36 | |
| 37 | 4.6.1. RTCSdpType |
| 38 | enum RTCSdpType { |
| 39 | "offer", |
| 40 | "pranswer", |
| 41 | "answer", |
| 42 | "rollback" |
| 43 | }; |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 44 | */ |
| 45 | |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 46 | promise_test(t => { |
| 47 | const pc = new RTCPeerConnection(); |
| Philipp Hancke | 1622a02 | 2018-06-11 10:00:53 | [diff] [blame] | 48 | t.add_cleanup(() => pc.close()); |
| Philipp Hancke | eb37a99 | 2018-05-30 15:55:02 | [diff] [blame] | 49 | |
| 50 | const states = []; |
| 51 | pc.addEventListener('signalingstatechange', () => states.push(pc.signalingState)); |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 52 | |
| Youenn Fablet | 7b16358 | 2018-11-04 07:30:59 | [diff] [blame] | 53 | return generateAudioReceiveOnlyOffer(pc) |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 54 | .then(offer1 => |
| 55 | pc.setLocalDescription(offer1) |
| 56 | .then(() => generateAnswer(offer1)) |
| 57 | .then(answer => pc.setRemoteDescription(answer)) |
| 58 | .then(() => { |
| 59 | pc.createDataChannel('test'); |
| Youenn Fablet | 7b16358 | 2018-11-04 07:30:59 | [diff] [blame] | 60 | return generateVideoReceiveOnlyOffer(pc); |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 61 | }) |
| 62 | .then(offer2 => |
| 63 | pc.setLocalDescription(offer2) |
| 64 | .then(() => { |
| 65 | assert_equals(pc.signalingState, 'have-local-offer'); |
| Jan-Ivar Bruaroey | 6d3fda5 | 2018-07-04 13:49:09 | [diff] [blame] | 66 | assert_session_desc_not_similar(offer1, offer2); |
| 67 | assert_session_desc_similar(pc.localDescription, offer2); |
| 68 | assert_session_desc_similar(pc.currentLocalDescription, offer1); |
| 69 | assert_session_desc_similar(pc.pendingLocalDescription, offer2); |
| Raphael Kubo da Costa | a204983 | 2018-07-05 16:39:56 | [diff] [blame] | 70 | |
| 71 | assert_array_equals(states, ['have-local-offer', 'stable', 'have-local-offer']); |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 72 | }))); |
| 73 | }, 'Calling createOffer() and setLocalDescription() again after one round of local-offer/remote-answer should succeed'); |
| 74 | |
| Byron Campen [:bwc] | f2d6b2d | 2019-03-11 13:12:28 | [diff] [blame] | 75 | promise_test(async t => { |
| 76 | const pc1 = new RTCPeerConnection(); |
| 77 | t.add_cleanup(() => pc1.close()); |
| 78 | |
| 79 | const pc2 = new RTCPeerConnection(); |
| 80 | t.add_cleanup(() => pc2.close()); |
| Philipp Hancke | eb37a99 | 2018-05-30 15:55:02 | [diff] [blame] | 81 | |
| 82 | const states = []; |
| Byron Campen [:bwc] | f2d6b2d | 2019-03-11 13:12:28 | [diff] [blame] | 83 | pc1.addEventListener('signalingstatechange', () => states.push(pc1.signalingState)); |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 84 | |
| Byron Campen [:bwc] | f2d6b2d | 2019-03-11 13:12:28 | [diff] [blame] | 85 | assert_equals(pc1.localDescription, null); |
| 86 | assert_equals(pc1.currentLocalDescription, null); |
| 87 | assert_equals(pc1.pendingLocalDescription, null); |
| Philipp Hancke | eb37a99 | 2018-05-30 15:55:02 | [diff] [blame] | 88 | |
| Byron Campen [:bwc] | f2d6b2d | 2019-03-11 13:12:28 | [diff] [blame] | 89 | pc1.createDataChannel('test'); |
| 90 | const offer = await pc1.createOffer(); |
| 91 | |
| 92 | assert_equals(pc1.localDescription, null); |
| 93 | assert_equals(pc1.currentLocalDescription, null); |
| 94 | assert_equals(pc1.pendingLocalDescription, null); |
| 95 | |
| 96 | await pc1.setLocalDescription(offer); |
| 97 | |
| 98 | assert_session_desc_similar(pc1.localDescription, offer); |
| 99 | assert_equals(pc1.currentLocalDescription, null); |
| 100 | assert_session_desc_similar(pc1.pendingLocalDescription, offer); |
| 101 | |
| 102 | await pc2.setRemoteDescription(offer); |
| 103 | const answer = await pc2.createAnswer(); |
| 104 | await pc2.setLocalDescription(answer); |
| 105 | await pc1.setRemoteDescription(answer); |
| 106 | |
| 107 | assert_equals(pc1.signalingState, 'stable'); |
| 108 | assert_session_desc_similar(pc1.localDescription, offer); |
| 109 | assert_session_desc_similar(pc1.currentLocalDescription, offer); |
| 110 | assert_equals(pc1.pendingLocalDescription, null); |
| 111 | |
| 112 | const stream = await getNoiseStream({audio:true}); |
| 113 | pc2.addTrack(stream.getTracks()[0], stream); |
| 114 | |
| 115 | const reoffer = await pc2.createOffer(); |
| 116 | await pc2.setLocalDescription(reoffer); |
| 117 | await pc1.setRemoteDescription(reoffer); |
| 118 | const reanswer = await pc1.createAnswer(); |
| 119 | await pc1.setLocalDescription(reanswer); |
| 120 | |
| 121 | assert_session_desc_similar(pc1.localDescription, reanswer); |
| 122 | assert_session_desc_similar(pc1.currentLocalDescription, reanswer); |
| 123 | assert_equals(pc1.pendingLocalDescription, null); |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 124 | }, 'Switching role from answerer to offerer after going back to stable state should succeed'); |
| 125 | |
| Henrik Boström | 3d14e69 | 2018-06-07 16:45:55 | [diff] [blame] | 126 | promise_test(async t => { |
| 127 | const pc = new RTCPeerConnection(); |
| 128 | const offer = await pc.createOffer(); |
| 129 | let eventSequence = ''; |
| 130 | const signalingstatechangeResolver = new Resolver(); |
| 131 | pc.onsignalingstatechange = () => { |
| 132 | eventSequence += 'onsignalingstatechange;'; |
| 133 | signalingstatechangeResolver.resolve(); |
| 134 | }; |
| 135 | await pc.setLocalDescription(offer); |
| 136 | eventSequence += 'setLocalDescription;'; |
| Youenn Fablet | d77afea | 2018-12-20 22:13:46 | [diff] [blame] | 137 | await signalingstatechangeResolver; |
| Henrik Boström | 3d14e69 | 2018-06-07 16:45:55 | [diff] [blame] | 138 | assert_equals(eventSequence, 'onsignalingstatechange;setLocalDescription;'); |
| 139 | }, 'onsignalingstatechange fires before setLocalDescription resolves'); |
| 140 | |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 141 | /* |
| Soares Chen | a01b1e5 | 2017-07-07 09:52:03 | [diff] [blame] | 142 | TODO |
| Soares Chen | a158fe1 | 2017-07-10 04:58:34 | [diff] [blame] | 143 | 4.3.2. setLocalDescription |
| Soares Chen | a01b1e5 | 2017-07-07 09:52:03 | [diff] [blame] | 144 | 4. If description.sdp is null and description.type is pranswer, set description.sdp |
| 145 | to lastAnswer. |
| 146 | 7. If description.type is pranswer and description.sdp does not match lastAnswer, |
| 147 | reject the promise with a newly created InvalidModificationError and abort these |
| 148 | steps. |
| 149 | */ |
| 150 | |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 151 | </script> |