From: David Seifried Date: Tue, 20 Sep 2011 13:21:15 +0000 (-0400) Subject: added in destroy function and unit tests. testDestroy.html is only for this commit... X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=9ad1f334cc5b08020bf36abdaa51cacb97703ebc;p=popcorn-js.git added in destroy function and unit tests. testDestroy.html is only for this commit to show what I mean to others --- diff --git a/popcorn.js b/popcorn.js index e2c7320..6db21a9 100644 --- a/popcorn.js +++ b/popcorn.js @@ -4,7 +4,7 @@ if ( !document.addEventListener ) { global.Popcorn = {}; - var methods = ( "removeInstance addInstance getInstanceById removeInstanceById " + + var methods = ( "removeInstance destroy addInstance getInstanceById removeInstanceById " + "forEach extend effects error guid sizeOf isArray nop position disable enable " + "addTrackEvent removeTrackEvent getTrackEvents getTrackEvent getLastTrackEventId " + "timeUpdate plugin removePlugin compose effect parser xhr getJSONP getScript" ).split(/\s+/); @@ -82,6 +82,29 @@ return Popcorn.instances; }; + Popcorn.destroy = function( instance ) { + var events = instance.data.events, + singleEvent; + + // Iterate through all events and remove them + for( item in events ) { + singleEvent = events[ item ]; + for( fn in singleEvent ) { + delete singleEvent[ fn ]; + } + events[ item ] = null; + } + + //instance.media.removeEventListener( "timeupdate", function( event ) { + //console.log("HERE"); + //Popcorn.timeUpdate( that, event ); + //}, false ); + + instance.media.removeEventListener( "timeupdate", instance.data.timeUpdateFunction, false ); + + Popcorn.removeInstance( instance ); + }; + // Addes a Popcorn instance to the Popcorn instance array Popcorn.addInstance = function( instance ) { @@ -259,10 +282,17 @@ } else { - that.media.addEventListener( "timeupdate", function( event ) { + that.data.timeUpdateFunction = function( event ) { + console.log( "here" ); + Popcorn.timeUpdate( that, event ); + } - Popcorn.timeUpdate( that, event ); - }, false ); + // that.media.addEventListener( "timeupdate", function( event ) { + // console.log("HERE"); + // Popcorn.timeUpdate( that, event ); + //}, false ); + // + that.media.addEventListener( "timeupdate", that.data.timeUpdateFunction, false ); } } else { global.setTimeout(function() { @@ -1143,6 +1173,11 @@ timeUpdate: function( event ) { Popcorn.timeUpdate.call( null, this, event ); return this; + }, + + destroy: function() { + Popcorn.destroy.call( null, this ); + return this; } }); diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index 6dfff8f..77417f6 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -282,6 +282,80 @@ test("Instances", function() { plus(); }); +test( "Popcorn.destroy", function() { + var popcorn = Popcorn( "#video" ), + expects = 8, + count = 0, + playCounter = 0, + timeUpdateCounter = 0; + + expect( expects ); + + function plus() { + if( ++count === expects ) { + start(); + } + } + + function howMany() { + var counter = 0; + + for( things in popcorn.data.events ) { + counter++; + } + + return counter; + } + + stop(); + + // initially no listeners + equals( howMany(), 0, "Initially no events have been added" ); + plus(); + + equals( playCounter, 0, "playCounter is intially 0" ); + plus(); + + equals( timeUpdateCounter, 0, "timeUpdateCounter is intially 0" ); + plus(); + + // add some event listeners for testing + popcorn.listen( "timeupdate", function( event ) { timeUpdateCounter++; }, false ); + popcorn.listen( "play", function( event ) { playCounter++; }, false ); + + popcorn.play( 0 ); + + popcorn.exec( 1, function() { + popcorn.pause(); + + equals( howMany(), 2, "popcorn.data.events has correct number of events - before Popcorn.destroy" ); + plus(); + + equals( playCounter, 1, "playCounter triggered exactly 1 time" ); + plus(); + + equals( timeUpdateCounter, 4, "timeUpdateCounter triggered exactly 4 times" ); + plus(); + + playCounter = timeUpdateCounter = 0; + + popcorn.destroy(); + popcorn.currentTime( 0 ).play(); + + popcorn.exec( 0.25, function() { + popcorn.pause(); + + equals( playCounter, 0, "playCounter was not triggered - after destroy called" ); + plus(); + + equals( timeUpdateCounter, 0, "timeUpdateCounter was not triggered - after destroy called" ); + plus(); + }); + + }); + +}); + test("guid", function() { expect(6); diff --git a/testDestroy.html b/testDestroy.html new file mode 100644 index 0000000..94c3894 --- /dev/null +++ b/testDestroy.html @@ -0,0 +1,48 @@ + + + + + + + + + + +