Skip to content

Commit

Permalink
Updated unit tests to account for new events.
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianDomingo committed May 23, 2022
1 parent 5d8d729 commit 018cd97
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
5 changes: 2 additions & 3 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,7 @@ goog.requireType('shaka.routing.Payload');
/**
* @event shaka.Player.StallDetectedEvent
* @description Fired when a stall in playback is detected by the StallDetector.
* Not all stalls are caused by gaps in the buffered ranges. This event is
* fired only for the MediaSourcePlayhead.
* Not all stalls can be caused by gaps in the buffered ranges.
* @property {string} type
* 'stalldetected'
* @exportDoc
Expand All @@ -399,7 +398,7 @@ goog.requireType('shaka.routing.Payload');
/**
* @event shaka.Player.GapJumpedEvent
* @description Fired when the GapJumpingController jumps over a gap in the
* buffered ranges. This event is fired only for the MediaSourcePlayhead.
* buffered ranges.
* @property {string} type
* 'gapjumped'
* @exportDoc
Expand Down
42 changes: 41 additions & 1 deletion test/media/playhead_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ let TimeRange;
* start: number,
* waitingAt: number,
* expectedEndTime: number,
* expectEvent: boolean,
* }}
*
* @description
* Parameters for a test where we start playing inside a buffered range and play
* until the end of the buffer. Then, if we expect it, Playhead should jump
* to the expected time.
* to the expected time. We should get a 'stalldetected' event when the Playhead
* detects a stall through the StallDetector, and a 'gapjumped' event when the
* Playhead jumps over a gap in the buffered range(s).
*
* @property {!Array.<TimeRange>} buffered
* The buffered ranges for the test.
Expand All @@ -36,6 +39,9 @@ let TimeRange;
* The time to pause at and fire a 'waiting' event.
* @property {number} expectedEndTime
* The expected time at the end of the test.
* @property {boolean} expectEvent
* If true, expect either the 'stalldetected' or 'gapjumped' event to be
* fired.
*/
let PlayingTestInfo;

Expand Down Expand Up @@ -836,20 +842,23 @@ describe('Playhead', () => {
buffered: [{start: 0, end: 10}],
start: 3,
waitingAt: 10,
expectEvent: false,
expectedEndTime: 10,
});

playingTest('won\'t jump at end of multiple regions', {
buffered: [{start: 0, end: 10}, {start: 20, end: 30}],
start: 24,
waitingAt: 30,
expectEvent: false,
expectedEndTime: 30,
});

playingTest('will jump small gap', {
buffered: [{start: 0, end: 10}, {start: 11, end: 20}],
start: 5,
waitingAt: 10,
expectEvent: true,
expectedEndTime: 11,
});

Expand All @@ -858,6 +867,7 @@ describe('Playhead', () => {
[{start: 0, end: 10}, {start: 11, end: 20}, {start: 21, end: 30}],
start: 5,
waitingAt: 10,
expectEvent: true,
expectedEndTime: 11,
});

Expand All @@ -866,6 +876,7 @@ describe('Playhead', () => {
[{start: 0, end: 10}, {start: 11, end: 20}, {start: 21, end: 30}],
start: 15,
waitingAt: 20,
expectEvent: true,
expectedEndTime: 21,
});
}); // with small gaps
Expand All @@ -875,6 +886,7 @@ describe('Playhead', () => {
buffered: [{start: 0, end: 10}, {start: 30, end: 40}],
start: 5,
waitingAt: 10,
expectEvent: true,
expectedEndTime: 30,
});

Expand All @@ -883,6 +895,7 @@ describe('Playhead', () => {
[{start: 0, end: 10}, {start: 30, end: 40}, {start: 50, end: 60}],
start: 5,
waitingAt: 10,
expectEvent: true,
expectedEndTime: 30,
});

