diff --git a/tests/unit/controller/stream-controller.js b/tests/unit/controller/stream-controller.js index b28742f4b81..5ece12f154d 100644 --- a/tests/unit/controller/stream-controller.js +++ b/tests/unit/controller/stream-controller.js @@ -4,8 +4,10 @@ import Hls from "../../../src/hls"; import Event from "../../../src/events"; import { FragmentTracker } from "../../../src/helper/fragment-tracker"; import StreamController, { State } from "../../../src/controller/stream-controller"; +import M3U8Parser from "../../../src/loader/m3u8-parser"; /** + * Create StreamController instance with initial setting * @returns {{hls: Hls, streamController: StreamController}} */ const createStreamController = () => { @@ -17,11 +19,30 @@ const createStreamController = () => { }; }; +/** + * Assert: streamController should be started + * @param {StreamController} streamController + */ +const assertStreamControllerStarted = (streamController) => { + assert.equal(streamController.hasInterval(), true, "StreamController should start interval"); + assert.notDeepEqual(streamController.state, State.STOPPED, "StreamController's state should not be STOPPED"); +}; + +/** + * Assert: streamController should be stopped + * @param {StreamController} streamController + */ +const assertStreamControllerStopped = (streamController) => { + assert.equal(streamController.hasInterval(), false, "StreamController should stop interval"); + assert.equal(streamController.state, State.STOPPED, "StreamController's state should be STOPPED"); +}; + describe("StreamController", function() { - it("initial state should be STOPPED", function() { + it("should be STOPPED when it is initialized", function() { const { streamController } = createStreamController(); - assert.equal(streamController.state, State.STOPPED); + assertStreamControllerStopped(streamController); }); + it("should trigger STREAM_STATE_TRANSITION when state is updated", function() { const { hls, streamController } = createStreamController(); const spy = sinon.spy(); @@ -29,4 +50,26 @@ describe("StreamController", function() { streamController.state = State.ENDED; assert.deepEqual(spy.args[0][1], { previousState: State.STOPPED, nextState: State.ENDED }); }); + + it("should not start when controller have not levels data", function() { + const { streamController } = createStreamController(); + streamController.startLoad(1); + assertStreamControllerStopped(streamController); + }); + + it("should start when controller have levels data", function() { + const { streamController } = createStreamController(); + const manifest = `#EXTM3U +#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=836280,RESOLUTION=848x360,NAME="480" +http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core`; + const levels = M3U8Parser.parseMasterPlaylist(manifest, 'http://www.dailymotion.com'); + // load levels data + streamController.onManifestParsed({ + levels + }); + streamController.startLoad(1); + assertStreamControllerStarted(streamController); + streamController.stopLoad(); + assertStreamControllerStopped(streamController) + }); });