Skip to content

Commit

Permalink
Merge pull request #335 from ooyala/PLAYER-4216c
Browse files Browse the repository at this point in the history
[PLAYER-4216] New unit tests for CC refactor
  • Loading branch information
aeng7 authored Sep 14, 2018
2 parents c891805 + b783da4 commit 24d0024
Show file tree
Hide file tree
Showing 6 changed files with 837 additions and 26 deletions.
19 changes: 16 additions & 3 deletions tests/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ global._ = OO._;
// In a browser environment, all of the properties of "window" (like navigator) are in the global scope:
OO._.extend(global, window);


OO.CONSTANTS = {
CLOSED_CAPTIONS: {
SHOWING: "showing",
HIDDEN: "hidden",
DISABLED: "disabled"
},
SEEK_TO_END_LIMIT: 3
};

require.requireActual("../html5-common/js/utils/InitModules/InitOOHazmat.js");

jest.dontMock('./utils/mock_vtc.js');
Expand Down Expand Up @@ -85,12 +95,15 @@ jest.mock('../src/main/js/text_track/text_track_helper', () => {
this.video.textTracks.push({
id: trackData.id,
language: trackData.srclang,
// Note that the implementation initially sets the label to track id in
// order to be able to recognize the TextTrack object on the addtrack event
label: trackData.id,
label: trackData.label,
kind: trackData.kind,
mode: 'disabled'
});
// Trigger add track handler in order to fully simulate
// browser behavior
if (this.video.textTracks.onaddtrack) {
this.video.textTracks.onaddtrack();
}
}
});

Expand Down
231 changes: 231 additions & 0 deletions tests/unit/main_html5/text_track/text-track-helper-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
import TextTrackHelper from "../../../../src/main/js/text_track/text_track_helper";
import TextTrackMap from "../../../../src/main/js/text_track/text_track_map";

