Skip to content

Commit

Permalink
test: add support for real segments in tests (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
gesinger authored and forbesjo committed Aug 7, 2018
1 parent 6c68761 commit 2b07fca
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -35,6 +35,7 @@ docs/api/
es5/
tmp
test/test-manifests.js
test/test-segments.js
test/test-expected.js
coverage/

Expand Down
13 changes: 9 additions & 4 deletions package.json
Expand Up @@ -15,7 +15,7 @@
"build:webworker": "rollup -c scripts/webworker.rollup.config.js",
"clean": "run-p clean:dist clean:test",
"clean:dist": "rimraf dist dist-test",
"clean:test": "node -e \"var b=require('./scripts/manifest-data.js'); b.clean();\"",
"clean:test": "node scripts/clean-tests.js",
"postclean": "mkdirp dist",
"docs": "run-p docs:*",
"docs:api": "jsdoc src -r -d docs/api",
Expand All @@ -26,14 +26,16 @@
"start:server": "node scripts/server.js",
"pretest": "run-s lint build:test:js",
"test": "karma start test/karma.conf.js",
"prebuild:test:js": "run-s build:webworker build:test:manifest",
"prebuild:test:js": "run-s build:webworker build:test:manifest build:test:segments",
"build:test:js": "rollup -c scripts/test.rollup.config.js",
"build:test:manifest": "node -e \"var b=require('./scripts/manifest-data.js'); b.build();\"",
"watch": "run-p watch:js watch:test:manifest watch:test:js watch:js:switcher",
"build:test:segments": "node -e \"var b=require('./scripts/segments-data.js'); b.build();\"",
"watch": "run-p watch:js watch:test:manifest watch:test:segments watch:test:js watch:js:switcher",
"watch:js": "rollup -c scripts/rollup.config.js -w",
"watch:js:switcher": "rollup -c scripts/switcher.rollup.config.js -w",
"watch:test:js": "rollup -c scripts/test.rollup.config.js -w",
"watch:test:manifest": "node -e \"var b=require('./scripts/manifest-data.js'); b.watch();\"",
"watch:test:segments": "node -e \"var b=require('./scripts/segments-data.js'); b.watch();\"",
"preversion": "npm test",
"version": "node scripts/version.js",
"prepublish": "in-publish && npm run build || not-in-publish"
Expand All @@ -46,13 +48,16 @@
"license": "Apache-2.0",
"vjsstandard": {
"ignore": [
"es5",
"dist",
"dist-test",
"docs",
"test/karma.conf.js",
"scripts",
"utils",
"test/test-manifests.js"
"test/test-manifests.js",
"test/test-segments.js",
"*.worker.*"
]
},
"files": [
Expand Down
5 changes: 5 additions & 0 deletions scripts/clean-tests.js
@@ -0,0 +1,5 @@
const manifestData = require('./manifest-data');
const segmentsData = require('./segments-data');

manifestData.clean();
segmentsData.clean();
70 changes: 70 additions & 0 deletions scripts/segments-data.js
@@ -0,0 +1,70 @@
const fs = require('fs');
const path = require('path');

const basePath = path.resolve(__dirname, '..');
const testDir = path.join(basePath, 'test');
const segmentsDir = path.join(testDir, 'segments');
const segmentsFilepath = path.join(testDir, 'test-segments.js');

const base64ToUint8Array = (base64) => {
const decoded = window.atob(base64);
const uint8Array = new Uint8Array(new ArrayBuffer(decoded.length));

for(let i = 0; i < decoded.length; i++) {
uint8Array[i] = decoded.charCodeAt(i);
}

return uint8Array;
};

module.exports = {
build() {
const files = fs.readdirSync(segmentsDir);
const segmentData = {};

while (files.length > 0) {
const file = path.resolve(segmentsDir, files.shift());
const extname = path.extname(file);

if (extname === '.ts') {
// read the file directly as a buffer before converting to base64
const base64Segment = fs.readFileSync(file).toString('base64');

segmentData[path.basename(file, extname)] = base64Segment;
} else {
console.log(`Unknown file ${file} found in segments dir ${segmentsDir}`);
}
}

const segmentDataExportStrings = Object.keys(segmentData).reduce((acc, key) => {
// use a function since the segment may be cleared out on usage
acc.push(`export const ${key} = () => base64ToUint8Array('${segmentData[key]}');`);
return acc;
}, []);

let segmentsFile =
`const base64ToUint8Array = ${base64ToUint8Array.toString()};\n` +
segmentDataExportStrings.join('\n');

fs.writeFileSync(segmentsFilepath, segmentsFile);
console.log('Wrote test data file ' + segmentsFilepath);
},

watch() {
this.build();
fs.watch(segmentsDir, (event, filename) => {
console.log('files in segments dir were changed rebuilding segments data');
this.build();
});
},

clean() {
if (fs.existsSync(segmentsFilepath)) {
try {
fs.unlinkSync(segmentsFilepath);
} catch(e) {
console.log(e);
}
}
}
};
11 changes: 7 additions & 4 deletions test/master-playlist-controller.test.js
Expand Up @@ -17,6 +17,7 @@ import Playlist from '../src/playlist';
import Config from '../src/config';
import PlaylistLoader from '../src/playlist-loader';
import DashPlaylistLoader from '../src/dash-playlist-loader';
import { muxed as muxedSegment } from './test-segments';

QUnit.module('MasterPlaylistController', {
beforeEach(assert) {
Expand Down Expand Up @@ -585,17 +586,19 @@ function(assert) {
this.standardXHRResponse(this.requests.shift(), audioMedia);

// video segment
this.standardXHRResponse(this.requests.shift());

this.standardXHRResponse(this.requests.shift(), muxedSegment());
// source buffer is mocked, so must manually trigger the video buffer
// video buffer is the first buffer created
MPC.mediaSource.sourceBuffers[0].trigger('updateend');

assert.equal(videoEnded, 1, 'main segment loader triggered ended');
assert.equal(audioEnded, 0, 'audio segment loader did not trigger ended');
assert.equal(MPC.mediaSource.readyState, 'open', 'Media Source not yet ended');

// audio segment
this.standardXHRResponse(this.requests.shift());

this.standardXHRResponse(this.requests.shift(), muxedSegment());
// source buffer is mocked, so must manually trigger the audio buffer
// audio buffer is the second buffer created
MPC.mediaSource.sourceBuffers[1].trigger('updateend');

assert.equal(videoEnded, 1, 'main segment loader did not trigger ended again');
Expand Down
Binary file added test/segments/muxed.ts
Binary file not shown.
8 changes: 6 additions & 2 deletions test/test-helpers.js
Expand Up @@ -348,8 +348,12 @@ export const standardXHRResponse = function(request, data) {
data = testDataManifests[manifestName];
}

request.response = new Uint8Array(1024).buffer;
request.respond(200, {'Content-Type': contentType}, data);
request.response =
// if segment data was passed, use that, otherwise use a placeholder
data instanceof Uint8Array ? data.buffer : new Uint8Array(1024).buffer;
request.respond(200,
{ 'Content-Type': contentType },
data instanceof Uint8Array ? '' : data);
};

// return an absolute version of a page-relative URL
Expand Down
1 change: 0 additions & 1 deletion test/videojs-http-streaming.test.js
Expand Up @@ -25,7 +25,6 @@ import {
import window from 'global/window';
// we need this so the plugin registers itself
import 'videojs-contrib-quality-levels';
/* eslint-enable no-unused-vars */

const ogHlsHandlerSetupQualityLevels = videojs.HlsHandler.prototype.setupQualityLevels_;

Expand Down

0 comments on commit 2b07fca

Please sign in to comment.