Skip to content

Commit

Permalink
update DashAdapter unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosang committed Jan 9, 2019
1 parent e7b5daa commit 1037bee
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 120 deletions.
9 changes: 6 additions & 3 deletions src/dash/DashAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function DashAdapter() {
}

const manifest = voPeriods[0].mpd.manifest;
let realAdaptation = getAdaptationForType(manifest, streamInfo.index, type, streamInfo);
let realAdaptation = getAdaptationForType(streamInfo.index, type, streamInfo);
if (!realAdaptation) return null;

let selectedVoPeriod = getPeriodForStreamInfo(streamInfo, voPeriods);
Expand Down Expand Up @@ -153,7 +153,8 @@ function DashAdapter() {
idx,
i,
j,
ln;
ln,
periodId;

if (manifest) {
checkSetConfigCall();
Expand All @@ -168,7 +169,9 @@ function DashAdapter() {
}

const selectedVoPeriod = getPeriodForStreamInfo(streamInfo, voLocalPeriods);
const periodId = selectedVoPeriod.id;
if (selectedVoPeriod) {
periodId = selectedVoPeriod.id;
}
const adaptationsForType = dashManifestModel.getAdaptationsForType(manifest, streamInfo.index, type !== constants.EMBEDDED_TEXT ? type : constants.VIDEO);

if (!adaptationsForType) return mediaArr;
Expand Down
235 changes: 122 additions & 113 deletions test/unit/dash.DashAdapter.js
Original file line number Diff line number Diff line change
@@ -1,170 +1,179 @@
import DashAdapter from '../../src/dash/DashAdapter';
import MediaInfo from '../../src/streaming/vo/MediaInfo';

import MediaInfo from '../../src/dash/vo/MediaInfo';
import Constants from '../../src/streaming/constants/Constants';
import DashManifestModel from '../../src/dash/models/DashManifestModel';

import DashManifestModelMock from './mocks/DashManifestModelMock';
import VoHelper from './helpers/VOHelper';

const expect = require('chai').expect;

const context = {};
const dashManifestModelMock = new DashManifestModelMock();
const voHelper = new VoHelper();
const dashAdapter = DashAdapter(context).getInstance();
dashAdapter.setConfig({
dashManifestModel: dashManifestModelMock
});

describe('DashAdapter', function () {
describe('SetConfig not previously called', function () {

it('should return the first adaptation when getAdaptationForType is called and streamInfo is undefined', () => {
const manifest = { Period_asArray: [{ AdaptationSet_asArray: [{ id: 0, mimeType: 'video' }, { id: 1, mimeType: 'video' }] }] };
const adaptation = dashAdapter.getAdaptationForType(manifest, 0, 'video');
it('should throw an exception when attempting to call getStreamsInfo While the setConfig function was not called, and externalManifest parameter is defined', function () {
expect(dashAdapter.getStreamsInfo.bind(dashAdapter,{})).to.throw('setConfig function has to be called previously');
});

expect(adaptation.id).to.equal(0); // jshint ignore:line
});
it('should throw an exception when attempting to call getAllMediaInfoForType While the setConfig function was not called, and externalManifest parameter is defined', function () {
expect(dashAdapter.getAllMediaInfoForType.bind(dashAdapter, null, null, {})).to.throw('setConfig function has to be called previously');
});

it('should return the correct adaptation when getAdaptationForType is called', () => {
const manifest = { Period_asArray: [{ AdaptationSet_asArray: [{ id: undefined, mimeType: 'audio', lang: 'eng', Role_asArray: [{ value: 'main' }] }, { id: undefined, mimeType: 'audio', lang: 'deu', Role_asArray: [{ value: 'main' }] }] }] };
it('should return an empty array when getEventsFor is called and voPeriods is an empty array', function () {
dashAdapter.reset();
const eventsArray = dashAdapter.getEventsFor();

const streamInfo = {
id: 'id'
};
expect(eventsArray).to.be.instanceOf(Array); // jshint ignore:line
expect(eventsArray).to.be.empty; // jshint ignore:line
});

const track = new MediaInfo();
it('should return an empty array when getAllMediaInfoForType is called and voPeriods is an empty array', function () {
const mediaInfoArray = dashAdapter.getAllMediaInfoForType();

track.id = undefined;
track.index = 1;
track.streamInfo = streamInfo;
track.representationCount = 0;
track.lang = 'deu';
track.roles = ['main'];
track.codec = 'audio/mp4;codecs="mp4a.40.2"';
track.mimeType = 'audio/mp4';
expect(mediaInfoArray).to.be.instanceOf(Array); // jshint ignore:line
expect(mediaInfoArray).to.be.empty; // jshint ignore:line
});

dashAdapter.setCurrentMediaInfo(streamInfo.id, 'audio', track);
it('should return null when updatePeriods is called and newManifest is undefined', function () {
const returnValue = dashAdapter.updatePeriods();

const adaptation = dashAdapter.getAdaptationForType(manifest, 0, 'audio', streamInfo);
expect(returnValue).to.be.null; // jshint ignore:line
});

expect(adaptation.lang).to.equal('eng'); // jshint ignore:line
});
it('should throw an error when updatePeriods is called and newManifest parameter is defined, while setConfig has not been called', function () {
expect(dashAdapter.updatePeriods.bind(dashAdapter,{})).to.be.throw('setConfig function has to be called previously');
});

it('should throw an exception when attempting to call getStreamsInfo While the setConfig function was not called, and externalManifest parameter is defined', function () {
expect(dashAdapter.getStreamsInfo.bind(dashAdapter,{})).to.throw('setConfig function has to be called previously');
});
it('should return null when getMediaInfoForType is called and voPeriods is an empty array', function () {
const mediaInfo = dashAdapter.getMediaInfoForType();

it('should throw an exception when attempting to call getAllMediaInfoForType While the setConfig function was not called, and externalManifest parameter is defined', function () {
expect(dashAdapter.getAllMediaInfoForType.bind(dashAdapter, null, null, {})).to.throw('setConfig function has to be called previously');
});
expect(mediaInfo).to.be.null; // jshint ignore:line
});

it('should return an empty array when getEventsFor is called and voPeriods is an empty array', function () {
dashAdapter.reset();
const eventsArray = dashAdapter.getEventsFor();
it('should return null when getDataForMedia is called and voPeriods is an empty array, mediaInfo parameter is undefined', function () {
const adaptation = dashAdapter.getDataForMedia();

expect(eventsArray).to.be.instanceOf(Array); // jshint ignore:line
expect(eventsArray).to.be.empty; // jshint ignore:line
});
expect(adaptation).to.be.null; // jshint ignore:line
});

it('should return an empty array when getAllMediaInfoForType is called and voPeriods is an empty array', function () {
const mediaInfoArray = dashAdapter.getAllMediaInfoForType();
it('should return null when getDataForMedia is called and voPeriods is an empty array, mediaInfo parameter is an empty object', function () {
const adaptation = dashAdapter.getDataForMedia({});

expect(mediaInfoArray).to.be.instanceOf(Array); // jshint ignore:line
expect(mediaInfoArray).to.be.empty; // jshint ignore:line
});
expect(adaptation).to.be.null; // jshint ignore:line
});

it('should return null when updatePeriods is called and newManifest is undefined', function () {
const returnValue = dashAdapter.updatePeriods();
it('should return null when getEvent is called and no parameter is set', function () {
const event = dashAdapter.getEvent();

expect(returnValue).to.be.null; // jshint ignore:line
});
expect(event).to.be.null; // jshint ignore:line
});

it('should throw an error when updatePeriods is called and newManifest parameter is defined, while setConfig has not been called', function () {
expect(dashAdapter.updatePeriods.bind(dashAdapter,{})).to.be.throw('setConfig function has to be called previously');
});
it('should return null when getEvent is called and an empty eventBox parameter is set and eventStreams is undefined', function () {
const event = dashAdapter.getEvent({});

it('should return null when getMediaInfoForType is called and voPeriods is an empty array', function () {
const mediaInfo = dashAdapter.getMediaInfoForType();
expect(event).to.be.null; // jshint ignore:line
});

expect(mediaInfo).to.be.null; // jshint ignore:line
});
it('should return null when getEvent is called and an empty eventBox and eventStreams parameters are set', function () {
const event = dashAdapter.getEvent({}, []);

it('should return null when getDataForMedia is called and voPeriods is an empty array, mediaInfo parameter is undefined', function () {
const adaptation = dashAdapter.getDataForMedia();
expect(event).to.be.null; // jshint ignore:line
});

expect(adaptation).to.be.null; // jshint ignore:line
});
it('should return an empty event object when getEvent is called and eventBox and eventStreams parameters are set', function () {
const event = dashAdapter.getEvent({scheme_id_uri: 'id', value: 'value'}, {'id/value': {}});

it('should return null when getDataForMedia is called and voPeriods is an empty array, mediaInfo parameter is an empty object', function () {
const adaptation = dashAdapter.getDataForMedia({});
expect(event).to.be.an('object');
});

expect(adaptation).to.be.null; // jshint ignore:line
});

it('should return null when getEvent is called and no parameter is set', function () {
const event = dashAdapter.getEvent();
it('should return undefined when getRealAdaptation is called and streamInfo parameter is null or undefined', function () {
const realAdaptation = dashAdapter.getRealAdaptation(null,voHelper.getDummyMediaInfo('video'));

expect(event).to.be.null; // jshint ignore:line
});
expect(realAdaptation).to.be.undefined; // jshint ignore:line
});

it('should return null when getEvent is called and an empty eventBox parameter is set and eventStreams is undefined', function () {
const event = dashAdapter.getEvent({});
it('should return undefined when getRealAdaptation is called and mediaInfo parameter is null or undefined', function () {
const realAdaptation = dashAdapter.getRealAdaptation(voHelper.getDummyStreamInfo(), null);

expect(event).to.be.null; // jshint ignore:line
expect(realAdaptation).to.be.undefined; // jshint ignore:line
});
});

it('should return null when getEvent is called and an empty eventBox and eventStreams parameters are set', function () {
const event = dashAdapter.getEvent({}, []);
describe('SetConfig previously called', function () {
it('should return undefined when getVoRepresentations is called and mediaInfo parameter is null or undefined', function () {
dashAdapter.setConfig({dashManifestModel: new DashManifestModelMock()});
const voRepresentations = dashAdapter.getVoRepresentations();

expect(event).to.be.null; // jshint ignore:line
});
expect(voRepresentations).to.be.instanceOf(Array); // jshint ignore:line
expect(voRepresentations).to.be.empty; // jshint ignore:line
});

it('should return an empty event object when getEvent is called and eventBox and eventStreams parameters are set', function () {
const event = dashAdapter.getEvent({scheme_id_uri: 'id', value: 'value'}, {'id/value': {}});
it('should return null when getBandwidthForRepresentation is called and representationId and periodId are undefined', () => {
dashAdapter.setConfig({dashManifestModel: new DashManifestModelMock()});
dashAdapter.updatePeriods({});
const bdwth = dashAdapter.getBandwidthForRepresentation();

expect(event).to.be.an('object');
});
expect(bdwth).to.be.null; // jshint ignore:line
});

it('should return undefined when getRealAdaptation is called and streamInfo parameter is null or undefined', function () {
const realAdaptation = dashAdapter.getRealAdaptation(null,voHelper.getDummyMediaInfo('video'));
it('should return -1 when getIndexForRepresentation is called and representationId and periodIdx are undefined', () => {
dashAdapter.setConfig({dashManifestModel: new DashManifestModelMock()});
dashAdapter.updatePeriods({});
const index = dashAdapter.getIndexForRepresentation();

expect(realAdaptation).to.be.undefined; // jshint ignore:line
});
expect(index).to.be.equal(-1); // jshint ignore:line
});

it('should return undefined when getRealAdaptation is called and mediaInfo parameter is null or undefined', function () {
const realAdaptation = dashAdapter.getRealAdaptation(voHelper.getDummyStreamInfo(), null);
it('should return -1 when getMaxIndexForBufferType is called and bufferType and periodIdx are undefined', () => {
dashAdapter.setConfig({dashManifestModel: new DashManifestModelMock()});
dashAdapter.updatePeriods({});
const index = dashAdapter.getMaxIndexForBufferType();

expect(realAdaptation).to.be.undefined; // jshint ignore:line
});
expect(index).to.be.equal(-1); // jshint ignore:line
});

it('should return undefined when getVoRepresentations is called and mediaInfo parameter is null or undefined', function () {
dashAdapter.setConfig({dashManifestModel: new DashManifestModelMock()});
const voRepresentations = dashAdapter.getVoRepresentations();
it('should return the first adaptation when getAdaptationForType is called and streamInfo is undefined', () => {
dashAdapter.setConfig({
dashManifestModel: DashManifestModel(context).getInstance(),
constants: Constants
});
const manifest = { loadedTime: new Date(), mediaPresentationDuration: 10, Period_asArray: [{ AdaptationSet_asArray: [{ id: 0, mimeType: 'video' }, { id: 1, mimeType: 'video' }] }] };
dashAdapter.updatePeriods(manifest);
const adaptation = dashAdapter.getAdaptationForType(0, 'video');

expect(voRepresentations).to.be.instanceOf(Array); // jshint ignore:line
expect(voRepresentations).to.be.empty; // jshint ignore:line
});
expect(adaptation.id).to.equal(0); // jshint ignore:line
});

it('should return null when getBandwidthForRepresentation is called and representationId and periodId are undefined', () => {
dashAdapter.setConfig({dashManifestModel: new DashManifestModelMock()});
dashAdapter.updatePeriods({});
const bdwth = dashAdapter.getBandwidthForRepresentation();
it('should return the correct adaptation when getAdaptationForType is called', () => {
const manifest = { loadedTime: new Date(), mediaPresentationDuration: 10, Period_asArray: [{ AdaptationSet_asArray: [{ id: undefined, mimeType: 'audio', lang: 'eng', Role_asArray: [{ value: 'main' }] }, { id: undefined, mimeType: 'audio', lang: 'deu', Role_asArray: [{ value: 'main' }] }] }] };
dashAdapter.setConfig({
dashManifestModel: DashManifestModel(context).getInstance(),
constants: Constants
});
const streamInfo = {
id: 'id'
};

expect(bdwth).to.be.null; // jshint ignore:line
});
const track = new MediaInfo();

it('should return -1 when getIndexForRepresentation is called and representationId and periodIdx are undefined', () => {
dashAdapter.setConfig({dashManifestModel: new DashManifestModelMock()});
dashAdapter.updatePeriods({});
const index = dashAdapter.getIndexForRepresentation();

expect(index).to.be.equal(-1); // jshint ignore:line
});
track.id = undefined;
track.index = 1;
track.streamInfo = streamInfo;
track.representationCount = 0;
track.lang = 'deu';
track.roles = ['main'];
track.codec = 'audio/mp4;codecs="mp4a.40.2"';
track.mimeType = 'audio/mp4';

it('should return -1 when getMaxIndexForBufferType is called and bufferType and periodIdx are undefined', () => {
dashAdapter.setConfig({dashManifestModel: new DashManifestModelMock()});
dashAdapter.updatePeriods({});
const index = dashAdapter.getMaxIndexForBufferType();
dashAdapter.setCurrentMediaInfo(streamInfo.id, 'audio', track);
dashAdapter.updatePeriods(manifest);
const adaptation = dashAdapter.getAdaptationForType(0, 'audio', streamInfo);

expect(index).to.be.equal(-1); // jshint ignore:line
expect(adaptation.lang).to.equal('eng'); // jshint ignore:line
});
});
});
28 changes: 24 additions & 4 deletions test/unit/mocks/DashManifestModelMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,32 @@ function DashManifestModelMock () {
return [];
};

this.getMpd = function () {
return {};
this.getMpd = function (manifest) {
let mpd = {};
if (manifest) {
mpd.manifest = manifest;
}

return mpd;
};

this.getRegularPeriods = function () {
return [];
this.getRegularPeriods = function (mpd) {
const voPeriods = [];
if (mpd && mpd.manifest && mpd.manifest.Period_asArray) {
voPeriods.push({mpd: mpd});
}

return voPeriods;
};

this.getAdaptationsForPeriod = function (voPeriod) {
let voAdaptations = [];

if (voPeriod) {
voAdaptations.push(voPeriod.mpd.manifest.Period_asArray[0]);
}

return voAdaptations;
};
}

Expand Down

0 comments on commit 1037bee

Please sign in to comment.