Skip to content

Commit

Permalink
Merge pull request video-dev#1575 from ssreed/add-unit-test
Browse files Browse the repository at this point in the history
add level controller unit test, update eme controller unit test
  • Loading branch information
azu authored Feb 28, 2018
2 parents bbd69fc + 30b7fdd commit dda34ba
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 5 deletions.
44 changes: 39 additions & 5 deletions tests/unit/controller/eme-controller.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import EMEController from '../../../src/controller/eme-controller';

import assert from 'assert';
import HlsMock from '../../mocks/hls.mock';
import EventEmitter from 'events';
import {ErrorTypes, ErrorDetails} from '../../../src/errors';

const MediaMock = function() {
return {
setMediaKeys: sinon.spy(),
addEventListener: sinon.spy()
}
let media = new EventEmitter();
media.setMediaKeys = sinon.spy();
media.addEventListener = media.addListener.bind(media);
return media;
};

const fakeLevels = [
Expand Down Expand Up @@ -77,4 +79,36 @@ describe('EMEController', () => {
}, 0)

});

it('should trigger key system error when bad encrypted data is received', (done) => {

let reqMediaKsAccessSpy = sinon.spy(() => {
return Promise.resolve({
// Media-keys mock
})
});

setupEach({
emeEnabled: true,
requestMediaKeySystemAccessFunc: reqMediaKsAccessSpy
});

let badData = {
initDataType: "cenc",
initData: "bad data"
};

emeController.onMediaAttached({media});
emeController.onManifestParsed({levels: fakeLevels});

media.emit('encrypted', badData);

setTimeout(() => {
assert.equal(emeController.hls.trigger.args[0][1].details, ErrorDetails.KEY_SYSTEM_NO_KEYS);
assert.equal(emeController.hls.trigger.args[1][1].details, ErrorDetails.KEY_SYSTEM_NO_ACCESS);
done();
}, 0)

});

})
93 changes: 93 additions & 0 deletions tests/unit/controller/level-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
const assert = require('assert');
const sinon = require('sinon');

import LevelController from '../../../src/controller/level-controller';
import HlsMock from '../../mocks/hls.mock';
import Event from '../../../src/events';
import { ErrorTypes, ErrorDetails } from '../../../src/errors'



describe('LevelController', () => {
let hls, levelController;

beforeEach(() => {
hls = new HlsMock();
levelController = new LevelController(hls);
});

afterEach(() => {
hls.destroy();
hls = null;
levelController = null;
});

it('should trigger an error when no levels are found in the manifest', () => {

levelController.onManifestLoaded({
audioTracks: [],
levels: [],
networkDetails: "",
subtitles: [],
url: "https://video-dev.github.io/streams/x36xhzz/x36xhzz.m3u8"
});

assert.equal(levelController.hls.trigger.args[0][1].details, ErrorDetails.MANIFEST_INCOMPATIBLE_CODECS_ERROR);

});

it('should trigger hlsManifestParsed when levels are found in the manifest', () => {

let data = {
audioTracks: [],
levels: [
{ bitrate: 105000, name: "144", details: { totalduration: 10, fragments: [{}] } },
{ bitrate: 246440, name: "240", details: { totalduration: 10, fragments: [{}] } },
{ bitrate: 460560, name: "380", details: { totalduration: 10, fragments: [{}] } },
{ bitrate: 836280, name: "480", details: { totalduration: 10, fragments: [{}] } },
{ bitrate: 2149280, name: "720", details: { totalduration: 10, fragments: [{}] } },
{ bitrate: 6221600, name: "1080", details: { totalduration: 10, fragments: [{}] } }
],
networkDetails: "",
subtitles: [],
stats: {},
url: "https://video-dev.github.io/streams/x36xhzz/x36xhzz.m3u8"
};

levelController.onManifestLoaded(data);


assert.equal(levelController.hls.trigger.args[0][0], Event.MANIFEST_PARSED);
assert.equal(levelController.hls.trigger.args[0][1].levels.length, data.levels.length);


});

it('should trigger level switch when level is manually set', () => {

let data = {
audioTracks: [],
levels: [
{ bitrate: 105000, name: "144", details: { totalduration: 10, fragments: [{}] } },
{ bitrate: 246440, name: "240", details: { totalduration: 10, fragments: [{}] } },
{ bitrate: 460560, name: "380", details: { totalduration: 10, fragments: [{}] } },
{ bitrate: 836280, name: "480", details: { totalduration: 10, fragments: [{}] } },
{ bitrate: 2149280, name: "720", details: { totalduration: 10, fragments: [{}] } },
{ bitrate: 6221600, name: "1080", details: { totalduration: 10, fragments: [{}] } }
],
networkDetails: "",
subtitles: [],
url: "https://video-dev.github.io/streams/x36xhzz/x36xhzz.m3u8"
};

let nextLevel = 1;

levelController.onManifestLoaded(data);
levelController.level = nextLevel;

assert.equal(levelController.hls.trigger.args[1][0],Event.LEVEL_SWITCH);
assert.equal(levelController.hls.trigger.args[1][1].level, nextLevel);

});

});

0 comments on commit dda34ba

Please sign in to comment.