describe('TextTrackHelper', function() {
let textTrackHelper, textTrackMap;

const getDummyTrackData = (overrides = {}) => {
return {
id: overrides.id || `id-${Date.now()}`,
kind: overrides.kind || 'kind',
label: overrides.label || 'label',
srclang: overrides.srclang || 'srclang',
src: overrides.src || 'src'
};
};

const mapTracks = (isExternal = false) => {
for (let textTrack of textTrackHelper.video.textTracks) {
const hasBeenMaped = textTrackMap.existsEntry({
textTrack: textTrack
});

if (!hasBeenMaped) {
textTrackMap.addEntry({
label: textTrack.label,
language: textTrack.language,
mode: textTrack.mode,
textTrack: textTrack
}, isExternal);
}
}
};

beforeEach(function() {
textTrackHelper = new TextTrackHelper(document.createElement('video'));
textTrackMap = new TextTrackMap();
});

it('should create a TextTrackHelper instance', function() {
expect(textTrackHelper).to.be.ok();
});

describe('addTrack', function() {

it('should create a track element with the specified attributes', function() {
textTrackHelper.addTrack({
id: 'id',
kind: 'kind',
label: 'label',
srclang: 'srclang',
src: 'src'
});
const trackElement = textTrackHelper.video.querySelector('track');
expect(trackElement.getAttribute('id')).to.be('id');
expect(trackElement.getAttribute('kind')).to.be('kind');
expect(trackElement.getAttribute('label')).to.be('label');
expect(trackElement.getAttribute('srclang')).to.be('srclang');
expect(trackElement.getAttribute('src')).to.be('src');
});
});

describe('updateTrackLabel', function() {

it('should update the label of the matching track element', function() {
const id = 'id1';
textTrackHelper.addTrack(getDummyTrackData({
id: id,
label: 'label'
}));
const trackElement = textTrackHelper.video.querySelector(`#${id}`);
expect(trackElement.getAttribute('label')).to.be('label');
textTrackHelper.updateTrackLabel(id, 'marlonrando');
expect(trackElement.getAttribute('label')).to.be('marlonrando');
});
});

describe('forEach', function() {

it('should execute a call back for each text track on the video element', function() {
let callCount = 0;
textTrackHelper.addTrack(getDummyTrackData());
textTrackHelper.addTrack(getDummyTrackData());
textTrackHelper.addTrack(getDummyTrackData());
textTrackHelper.addTrack(getDummyTrackData());
textTrackHelper.forEach(() => callCount++);
expect(callCount).to.be(4);
});
});

describe('filter', function() {

it('should return the elements that match the search criteria', function() {
textTrackHelper.addTrack(getDummyTrackData());
textTrackHelper.addTrack(getDummyTrackData({ id: 'match1', label: 'match' }));
textTrackHelper.addTrack(getDummyTrackData({ id: 'match2', label: 'match' }));
textTrackHelper.addTrack(getDummyTrackData());
const result = textTrackHelper.filter(track => track.label === 'match');
expect(result.length).to.be(2);
expect(result[0].id).to.be('match1');
expect(result[1].id).to.be('match2');
});

it('should return an empty array if no matches are found', function() {
const result = textTrackHelper.filter(track => track.label === 'match');
expect(result).to.eql([]);
});
});

describe('find', function() {

it('should return the first track to match the search criteria', function() {
textTrackHelper.addTrack(getDummyTrackData());
textTrackHelper.addTrack(getDummyTrackData({ id: 'match1', label: 'match' }));
textTrackHelper.addTrack(getDummyTrackData({ id: 'match2', label: 'match' }));
textTrackHelper.addTrack(getDummyTrackData());
const result = textTrackHelper.find(track => track.label === 'match');
expect(result.id).to.be('match1');
});

it('should return undefined if no matches are found', function() {
const result = textTrackHelper.find(track => track.label === 'match');
expect(result).to.be(undefined);
});
});

describe('findTrackByKey', function() {

it('should return the first external track that matches the language', function() {
let isExternal;
textTrackHelper.addTrack(getDummyTrackData({ id: 'CC1' }));
textTrackHelper.addTrack(getDummyTrackData({ id: 'CC2' }));
isExternal = false;
mapTracks(isExternal);
textTrackHelper.addTrack(getDummyTrackData({ id: 'VTT1', srclang: 'es' }));
textTrackHelper.addTrack(getDummyTrackData({ id: 'VTT2', srclang: 'en' }));
isExternal = true;
mapTracks(isExternal);
const result = textTrackHelper.findTrackByKey('en', textTrackMap);
expect(result.id).to.be('VTT2');
});

it('should return the first internal track that matches the track id', function() {
let isExternal;
textTrackHelper.addTrack(getDummyTrackData({ id: 'VTT1' }));
textTrackHelper.addTrack(getDummyTrackData({ id: 'VTT2' }));
isExternal = true;
mapTracks(isExternal);
textTrackHelper.addTrack(getDummyTrackData({ id: 'CC1', srclang: 'es' }));
textTrackHelper.addTrack(getDummyTrackData({ id: 'CC2', srclang: 'en' }));
isExternal = false;
mapTracks(isExternal);
const result = textTrackHelper.findTrackByKey('CC2', textTrackMap);
expect(result.id).to.be('CC2');
});

it('should ignore external tracks that match the track id', function() {
textTrackHelper.addTrack(getDummyTrackData({ id: 'VTT1', srclang: 'es' }));
textTrackHelper.addTrack(getDummyTrackData({ id: 'VTT2', srclang: 'en' }));
let isExternal = true;
mapTracks(isExternal);
const result = textTrackHelper.findTrackByKey('VTT2', textTrackMap);
expect(result).to.be(undefined);
});

it('should ignore internal tracks that match the language', function() {
let isExternal;
textTrackHelper.addTrack(getDummyTrackData({ id: 'CC1', srclang: 'en' }));
isExternal = false;
mapTracks(isExternal);
textTrackHelper.addTrack(getDummyTrackData({ id: 'VTT1', srclang: 'en' }));
textTrackHelper.addTrack(getDummyTrackData({ id: 'VTT2', srclang: 'es' }));
isExternal = true;
mapTracks(isExternal);
const result = textTrackHelper.findTrackByKey('en', textTrackMap);
expect(result.id).to.be('VTT1');
});
});

describe('filterChangedTracks', function() {

it('should return all tracks that have a mode that differs from the one stored in the TextTrackMap', function() {
textTrackHelper.addTrack(getDummyTrackData());
textTrackHelper.addTrack(getDummyTrackData());
textTrackHelper.addTrack(getDummyTrackData());
textTrackHelper.addTrack(getDummyTrackData());
mapTracks();
textTrackHelper.video.textTracks[1].mode = 'showing';
textTrackHelper.video.textTracks[2].mode = 'showing';
const result = textTrackHelper.filterChangedTracks(textTrackMap);
expect(result.length).to.be(2);
expect(result).to.eql([
textTrackHelper.video.textTracks[1],
textTrackHelper.video.textTracks[2]
]);
});

it('should return an empty array if no tracks have been changed', function() {
textTrackHelper.addTrack(getDummyTrackData());
textTrackHelper.addTrack(getDummyTrackData());
textTrackHelper.addTrack(getDummyTrackData());
textTrackHelper.addTrack(getDummyTrackData());
mapTracks();
const result = textTrackHelper.filterChangedTracks(textTrackMap);
expect(result.length).to.be(0);
expect(result).to.eql([]);
});
});

describe('removeExternalTracks', function() {

it('should remove all tracks marked as isExternal in the given TextTrackMap', function() {
let isExternal, trackElements;
textTrackHelper.addTrack(getDummyTrackData({ id: 'CC1' }));
textTrackHelper.addTrack(getDummyTrackData({ id: 'CC2' }));
isExternal = false;
mapTracks(isExternal);
// Note: These need to match the ids generated by TextTrackMap
textTrackHelper.addTrack(getDummyTrackData({ id: 'VTT1' }));
textTrackHelper.addTrack(getDummyTrackData({ id: 'VTT2' }));
isExternal = true;
mapTracks(isExternal);
trackElements = textTrackHelper.video.querySelectorAll('track');
expect(trackElements.length).to.be(4);
textTrackHelper.removeExternalTracks(textTrackMap);
trackElements = textTrackHelper.video.querySelectorAll('track');
expect(trackElements.length).to.be(2);
expect(trackElements[0].id).to.be('CC1');
expect(trackElements[1].id).to.be('CC2');
});
});
});
Loading

0 comments on commit 24d0024

Please sign in to comment.