| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  | /* eslint-env qunit */ | 
					
						
							| 
									
										
										
										
											2016-07-22 13:50:29 -04:00
										 |  |  | import {IE_VERSION} from '../../src/js/utils/browser'; | 
					
						
							| 
									
										
										
										
											2015-09-02 15:47:10 -07:00
										 |  |  | import registerPlugin from '../../src/js/plugins.js'; | 
					
						
							| 
									
										
										
										
											2015-03-25 21:43:41 -07:00
										 |  |  | import Player from '../../src/js/player.js'; | 
					
						
							|  |  |  | import TestHelpers from './test-helpers.js'; | 
					
						
							| 
									
										
										
										
											2015-03-06 01:53:00 +01:00
										 |  |  | import window from 'global/window'; | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  | import sinon from 'sinon'; | 
					
						
							| 
									
										
										
										
											2015-03-25 21:43:41 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  | QUnit.module('Plugins'); | 
					
						
							| 
									
										
										
										
											2013-02-04 07:55:31 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 13:51:31 -04:00
										 |  |  | QUnit.test('Plugin should get initialized and receive options', function(assert) { | 
					
						
							|  |  |  |   assert.expect(2); | 
					
						
							| 
									
										
										
										
											2013-02-04 07:55:31 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   registerPlugin('myPlugin1', function(options) { | 
					
						
							| 
									
										
										
										
											2016-08-12 13:51:31 -04:00
										 |  |  |     assert.ok(true, 'Plugin initialized'); | 
					
						
							|  |  |  |     assert.ok(options.test, 'Option passed through'); | 
					
						
							| 
									
										
										
										
											2013-02-04 07:55:31 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   registerPlugin('myPlugin2', function(options) { | 
					
						
							| 
									
										
										
										
											2016-08-12 13:51:31 -04:00
										 |  |  |     assert.ok(false, 'Plugin initialized and should not have been'); | 
					
						
							| 
									
										
										
										
											2013-02-04 07:55:31 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   const player = TestHelpers.makePlayer({ | 
					
						
							|  |  |  |     plugins: { | 
					
						
							|  |  |  |       myPlugin1: { | 
					
						
							|  |  |  |         test: true | 
					
						
							| 
									
										
										
										
											2013-02-04 07:55:31 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   player.dispose(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 13:51:31 -04:00
										 |  |  | QUnit.test('Plugin should have the option of being initilized outside of player init', function(assert) { | 
					
						
							|  |  |  |   assert.expect(3); | 
					
						
							| 
									
										
										
										
											2013-02-04 07:55:31 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   registerPlugin('myPlugin3', function(options) { | 
					
						
							| 
									
										
										
										
											2016-08-12 13:51:31 -04:00
										 |  |  |     assert.ok(true, 'Plugin initialized after player init'); | 
					
						
							|  |  |  |     assert.ok(options.test, 'Option passed through'); | 
					
						
							| 
									
										
										
										
											2013-02-04 07:55:31 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   const player = TestHelpers.makePlayer({}); | 
					
						
							| 
									
										
										
										
											2013-02-04 07:55:31 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 13:51:31 -04:00
										 |  |  |   assert.ok(player.myPlugin3, 'Plugin has direct access on player instance'); | 
					
						
							| 
									
										
										
										
											2013-02-04 07:55:31 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   player.myPlugin3({ | 
					
						
							|  |  |  |     test: true | 
					
						
							| 
									
										
										
										
											2013-02-04 07:55:31 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   player.dispose(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 13:51:31 -04:00
										 |  |  | QUnit.test('Plugin should be able to add a UI component', function(assert) { | 
					
						
							|  |  |  |   assert.expect(2); | 
					
						
							| 
									
										
										
										
											2013-02-04 07:55:31 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   registerPlugin('myPlugin4', function(options) { | 
					
						
							| 
									
										
										
										
											2016-08-12 13:51:31 -04:00
										 |  |  |     assert.ok((this instanceof Player), 'Plugin executed in player scope by default'); | 
					
						
							| 
									
										
										
										
											2013-02-04 07:55:31 -08:00
										 |  |  |     this.addChild('component'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   const player = TestHelpers.makePlayer({}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   player.myPlugin4({ | 
					
						
							|  |  |  |     test: true | 
					
						
							| 
									
										
										
										
											2013-02-04 07:55:31 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   const comp = player.getChild('component'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 13:51:31 -04:00
										 |  |  |   assert.ok(comp, 'Plugin added a component to the player'); | 
					
						
							| 
									
										
										
										
											2013-02-04 07:55:31 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   player.dispose(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 13:51:31 -04:00
										 |  |  | QUnit.test('Plugin should overwrite plugin of same name', function(assert) { | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   let v1Called = 0; | 
					
						
							|  |  |  |   let v2Called = 0; | 
					
						
							|  |  |  |   let v3Called = 0; | 
					
						
							| 
									
										
										
										
											2013-02-04 16:13:37 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Create initial plugin
 | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   registerPlugin('myPlugin5', function(options) { | 
					
						
							| 
									
										
										
										
											2013-02-04 16:13:37 -08:00
										 |  |  |     v1Called++; | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   const player = TestHelpers.makePlayer({}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   player.myPlugin5({}); | 
					
						
							| 
									
										
										
										
											2013-02-04 16:13:37 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Overwrite and create new player
 | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   registerPlugin('myPlugin5', function(options) { | 
					
						
							| 
									
										
										
										
											2013-02-04 16:13:37 -08:00
										 |  |  |     v2Called++; | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   const player2 = TestHelpers.makePlayer({}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   player2.myPlugin5({}); | 
					
						
							| 
									
										
										
										
											2013-02-04 16:13:37 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Overwrite and init new version on existing player
 | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   registerPlugin('myPlugin5', function(options) { | 
					
						
							| 
									
										
										
										
											2013-02-04 16:13:37 -08:00
										 |  |  |     v3Called++; | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   player2.myPlugin5({}); | 
					
						
							| 
									
										
										
										
											2013-02-04 16:13:37 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 13:51:31 -04:00
										 |  |  |   assert.ok(v1Called === 1, 'First version of plugin called once'); | 
					
						
							|  |  |  |   assert.ok(v2Called === 1, 'Plugin overwritten for new player'); | 
					
						
							|  |  |  |   assert.ok(v3Called === 1, 'Plugin overwritten for existing player'); | 
					
						
							| 
									
										
										
										
											2013-02-04 16:13:37 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   player.dispose(); | 
					
						
							|  |  |  |   player2.dispose(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 13:51:31 -04:00
										 |  |  | QUnit.test('Plugins should get events in registration order', function(assert) { | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   const order = []; | 
					
						
							|  |  |  |   const expectedOrder = []; | 
					
						
							|  |  |  |   const pluginName = 'orderPlugin'; | 
					
						
							|  |  |  |   const player = TestHelpers.makePlayer({}); | 
					
						
							|  |  |  |   const plugin = function(name) { | 
					
						
							|  |  |  |     registerPlugin(name, function(opts) { | 
					
						
							|  |  |  |       this.on('test', function(event) { | 
					
						
							| 
									
										
										
										
											2013-02-11 16:18:12 -05:00
										 |  |  |         order.push(name); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     player[name]({}); | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2013-02-06 16:14:42 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   for (let i = 0; i < 3; i++) { | 
					
						
							|  |  |  |     const name = pluginName + i; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-06 16:14:42 -05:00
										 |  |  |     expectedOrder.push(name); | 
					
						
							| 
									
										
										
										
											2013-02-11 16:18:12 -05:00
										 |  |  |     plugin(name); | 
					
						
							| 
									
										
										
										
											2013-02-06 16:14:42 -05:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-04-09 10:42:41 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   registerPlugin('testerPlugin', function(opts) { | 
					
						
							| 
									
										
										
										
											2013-02-06 16:14:42 -05:00
										 |  |  |     this.trigger('test'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   player.testerPlugin({}); | 
					
						
							| 
									
										
										
										
											2013-02-06 16:14:42 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 13:51:31 -04:00
										 |  |  |   assert.deepEqual(order, | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |             expectedOrder, | 
					
						
							|  |  |  |             'plugins should receive events in order of initialization'); | 
					
						
							| 
									
										
										
										
											2013-02-06 16:14:42 -05:00
										 |  |  |   player.dispose(); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2013-02-08 13:31:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 13:51:31 -04:00
										 |  |  | QUnit.test('Plugins should not get events after stopImmediatePropagation is called', function(assert) { | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   const order = []; | 
					
						
							|  |  |  |   const expectedOrder = []; | 
					
						
							|  |  |  |   const pluginName = 'orderPlugin'; | 
					
						
							|  |  |  |   const player = TestHelpers.makePlayer({}); | 
					
						
							|  |  |  |   const plugin = function(name) { | 
					
						
							|  |  |  |     registerPlugin(name, function(opts) { | 
					
						
							|  |  |  |       this.on('test', function(event) { | 
					
						
							| 
									
										
										
										
											2013-02-11 16:18:12 -05:00
										 |  |  |         order.push(name); | 
					
						
							|  |  |  |         event.stopImmediatePropagation(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     player[name]({}); | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2013-02-08 13:31:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   for (let i = 0; i < 3; i++) { | 
					
						
							|  |  |  |     const name = pluginName + i; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-08 13:31:15 -05:00
										 |  |  |     expectedOrder.push(name); | 
					
						
							| 
									
										
										
										
											2013-02-11 16:35:43 -05:00
										 |  |  |     plugin(name); | 
					
						
							| 
									
										
										
										
											2013-02-08 13:31:15 -05:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-04-09 10:42:41 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   registerPlugin('testerPlugin', function(opts) { | 
					
						
							| 
									
										
										
										
											2013-02-08 13:31:15 -05:00
										 |  |  |     this.trigger('test'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   player.testerPlugin({}); | 
					
						
							| 
									
										
										
										
											2013-02-08 13:31:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 13:51:31 -04:00
										 |  |  |   assert.deepEqual(order, | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |             expectedOrder.slice(0, order.length), | 
					
						
							|  |  |  |             'plugins should receive events in order of ' + | 
					
						
							|  |  |  |             'initialization, until stopImmediatePropagation'); | 
					
						
							| 
									
										
										
										
											2013-02-08 13:31:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 13:51:31 -04:00
										 |  |  |   assert.equal(order.length, 1, 'only one event listener should have triggered'); | 
					
						
							| 
									
										
										
										
											2013-02-08 13:35:35 -05:00
										 |  |  |   player.dispose(); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2015-03-06 01:53:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 13:51:31 -04:00
										 |  |  | QUnit.test('Plugin that does not exist logs an error', function(assert) { | 
					
						
							| 
									
										
										
										
											2016-07-22 13:50:29 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 01:53:00 +01:00
										 |  |  |   // stub the global log functions
 | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   const console = window.console = { | 
					
						
							|  |  |  |     log() {}, | 
					
						
							|  |  |  |     warn() {}, | 
					
						
							|  |  |  |     error() {} | 
					
						
							| 
									
										
										
										
											2015-03-06 01:53:00 +01:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |   const log = sinon.stub(console, 'log'); | 
					
						
							|  |  |  |   const error = sinon.stub(console, 'error'); | 
					
						
							|  |  |  |   const origConsole = window.console; | 
					
						
							| 
									
										
										
										
											2015-03-06 01:53:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // enable a non-existing plugin
 | 
					
						
							|  |  |  |   TestHelpers.makePlayer({ | 
					
						
							|  |  |  |     plugins: { | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |       nonExistingPlugin: { | 
					
						
							|  |  |  |         foo: 'bar' | 
					
						
							| 
									
										
										
										
											2015-03-06 01:53:00 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 13:51:31 -04:00
										 |  |  |   assert.ok(error.called, 'error was called'); | 
					
						
							| 
									
										
										
										
											2016-07-22 13:50:29 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (IE_VERSION && IE_VERSION < 11) { | 
					
						
							| 
									
										
										
										
											2016-08-12 13:51:31 -04:00
										 |  |  |     assert.equal(error.firstCall.args[0], | 
					
						
							| 
									
										
										
										
											2016-08-03 15:27:03 -04:00
										 |  |  |                 'VIDEOJS: ERROR: Unable to find plugin: nonExistingPlugin'); | 
					
						
							| 
									
										
										
										
											2016-07-22 13:50:29 -04:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2016-08-12 13:51:31 -04:00
										 |  |  |     assert.equal(error.firstCall.args[2], 'Unable to find plugin:'); | 
					
						
							|  |  |  |     assert.equal(error.firstCall.args[3], 'nonExistingPlugin'); | 
					
						
							| 
									
										
										
										
											2016-07-22 13:50:29 -04:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-03-06 01:53:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // tear down logging stubs
 | 
					
						
							|  |  |  |   log.restore(); | 
					
						
							|  |  |  |   error.restore(); | 
					
						
							| 
									
										
										
										
											2016-07-22 13:50:29 -04:00
										 |  |  |   window.console = origConsole; | 
					
						
							| 
									
										
										
										
											2015-03-06 01:53:00 +01:00
										 |  |  | }); |