1
0
mirror of https://github.com/videojs/video.js.git synced 2024-12-16 11:37:29 +02:00
video.js/test/unit/media.js

143 lines
3.4 KiB
JavaScript
Raw Normal View History

var noop = function() {}, clock, features;
module('Media Tech', {
'setup': function() {
clock = sinon.useFakeTimers();
features = videojs.util.mergeOptions({}, videojs.MediaTechController.prototype.features);
},
'teardown': function() {
clock.restore();
videojs.MediaTechController.prototype.features = features;
}
});
test('should synthesize timeupdate events by default', function() {
var timeupdates = 0, playHandler, i, tech;
tech = new videojs.MediaTechController({
id: noop,
on: function(event, handler) {
if (event === 'play') {
playHandler = handler;
}
},
trigger: function(event) {
if (event === 'timeupdate') {
timeupdates++;
}
}
});
playHandler.call(tech);
tech.on('timeupdate', function() {
timeupdates++;
});
clock.tick(250);
equal(timeupdates, 1, 'triggered one timeupdate');
});
test('stops timeupdates if the tech produces them natively', function() {
var timeupdates = 0, tech, playHandler, expected;
tech = new videojs.MediaTechController({
id: noop,
on: function(event, handler) {
if (event === 'play') {
playHandler = handler;
}
},
bufferedPercent: noop,
trigger: function(event) {
if (event === 'timeupdate') {
timeupdates++;
}
}
});
playHandler.call(tech);
// simulate a native timeupdate event
tech.trigger('timeupdate');
expected = timeupdates;
clock.tick(10 * 1000);
equal(timeupdates, expected, 'did not simulate timeupdates');
});
test('stops manual timeupdates while paused', function() {
var timeupdates = 0, tech, playHandler, pauseHandler, expected;
tech = new videojs.MediaTechController({
id: noop,
on: function(event, handler) {
if (event === 'play') {
playHandler = handler;
} else if (event === 'pause') {
pauseHandler = handler;
}
},
bufferedPercent: noop,
trigger: function(event) {
if (event === 'timeupdate') {
timeupdates++;
}
}
});
playHandler.call(tech);
clock.tick(10 * 250);
ok(timeupdates > 0, 'timeupdates fire during playback');
pauseHandler.call(tech);
timeupdates = 0;
clock.tick(10 * 250);
equal(timeupdates, 0, 'timeupdates do not fire when paused');
playHandler.call(tech);
clock.tick(10 * 250);
ok(timeupdates > 0, 'timeupdates fire when playback resumes');
});
test('should synthesize progress events by default', function() {
var progresses = 0, tech;
tech = new videojs.MediaTechController({
id: noop,
on: noop,
bufferedPercent: function() {
return 0;
},
trigger: function(event) {
if (event === 'progress') {
progresses++;
}
}
});
tech.on('progress', function() {
progresses++;
});
clock.tick(500);
equal(progresses, 1, 'triggered one event');
});
test('stops progress events if the tech produces them natively', function() {
var end = 0, buffered = 0, progresses = 0, tech;
tech = new videojs.MediaTechController({
id: noop,
on: noop,
// progress will be detected any time it is queried
bufferedPercent: function() {
return buffered++;
},
trigger: function(event) {
if (event === 'progress') {
progresses++;
}
}
});
// simulate a native progress event
tech.trigger('progress');
tech.on('progress', function() {
progresses++;
});
clock.tick(10 * 1000);
equal(progresses, 0, 'did not simulate progress');
});