UNPKG

playable

Version:

Video player based on HTML5Video

158 lines 8.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var chai_1 = require("chai"); var sinon = (0, tslib_1.__importStar)(require("sinon")); var event_emitter_1 = (0, tslib_1.__importDefault)(require("../../../../modules/event-emitter/event-emitter")); var constants_1 = require("../../../../constants"); var state_engine_1 = (0, tslib_1.__importStar)(require("./state-engine")); var testkit_1 = require("../../../../testkit"); var NATIVE_EVENTS = { LOAD_START: { type: 'loadstart' }, LOADED_META_DATA: { type: 'loadedmetadata' }, CAN_PLAY: { type: 'canplay' }, PLAY: { type: 'play' }, PLAYING: { type: 'playing' }, WAITING: { type: 'waiting' }, PAUSE: { type: 'pause' }, ENDED: { type: 'ended' }, SEEKING: { type: 'seeking' }, SEEKED: { type: 'seeked' }, }; describe('NativeEventsBroadcaster', function () { var video; var engine; var eventEmitter; beforeEach(function () { video = { addEventListener: sinon.spy(), removeEventListener: sinon.spy(), played: { length: 1, }, tagName: 'VIDEO', }; eventEmitter = new event_emitter_1.default(); sinon.spy(eventEmitter, 'emitAsync'); engine = new state_engine_1.default(eventEmitter, video); sinon.spy(engine, 'setState'); }); afterEach(function () { (0, testkit_1.resetProperty)(navigator, 'userAgent'); eventEmitter.emitAsync.restore(); engine.setState.restore(); }); it('should attach events to video tag on initialization', function () { (0, chai_1.expect)(video.addEventListener.args.length).to.be.equal(state_engine_1.NATIVE_VIDEO_EVENTS_TO_STATE.length); video.addEventListener.args.forEach(function (arg) { (0, chai_1.expect)(state_engine_1.NATIVE_VIDEO_EVENTS_TO_STATE.indexOf(arg[0]) !== -1).to.be.true; (0, chai_1.expect)(arg[1] === engine._processEventFromVideo).to.be.true; }); }); it('should detach events from video tag on destroy', function () { engine.destroy(); (0, chai_1.expect)(video.removeEventListener.args.length).to.be.equal(state_engine_1.NATIVE_VIDEO_EVENTS_TO_STATE.length); video.removeEventListener.args.forEach(function (arg) { (0, chai_1.expect)(state_engine_1.NATIVE_VIDEO_EVENTS_TO_STATE.indexOf(arg[0]) !== -1).to.be.true; (0, chai_1.expect)(arg[1] === engine._processEventFromVideo).to.be.true; }); }); it('should have method for setting state', function () { (0, chai_1.expect)(engine.setState).to.exist; engine._currentState = constants_1.EngineState.LOAD_STARTED; engine.setState(constants_1.EngineState.READY_TO_PLAY); (0, chai_1.expect)(eventEmitter.emitAsync.calledWith(constants_1.VideoEvent.STATE_CHANGED, { prevState: constants_1.EngineState.LOAD_STARTED, nextState: constants_1.EngineState.READY_TO_PLAY, })); (0, chai_1.expect)(engine._currentState).to.be.equal(constants_1.EngineState.READY_TO_PLAY); }); it('should not trigger change of state on same state', function () { engine.setState(constants_1.EngineState.READY_TO_PLAY); (0, chai_1.expect)(eventEmitter.emitAsync.calledTwice).to.be.true; engine.setState(constants_1.EngineState.READY_TO_PLAY); (0, chai_1.expect)(eventEmitter.emitAsync.calledTwice).to.be.true; }); it('should set state on loadstart event', function () { engine._processEventFromVideo(NATIVE_EVENTS.LOAD_START); (0, chai_1.expect)(engine.setState.calledWith(constants_1.EngineState.LOAD_STARTED)).to.be.true; }); it('should set state on loadedmetadata event', function () { (0, chai_1.expect)(engine._isMetadataLoaded).to.be.false; engine._processEventFromVideo(NATIVE_EVENTS.LOADED_META_DATA); (0, chai_1.expect)(engine.setState.calledWith(constants_1.EngineState.METADATA_LOADED)).to.be.true; (0, chai_1.expect)(engine.isMetadataLoaded).to.be.true; }); it('should set state on canplay event only after medatada loaded', function () { engine._processEventFromVideo(NATIVE_EVENTS.CAN_PLAY); (0, chai_1.expect)(engine.setState.calledWith(constants_1.EngineState.READY_TO_PLAY)).to.be.false; engine._processEventFromVideo(NATIVE_EVENTS.LOADED_META_DATA); engine._processEventFromVideo(NATIVE_EVENTS.CAN_PLAY); (0, chai_1.expect)(engine.setState.calledWith(constants_1.EngineState.READY_TO_PLAY)).to.be.true; }); it('should set state on play event', function () { engine._processEventFromVideo(NATIVE_EVENTS.PLAY); (0, chai_1.expect)(engine.setState.calledWith(constants_1.EngineState.PLAY_REQUESTED)).to.be.true; }); it('should set state on playing event', function () { engine._processEventFromVideo(NATIVE_EVENTS.PLAYING); (0, chai_1.expect)(engine.setState.calledWith(constants_1.EngineState.PLAYING)).to.be.true; }); it('should not set state on playing event if video is not actually playing', function () { (0, testkit_1.setProperty)(navigator, 'userAgent', 'safari'); video.paused = true; engine._processEventFromVideo(NATIVE_EVENTS.PLAYING); (0, chai_1.expect)(engine.setState.calledWith(constants_1.EngineState.PLAYING)).to.be.false; }); it('should set state on waiting event', function () { engine._processEventFromVideo(NATIVE_EVENTS.WAITING); (0, chai_1.expect)(engine.setState.calledWith(constants_1.EngineState.WAITING)).to.be.true; }); it('should set state on pause event', function () { engine._processEventFromVideo(NATIVE_EVENTS.PAUSE); (0, chai_1.expect)(engine.setState.calledWith(constants_1.EngineState.PAUSED)).to.be.true; }); it('should not set state on pause event if there is no played chunks', function () { (0, testkit_1.setProperty)(navigator, 'userAgent', 'safari'); video.played.length = 0; engine._processEventFromVideo(NATIVE_EVENTS.PAUSE); (0, chai_1.expect)(engine.setState.calledWith(constants_1.EngineState.PAUSED)).to.be.false; }); it('should set state on ended event', function () { engine._processEventFromVideo(NATIVE_EVENTS.ENDED); (0, chai_1.expect)(engine.setState.calledWith(constants_1.EngineState.ENDED)).to.be.true; }); it('should set state on seeking event', function () { engine._processEventFromVideo(NATIVE_EVENTS.SEEKING); (0, chai_1.expect)(engine.setState.calledWith(constants_1.EngineState.SEEK_IN_PROGRESS)).to.be.true; }); it('should set state on seeked event', function () { video.paused = true; engine._processEventFromVideo(NATIVE_EVENTS.SEEKED); (0, chai_1.expect)(engine.setState.calledWith(constants_1.EngineState.PAUSED)).to.be.true; video.paused = false; engine._processEventFromVideo(NATIVE_EVENTS.SEEKED); (0, chai_1.expect)(engine.setState.calledWith(constants_1.EngineState.PLAYING)).to.be.true; }); it('should dodge sneaky bug with dash manifest', function () { engine.setState(constants_1.EngineState.METADATA_LOADED); engine.setState(constants_1.EngineState.SEEK_IN_PROGRESS); (0, chai_1.expect)(engine.state).to.be.equal(constants_1.EngineState.METADATA_LOADED); engine.setState(constants_1.EngineState.PAUSED); (0, chai_1.expect)(engine.state).to.be.equal(constants_1.EngineState.METADATA_LOADED); }); it('should collect timestamps', function () { engine._processEventFromVideo(NATIVE_EVENTS.LOAD_START); engine._processEventFromVideo(NATIVE_EVENTS.LOADED_META_DATA); engine._processEventFromVideo(NATIVE_EVENTS.CAN_PLAY); (0, chai_1.expect)(Object.keys(engine.stateTimestamps)).to.be.deep.equal([ 'engine-state/metadata-loaded', 'engine-state/ready-to-play', ]); }); it('should do nothing if event is not in list', function () { engine._processEventFromVideo(); (0, chai_1.expect)(eventEmitter.emitAsync.called).to.be.false; }); }); //# sourceMappingURL=state-engine.spec.js.map