]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
added in destroy function and unit tests. testDestroy.html is only for this commit...
authorDavid Seifried <david.c.seifried@gmail.com>
Tue, 20 Sep 2011 13:21:15 +0000 (09:21 -0400)
committerDavid Seifried <david.c.seifried@gmail.com>
Tue, 20 Sep 2011 13:21:15 +0000 (09:21 -0400)
popcorn.js
test/popcorn.unit.js
testDestroy.html [new file with mode: 0644]

index e2c7320a73229cfaa39255e2c7fe59c740cf6e82..6db21a9809ce26f933b556c55472ec98b34a8809 100644 (file)
@@ -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+/);
     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 ) {
 
 
           } 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() {
     timeUpdate: function( event ) {
       Popcorn.timeUpdate.call( null, this, event );
       return this;
+    },
+
+    destroy: function() {
+      Popcorn.destroy.call( null, this );
+      return this;
     }
   });
 
index 6dfff8f4996c8d8ecd5150f130ac1e8564ec6b5d..77417f6ffb92fa89c609e62b8c664a61545aedd8 100644 (file)
@@ -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 (file)
index 0000000..94c3894
--- /dev/null
@@ -0,0 +1,48 @@
+<html>
+  <head>
+    <script src="popcorn.js"></script>
+    <script>
+    var popcorn;
+
+    function destroy() {
+      Popcorn.destroy( popcorn );
+    }
+
+
+      document.addEventListener( "DOMContentLoaded", function( event ) {
+        popcorn = Popcorn( "#video" );
+        
+        popcorn.listen( "timeupdate", function() { });
+        popcorn.listen( "play", function() {});
+
+        for( item in popcorn.data.events ) {
+          console.log( "EVENT: ", popcorn.data.events[ item ] );
+        }
+
+      }, false );
+    </script>
+  </head>
+  <body>
+    <button onclick="destroy()">DESTROY</button>
+  
+  <video id='video'
+    style="display:;width:300px"
+    controls
+    preload="auto">
+
+    <source id='mp4'
+      src="test/trailer.mp4"
+      type='video/mp4; codecs="avc1, mp4a"'>
+
+    <source id='ogv'
+      src="test/trailer.ogv"
+      type='video/ogg; codecs="theora, vorbis"'>
+      
+    <source id='webm'
+      src="test/trailer.webm"
+      type='video/webm; codecs="vp8, vorbis"'>
+
+    <p>Your user agent does not support the HTML5 Video element.</p>
+  </video>
+  </body>
+</html>