Skip to content

Commit

Permalink
addIceCandidate shim: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fippo committed Feb 14, 2021
1 parent d02dbe1 commit 8e6216c
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/js/adapter_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function adapterFactory({window} = {}, options = {
adapter.browserShim = chromeShim;

// Must be called before shimPeerConnection.
commonShim.shimAddIceCandidateNullOrEmpty(window);
commonShim.shimAddIceCandidateNullOrEmpty(window, browserDetails);

chromeShim.shimGetUserMedia(window);
chromeShim.shimMediaStream(window);
Expand Down Expand Up @@ -79,7 +79,7 @@ export function adapterFactory({window} = {}, options = {
adapter.browserShim = firefoxShim;

// Must be called before shimPeerConnection.
commonShim.shimAddIceCandidateNullOrEmpty(window);
commonShim.shimAddIceCandidateNullOrEmpty(window, browserDetails);

firefoxShim.shimGetUserMedia(window);
firefoxShim.shimPeerConnection(window);
Expand Down Expand Up @@ -127,7 +127,7 @@ export function adapterFactory({window} = {}, options = {
adapter.browserShim = safariShim;

// Must be called before shimCallbackAPI.
commonShim.shimAddIceCandidateNullOrEmpty(window);
commonShim.shimAddIceCandidateNullOrEmpty(window, browserDetails);

safariShim.shimRTCIceServerUrls(window);
safariShim.shimCreateOfferLegacy(window);
Expand Down
3 changes: 1 addition & 2 deletions src/js/common_shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,11 @@ export function removeAllowExtmapMixed(window) {
};
}

export function shimAddIceCandidateNullOrEmpty(window) {
export function shimAddIceCandidateNullOrEmpty(window, browserDetails) {
// Support for addIceCandidate(null or undefined)
// as well as addIceCandidate({candidate: "", ...})
// https://bugs.chromium.org/p/chromium/issues/detail?id=978582
// Note: must be called before other polyfills which change the signature.
const browserDetails = utils.detectBrowser(window);
if (!(window.RTCPeerConnection && window.RTCPeerConnection.prototype)) {
return;
}
Expand Down
134 changes: 134 additions & 0 deletions test/unit/addicecandidate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright (c) 2021 The adapter.js project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree.
*/
/* eslint-env node */
const chai = require('chai');
const expect = chai.expect;
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
chai.use(sinonChai);

describe('addIceCandidate with null or empty candidate', () => {
const shim = require('../../dist/common_shim');
let window;
let origAddIceCandidate;
beforeEach(() => {
window = {
RTCPeerConnection: sinon.stub(),
};
origAddIceCandidate = sinon.stub();
window.RTCPeerConnection.prototype.addIceCandidate = origAddIceCandidate;
});

describe('does nothing if', () => {
it('RTCPeerConnection is not defined', () => {
expect(() => shim.shimAddIceCandidateNullOrEmpty({}, {})).not.to.throw();
});
it('RTCPeerConnection.prototype.addIceCandidate is undefined', () => {
window.RTCPeerConnection.prototype.addIceCandidate = null;
expect(() => shim.shimAddIceCandidateNullOrEmpty({}, {})).not.to.throw();
});
it('the candidate argument is optional', () => {
expect(window.RTCPeerConnection.prototype.addIceCandidate.length)
.to.equal(0);
shim.shimAddIceCandidateNullOrEmpty({}, {});
expect(window.RTCPeerConnection.prototype.addIceCandidate)
.to.equal(origAddIceCandidate);
});
});

it('changes the number of arguments', () => {
window.RTCPeerConnection.prototype.addIceCandidate =
(candidate) => origAddIceCandidate(candidate);
shim.shimAddIceCandidateNullOrEmpty(window, {});
expect(window.RTCPeerConnection.prototype.addIceCandidate.length)
.to.equal(0);
});

it('ignores addIceCandidate(null)', () => {
window.RTCPeerConnection.prototype.addIceCandidate =
(candidate) => origAddIceCandidate(candidate);
shim.shimAddIceCandidateNullOrEmpty(window, {});
const pc = new window.RTCPeerConnection();
pc.addIceCandidate({candidate: '', sdpMLineIndex: 0});
expect(origAddIceCandidate.callCount).to.equal(1);
});

describe('Chrome behaviour', () => {
let browserDetails;
// Override addIceCandidate to simulate legacy behaviour.
beforeEach(() => {
window.RTCPeerConnection.prototype.addIceCandidate =
(candidate) => origAddIceCandidate(candidate);
browserDetails = {browser: 'chrome', version: '88'};
});

it('ignores {candidate: ""} before Chrome 78', () => {
browserDetails.version = 77;
shim.shimAddIceCandidateNullOrEmpty(window, browserDetails);

const pc = new window.RTCPeerConnection();
pc.addIceCandidate({candidate: '', sdpMLineIndex: 0});
expect(origAddIceCandidate.callCount).to.equal(0);
});

it('passes {candidate: ""} after Chrome 78', () => {
browserDetails.version = 78;
shim.shimAddIceCandidateNullOrEmpty(window, browserDetails);

const pc = new window.RTCPeerConnection();
pc.addIceCandidate({candidate: '', sdpMLineIndex: 0});
expect(origAddIceCandidate.callCount).to.equal(1);
});
});

describe('Firefox behaviour', () => {
let browserDetails;
// Override addIceCandidate to simulate legacy behaviour.
beforeEach(() => {
window.RTCPeerConnection.prototype.addIceCandidate =
(candidate) => origAddIceCandidate(candidate);
browserDetails = {browser: 'firefox', version: '69'};
});

it('ignores {candidate: ""} before Firefox 68', () => {
browserDetails.version = 67;
shim.shimAddIceCandidateNullOrEmpty(window, browserDetails);

const pc = new window.RTCPeerConnection();
pc.addIceCandidate({candidate: '', sdpMLineIndex: 0});
expect(origAddIceCandidate.callCount).to.equal(0);
});

it('passes {candidate: ""} after Firefox 68', () => {
browserDetails.version = 68;
shim.shimAddIceCandidateNullOrEmpty(window, browserDetails);

const pc = new window.RTCPeerConnection();
pc.addIceCandidate({candidate: '', sdpMLineIndex: 0});
expect(origAddIceCandidate.callCount).to.equal(1);
});
});

describe('Safari behaviour', () => {
let browserDetails;
// Override addIceCandidate to simulate legacy behaviour.
beforeEach(() => {
window.RTCPeerConnection.prototype.addIceCandidate =
(candidate) => origAddIceCandidate(candidate);
browserDetails = {browser: 'safari', version: 'some'};
});

it('ignores {candidate: ""}', () => {
shim.shimAddIceCandidateNullOrEmpty(window, browserDetails);

const pc = new window.RTCPeerConnection();
pc.addIceCandidate({candidate: '', sdpMLineIndex: 0});
expect(origAddIceCandidate.callCount).to.equal(0);
});
});
});

0 comments on commit 8e6216c

Please sign in to comment.