Expand All @@ -891,6 +904,7 @@ describe('Playhead', () => {
[{start: 0, end: 10}, {start: 20, end: 30}, {start: 50, end: 60}],
start: 24,
waitingAt: 30,
expectEvent: true,
expectedEndTime: 50,
});
}); // with large gaps
Expand All @@ -905,6 +919,8 @@ describe('Playhead', () => {
video.currentTime = data.start;
video.readyState = HTMLMediaElement.HAVE_ENOUGH_DATA;

onEvent.and.callFake((event) => {});

playhead = new shaka.media.MediaSourcePlayhead(
video,
manifest,
Expand Down Expand Up @@ -932,11 +948,14 @@ describe('Playhead', () => {
expect(video.currentTime).toBe(time + 0.4);
}

expect(onEvent).not.toHaveBeenCalled();

video.currentTime = data.waitingAt;
video.readyState = HTMLMediaElement.HAVE_CURRENT_DATA;
video.on['waiting']();
jasmine.clock().tick(500);

expect(onEvent).toHaveBeenCalledTimes(data.expectEvent ? 1 : 0);
expect(video.currentTime).toBe(data.expectedEndTime);
});
}
Expand All @@ -948,13 +967,15 @@ describe('Playhead', () => {
buffered: [{start: 0, end: 10}],
start: 4,
seekTo: 14,
expectEvent: false,
expectedEndTime: 14,
});

seekTest('will jump when seeking into gap', {
buffered: [{start: 0, end: 10}, {start: 11, end: 20}],
start: 3,
seekTo: 10.4,
expectEvent: true,
expectedEndTime: 11,
});

Expand All @@ -963,6 +984,7 @@ describe('Playhead', () => {
[{start: 0, end: 10}, {start: 11, end: 20}, {start: 21, end: 30}],
start: 3,
seekTo: 10.4,
expectEvent: true,
expectedEndTime: 11,
});

Expand All @@ -971,13 +993,15 @@ describe('Playhead', () => {
[{start: 0, end: 10}, {start: 11, end: 20}, {start: 21, end: 30}],
start: 3,
seekTo: 20.5,
expectEvent: true,
expectedEndTime: 21,
});

seekTest('treats large gaps as small if playhead near end', {
buffered: [{start: 0, end: 10}, {start: 30, end: 40}],
start: 3,
seekTo: 29.2,
expectEvent: true,
expectedEndTime: 30,
});
}); // with small gaps
Expand All @@ -987,6 +1011,7 @@ describe('Playhead', () => {
buffered: [{start: 0, end: 10}, {start: 30, end: 40}],
start: 5,
seekTo: 12,
expectEvent: true,
expectedEndTime: 30,
});
}); // with large gaps
Expand All @@ -1000,6 +1025,7 @@ describe('Playhead', () => {
newBuffered: [{start: 20, end: 30}, {start: 31, end: 40}],
start: 3,
seekTo: 22,
expectEvent: false,
expectedEndTime: 22,
});

Expand All @@ -1010,6 +1036,7 @@ describe('Playhead', () => {
newBuffered: [{start: 0.2, end: 10}],
start: 4,
seekTo: 0,
expectEvent: true,
expectedEndTime: 0.2,
});

Expand All @@ -1019,6 +1046,7 @@ describe('Playhead', () => {
newBuffered: [{start: 20, end: 30}, {start: 31, end: 40}],
start: 3,
seekTo: 30.2,
expectEvent: true,
expectedEndTime: 31,
});

Expand All @@ -1028,6 +1056,7 @@ describe('Playhead', () => {
newBuffered: [{start: 20, end: 30}, {start: 31, end: 40}],
start: 3,
seekTo: 30,
expectEvent: true,
expectedEndTime: 31,
});

Expand All @@ -1037,6 +1066,7 @@ describe('Playhead', () => {
newBuffered: [{start: 20, end: 30}],
start: 3,
seekTo: 34,
expectEvent: false,
expectedEndTime: 34,
});

Expand All @@ -1046,6 +1076,7 @@ describe('Playhead', () => {
newBuffered: [{start: 0, end: 10}],
start: 24,
seekTo: 4,
expectEvent: false,
expectedEndTime: 4,
});

Expand All @@ -1056,6 +1087,7 @@ describe('Playhead', () => {
// should still be waiting.
start: 24,
seekTo: 4,
expectEvent: false,
expectedEndTime: 4,
});

Expand All @@ -1065,6 +1097,7 @@ describe('Playhead', () => {
newBuffered: [{start: 2, end: 10}],
start: 24,
seekTo: 1.6,
expectEvent: true,
expectedEndTime: 2,
});
}); // with small gaps
Expand All @@ -1075,6 +1108,7 @@ describe('Playhead', () => {
newBuffered: [{start: 20, end: 30}],
start: 25,
seekTo: 0,
expectEvent: true,
expectedEndTime: 20,
});

