Skip to content

Commit

Permalink
[mozilla#414] Adds support for disable/enable plugin commands; Includ…
Browse files Browse the repository at this point in the history
…es unit tests
  • Loading branch information
rwaldron committed May 11, 2011
1 parent 6be6fc6 commit 88a906d
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 23 deletions.
63 changes: 55 additions & 8 deletions popcorn.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
(function(global, document) {

// Cache refs to speed up calls to native utils

var
forEach = Array.prototype.forEach,
hasOwn = Object.prototype.hasOwnProperty,
slice = Array.prototype.slice,
toString = Object.prototype.toString,

AP = Array.prototype,
OP = Object.prototype,

forEach = AP.forEach,
slice = AP.slice,
hasOwn = OP.hasOwnProperty,
toString = OP.toString,

// ID string matching
rIdExp = /^(#([\w\-\_\.]+))$/,
Expand Down Expand Up @@ -143,10 +148,20 @@
// Register new instance
Popcorn.addInstance( this );

this.options = options || { };
this.options = options || {};

this.data = {
history: [],

// Allows disabling a plugin per instance
disabled: [],

// Stores DOM event queues by type
events: {},

// Store track event history data
history: [],

// Playback track event queues
trackEvents: {
byStart: [{
start: -1,
Expand Down Expand Up @@ -207,7 +222,10 @@
while ( tracksByStart[ tracks.startIndex ] && tracksByStart[ tracks.startIndex ].start <= currentTime ) {
// If plugin does not exist on this instance, remove it
if ( !tracksByStart[ tracks.startIndex ]._natives || !!that[ tracksByStart[ tracks.startIndex ]._natives.type ] ) {
if ( tracksByStart[ tracks.startIndex ].end > currentTime && tracksByStart[ tracks.startIndex ]._running === false ) {
if ( tracksByStart[ tracks.startIndex ].end > currentTime &&
tracksByStart[ tracks.startIndex ]._running === false &&
that.data.disabled.indexOf( tracksByStart[ tracks.endIndex ]._natives.type ) === -1 ) {

tracksByStart[ tracks.startIndex ]._running = true;
tracksByStart[ tracks.startIndex ]._natives.start.call( that, event, tracksByStart[ tracks.startIndex ] );
}
Expand Down Expand Up @@ -240,7 +258,10 @@
while ( tracksByEnd[ tracks.endIndex ] && tracksByEnd[ tracks.endIndex ].end > currentTime ) {
// if plugin does not exist on this instance, remove it
if ( !tracksByEnd[ tracks.endIndex ]._natives || !!that[ tracksByEnd[ tracks.endIndex ]._natives.type ] ) {
if ( tracksByEnd[ tracks.endIndex ].start <= currentTime && tracksByEnd[ tracks.endIndex ]._running === false ) {
if ( tracksByEnd[ tracks.endIndex ].start <= currentTime &&
tracksByEnd[ tracks.endIndex ]._running === false &&
that.data.disabled.indexOf( tracksByStart[ tracks.endIndex ]._natives.type ) === -1 ) {

tracksByEnd[ tracks.endIndex ]._running = true;
tracksByEnd[ tracks.endIndex ]._natives.start.call( that, event, tracksByEnd[tracks.endIndex] );
}
Expand Down Expand Up @@ -356,6 +377,26 @@
}

return Popcorn.extend({}, bounds, { top: top, left: left });
},

disable: function( instance, plugin ) {

var disabled = instance.data.disabled;

if ( disabled.indexOf( plugin ) === -1 ) {
disabled.push( plugin );
}
return instance;
},
enable: function( instance, plugin ) {

var disabled = instance.data.disabled,
index = disabled.indexOf( plugin );

if ( index > -1 ) {
disabled.splice( index, 1 );
}
return instance;
}
});

Expand Down Expand Up @@ -428,6 +469,12 @@
}
});

Popcorn.forEach( "enable disable".split(" "), function( method ) {
Popcorn.p[ method ] = function( plugin ) {
return Popcorn[ method ]( this, plugin );
};
});

Popcorn.Events = {
UIEvents: "blur focus focusin focusout load resize scroll unload ",
MouseEvents: "mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave click dblclick",
Expand Down
72 changes: 57 additions & 15 deletions test/popcorn.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,19 @@ test("API", function () {

test("Utility", function () {

expect(8);
expect(10);
// TODO: comprehensive tests for these utilities

equals( typeof Popcorn.forEach, "function" , "Popcorn.forEach is a provided utility function");
equals( typeof Popcorn.extend, "function" , "Popcorn.extend is a provided utility function");
equals( typeof Popcorn.error, "function" , "Popcorn.error is a provided utility function");
equals( typeof Popcorn.guid, "function" , "Popcorn.guid is a provided utility function");
equals( typeof Popcorn.sizeOf, "function" , "Popcorn.sizeOf is a provided utility function");
equals( typeof Popcorn.nop, "function" , "Popcorn.nop is a provided utility function");
equals( typeof Popcorn.addTrackEvent, "function" , "Popcorn.addTrackEvent is a provided utility function");
equals( typeof Popcorn.position, "function" , "Popcorn.position is a provided utility function");
equals( typeof Popcorn.forEach, "function" , "Popcorn.forEach is a provided static function");
equals( typeof Popcorn.extend, "function" , "Popcorn.extend is a provided static function");
equals( typeof Popcorn.error, "function" , "Popcorn.error is a provided static function");
equals( typeof Popcorn.guid, "function" , "Popcorn.guid is a provided static function");
equals( typeof Popcorn.sizeOf, "function" , "Popcorn.sizeOf is a provided static function");
equals( typeof Popcorn.nop, "function" , "Popcorn.nop is a provided static function");
equals( typeof Popcorn.addTrackEvent, "function" , "Popcorn.addTrackEvent is a provided static function");
equals( typeof Popcorn.position, "function" , "Popcorn.position is a provided static function");
equals( typeof Popcorn.disable, "function" , "Popcorn.disable is a provided static function");
equals( typeof Popcorn.enable, "function" , "Popcorn.enable is a provided static function");
});

test("Standard Time Strings" , function () {
Expand Down Expand Up @@ -203,16 +205,16 @@ test("Instances", function() {

Popcorn("#video");

ok( typeof Popcorn.addInstance === "function" , "Popcorn.addInstance is a provided utility function");
ok( typeof Popcorn.addInstance === "function" , "Popcorn.addInstance is a provided static function");
plus();

ok( typeof Popcorn.removeInstance === "function" , "Popcorn.removeInstance is a provided utility function");
ok( typeof Popcorn.removeInstance === "function" , "Popcorn.removeInstance is a provided static function");
plus();

ok( typeof Popcorn.getInstanceById === "function" , "Popcorn.getInstanceById is a provided utility function");
ok( typeof Popcorn.getInstanceById === "function" , "Popcorn.getInstanceById is a provided static function");
plus();

ok( typeof Popcorn.removeInstanceById === "function" , "Popcorn.removeInstanceById is a provided utility function");
ok( typeof Popcorn.removeInstanceById === "function" , "Popcorn.removeInstanceById is a provided static function");
plus();

ok( typeof Popcorn.instanceIds === "object" , "Popcorn.instanceIds is a provided cache object");
Expand Down Expand Up @@ -1694,19 +1696,59 @@ test("Index Integrity", function () {

});

test("Popcorn.disable/enable", function() {


var $pop = Popcorn.getInstanceById( "video" ),
count = 0,
expects = 4;

expect( expects );

function plus() {
if ( ++count === expects ) {
start();
}
}

stop( 10000 );

// rw/ff

// Test static function call
Popcorn.disable( $pop, "rw" );

ok( $pop.data.disabled.indexOf("rw") > -1, "Plugin: rw is disabled" );
plus();

// Test per-instance function call
$pop.disable( "ff" );

ok( $pop.data.disabled.indexOf("ff") > -1, "Plugin: ff is disabled" );
plus();

// Test static function call
Popcorn.enable( $pop, "rw" );

ok( $pop.data.disabled.indexOf("rw") === -1, "Plugin: rw is enabled" );
plus();

// Test per-instance function call
$pop.enable( "ff" );

ok( $pop.data.disabled.indexOf("ff") === -1, "Plugin: ff is enabled" );
plus();

//console.log( Popcorn.instances[ Popcorn.instanceIds.video ], );
});

module("Popcorn XHR");
test("Basic", function () {

expect(2);

equals( typeof Popcorn.xhr, "function" , "Popcorn.xhr is a provided utility function");
equals( typeof Popcorn.xhr.httpData, "function" , "Popcorn.xhr.httpData is a provided utility function");
equals( typeof Popcorn.xhr, "function" , "Popcorn.xhr is a provided static function");
equals( typeof Popcorn.xhr.httpData, "function" , "Popcorn.xhr.httpData is a provided static function");


});
Expand Down

0 comments on commit 88a906d

Please sign in to comment.