1
0
mirror of https://github.com/videojs/video.js.git synced 2025-01-23 11:04:59 +02:00

test handlers to incresase coverage

This commit is contained in:
mister-ben 2023-06-09 13:17:45 +02:00
parent 6332b13c2f
commit 662989b139
2 changed files with 127 additions and 4 deletions

View File

@ -10,7 +10,7 @@ export const initMediaSession = function() {
return;
}
const ms = window.navigator.mediaSession;
const skipTime = this.options_.mediaSession.skipTime || 15;
const defaultSkipTime = 15;
const actionHandlers = [
['play', () => {
@ -28,13 +28,13 @@ export const initMediaSession = function() {
if (this.usingPlugin('ads') && this.ads.inAdBreak()) {
return;
}
this.currentTime(Math.max(0, this.currentTime() - (details.skipOffset || skipTime)));
this.currentTime(Math.max(0, this.currentTime() - (details.skipOffset || defaultSkipTime)));
}],
['seekforward', (details) => {
if (this.usingPlugin('ads') && this.ads.inAdBreak()) {
return;
}
this.currentTime(Math.min(this.duration(), this.currentTime() + (details.skipOffset || skipTime)));
this.currentTime(Math.min(this.duration(), this.currentTime() + (details.skipOffset || defaultSkipTime)));
}],
['seekto', (details) => {
if (this.usingPlugin('ads') && this.ads.inAdBreak()) {

View File

@ -241,7 +241,7 @@ QUnit.test('allows for action handlers that are not settable', function(assert)
sinon.stub(this.player.log, 'debug');
this.player.trigger('pluginsetup:playlist');
this.player.trigger('playing');
this.clock.tick(10);
@ -286,3 +286,126 @@ QUnit.test('playback and position state', function(assert) {
window.navigator.mediaSession.setPositionState.restore();
});
QUnit.test('action handlers', function(assert) {
const actions = {};
sinon.stub(window.navigator.mediaSession, 'setActionHandler').callsFake((action, handler) => {
actions[action] = handler;
});
this.clock = sinon.useFakeTimers();
this.player = sinon.spy(TestHelpers.makePlayer({
mediaSession: true
}));
this.player.trigger('playing');
this.clock.tick(10);
sinon.resetHistory();
actions.play();
assert.true(this.player.play.called, 'play handler calls play on player');
actions.pause();
assert.true(this.player.pause.called, 'pause handler calls play on player');
sinon.resetHistory();
assert.false(this.player.pause.called, 'pause handler should have been reset');
actions.stop();
assert.true(this.player.pause.called, 'pause handler calls play on player');
assert.true(this.player.currentTime.calledWith(0), 'pause handler calls play on player');
this.player.duration(600);
this.player.currentTime(0);
actions.seekforward({skipOffset: 10});
assert.true(this.player.currentTime.calledWith(10), 'seek handler sets current time to requested offset');
actions.seekforward({});
assert.true(this.player.currentTime.calledWith(10 + 15), 'seek handler sets current time to default offset');
actions.seekbackward({skipOffset: 10});
assert.true(this.player.currentTime.calledWith(10 + 15 - 10), 'seek handler sets current time to requested offset');
actions.seekbackward({});
assert.true(this.player.currentTime.calledWith(10 + 15 - 10 - 15), 'seek handler sets current time to default offset');
actions.seekbackward({});
assert.true(this.player.currentTime.calledWith(10 + 15 - 10 - 15), 'seek handler does not set current time below 0');
actions.seekto({seekTime: 10});
assert.true(this.player.currentTime.calledWith(10), 'seek handler sets current time');
window.navigator.mediaSession.setActionHandler.restore();
});
QUnit.test('seek action handlers ignored in ad breaks', function(assert) {
const actions = {};
sinon.stub(window.navigator.mediaSession, 'setActionHandler').callsFake((action, handler) => {
actions[action] = handler;
});
this.clock = sinon.useFakeTimers();
this.player = sinon.spy(TestHelpers.makePlayer({
mediaSession: true
}));
this.player.trigger('playing');
this.clock.tick(10);
// Mocked contrib-ads
this.player.activePlugins_ = {
ads: true
};
this.player.ads = {
inAdBreak: () => true
};
actions.seekto({seekTime: 15});
assert.false(this.player.currentTime.calledWith(15), 'seek handler does not sets current time in ad break');
actions.seekbackward({skipOffset: 10});
assert.false(this.player.currentTime.called, 'seekbackward handler does not sets current time in ad break');
actions.seekforward({skipOffset: 10});
assert.false(this.player.currentTime.called, 'seekforward handler does not sets current time in ad break');
window.navigator.mediaSession.setActionHandler.restore();
});
QUnit.test('playlist action handlers are called', function(assert) {
const actions = {};
sinon.stub(window.navigator.mediaSession, 'setActionHandler').callsFake((action, handler) => {
actions[action] = handler;
});
this.clock = sinon.useFakeTimers();
this.player = sinon.spy(TestHelpers.makePlayer({
mediaSession: true
}));
this.player.trigger('pluginsetup:playlist');
this.clock.tick(10);
// Mocked playlist
const mockPlaylist = {
next: sinon.spy(),
previous: sinon.spy()
};
this.player.activePlugins_ = {
playlist: true
};
this.player.playlist = mockPlaylist;
actions.previoustrack();
assert.true(mockPlaylist.previous.called, 'playlist previous handler called');
actions.nexttrack();
assert.true(mockPlaylist.next.called, 'playlist next handler called');
window.navigator.mediaSession.setActionHandler.restore();
});