Expand All @@ -1084,6 +1118,7 @@ describe('Playhead', () => {
newBuffered: [{start: 20, end: 30}, {start: 40, end: 50}],
start: 3,
seekTo: 32,
expectEvent: true,
expectedEndTime: 40,
});
}); // with large gaps
Expand All @@ -1104,6 +1139,7 @@ describe('Playhead', () => {
Util.spyFunc(onEvent));

jasmine.clock().tick(500);
expect(onEvent).not.toHaveBeenCalled();

// Append a segment before seeking.
playhead.notifyOfBufferingChange();
Expand Down Expand Up @@ -1237,6 +1273,8 @@ describe('Playhead', () => {
video.currentTime = data.start;
video.readyState = HTMLMediaElement.HAVE_ENOUGH_DATA;

onEvent.and.callFake((event) => {});

playhead = new shaka.media.MediaSourcePlayhead(
video,
manifest,
Expand All @@ -1246,6 +1284,7 @@ describe('Playhead', () => {
Util.spyFunc(onEvent));

jasmine.clock().tick(500);
expect(onEvent).not.toHaveBeenCalled();

// Seek to the given position and update ready state.
video.currentTime = data.seekTo;
Expand Down Expand Up @@ -1277,6 +1316,7 @@ describe('Playhead', () => {
jasmine.clock().tick(250);
}

expect(onEvent).toHaveBeenCalledTimes(data.expectEvent ? 1 : 0);
expect(video.currentTime).toBe(data.expectedEndTime);
});
}
Expand Down
17 changes: 15 additions & 2 deletions test/media/stall_detector_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/

describe('StallDetector', () => {
const Util = shaka.test.Util;

/**
* @implements {shaka.media.StallDetector.Implementation}
* @final
Expand Down Expand Up @@ -50,9 +52,9 @@ describe('StallDetector', () => {
detector = new shaka.media.StallDetector(
implementation,
/* stallThresholdSeconds= */ 1,
shaka.test.Util.spyFunc(onEvent));
Util.spyFunc(onEvent));

detector.onStall(shaka.test.Util.spyFunc(onStall));
detector.onStall(Util.spyFunc(onStall));
});

it('does not call onStall when values changes', () => {
Expand All @@ -68,6 +70,7 @@ describe('StallDetector', () => {

detector.poll();

expect(onEvent).not.toHaveBeenCalled();
expect(onStall).not.toHaveBeenCalled();
});

Expand All @@ -79,13 +82,15 @@ describe('StallDetector', () => {
implementation.wallSeconds = time;

detector.poll();
expect(onEvent).not.toHaveBeenCalled();
expect(onStall).not.toHaveBeenCalled();
}

implementation.presentationSeconds = 5;
implementation.wallSeconds = 1;

detector.poll();
expect(onEvent).toHaveBeenCalled();
expect(onStall).toHaveBeenCalledOnceMoreWith([
/* stalledWith= */ 5,
/* stallDurationSeconds= */ 1,
Expand All @@ -101,6 +106,7 @@ describe('StallDetector', () => {
implementation.wallSeconds = 10;
detector.poll();

expect(onEvent).toHaveBeenCalled();
expect(onStall).toHaveBeenCalledOnceMoreWith([
/* stalledWith= */ 5,
/* stallDurationms= */ 10,
Expand All @@ -122,6 +128,7 @@ describe('StallDetector', () => {

detector.poll();

expect(onEvent).not.toHaveBeenCalled();
expect(onStall).not.toHaveBeenCalled();
});

Expand All @@ -140,6 +147,7 @@ describe('StallDetector', () => {
implementation.wallSeconds = 10;
detector.poll();

expect(onEvent).not.toHaveBeenCalled();
expect(onStall).not.toHaveBeenCalled();
});

Expand All @@ -148,16 +156,20 @@ describe('StallDetector', () => {
implementation.presentationSeconds = 0;
implementation.wallSeconds = 0;
detector.poll();
expect(onEvent).not.toHaveBeenCalled();
expect(onStall).not.toHaveBeenCalled();

implementation.wallSeconds = 10;
detector.poll();
expect(onEvent).toHaveBeenCalled();
expect(onStall).toHaveBeenCalled();
onEvent.calls.reset();
onStall.calls.reset();

// This is the same stall, should not be called again.
implementation.wallSeconds = 20;
detector.poll();
expect(onEvent).not.toHaveBeenCalled();
expect(onStall).not.toHaveBeenCalled();

// Now that we changed time, we should get another call.
Expand All @@ -166,6 +178,7 @@ describe('StallDetector', () => {
detector.poll();
implementation.wallSeconds = 40;
detector.poll();
expect(onEvent).toHaveBeenCalled();
expect(onStall).toHaveBeenCalled();
});
});

0 comments on commit 018cd97

Please sign in to comment.