-
Notifications
You must be signed in to change notification settings - Fork 92
/
test-runner.js
388 lines (355 loc) · 12.3 KB
/
test-runner.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/**
* Copyright 2013 vtt.js Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var NodeVTT = require("node-vtt"),
deepEqual = require("underscore").isEqual,
difflet = require("difflet")({ indent: 2, deepEqual: deepEqual }),
fs = require("fs"),
path = require("path"),
assert = Object.create(require("assert")),
async = require("async");
// Re-root paths to tests/* since we're in tests/util/*
function fixTestPath(filename) {
return path.join(__dirname, "..", "tests", filename);
}
function evalTest(vtt, json, testType, onDone) {
try {
vtt = JSON.parse(JSON.stringify(vtt));
} catch(e) {
return onDone(e);
}
if (!deepEqual(vtt, json)) {
return onDone(new Error("Failed JSON diff while " + testType + ".\n" +
difflet.compare(vtt, json)));
}
onDone();
}
function TestRunner() {
Object.defineProperty(this, "ready", {
get: function() { return this.nodeVTT; }
});
}
// Set up an instance of NodeVTT that we can use to run our tests.
TestRunner.prototype.init = function(onInit) {
this.nodeVTT = new NodeVTT();
this.nodeVTT.init({ uri: path.resolve(__dirname, "../utils/basic.html") }, onInit);
};
// Shutdown the NodeVTT instance that we've been using.
TestRunner.prototype.shutdown = function() {
this.nodeVTT.shutdown();
};
// Assert that the TestRunner is ready to run tests.
TestRunner.prototype.assertReady = function() {
if (!this.ready) {
assert.ok(false,
"TestRunner configured incorrectly. " +
"You must call TestRunner.init() before calling anything else.");
}
};
TestRunner.prototype.report = function(error, onDone) {
this.assertReady();
this.nodeVTT.setupParser("utf8", function() {
if (error) {
return onDone(error);
} else {
assert.ok(true, "Passed basic string and utf8 parsing.");
}
onDone();
});
};
// Compare JSON to live parsed utf8 and string data that has been passed as a whole to the parser.
TestRunner.prototype.jsonEqual = function(vttFile, jsonFile, message, onTestFinish) {
this.assertReady();
if (typeof message === "function") {
onTestFinish = message;
message = null;
}
message = message || "Compare parsed output of " + vttFile + " to " + jsonFile + ".";
jsonFile = fixTestPath(jsonFile);
vttFile = fixTestPath(vttFile);
var self = this,
json = require(jsonFile);
async.series({
utf8Parse: function(onDone) {
self.nodeVTT.parseFile(vttFile, onDone);
},
firstTest: function(onDone) {
evalTest(self.nodeVTT.vtt, json, "parsing utf8 without streaming",
onDone);
},
setupParser: function(onDone) {
self.nodeVTT.setupParser("string", onDone);
},
stringParse: function(onDone) {
var vtt = fs.readFileSync(vttFile, { encoding: "utf8" });
// Strip the BOM character if there is one.
vtt.charCodeAt(0) === 65279 && (vtt = vtt.substr(1));
self.nodeVTT.parse(vtt, onDone);
},
flush: function(onDone) {
self.nodeVTT.flush(onDone);
},
secondTest: function(onDone) {
evalTest(self.nodeVTT.vtt, json, "parsing string without streaming",
onDone);
}
}, function onDone(error) {
self.report(error, onTestFinish);
});
};
// Compare JSON to live parsed data that has been streamed to the parser.
TestRunner.prototype.jsonEqualStreaming = function(vttFile, jsonFile, message, onTestFinish) {
this.assertReady();
if (typeof message === "function") {
onTestFinish = message;
message = null;
}
message = message || "Compare parsed output of " + vttFile + " to " + jsonFile + " with streaming.";
jsonFile = fixTestPath(jsonFile);
vttFile = fixTestPath(vttFile);
var self = this,
json = require(jsonFile),
vtt = fs.readFileSync(vttFile),
size = vtt.length;
async.whilst(
function() {
return --size >= 3;
},
function(onWhilst) {
async.series({
parseFirst: function(onSeries) {
self.nodeVTT.parse(vtt.slice(0, size), onSeries);
},
parseSecond: function(onSeries) {
self.nodeVTT.parse(vtt.slice(size), onSeries);
},
flush: function(onSeries) {
self.nodeVTT.flush(onSeries);
},
vtt: function(onSeries) {
onSeries(null, self.nodeVTT.vtt);
},
clear: function(onSeries) {
self.nodeVTT.clear(onSeries);
}
}, function(error, results) {
evalTest(results.vtt, json,
"parsing utf8 with streaming; chunk number: " + size, onWhilst);
});
},
function(error) {
self.report(error, onTestFinish);
}
);
};
// Compare JSON to live parsed output for streaming VTT data and parsing whole
// VTT data.
TestRunner.prototype.jsonEqualParsing = function(vttFile, jsonFile, message, onTestFinish) {
this.assertReady();
var self = this;
if (typeof message === "function") {
onTestFinish = message;
message = null;
}
async.series({
first: function(onContinue) {
self.jsonEqual(vttFile, jsonFile, message, onContinue);
},
second: function(onContinue) {
self.jsonEqualStreaming(vttFile, jsonFile, message, onContinue);
}
}, onTestFinish);
};
const LINE_HEIGHT = 8;
const EDGES = {
"": {
line: "height",
topEdge: "top",
bottomEdge: "bottom",
max: 150
},
"rl": {
line: "width",
topEdge: "right",
bottomEdge: "left",
max: 300
},
"lr": {
line: "width",
topEdge: "left",
bottomEdge: "right",
max: 300
}
};
// This function attempts to 'balance' the computed values of the processing
// model CSS styles. On each platform and browser we will get different computed
// heights/widths, etc. Therefore, we can't use straight comparison of JSON.
function balanceComputedValues(cues, vtt, json) {
function getEdges(cue) {
var edges = EDGES[cue.vertical];
// If the cue has, or will have in the case of 'auto', a negative value
// then the reference edges are reversed. Line counts start fom the bottom
// of the writing direction, not the top.
var top = edges.topEdge,
bot = edges.bottomEdge;
if (cue.line < 0 || cue.line === "auto") {
var temp = top;
top = bot;
bot = temp;
}
return {
line: edges.line,
max: edges.max,
topEdge: top,
bottomEdge: bot
};
}
function pxToNum(num) {
return parseFloat(num.replace("px", ""));
}
try {
// Assumes that the 'json' object is a JSONified array and loops through
// each index, in this case key, and attempts to balance the comptued values
// of it in relation to the expected values in the corresponding JSON test
// file. It does this by calculating the computed difference in line heights
// of the expected value and the actual computed value and offsetting the
// actual value by a multiple of that amount depending on if the cue has been
// set to snap to lines, or a percentage.
Object.keys(json).forEach(function(index) {
var i = index | 0,
cue = cues[i],
js = json[i].style,
vs = vtt[i].style,
offset;
// If the cue has been set to snap to lines then we can figure out the
// difference between the computed and expected line height, determine how
// many 'steps' the cue has moved in any direction on the video (one step
// is the amount of one line height), and offset the actual computed values
// by that amount. This works because if the calculated position has the
// same step ratios as the expected position then it will be offset by the
// correct amount.
if (cue.snapToLines) {
var edges = getEdges(cue);
// If the line heights are the same then we don't need to balance them.
if (js[edges.line] !== vs[edges.line]) {
var lineCount = pxToNum(js[edges.line]) / LINE_HEIGHT,
computeDiff = (pxToNum(vs[edges.line]) - pxToNum(js[edges.line])) / lineCount,
vsTop = pxToNum(vs[edges.topEdge]),
vsBottom = pxToNum(vs[edges.bottomEdge]);
offset = computeDiff * (pxToNum(js[edges.topEdge]) / LINE_HEIGHT);
// Balance line height; should be the same.
vs[edges.line] = js[edges.line];
// Balance top and bottom edges by the offset amount.
if (vsTop !== 0 && vsTop !== edges.max) {
vs[edges.topEdge] = (vsTop - offset) + "px";
}
if (vsBottom !== 0 && vsBottom !== edges.max) {
var boxOffset = edges.max - Math.abs(pxToNum(js[edges.bottomEdge]));
if ((vsTop < 0 || vsBottom < pxToNum(js[edges.bottomEdge])) &&
boxOffset <= 0) {
offset *= -1;
}
if (boxOffset <= 0 || vsTop >= 0) {
boxOffset = computeDiff * lineCount;
} else {
boxOffset = ((boxOffset / LINE_HEIGHT) - 1) * computeDiff;
}
vs[edges.bottomEdge] = (vsBottom - offset + boxOffset) + "px";
}
}
// If the cue has not had its snap to lines flag set, meaning it is being
// positioned by a percentage, than we can simply move the percentage by
// the difference of the computed values.
} else {
var edge = EDGES[cue.vertical];
offset = pxToNum(vs[edge.line]) - pxToNum(js[edge.line]);
vs[edge.bottomEdge] = (pxToNum(vs[edge.bottomEdge]) + offset) + "px";
vs[edge.line] = js[edge.line];
}
});
} catch (e) {
fail("Failed on balancing computed values.", e);
}
}
// Compare JSON to live parsed data that has been run through the WebVTT
// processing model.
TestRunner.prototype.jsonEqualProcModel = function(vttFile, jsonFile, message, onTestFinish) {
this.assertReady();
if (typeof message === "function") {
onTestFinish = message;
message = null;
}
message = message || "Compare processed output of " + vttFile + " to " + jsonFile + ".";
jsonFile = fixTestPath(jsonFile);
vttFile = fixTestPath(vttFile);
var self = this,
json = require(jsonFile);
async.waterfall([
function(onContinue) {
self.nodeVTT.processFile(vttFile, function(error, processedData) {
return onContinue(error, processedData);
});
},
function(processedData, onContinue) {
balanceComputedValues(self.nodeVTT.cues, processedData, json);
onContinue(null, processedData);
},
function(processedData, onContinue) {
evalTest(processedData, json, "running the processing model",
onContinue);
},
], function(error) {
self.report(error, onTestFinish);
});
};
// Compare JSON to live parsed data that has been run through the processing model
// and parsed data without streaming.
TestRunner.prototype.jsonEqualAllNoStream = function(vttFile, jsonFile, message, onTestFinish) {
this.assertReady();
var self = this;
if (typeof message === "function") {
onTestFinish = message;
message = null;
}
async.series({
first: function(onContinue) {
self.jsonEqual(vttFile, jsonFile, message, onContinue);
},
second: function(onContinue) {
jsonFile = vttFile.replace(/\.vtt$/, "-proc.json");
self.jsonEqualProcModel(vttFile, jsonFile, message, onContinue);
}
}, onTestFinish);
};
// Compare JSON to live parsed output for streaming, parsing whole, and processing
// VTT data.
TestRunner.prototype.jsonEqualAll = function(vttFile, jsonFile, message, onTestFinish) {
this.assertReady();
var self = this;
if (typeof message === "function") {
onTestFinish = message;
message = null;
}
async.series({
first: function(onContinue) {
self.jsonEqualParsing(vttFile, jsonFile, message, onContinue);
},
second: function(onContinue) {
jsonFile = vttFile.replace(/\.vtt$/, "-proc.json");
self.jsonEqualProcModel(vttFile, jsonFile, message, onContinue);
}
}, onTestFinish);
};
module.exports = TestRunner;