Skip to content

Commit

Permalink
Code refactoring: update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertrand Berthelot committed May 19, 2020
1 parent 6208a90 commit db9b083
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 293 deletions.
62 changes: 29 additions & 33 deletions test/unit/Streaming.StreamProcessor.js
Expand Up @@ -24,52 +24,48 @@ const adapterMock = new AdapterMock();
const eventBus = EventBus(context).getInstance();

const streamInfo = {
streamId: 'streamId',
id: 'streamId',
manifestInfo: {
isDynamic: true
}
};

describe('StreamProcessor', function () {
it('should return NaN when getIndexHandlerTime is called and streamProcessor is defined, without its attributes', function () {
const streamProcessor = StreamProcessor(context).create({});
const time = streamProcessor.getIndexHandlerTime();
describe('StreamProcessor not initialized', function () {
let streamProcessor = null;

expect(time).to.be.NaN; // jshint ignore:line
});

it('should not throw an error when setIndexHandlerTime is called and indexHandler is undefined', function () {
const streamProcessor = StreamProcessor(context).create({});

expect(streamProcessor.setIndexHandlerTime.bind(streamProcessor)).to.not.throw();
});

it('should return null when getInitRequest is called and indexHandler is undefined', function () {
const streamProcessor = StreamProcessor(context).create({});

const initRequest = streamProcessor.getInitRequest(0);

expect(initRequest).to.be.null; // jshint ignore:line
});
beforeEach(function () {
streamProcessor = StreamProcessor(context).create({});
});

it('should throw an error when getInitRequest is called and streamProcessor is defined, but quality is not a number', function () {
const streamProcessor = StreamProcessor(context).create({});
afterEach(function () {
streamProcessor.reset();
});

expect(streamProcessor.getInitRequest.bind(streamProcessor, {})).to.be.throw(Constants.BAD_ARGUMENT_ERROR + ' : argument is not an integer');
});
it('getIndexHandlerTime should return NaN', function () {
const time = streamProcessor.getIndexHandlerTime();
expect(time).to.be.NaN; // jshint ignore:line
});

it('should return null when getFragmentRequest is called and without parameters', function () {
const streamProcessor = StreamProcessor(context).create({});
it('setIndexHandlerTime should not throw an error', function () {
expect(streamProcessor.setIndexHandlerTime.bind(streamProcessor)).to.not.throw();
});

const nextFragRequest = streamProcessor.getFragmentRequest();
it('getInitRequest should return null', function () {
const initRequest = streamProcessor.getInitRequest(0);
expect(initRequest).to.be.null; // jshint ignore:line
});

expect(nextFragRequest).to.be.null; // jshint ignore:line
});
it('getInitRequest should throw an error when quality is not a number', function () {
expect(streamProcessor.getInitRequest.bind(streamProcessor, {})).to.be.throw(Constants.BAD_ARGUMENT_ERROR + ' : argument is not an integer');
});

describe('representationController parameter is properly defined, without its attributes', () => {
const streamProcessor = StreamProcessor(context).create({});
it('getFragmentRequest should return null', function () {
const nextFragRequest = streamProcessor.getFragmentRequest();
expect(nextFragRequest).to.be.null; // jshint ignore:line
});

it('should throw an error when getRepresentationInfo is called and representationController parameter is defined, but quality is not a number', function () {
it('getRepresentationInfo should throw an error when quality is not a number', function () {
expect(streamProcessor.getRepresentationInfo.bind(streamProcessor, {})).to.be.throw(Constants.BAD_ARGUMENT_ERROR + ' : argument is not an integer');
});
});
Expand All @@ -91,7 +87,7 @@ describe('StreamProcessor', function () {
let dvrInfo = dashMetricsMock.getCurrentDVRInfo();
expect(dvrInfo).to.be.null; // jshint ignore:line

eventBus.trigger(Events.BUFFER_LEVEL_UPDATED, { sender: { getStreamProcessor() { return streamProcessor;}}, bufferLevel: 50 });
eventBus.trigger(Events.BUFFER_LEVEL_UPDATED, { streamId: streamInfo.id, mediaType: testType, bufferLevel: 50 });

dvrInfo = dashMetricsMock.getCurrentDVRInfo();
expect(dvrInfo).not.to.be.null; // jshint ignore:line
Expand Down
2 changes: 1 addition & 1 deletion test/unit/mocks/AdapterMock.js
Expand Up @@ -111,4 +111,4 @@ function AdapterMock () {
};
}

export default AdapterMock;
export default AdapterMock;
72 changes: 38 additions & 34 deletions test/unit/streaming.Stream.js
Expand Up @@ -48,25 +48,29 @@ describe('Stream', function () {
const timelineConverter = objectsHelper.getDummyTimelineConverter();
const streamInfo = {
id: 'id',
index: 'index'
index: 'index',
manifestInfo: {
isDynamic: false
}
};
Events.extend(ProtectionEvents);

describe('Well initialized', function () {
beforeEach(function () {
stream = Stream(context).create({errHandler: errHandlerMock,
manifestModel: manifestModelMock,
adapter: adapterMock,
abrController: abrControllerMock,
manifestUpdater: manifestUpdaterMock,
playbackController: playbackControllerMock,
capabilities: capabilitiesMock,
mediaController: mediaControllerMock,
timelineConverter: timelineConverter,
dashMetrics: dashMetricsMock,
textController: textControllerMock,
videoModel: videoModelMock,
settings: settings});
stream = Stream(context).create({
errHandler: errHandlerMock,
manifestModel: manifestModelMock,
adapter: adapterMock,
abrController: abrControllerMock,
manifestUpdater: manifestUpdaterMock,
playbackController: playbackControllerMock,
capabilities: capabilitiesMock,
mediaController: mediaControllerMock,
timelineConverter: timelineConverter,
dashMetrics: dashMetricsMock,
textController: textControllerMock,
videoModel: videoModelMock,
settings: settings});
});

afterEach(function () {
Expand All @@ -76,14 +80,14 @@ describe('Stream', function () {
it('should return false when isActive is called', () => {
const isActive = stream.isActive();

expect(isActive).to.be.false; // jshint ignore:line
expect(isActive).to.be.false; // jshint ignore:line
});

it('should return an empty array when getProcessors is called but streamProcessors attribute is an empty array', () => {
const processors = stream.getProcessors();

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

it('should trigger MANIFEST_ERROR_ID_NOSTREAMS_CODE error when setMediaSource is called but streamProcessors array is empty', () => {
Expand All @@ -100,51 +104,51 @@ describe('Stream', function () {
it('should return an NaN when getStartTime is called but streamInfo attribute is null or undefined', () => {
const startTime = stream.getStartTime();

expect(startTime).to.be.NaN; // jshint ignore:line
expect(startTime).to.be.NaN; // jshint ignore:line
});

it('should return an NaN when getDuration is called but streamInfo attribute is null or undefined', () => {
const duration = stream.getDuration();

expect(duration).to.be.NaN; // jshint ignore:line
expect(duration).to.be.NaN; // jshint ignore:line
});

it('should return null false isMediaCodecCompatible is called but stream attribute is undefined', () => {
const isCompatible = stream.isMediaCodecCompatible();

expect(isCompatible).to.be.false; // jshint ignore:line
expect(isCompatible).to.be.false; // jshint ignore:line
});

it('should return false when isMediaCodecCompatible is called but stream attribute is an empty object', () => {
const isCompatible = stream.isMediaCodecCompatible({});

expect(isCompatible).to.be.false; // jshint ignore:line
expect(isCompatible).to.be.false; // jshint ignore:line
});

it('should return false when isMediaCodecCompatible is called with a correct stream attribute', () => {
const isCompatible = stream.isMediaCodecCompatible(new StreamMock());

expect(isCompatible).to.be.false; // jshint ignore:line
expect(isCompatible).to.be.false; // jshint ignore:line
});

it('should return null when isProtectionCompatible is called but stream attribute is undefined', () => {
const isCompatible = stream.isProtectionCompatible();

expect(isCompatible).to.be.false; // jshint ignore:line
expect(isCompatible).to.be.false; // jshint ignore:line
});

it('should return an empty array when getBitrateListFor is called but no stream processor is defined', () => {
const bitrateList = stream.getBitrateListFor('');

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

it('should return an empty array when getBitrateListFor, for image type, is called but thumbnailController is not defined', () => {
const bitrateList = stream.getBitrateListFor(Constants.IMAGE);

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

it('should not call STREAM_INITIALIZED event if initializeMedia has not been called when updateData is called', () => {
Expand All @@ -154,7 +158,7 @@ describe('Stream', function () {

stream.updateData(streamInfo);

expect(spy.notCalled).to.be.true; // jshint ignore:line
expect(spy.notCalled).to.be.true; // jshint ignore:line

eventBus.off(Events.STREAM_INITIALIZED, spy);
});
Expand Down Expand Up @@ -209,31 +213,31 @@ describe('Stream', function () {

let isPreloaded = stream.getPreloaded();

expect(isPreloaded).to.be.false; // jshint ignore:line
expect(isPreloaded).to.be.false; // jshint ignore:line

stream.preload();

isPreloaded = stream.getPreloaded();

expect(isPreloaded).to.be.true; // jshint ignore:line
expect(isPreloaded).to.be.true; // jshint ignore:line
});

it('should return undefined when getThumbnailController is called without a call to initializeMediaForType', () => {
stream.initialize(streamInfo, {});

const thumbnailController = stream.getThumbnailController();

expect(thumbnailController).to.be.undefined; // jshint ignore:line
expect(thumbnailController).to.be.undefined; // jshint ignore:line
});

it('should returns an empty array when activate is called', function () {
stream.initialize(streamInfo, {});
// it('should returns an array of buffers when activate is called', function () {
// stream.initialize(streamInfo, {});

const buffers = stream.activate();
// const buffers = stream.activate();

expect(buffers).to.be.instanceOf(Object); // jshint ignore:line
expect(buffers).to.not.equal({}); // jshint ignore:line
});
// expect(buffers).to.be.instanceOf(Object); // jshint ignore:line
// expect(buffers).to.not.equal({}); // jshint ignore:line
// });
});

describe('Not well initialized with no config parameter', function () {
Expand Down

0 comments on commit db9b083

Please sign in to comment.