1
0
mirror of https://github.com/videojs/video.js.git synced 2024-12-14 11:23:30 +02:00
video.js/test/unit/plugins.js

165 lines
3.9 KiB
JavaScript
Raw Normal View History

module('Plugins');
2013-02-04 18:55:31 +03:00
test('Plugin should get initialized and receive options', function(){
expect(2);
vjs.plugin('myPlugin1', function(options){
ok(true, 'Plugin initialized');
ok(options['test'], 'Option passed through');
});
vjs.plugin('myPlugin2', function(options){
ok(false, 'Plugin initialized and should not have been');
2013-02-04 18:55:31 +03:00
});
var player = PlayerTest.makePlayer({
'plugins': {
'myPlugin1': {
'test': true
}
}
});
player.dispose();
});
test('Plugin should have the option of being initilized outside of player init', function(){
2013-02-04 21:31:53 +03:00
expect(3);
2013-02-04 18:55:31 +03:00
vjs.plugin('myPlugin3', function(options){
ok(true, 'Plugin initialized after player init');
ok(options['test'], 'Option passed through');
});
var player = PlayerTest.makePlayer({});
ok(player['myPlugin3'], 'Plugin has direct access on player instance');
2013-02-04 18:55:31 +03:00
player['myPlugin3']({
2013-02-04 18:55:31 +03:00
'test': true
});
player.dispose();
});
test('Plugin should be able to add a UI component', function(){
2013-02-04 21:31:53 +03:00
expect(2);
2013-02-04 18:55:31 +03:00
vjs.plugin('myPlugin4', function(options){
2013-02-04 21:31:53 +03:00
ok((this instanceof vjs.Player), 'Plugin executed in player scope by default');
2013-02-04 18:55:31 +03:00
this.addChild('component');
});
var player = PlayerTest.makePlayer({});
player['myPlugin4']({
2013-02-04 18:55:31 +03:00
'test': true
});
var comp = player.getChild('component');
2013-02-04 21:31:53 +03:00
ok(comp, 'Plugin added a component to the player');
2013-02-04 18:55:31 +03:00
player.dispose();
});
test('Plugin should overwrite plugin of same name', function(){
var v1Called = 0,
v2Called = 0,
v3Called = 0;
// Create initial plugin
vjs.plugin('myPlugin5', function(options){
v1Called++;
});
var player = PlayerTest.makePlayer({});
player['myPlugin5']({});
// Overwrite and create new player
vjs.plugin('myPlugin5', function(options){
v2Called++;
});
var player2 = PlayerTest.makePlayer({});
player2['myPlugin5']({});
// Overwrite and init new version on existing player
vjs.plugin('myPlugin5', function(options){
v3Called++;
});
player2['myPlugin5']({});
var comp = player.getChild('component');
ok(v1Called === 1, 'First version of plugin called once');
ok(v2Called === 1, 'Plugin overwritten for new player');
ok(v3Called === 1, 'Plugin overwritten for existing player');
player.dispose();
player2.dispose();
});
2013-02-07 00:14:42 +03:00
2013-02-12 00:18:12 +03:00
test('Plugins should get events in registration order', function() {
2013-02-07 00:14:42 +03:00
var order = [];
var expectedOrder = [];
var pluginName = 'orderPlugin';
var i = 0;
var name;
var player = PlayerTest.makePlayer({});
2013-02-12 00:18:12 +03:00
var plugin = function (name) {
vjs.plugin(name, function (opts) {
this.on('test', function (event) {
order.push(name);
});
});
player[name]({});
};
2013-02-07 00:14:42 +03:00
for (; i < 3; i++ ) {
name = pluginName + i;
expectedOrder.push(name);
2013-02-12 00:18:12 +03:00
plugin(name);
2013-02-07 00:14:42 +03:00
}
2013-02-12 00:18:12 +03:00
vjs.plugin('testerPlugin', function (opts) {
2013-02-07 00:14:42 +03:00
this.trigger('test');
});
player['testerPlugin']({});
2013-02-12 00:18:12 +03:00
deepEqual(order, expectedOrder, 'plugins should receive events in order of initialization');
2013-02-07 00:14:42 +03:00
player.dispose();
});
2013-02-08 21:31:15 +03:00
test('Plugins should not get events after stopImmediatePropagation is called', function () {
var order = [];
var expectedOrder = [];
var pluginName = 'orderPlugin';
var i = 0;
var name;
var player = PlayerTest.makePlayer({});
2013-02-12 00:18:12 +03:00
var plugin = function (name) {
vjs.plugin(name, function (opts) {
this.on('test', function (event) {
order.push(name);
event.stopImmediatePropagation();
});
});
player[name]({});
};
2013-02-08 21:31:15 +03:00
for (; i < 3; i++ ) {
name = pluginName + i;
expectedOrder.push(name);
2013-02-12 00:35:43 +03:00
plugin(name);
2013-02-08 21:31:15 +03:00
}
2013-02-12 00:18:12 +03:00
vjs.plugin('testerPlugin', function (opts) {
2013-02-08 21:31:15 +03:00
this.trigger('test');
});
player['testerPlugin']({});
2013-02-12 00:18:12 +03:00
deepEqual(order, expectedOrder.slice(0, order.length), 'plugins should receive events in order of initialization, until stopImmediatePropagation');
2013-02-08 21:31:15 +03:00
2013-02-12 00:18:12 +03:00
equal(order.length, 1, 'only one event listener should have triggered');
2013-02-08 21:35:35 +03:00
player.dispose();
});