-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
subtitle-stream-controller.js
139 lines (118 loc) · 4.44 KB
/
subtitle-stream-controller.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import sinon from 'sinon';
import Hls from '../../../src/hls';
import Event from '../../../src/events';
import { FragmentTracker } from '../../../src/controller/fragment-tracker';
import { SubtitleStreamController } from '../../../src/controller/subtitle-stream-controller';
const mediaMock = {
currentTime: 0,
addEventListener () {},
removeEventListener () {}
};
const tracksMock = [
{ id: 0, details: {} },
{ id: 1 }
];
describe('SubtitleStreamController', function () {
let hls;
let fragmentTracker;
let subtitleStreamController;
beforeEach(function () {
hls = new Hls({});
fragmentTracker = new FragmentTracker(hls);
subtitleStreamController = new SubtitleStreamController(hls, fragmentTracker);
subtitleStreamController.onMediaAttached({ media: mediaMock });
});
afterEach(function () {
subtitleStreamController.onMediaDetaching({ media: mediaMock });
});
describe('onSubtitleTracksUpdate', function () {
beforeEach(function () {
hls.trigger(Event.SUBTITLE_TRACKS_UPDATED, {
subtitleTracks: tracksMock
});
});
it('should update tracks list', function () {
expect(subtitleStreamController.tracks).to.equal(tracksMock);
});
});
describe('onSubtitleTrackSwitch', function () {
beforeEach(function () {
subtitleStreamController.tracks = tracksMock;
subtitleStreamController.clearInterval = sinon.spy();
subtitleStreamController.setInterval = sinon.spy();
hls.trigger(Event.SUBTITLE_TRACK_SWITCH, {
id: 0
});
});
it('should call setInterval if details available', function () {
expect(subtitleStreamController.setInterval).to.have.been.calledOnce;
});
it('should call clearInterval if no tracks present', function () {
subtitleStreamController.tracks = [];
hls.trigger(Event.SUBTITLE_TRACK_SWITCH, {
id: 0
});
expect(subtitleStreamController.clearInterval).to.have.been.calledOnce;
});
it('should call clearInterval if new track id === -1', function () {
hls.trigger(Event.SUBTITLE_TRACK_SWITCH, {
id: -1
});
expect(subtitleStreamController.clearInterval).to.have.been.calledOnce;
});
});
describe('onSubtitleTrackLoaded', function () {
beforeEach(function () {
subtitleStreamController.setInterval = sinon.spy();
subtitleStreamController.tracks = tracksMock;
});
// Details are in subtitle-track-controller.js' onSubtitleTrackLoaded handler
it('should handle the event if the data matches the current track', function () {
const details = { foo: 'bar' };
subtitleStreamController.currentTrackId = 1;
hls.trigger(Event.SUBTITLE_TRACK_LOADED, {
id: 1, details
});
expect(subtitleStreamController.tracks[1].details).to.equal(details);
expect(subtitleStreamController.setInterval).to.have.been.calledOnce;
});
it('should ignore the event if the data does not match the current track', function () {
const details = { foo: 'bar' };
subtitleStreamController.currentTrackId = 0;
hls.trigger(Event.SUBTITLE_TRACK_LOADED, {
id: 1, details
});
expect(subtitleStreamController.tracks[0].details).to.not.equal(details);
expect(subtitleStreamController.setInterval).to.not.have.been.called;
});
it('should ignore the event if there are no tracks, or the id is not within the tracks array', function () {
subtitleStreamController.tracks = [];
subtitleStreamController.trackId = 0;
const details = { foo: 'bar' };
hls.trigger(Event.SUBTITLE_TRACK_LOADED, {
id: 0, details
});
expect(subtitleStreamController.tracks[0]).to.not.exist;
expect(subtitleStreamController.setInterval).to.not.have.been.called;
});
});
describe('onLevelLoaded', function () {
it('records the start time of the last known A/V track', function () {
hls.trigger(Event.LEVEL_UPDATED, {
details: { fragments: [{ start: 5 }] }
});
expect(subtitleStreamController.lastAVStart).to.equal(5);
hls.trigger(Event.LEVEL_UPDATED, {
details: { fragments: [] }
});
expect(subtitleStreamController.lastAVStart).to.equal(0);
});
});
describe('onMediaSeeking', function () {
it('nulls fragPrevious', function () {
subtitleStreamController.fragPrevious = {};
subtitleStreamController.onMediaSeeking();
expect(subtitleStreamController.fragPrevious).to.not.exist;
});
});
});