Separate text parsing and display logic.

Closes #796.
Closes #923.

Change-Id: Ifc2017b40a0fb570103f0fed7bc130aa24819e9f
This commit is contained in:
Sandra Lokshina
2017-06-05 14:56:16 -07:00
parent d70f78b5c7
commit c70367dc97
23 changed files with 1425 additions and 472 deletions
+62 -61
View File
@@ -16,28 +16,19 @@
*/
describe('VttTextParser', function() {
/** @const */
var originalVTTCue = window.VTTCue;
/** @type {!jasmine.Spy} */
var logWarningSpy;
/** @const */
var Cue = shaka.text.Cue;
beforeAll(function() {
logWarningSpy = jasmine.createSpy('shaka.log.warning');
shaka.log.warning = shaka.test.Util.spyFunc(logWarningSpy);
});
afterAll(function() {
window.VTTCue = originalVTTCue;
});
beforeEach(function() {
logWarningSpy.calls.reset();
window.VTTCue = function(start, end, text) {
this.startTime = start;
this.endTime = end;
this.text = text;
};
});
it('supports no cues', function() {
@@ -71,7 +62,7 @@ describe('VttTextParser', function() {
it('handles a blank line at the end of the file', function() {
verifyHelper(
[
{start: 20, end: 40, text: 'Test'}
{start: 20, end: 40, payload: 'Test'}
],
'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000\n' +
@@ -82,7 +73,7 @@ describe('VttTextParser', function() {
it('handles no blank line at the end of the file', function() {
verifyHelper(
[
{start: 20, end: 40, text: 'Test'}
{start: 20, end: 40, payload: 'Test'}
],
'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000\n' +
@@ -94,7 +85,7 @@ describe('VttTextParser', function() {
it('handles no newline after the final text payload', function() {
verifyHelper(
[
{start: 20, end: 40, text: 'Test'}
{start: 20, end: 40, payload: 'Test'}
],
'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000\n' +
@@ -105,7 +96,7 @@ describe('VttTextParser', function() {
it('ignores offset', function() {
verifyHelper(
[
{start: 20, end: 40, text: 'Test'}
{start: 20, end: 40, payload: 'Test'}
],
'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000\n' +
@@ -116,8 +107,8 @@ describe('VttTextParser', function() {
it('supports cues with no settings', function() {
verifyHelper(
[
{start: 20, end: 40, text: 'Test', id: '1'},
{start: 40, end: 50, text: 'Test2', id: '2'}
{start: 20, end: 40, payload: 'Test', id: '1'},
{start: 40, end: 50, payload: 'Test2', id: '2'}
],
'WEBVTT\n\n' +
'1\n' +
@@ -132,8 +123,8 @@ describe('VttTextParser', function() {
it('supports cues with no ID', function() {
verifyHelper(
[
{start: 20, end: 40, text: 'Test'},
{start: 40, end: 50, text: 'Test2'}
{start: 20, end: 40, payload: 'Test'},
{start: 40, end: 50, payload: 'Test2'}
],
'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000\n' +
@@ -146,8 +137,8 @@ describe('VttTextParser', function() {
it('supports comments within cues', function() {
verifyHelper(
[
{start: 20, end: 40, text: 'Test'},
{start: 40, end: 50, text: 'Test2'}
{start: 20, end: 40, payload: 'Test'},
{start: 40, end: 50, payload: 'Test2'}
],
'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000\n' +
@@ -162,7 +153,7 @@ describe('VttTextParser', function() {
it('supports non-integer timecodes', function() {
verifyHelper(
[
{start: 20.1, end: 40.505, text: 'Test'}
{start: 20.1, end: 40.505, payload: 'Test'}
],
'WEBVTT\n\n' +
'00:00:20.100 --> 00:00:40.505\n' +
@@ -173,7 +164,7 @@ describe('VttTextParser', function() {
it('supports large timecodes', function() {
verifyHelper(
[
{start: 20, end: 108000, text: 'Test'}
{start: 20, end: 108000, payload: 'Test'}
],
'WEBVTT\n\n' +
'00:00:20.000 --> 30:00:00.000\n' +
@@ -220,8 +211,18 @@ describe('VttTextParser', function() {
it('supports vertical setting', function() {
verifyHelper(
[
{start: 20, end: 40, text: 'Test', vertical: 'rl'},
{start: 40, end: 50, text: 'Test2', vertical: 'lr'}
{
start: 20,
end: 40,
payload: 'Test',
writingDirection: Cue.writingDirection.VERTICAL_RIGHT
},
{
start: 40,
end: 50,
payload: 'Test2',
writingDirection: Cue.writingDirection.VERTICAL_LEFT
}
],
'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000 vertical:rl\n' +
@@ -234,8 +235,8 @@ describe('VttTextParser', function() {
it('supports line setting', function() {
verifyHelper(
[
{start: 20, end: 40, text: 'Test', line: 0},
{start: 40, end: 50, text: 'Test2', line: -1}
{start: 20, end: 40, payload: 'Test', line: 0},
{start: 40, end: 50, payload: 'Test2', line: -1}
] ,
'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000 line:0\n' +
@@ -248,8 +249,8 @@ describe('VttTextParser', function() {
it('supports line setting with optional part', function() {
verifyHelper(
[
{start: 20, end: 40, text: 'Test', line: 10},
{start: 40, end: 50, text: 'Test2', line: -1}
{start: 20, end: 40, payload: 'Test', line: 10},
{start: 40, end: 50, payload: 'Test2', line: -1}
] ,
'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000 line:10%,start\n' +
@@ -262,7 +263,7 @@ describe('VttTextParser', function() {
it('supports position setting', function() {
verifyHelper(
[
{start: 20, end: 40, text: 'Test2', position: 45}
{start: 20, end: 40, payload: 'Test2', position: 45}
],
'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000 position:45%\n' +
@@ -273,8 +274,8 @@ describe('VttTextParser', function() {
it('supports position setting with optional part', function() {
verifyHelper(
[
{start: 20, end: 40, text: 'Test', position: 45},
{start: 20, end: 40, text: 'Test2', position: 45}
{start: 20, end: 40, payload: 'Test', position: 45},
{start: 20, end: 40, payload: 'Test2', position: 45}
],
'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000 position:45%,line-left\n' +
@@ -287,7 +288,7 @@ describe('VttTextParser', function() {
it('supports size setting', function() {
verifyHelper(
[
{start: 20, end: 40, text: 'Test', size: 56}
{start: 20, end: 40, payload: 'Test', size: 56}
],
'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000 size:56%\n' +
@@ -298,7 +299,7 @@ describe('VttTextParser', function() {
it('supports align setting', function() {
verifyHelper(
[
{start: 20, end: 40, text: 'Test', align: 'center'}
{start: 20, end: 40, payload: 'Test', align: 'center'}
],
'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000 align:center\n' +
@@ -312,10 +313,10 @@ describe('VttTextParser', function() {
{
start: 20,
end: 40,
text: 'Test',
align: 'center',
payload: 'Test',
textAlign: Cue.textAlign.CENTER,
size: 56,
vertical: 'lr'
writingDirection: Cue.writingDirection.VERTICAL_LEFT
}
],
'WEBVTT\n\n' +
@@ -330,10 +331,10 @@ describe('VttTextParser', function() {
{
start: 20,
end: 40,
text: 'Test',
align: 'center',
payload: 'Test',
textAlign: Cue.textAlign.CENTER,
size: 56,
vertical: 'lr'
writingDirection: Cue.writingDirection.VERTICAL_LEFT
}
],
'WEBVTT\n\n' +
@@ -348,10 +349,10 @@ describe('VttTextParser', function() {
{
start: 20,
end: 40,
text: 'Test',
align: 'center',
payload: 'Test',
textAlign: Cue.textAlign.CENTER,
size: 56,
vertical: 'lr'
writingDirection: Cue.writingDirection.VERTICAL_LEFT
}
],
'WEBVTT\n\n' +
@@ -366,10 +367,10 @@ describe('VttTextParser', function() {
{
start: 20,
end: 40,
text: 'Test',
payload: 'Test',
align: 'center',
size: 56,
vertical: 'lr'
writingDirection: Cue.writingDirection.VERTICAL_LEFT
}
],
'WEBVTT\n\n' +
@@ -384,10 +385,10 @@ describe('VttTextParser', function() {
{
start: 40, // Note these are 20s off of the cue
end: 60, // because using relative timestamps
text: 'Test',
payload: 'Test',
align: 'center',
size: 56,
vertical: 'lr'
writingDirection: Cue.writingDirection.VERTICAL_LEFT
}
],
'WEBVTT\n\n' +
@@ -401,7 +402,7 @@ describe('VttTextParser', function() {
verifyHelper(
[
{start: 20, end: 40, text: 'Test'}
{start: 20, end: 40, payload: 'Test'}
],
'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000 vertical:es\n' +
@@ -410,7 +411,7 @@ describe('VttTextParser', function() {
verifyHelper(
[
{start: 20, end: 40, text: 'Test'}
{start: 20, end: 40, payload: 'Test'}
],
'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000 vertical:\n' +
@@ -419,7 +420,7 @@ describe('VttTextParser', function() {
verifyHelper(
[
{start: 20, end: 40, text: 'Test'}
{start: 20, end: 40, payload: 'Test'}
],
'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000 vertical\n' +
@@ -428,7 +429,7 @@ describe('VttTextParser', function() {
verifyHelper(
[
{start: 20, end: 40, text: 'Test'}
{start: 20, end: 40, payload: 'Test'}
],
'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000 line:-3%\n' +
@@ -437,7 +438,7 @@ describe('VttTextParser', function() {
verifyHelper(
[
{start: 20, end: 40, text: 'Test'}
{start: 20, end: 40, payload: 'Test'}
],
'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000 line:45%%\n' +
@@ -446,7 +447,7 @@ describe('VttTextParser', function() {
verifyHelper(
[
{start: 20, end: 40, text: 'Test'}
{start: 20, end: 40, payload: 'Test'}
],
'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000 align:10\n' +
@@ -455,7 +456,7 @@ describe('VttTextParser', function() {
verifyHelper(
[
{start: 20, end: 40, text: 'Test'}
{start: 20, end: 40, payload: 'Test'}
],
'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000 align:foo\n' +
@@ -468,8 +469,8 @@ describe('VttTextParser', function() {
it('respects X-TIMESTAMP-MAP header', function() {
verifyHelper(
[
{start: 30, end: 50, text: 'Test'},
{start: 50, end: 60, text: 'Test2'}
{start: 30, end: 50, payload: 'Test'},
{start: 50, end: 60, payload: 'Test2'}
] ,
// 900000 = 10 sec, so expect every timestamp to be 10
// seconds ahead of what is specified.
@@ -497,18 +498,18 @@ describe('VttTextParser', function() {
for (var i = 0; i < cues.length; i++) {
expect(result[i].startTime).toBe(cues[i].start);
expect(result[i].endTime).toBe(cues[i].end);
expect(result[i].text).toBe(cues[i].text);
expect(result[i].payload).toBe(cues[i].payload);
// Workaround a bug in the compiler's externs.
// TODO: Remove when compiler is updated.
if (cues[i].id)
expect(result[i].id).toBe(cues[i].id);
if (cues[i].vertical)
expect(result[i].vertical).toBe(cues[i].vertical);
expect(result[i].writingDirection).toBe(cues[i].writingDirection);
if (cues[i].line)
expect(result[i].line).toBe(cues[i].line);
if (cues[i].align)
expect(/** @type {?} */ (result[i]).align).toBe(cues[i].align);
if (cues[i].textAlign)
expect(/** @type {?} */ (result[i]).textAlign).toBe(cues[i].textAlign);
if (cues[i].size)
expect(/** @type {?} */ (result[i]).size).toBe(cues[i].size);
if (cues[i].position)