2015-02-14 01:18:07 +02:00
|
|
|
var noop = function() {}, clock, oldTextTracks;
|
|
|
|
|
2014-08-14 02:44:36 +03:00
|
|
|
module('Media Tech', {
|
|
|
|
'setup': function() {
|
2014-12-03 21:31:39 +02:00
|
|
|
this.noop = function() {};
|
|
|
|
this.clock = sinon.useFakeTimers();
|
|
|
|
this.featuresProgessEvents = videojs.MediaTechController.prototype['featuresProgessEvents'];
|
2014-09-03 02:22:43 +03:00
|
|
|
videojs.MediaTechController.prototype['featuresProgressEvents'] = false;
|
2015-02-14 01:18:07 +02:00
|
|
|
videojs.MediaTechController.prototype['featuresNativeTextTracks'] = true;
|
|
|
|
oldTextTracks = videojs.MediaTechController.prototype.textTracks;
|
|
|
|
videojs.MediaTechController.prototype.textTracks = function() {
|
|
|
|
return {
|
|
|
|
addEventListener: Function.prototype,
|
|
|
|
removeEventListener: Function.prototype
|
|
|
|
};
|
|
|
|
};
|
2014-08-14 02:44:36 +03:00
|
|
|
},
|
|
|
|
'teardown': function() {
|
2014-12-03 21:31:39 +02:00
|
|
|
this.clock.restore();
|
|
|
|
videojs.MediaTechController.prototype['featuresProgessEvents'] = this.featuresProgessEvents;
|
2015-02-14 01:18:07 +02:00
|
|
|
videojs.MediaTechController.prototype['featuresNativeTextTracks'] = false;
|
|
|
|
videojs.MediaTechController.prototype.textTracks = oldTextTracks;
|
2014-08-14 02:44:36 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should synthesize timeupdate events by default', function() {
|
|
|
|
var timeupdates = 0, playHandler, i, tech;
|
|
|
|
tech = new videojs.MediaTechController({
|
2014-12-03 21:31:39 +02:00
|
|
|
id: this.noop,
|
2014-08-14 02:44:36 +03:00
|
|
|
on: function(event, handler) {
|
|
|
|
if (event === 'play') {
|
|
|
|
playHandler = handler;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
trigger: function(event) {
|
|
|
|
if (event === 'timeupdate') {
|
|
|
|
timeupdates++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
playHandler.call(tech);
|
|
|
|
tech.on('timeupdate', function() {
|
|
|
|
timeupdates++;
|
|
|
|
});
|
|
|
|
|
2014-12-03 21:31:39 +02:00
|
|
|
this.clock.tick(250);
|
2014-08-14 02:44:36 +03:00
|
|
|
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({
|
2014-12-03 21:31:39 +02:00
|
|
|
id: this.noop,
|
2015-01-16 22:05:21 +02:00
|
|
|
off: this.noop,
|
2014-08-14 02:44:36 +03:00
|
|
|
on: function(event, handler) {
|
|
|
|
if (event === 'play') {
|
|
|
|
playHandler = handler;
|
|
|
|
}
|
|
|
|
},
|
2014-12-03 21:31:39 +02:00
|
|
|
bufferedPercent: this.noop,
|
2014-08-14 02:44:36 +03:00
|
|
|
trigger: function(event) {
|
|
|
|
if (event === 'timeupdate') {
|
|
|
|
timeupdates++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
playHandler.call(tech);
|
|
|
|
// simulate a native timeupdate event
|
|
|
|
tech.trigger('timeupdate');
|
|
|
|
|
|
|
|
expected = timeupdates;
|
2014-12-03 21:31:39 +02:00
|
|
|
this.clock.tick(10 * 1000);
|
2014-08-14 02:44:36 +03:00
|
|
|
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({
|
2014-12-03 21:31:39 +02:00
|
|
|
id: this.noop,
|
2014-08-14 02:44:36 +03:00
|
|
|
on: function(event, handler) {
|
|
|
|
if (event === 'play') {
|
|
|
|
playHandler = handler;
|
|
|
|
} else if (event === 'pause') {
|
|
|
|
pauseHandler = handler;
|
|
|
|
}
|
|
|
|
},
|
2014-12-03 21:31:39 +02:00
|
|
|
bufferedPercent: this.noop,
|
2014-08-14 02:44:36 +03:00
|
|
|
trigger: function(event) {
|
|
|
|
if (event === 'timeupdate') {
|
|
|
|
timeupdates++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
playHandler.call(tech);
|
2014-12-03 21:31:39 +02:00
|
|
|
this.clock.tick(10 * 250);
|
2014-08-14 02:44:36 +03:00
|
|
|
ok(timeupdates > 0, 'timeupdates fire during playback');
|
|
|
|
|
|
|
|
pauseHandler.call(tech);
|
|
|
|
timeupdates = 0;
|
2014-12-03 21:31:39 +02:00
|
|
|
this.clock.tick(10 * 250);
|
2014-08-14 02:44:36 +03:00
|
|
|
equal(timeupdates, 0, 'timeupdates do not fire when paused');
|
|
|
|
|
|
|
|
playHandler.call(tech);
|
2014-12-03 21:31:39 +02:00
|
|
|
this.clock.tick(10 * 250);
|
2014-08-14 02:44:36 +03:00
|
|
|
ok(timeupdates > 0, 'timeupdates fire when playback resumes');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should synthesize progress events by default', function() {
|
|
|
|
var progresses = 0, tech;
|
|
|
|
tech = new videojs.MediaTechController({
|
2014-12-03 21:31:39 +02:00
|
|
|
id: this.noop,
|
|
|
|
on: this.noop,
|
2014-08-14 02:44:36 +03:00
|
|
|
bufferedPercent: function() {
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
trigger: function(event) {
|
|
|
|
if (event === 'progress') {
|
|
|
|
progresses++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
tech.on('progress', function() {
|
|
|
|
progresses++;
|
|
|
|
});
|
|
|
|
|
2014-12-03 21:31:39 +02:00
|
|
|
this.clock.tick(500);
|
2014-08-14 02:44:36 +03:00
|
|
|
equal(progresses, 1, 'triggered one event');
|
|
|
|
});
|
2014-08-20 21:39:02 +03:00
|
|
|
|
|
|
|
test('dispose() should stop time tracking', function() {
|
|
|
|
var tech = new videojs.MediaTechController({
|
2014-12-03 21:31:39 +02:00
|
|
|
id: this.noop,
|
|
|
|
on: this.noop,
|
|
|
|
off: this.noop,
|
|
|
|
trigger: this.noop
|
2014-08-20 21:39:02 +03:00
|
|
|
});
|
|
|
|
tech.dispose();
|
|
|
|
|
|
|
|
// progress and timeupdate events will throw exceptions after the
|
|
|
|
// tech is disposed
|
|
|
|
try {
|
2014-12-03 21:31:39 +02:00
|
|
|
this.clock.tick(10 * 1000);
|
2014-08-20 21:39:02 +03:00
|
|
|
} catch (e) {
|
|
|
|
return equal(e, undefined, 'threw an exception');
|
|
|
|
}
|
|
|
|
ok(true, 'no exception was thrown');
|
|
|
|
});
|
2014-12-03 00:22:34 +02:00
|
|
|
|
|
|
|
test('should add the source hanlder interface to a tech', function(){
|
|
|
|
var mockPlayer = {
|
2014-12-03 21:31:39 +02:00
|
|
|
off: this.noop,
|
|
|
|
trigger: this.noop
|
2014-12-03 00:22:34 +02:00
|
|
|
};
|
|
|
|
var sourceA = { src: 'foo.mp4', type: 'video/mp4' };
|
|
|
|
var sourceB = { src: 'no-support', type: 'no-support' };
|
|
|
|
|
|
|
|
// Define a new tech class
|
|
|
|
var Tech = videojs.MediaTechController.extend();
|
|
|
|
|
|
|
|
// Extend Tech with source handlers
|
|
|
|
vjs.MediaTechController.withSourceHandlers(Tech);
|
|
|
|
|
|
|
|
// Check for the expected class methods
|
|
|
|
ok(Tech.registerSourceHandler, 'added a registerSourceHandler function to the Tech');
|
|
|
|
ok(Tech.selectSourceHandler, 'added a selectSourceHandler function to the Tech');
|
|
|
|
|
|
|
|
// Create an instance of Tech
|
|
|
|
var tech = new Tech(mockPlayer);
|
|
|
|
|
|
|
|
// Check for the expected instance methods
|
|
|
|
ok(tech.setSource, 'added a setSource function to the tech instance');
|
|
|
|
|
|
|
|
// Create an internal state class for the source handler
|
|
|
|
// The internal class would be used by a source hanlder to maintain state
|
|
|
|
// and provde a dispose method for the handler.
|
|
|
|
// This is optional for source handlers
|
|
|
|
var disposeCalled = false;
|
|
|
|
var handlerInternalState = function(){};
|
|
|
|
handlerInternalState.prototype.dispose = function(){
|
|
|
|
disposeCalled = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create source handlers
|
|
|
|
var handlerOne = {
|
|
|
|
canHandleSource: function(source){
|
|
|
|
if (source.type !=='no-support') {
|
|
|
|
return 'probably';
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
},
|
|
|
|
handleSource: function(s, t){
|
|
|
|
strictEqual(tech, t, 'the tech instance was passed to the source handler');
|
|
|
|
strictEqual(sourceA, s, 'the tech instance was passed to the source handler');
|
|
|
|
return new handlerInternalState();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var handlerTwo = {
|
|
|
|
canHandleSource: function(source){
|
|
|
|
return ''; // no support
|
|
|
|
},
|
|
|
|
handleSource: function(source, tech){
|
|
|
|
ok(false, 'handlerTwo supports nothing and should never be called');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Test registering source handlers
|
|
|
|
Tech.registerSourceHandler(handlerOne);
|
|
|
|
strictEqual(Tech.sourceHandlers[0], handlerOne, 'handlerOne was added to the source handler array');
|
|
|
|
Tech.registerSourceHandler(handlerTwo, 0);
|
|
|
|
strictEqual(Tech.sourceHandlers[0], handlerTwo, 'handlerTwo was registered at the correct index (0)');
|
|
|
|
|
|
|
|
// Test handler selection
|
|
|
|
strictEqual(Tech.selectSourceHandler(sourceA), handlerOne, 'handlerOne was selected to handle the valid source');
|
|
|
|
strictEqual(Tech.selectSourceHandler(sourceB), null, 'no handler was selected to handle the invalid source');
|
|
|
|
|
|
|
|
// Test canPlaySource return values
|
|
|
|
strictEqual(Tech.canPlaySource(sourceA), 'probably', 'the Tech returned probably for the valid source');
|
|
|
|
strictEqual(Tech.canPlaySource(sourceB), '', 'the Tech returned an empty string for the invalid source');
|
|
|
|
|
|
|
|
// Pass a source through the source handler process of a tech instance
|
|
|
|
tech.setSource(sourceA);
|
|
|
|
strictEqual(tech.currentSource_, sourceA, 'sourceA was handled and stored');
|
|
|
|
ok(tech.sourceHandler_.dispose, 'the handlerOne state instance was stored');
|
|
|
|
|
|
|
|
// Check that the handler dipose method works
|
|
|
|
ok(!disposeCalled, 'dispose has not been called for the handler yet');
|
|
|
|
tech.dispose();
|
|
|
|
ok(disposeCalled, 'the handler dispose method was called when the tech was disposed');
|
|
|
|
});
|
2015-02-28 01:50:13 +02:00
|
|
|
|
|
|
|
test('should handle unsupported sources with the source hanlder API', function(){
|
|
|
|
var mockPlayer = {
|
|
|
|
off: this.noop,
|
|
|
|
trigger: this.noop
|
|
|
|
};
|
|
|
|
|
|
|
|
// Define a new tech class
|
|
|
|
var Tech = videojs.MediaTechController.extend();
|
|
|
|
// Extend Tech with source handlers
|
|
|
|
vjs.MediaTechController.withSourceHandlers(Tech);
|
|
|
|
// Create an instance of Tech
|
|
|
|
var tech = new Tech(mockPlayer);
|
|
|
|
|
|
|
|
var usedNative;
|
|
|
|
Tech.nativeSourceHandler = {
|
|
|
|
handleSource: function(){ usedNative = true; }
|
|
|
|
};
|
|
|
|
|
|
|
|
tech.setSource('');
|
|
|
|
ok(usedNative, 'native source handler was used when an unsupported source was set');
|
|
|
|
});
|