2016-08-03 21:27:03 +02:00
|
|
|
/* eslint-env qunit */
|
2016-03-17 20:53:00 +02:00
|
|
|
import window from 'global/window';
|
|
|
|
import EventTarget from '../../../src/js/event-target.js';
|
2016-04-22 20:31:12 +02:00
|
|
|
import TrackBaseline from './track-baseline';
|
|
|
|
import TechFaker from '../tech/tech-faker';
|
2015-03-26 06:43:41 +02:00
|
|
|
import TextTrack from '../../../src/js/tracks/text-track.js';
|
|
|
|
import TestHelpers from '../test-helpers.js';
|
2016-03-18 00:56:25 +02:00
|
|
|
import proxyquireify from 'proxyquireify';
|
2016-08-03 21:27:03 +02:00
|
|
|
import sinon from 'sinon';
|
2016-03-18 00:56:25 +02:00
|
|
|
|
|
|
|
const proxyquire = proxyquireify(require);
|
2016-08-03 21:27:03 +02:00
|
|
|
|
2017-03-02 18:16:12 +02:00
|
|
|
QUnit.module('Text Track', {
|
|
|
|
beforeEach() {
|
2017-03-02 21:35:45 +02:00
|
|
|
this.tech = new TechFaker();
|
2017-03-02 18:16:12 +02:00
|
|
|
},
|
|
|
|
afterEach() {
|
2017-03-02 21:35:45 +02:00
|
|
|
this.tech.dispose();
|
|
|
|
this.tech = null;
|
2017-03-02 18:16:12 +02:00
|
|
|
}
|
|
|
|
});
|
2015-02-14 01:18:07 +02:00
|
|
|
|
2016-04-22 20:31:12 +02:00
|
|
|
// do baseline track testing
|
|
|
|
TrackBaseline(TextTrack, {
|
|
|
|
id: '1',
|
|
|
|
kind: 'subtitles',
|
|
|
|
mode: 'disabled',
|
|
|
|
label: 'English',
|
|
|
|
language: 'en',
|
2017-03-02 21:35:45 +02:00
|
|
|
tech: new TechFaker()
|
2016-04-22 20:31:12 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('requires a tech', function(assert) {
|
2016-08-03 21:27:03 +02:00
|
|
|
const error = new Error('A tech was not provided.');
|
2016-03-07 21:54:32 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.throws(() => new TextTrack({}), error, 'a tech is required');
|
|
|
|
assert.throws(() => new TextTrack({tech: null}), error, 'a tech is required');
|
2015-02-14 01:18:07 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('can create a TextTrack with a mode property', function(assert) {
|
2016-08-03 21:27:03 +02:00
|
|
|
const mode = 'disabled';
|
|
|
|
const tt = new TextTrack({
|
2016-03-07 21:54:32 +02:00
|
|
|
mode,
|
2017-03-02 21:35:45 +02:00
|
|
|
tech: this.tech
|
2016-03-07 21:54:32 +02:00
|
|
|
});
|
2015-02-14 01:18:07 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(tt.mode, mode, 'we have a mode');
|
2015-02-14 01:18:07 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('defaults when items not provided', function(assert) {
|
2016-08-03 21:27:03 +02:00
|
|
|
const tt = new TextTrack({
|
2017-03-02 21:35:45 +02:00
|
|
|
tech: this.tech
|
2015-02-14 01:18:07 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(tt.kind, 'subtitles', 'kind defaulted to subtitles');
|
|
|
|
assert.equal(tt.mode, 'disabled', 'mode defaulted to disabled');
|
|
|
|
assert.equal(tt.label, '', 'label defaults to empty string');
|
|
|
|
assert.equal(tt.language, '', 'language defaults to empty string');
|
2015-02-14 01:18:07 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('kind can only be one of several options, defaults to subtitles', function(assert) {
|
2016-03-07 21:54:32 +02:00
|
|
|
let tt = new TextTrack({
|
2017-03-02 21:35:45 +02:00
|
|
|
tech: this.tech,
|
2015-02-14 01:18:07 +02:00
|
|
|
kind: 'foo'
|
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(tt.kind, 'subtitles', 'the kind is set to subtitles, not foo');
|
|
|
|
assert.notEqual(tt.kind, 'foo', 'the kind is set to subtitles, not foo');
|
2015-02-14 01:18:07 +02:00
|
|
|
|
2015-03-26 06:43:41 +02:00
|
|
|
tt = new TextTrack({
|
2017-03-02 21:35:45 +02:00
|
|
|
tech: this.tech,
|
2015-02-14 01:18:07 +02:00
|
|
|
kind: 'subtitles'
|
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(tt.kind, 'subtitles', 'the kind is set to subtitles');
|
2015-02-14 01:18:07 +02:00
|
|
|
|
2015-03-26 06:43:41 +02:00
|
|
|
tt = new TextTrack({
|
2017-03-02 21:35:45 +02:00
|
|
|
tech: this.tech,
|
2015-02-14 01:18:07 +02:00
|
|
|
kind: 'captions'
|
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(tt.kind, 'captions', 'the kind is set to captions');
|
2015-02-14 01:18:07 +02:00
|
|
|
|
2015-03-26 06:43:41 +02:00
|
|
|
tt = new TextTrack({
|
2017-03-02 21:35:45 +02:00
|
|
|
tech: this.tech,
|
2015-02-14 01:18:07 +02:00
|
|
|
kind: 'descriptions'
|
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(tt.kind, 'descriptions', 'the kind is set to descriptions');
|
2015-02-14 01:18:07 +02:00
|
|
|
|
2015-03-26 06:43:41 +02:00
|
|
|
tt = new TextTrack({
|
2017-03-02 21:35:45 +02:00
|
|
|
tech: this.tech,
|
2015-02-14 01:18:07 +02:00
|
|
|
kind: 'chapters'
|
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(tt.kind, 'chapters', 'the kind is set to chapters');
|
2015-02-14 01:18:07 +02:00
|
|
|
|
2015-03-26 06:43:41 +02:00
|
|
|
tt = new TextTrack({
|
2017-03-02 21:35:45 +02:00
|
|
|
tech: this.tech,
|
2015-02-14 01:18:07 +02:00
|
|
|
kind: 'metadata'
|
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(tt.kind, 'metadata', 'the kind is set to metadata');
|
2015-02-14 01:18:07 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('mode can only be one of several options, defaults to disabled', function(assert) {
|
2016-03-07 21:54:32 +02:00
|
|
|
let tt = new TextTrack({
|
2017-03-02 21:35:45 +02:00
|
|
|
tech: this.tech,
|
2015-02-14 01:18:07 +02:00
|
|
|
mode: 'foo'
|
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(tt.mode, 'disabled', 'the mode is set to disabled, not foo');
|
|
|
|
assert.notEqual(tt.mode, 'foo', 'the mode is set to disabld, not foo');
|
2015-02-14 01:18:07 +02:00
|
|
|
|
2015-03-26 06:43:41 +02:00
|
|
|
tt = new TextTrack({
|
2017-03-02 21:35:45 +02:00
|
|
|
tech: this.tech,
|
2015-02-14 01:18:07 +02:00
|
|
|
mode: 'disabled'
|
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(tt.mode, 'disabled', 'the mode is set to disabled');
|
2015-02-14 01:18:07 +02:00
|
|
|
|
2015-03-26 06:43:41 +02:00
|
|
|
tt = new TextTrack({
|
2017-03-02 21:35:45 +02:00
|
|
|
tech: this.tech,
|
2015-02-14 01:18:07 +02:00
|
|
|
mode: 'hidden'
|
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(tt.mode, 'hidden', 'the mode is set to hidden');
|
2015-02-14 01:18:07 +02:00
|
|
|
|
2015-03-26 06:43:41 +02:00
|
|
|
tt = new TextTrack({
|
2017-03-02 21:35:45 +02:00
|
|
|
tech: this.tech,
|
2015-02-14 01:18:07 +02:00
|
|
|
mode: 'showing'
|
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(tt.mode, 'showing', 'the mode is set to showing');
|
2015-02-14 01:18:07 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('cue and activeCues are read only', function(assert) {
|
2016-08-03 21:27:03 +02:00
|
|
|
const mode = 'disabled';
|
|
|
|
const tt = new TextTrack({
|
2016-03-07 21:54:32 +02:00
|
|
|
mode,
|
2017-03-02 21:35:45 +02:00
|
|
|
tech: this.tech
|
2016-03-07 21:54:32 +02:00
|
|
|
});
|
2015-02-14 01:18:07 +02:00
|
|
|
|
|
|
|
tt.cues = 'foo';
|
|
|
|
tt.activeCues = 'bar';
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.notEqual(tt.cues, 'foo', 'cues is still original value');
|
|
|
|
assert.notEqual(tt.activeCues, 'bar', 'activeCues is still original value');
|
2015-02-14 01:18:07 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('mode can only be set to a few options', function(assert) {
|
2016-08-03 21:27:03 +02:00
|
|
|
const tt = new TextTrack({
|
2017-03-02 21:35:45 +02:00
|
|
|
tech: this.tech
|
2015-02-14 01:18:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
tt.mode = 'foo';
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.notEqual(tt.mode, 'foo', 'the mode is still the old value, disabled');
|
|
|
|
assert.equal(tt.mode, 'disabled', 'still on the default mode, disabled');
|
2015-02-14 01:18:07 +02:00
|
|
|
|
|
|
|
tt.mode = 'hidden';
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(tt.mode, 'hidden', 'mode set to hidden');
|
2015-02-14 01:18:07 +02:00
|
|
|
|
|
|
|
tt.mode = 'bar';
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.notEqual(tt.mode, 'bar', 'the mode is still the old value, hidden');
|
|
|
|
assert.equal(tt.mode, 'hidden', 'still on the previous mode, hidden');
|
2015-02-14 01:18:07 +02:00
|
|
|
|
|
|
|
tt.mode = 'showing';
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(tt.mode, 'showing', 'mode set to showing');
|
2015-02-14 01:18:07 +02:00
|
|
|
|
|
|
|
tt.mode = 'baz';
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.notEqual(tt.mode, 'baz', 'the mode is still the old value, showing');
|
|
|
|
assert.equal(tt.mode, 'showing', 'still on the previous mode, showing');
|
2015-02-14 01:18:07 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('cues and activeCues return a TextTrackCueList', function(assert) {
|
2016-08-03 21:27:03 +02:00
|
|
|
const tt = new TextTrack({
|
2017-03-02 21:35:45 +02:00
|
|
|
tech: this.tech
|
2015-02-14 01:18:07 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(tt.cues.getCueById, 'cues are a TextTrackCueList');
|
|
|
|
assert.ok(tt.activeCues.getCueById, 'activeCues are a TextTrackCueList');
|
2015-02-14 01:18:07 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('cues can be added and removed from a TextTrack', function(assert) {
|
2016-08-03 21:27:03 +02:00
|
|
|
const tt = new TextTrack({
|
2017-03-02 21:35:45 +02:00
|
|
|
tech: this.tech
|
2016-03-07 21:54:32 +02:00
|
|
|
});
|
2016-08-03 21:27:03 +02:00
|
|
|
const cues = tt.cues;
|
2015-02-14 01:18:07 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(cues.length, 0, 'start with zero cues');
|
2015-02-14 01:18:07 +02:00
|
|
|
|
|
|
|
tt.addCue({id: '1'});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(cues.length, 1, 'we have one cue');
|
2015-02-14 01:18:07 +02:00
|
|
|
|
|
|
|
tt.removeCue(cues.getCueById('1'));
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(cues.length, 0, 'we have removed our one cue');
|
2015-02-14 01:18:07 +02:00
|
|
|
|
|
|
|
tt.addCue({id: '1'});
|
|
|
|
tt.addCue({id: '2'});
|
|
|
|
tt.addCue({id: '3'});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(cues.length, 3, 'we now have 3 cues');
|
2015-02-14 01:18:07 +02:00
|
|
|
});
|
|
|
|
|
2017-03-23 20:31:41 +02:00
|
|
|
QUnit.test('original cue can be used to remove cue from cues list', function(assert) {
|
|
|
|
const tt = new TextTrack({
|
|
|
|
tech: this.tech
|
|
|
|
});
|
|
|
|
const Cue = window.VTTCue ||
|
|
|
|
window.vttjs && window.vttjs.VTTCue ||
|
|
|
|
window.TextTrackCue;
|
|
|
|
|
|
|
|
const cue1 = new Cue(0, 1, 'some-cue');
|
|
|
|
|
|
|
|
assert.equal(tt.cues.length, 0, 'start with zero cues');
|
|
|
|
tt.addCue(cue1);
|
|
|
|
assert.equal(tt.cues.length, 1, 'we have one cue');
|
|
|
|
|
|
|
|
tt.removeCue(cue1);
|
|
|
|
assert.equal(tt.cues.length, 0, 'we have removed cue1');
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test('can only remove one cue at a time', function(assert) {
|
|
|
|
const tt = new TextTrack({
|
|
|
|
tech: this.tech
|
|
|
|
});
|
|
|
|
const Cue = window.VTTCue ||
|
|
|
|
window.vttjs && window.vttjs.VTTCue ||
|
|
|
|
window.TextTrackCue;
|
|
|
|
|
|
|
|
const cue1 = new Cue(0, 1, 'some-cue');
|
|
|
|
|
|
|
|
assert.equal(tt.cues.length, 0, 'start with zero cues');
|
|
|
|
tt.addCue(cue1);
|
|
|
|
tt.addCue(cue1);
|
|
|
|
assert.equal(tt.cues.length, 2, 'we have two cues');
|
|
|
|
|
|
|
|
tt.removeCue(cue1);
|
|
|
|
assert.equal(tt.cues.length, 1, 'we have removed one instance of cue1');
|
|
|
|
|
|
|
|
tt.removeCue(cue1);
|
|
|
|
assert.equal(tt.cues.length, 0, 'we have removed the other instance of cue1');
|
|
|
|
});
|
|
|
|
|
2017-03-02 21:35:45 +02:00
|
|
|
QUnit.test('does not fire cuechange before Tech is ready', function(assert) {
|
|
|
|
const done = assert.async();
|
|
|
|
const player = TestHelpers.makePlayer({techfaker: {autoReady: false}});
|
|
|
|
let changes = 0;
|
|
|
|
const tt = new TextTrack({
|
|
|
|
tech: player.tech_,
|
|
|
|
mode: 'showing'
|
|
|
|
});
|
|
|
|
const cuechangeHandler = function() {
|
|
|
|
changes++;
|
|
|
|
};
|
|
|
|
|
|
|
|
tt.addCue({
|
|
|
|
id: '1',
|
|
|
|
startTime: 0,
|
|
|
|
endTime: 5
|
|
|
|
});
|
|
|
|
|
|
|
|
tt.oncuechange = cuechangeHandler;
|
|
|
|
tt.addEventListener('cuechange', cuechangeHandler);
|
|
|
|
|
|
|
|
player.tech_.currentTime = function() {
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
player.tech_.trigger('timeupdate');
|
|
|
|
assert.equal(changes, 0, 'a cuechange event is not triggered');
|
|
|
|
|
|
|
|
player.tech_.on('ready', function() {
|
|
|
|
player.tech_.currentTime = function() {
|
|
|
|
return 0.2;
|
|
|
|
};
|
|
|
|
|
|
|
|
player.tech_.trigger('timeupdate');
|
|
|
|
|
|
|
|
assert.equal(changes, 2, 'a cuechange event trigger addEventListener and oncuechange');
|
|
|
|
|
|
|
|
player.dispose();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
player.tech_.triggerReady();
|
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('fires cuechange when cues become active and inactive', function(assert) {
|
2016-08-03 21:27:03 +02:00
|
|
|
const player = TestHelpers.makePlayer();
|
2016-03-07 21:54:32 +02:00
|
|
|
let changes = 0;
|
2016-08-03 21:27:03 +02:00
|
|
|
const tt = new TextTrack({
|
2016-03-07 21:54:32 +02:00
|
|
|
tech: player.tech_,
|
|
|
|
mode: 'showing'
|
|
|
|
});
|
2016-08-03 21:27:03 +02:00
|
|
|
const cuechangeHandler = function() {
|
2015-02-14 01:18:07 +02:00
|
|
|
changes++;
|
|
|
|
};
|
|
|
|
|
|
|
|
tt.addCue({
|
|
|
|
id: '1',
|
|
|
|
startTime: 1,
|
|
|
|
endTime: 5
|
|
|
|
});
|
|
|
|
|
|
|
|
tt.oncuechange = cuechangeHandler;
|
|
|
|
tt.addEventListener('cuechange', cuechangeHandler);
|
|
|
|
|
2015-09-15 02:45:14 +02:00
|
|
|
player.tech_.currentTime = function() {
|
2015-02-14 01:18:07 +02:00
|
|
|
return 2;
|
|
|
|
};
|
|
|
|
|
2015-09-15 02:45:14 +02:00
|
|
|
player.tech_.trigger('timeupdate');
|
2015-02-14 01:18:07 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(changes, 2, 'a cuechange event trigger addEventListener and oncuechange');
|
2015-02-14 01:18:07 +02:00
|
|
|
|
2015-09-15 02:45:14 +02:00
|
|
|
player.tech_.currentTime = function() {
|
2015-02-14 01:18:07 +02:00
|
|
|
return 7;
|
|
|
|
};
|
|
|
|
|
2015-09-15 02:45:14 +02:00
|
|
|
player.tech_.trigger('timeupdate');
|
2015-02-14 01:18:07 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(changes, 4, 'a cuechange event trigger addEventListener and oncuechange');
|
2015-08-25 02:46:54 +02:00
|
|
|
|
|
|
|
player.dispose();
|
2015-02-14 01:18:07 +02:00
|
|
|
});
|
2016-03-17 20:53:00 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('tracks are parsed if vttjs is loaded', function(assert) {
|
2016-03-17 20:53:00 +02:00
|
|
|
const clock = sinon.useFakeTimers();
|
|
|
|
const oldVTT = window.WebVTT;
|
|
|
|
let parserCreated = false;
|
|
|
|
|
|
|
|
window.WebVTT = () => {};
|
|
|
|
window.WebVTT.StringDecoder = () => {};
|
|
|
|
window.WebVTT.Parser = () => {
|
|
|
|
parserCreated = true;
|
|
|
|
return {
|
|
|
|
oncue() {},
|
|
|
|
onparsingerror() {},
|
|
|
|
onflush() {},
|
|
|
|
parse() {},
|
|
|
|
flush() {}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-03-18 00:56:25 +02:00
|
|
|
// use proxyquire to stub xhr module because IE8s XDomainRequest usage
|
|
|
|
let xhrHandler;
|
2016-08-03 21:27:03 +02:00
|
|
|
const TextTrack_ = proxyquire('../../../src/js/tracks/text-track.js', {
|
2016-03-18 00:56:25 +02:00
|
|
|
xhr(options, fn) {
|
2016-08-03 21:27:03 +02:00
|
|
|
xhrHandler = fn;
|
2016-03-18 00:56:25 +02:00
|
|
|
}
|
2016-08-17 23:16:41 +02:00
|
|
|
}).default;
|
2016-03-17 20:53:00 +02:00
|
|
|
|
2016-08-03 21:27:03 +02:00
|
|
|
/* eslint-disable no-unused-vars */
|
|
|
|
const tt = new TextTrack_({
|
2017-03-02 21:35:45 +02:00
|
|
|
tech: this.tech,
|
2016-03-17 20:53:00 +02:00
|
|
|
src: 'http://example.com'
|
|
|
|
});
|
2016-08-03 21:27:03 +02:00
|
|
|
/* eslint-enable no-unused-vars */
|
2016-03-17 20:53:00 +02:00
|
|
|
|
2016-03-18 00:56:25 +02:00
|
|
|
xhrHandler(null, {}, 'WEBVTT\n');
|
2016-03-17 20:53:00 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(parserCreated, 'WebVTT is loaded, so we can just parse');
|
2016-03-17 20:53:00 +02:00
|
|
|
|
|
|
|
clock.restore();
|
|
|
|
window.WebVTT = oldVTT;
|
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('tracks are parsed once vttjs is loaded', function(assert) {
|
2016-03-17 20:53:00 +02:00
|
|
|
const clock = sinon.useFakeTimers();
|
|
|
|
const oldVTT = window.WebVTT;
|
|
|
|
let parserCreated = false;
|
|
|
|
|
2016-03-18 00:56:25 +02:00
|
|
|
// use proxyquire to stub xhr module because IE8s XDomainRequest usage
|
|
|
|
let xhrHandler;
|
2016-08-03 21:27:03 +02:00
|
|
|
const TextTrack_ = proxyquire('../../../src/js/tracks/text-track.js', {
|
2016-03-18 00:56:25 +02:00
|
|
|
xhr(options, fn) {
|
2016-08-03 21:27:03 +02:00
|
|
|
xhrHandler = fn;
|
2016-03-18 00:56:25 +02:00
|
|
|
}
|
2016-08-17 23:16:41 +02:00
|
|
|
}).default;
|
2016-03-17 20:53:00 +02:00
|
|
|
|
2016-03-18 00:56:25 +02:00
|
|
|
window.WebVTT = true;
|
2016-03-17 20:53:00 +02:00
|
|
|
|
2016-08-03 21:27:03 +02:00
|
|
|
const testTech = new EventTarget();
|
|
|
|
|
2016-03-17 20:53:00 +02:00
|
|
|
testTech.textTracks = () => {};
|
|
|
|
testTech.currentTime = () => {};
|
|
|
|
|
2016-08-03 21:27:03 +02:00
|
|
|
/* eslint-disable no-unused-vars */
|
|
|
|
const tt = new TextTrack_({
|
2016-03-17 20:53:00 +02:00
|
|
|
tech: testTech,
|
|
|
|
src: 'http://example.com'
|
|
|
|
});
|
2016-08-03 21:27:03 +02:00
|
|
|
/* eslint-enable no-unused-vars */
|
2016-03-17 20:53:00 +02:00
|
|
|
|
2016-03-18 00:56:25 +02:00
|
|
|
xhrHandler(null, {}, 'WEBVTT\n');
|
2016-03-17 20:53:00 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(!parserCreated, 'WebVTT is not loaded, do not try to parse yet');
|
2016-03-17 20:53:00 +02:00
|
|
|
|
|
|
|
clock.tick(100);
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(!parserCreated, 'WebVTT still not loaded, do not try to parse yet');
|
2016-03-17 20:53:00 +02:00
|
|
|
|
|
|
|
window.WebVTT = () => {};
|
|
|
|
window.WebVTT.StringDecoder = () => {};
|
|
|
|
window.WebVTT.Parser = () => {
|
|
|
|
parserCreated = true;
|
|
|
|
return {
|
|
|
|
oncue() {},
|
|
|
|
onparsingerror() {},
|
|
|
|
onflush() {},
|
|
|
|
parse() {},
|
|
|
|
flush() {}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testTech.trigger('vttjsloaded');
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(parserCreated, 'WebVTT is loaded, so we can parse now');
|
2016-03-17 20:53:00 +02:00
|
|
|
|
|
|
|
clock.restore();
|
|
|
|
window.WebVTT = oldVTT;
|
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('stops processing if vttjs loading errored out', function(assert) {
|
2016-03-17 20:53:00 +02:00
|
|
|
const clock = sinon.useFakeTimers();
|
2016-07-18 20:32:31 +02:00
|
|
|
const errorSpy = sinon.spy();
|
2016-03-17 20:53:00 +02:00
|
|
|
const oldVTT = window.WebVTT;
|
2016-08-03 21:27:03 +02:00
|
|
|
const parserCreated = false;
|
|
|
|
|
2016-03-17 20:53:00 +02:00
|
|
|
window.WebVTT = true;
|
|
|
|
|
2016-03-18 00:56:25 +02:00
|
|
|
// use proxyquire to stub xhr module because IE8s XDomainRequest usage
|
|
|
|
let xhrHandler;
|
2016-08-03 21:27:03 +02:00
|
|
|
const TextTrack_ = proxyquire('../../../src/js/tracks/text-track.js', {
|
2016-03-18 00:56:25 +02:00
|
|
|
xhr(options, fn) {
|
2016-08-03 21:27:03 +02:00
|
|
|
xhrHandler = fn;
|
2016-03-18 00:56:25 +02:00
|
|
|
},
|
|
|
|
'../utils/log.js': {
|
2016-08-03 21:27:03 +02:00
|
|
|
default: {
|
2016-07-18 20:32:31 +02:00
|
|
|
error: errorSpy
|
2016-03-18 00:56:25 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-17 23:16:41 +02:00
|
|
|
}).default;
|
2016-03-17 20:53:00 +02:00
|
|
|
|
2016-08-03 21:27:03 +02:00
|
|
|
const testTech = new EventTarget();
|
|
|
|
|
2016-03-17 20:53:00 +02:00
|
|
|
testTech.textTracks = () => {};
|
|
|
|
testTech.currentTime = () => {};
|
|
|
|
|
|
|
|
sinon.stub(testTech, 'off');
|
|
|
|
testTech.off.withArgs('vttjsloaded');
|
|
|
|
|
2016-08-03 21:27:03 +02:00
|
|
|
/* eslint-disable no-unused-vars */
|
|
|
|
const tt = new TextTrack_({
|
2016-03-17 20:53:00 +02:00
|
|
|
tech: testTech,
|
|
|
|
src: 'http://example.com'
|
|
|
|
});
|
2016-08-03 21:27:03 +02:00
|
|
|
/* eslint-enable no-unused-vars */
|
2016-03-17 20:53:00 +02:00
|
|
|
|
2016-03-18 00:56:25 +02:00
|
|
|
xhrHandler(null, {}, 'WEBVTT\n');
|
2016-03-17 20:53:00 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(!parserCreated, 'WebVTT is not loaded, do not try to parse yet');
|
2016-03-17 20:53:00 +02:00
|
|
|
|
|
|
|
testTech.trigger('vttjserror');
|
2016-08-03 21:27:03 +02:00
|
|
|
const offSpyCall = testTech.off.getCall(0);
|
2016-03-17 20:53:00 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(errorSpy.called, 'vttjs failed to load, so log.error was called');
|
2016-07-18 20:32:31 +02:00
|
|
|
if (errorSpy.called) {
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(/^vttjs failed to load, stopping trying to process/.test(errorSpy.getCall(0).args[0]),
|
2016-07-18 20:32:31 +02:00
|
|
|
'log.error was called with the expected message');
|
|
|
|
}
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(!parserCreated, 'WebVTT is not loaded, do not try to parse yet');
|
|
|
|
assert.ok(offSpyCall, 'tech.off was called');
|
2016-03-17 20:53:00 +02:00
|
|
|
|
|
|
|
clock.restore();
|
|
|
|
window.WebVTT = oldVTT;
|
|
|
|
});
|