]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
tests for update forwards and backwards, initial removePlugin
authorScottDowne <sdowne@learn.senecac.on.ca>
Mon, 13 Dec 2010 19:46:29 +0000 (14:46 -0500)
committerScottDowne <sdowne@learn.senecac.on.ca>
Mon, 13 Dec 2010 19:46:29 +0000 (14:46 -0500)
popcorn.js
test/popcorn.unit.js

index 74931db06cbd655847d14585420f4cd1b6cb301e..c99986402aaa2b87ebb25f5edb1a770522df9045 100644 (file)
@@ -6,6 +6,9 @@
   hasOwn = Object.prototype.hasOwnProperty, 
   slice = Array.prototype.slice,
 
+  // intentionally left undefined
+  undef,
+
   //  ID string matching
   rIdExp  = /^(#([\w\-\_\.]+))$/, 
 
@@ -34,7 +37,7 @@
       
       this.data = {
         events: {},
-        tracks: {
+        trackEvents: {
           byStart: [{start: -1, end: -1}],
           byEnd:   [{start: -1, end: -1}],
           startIndex: 0,
           // this is so we do not fall off either end
 
           var videoDurationPlus = that.video.duration + 1;
-          Popcorn.addTrack(that, {start: videoDurationPlus, end: videoDurationPlus});
+          Popcorn.addTrackEvent(that, {start: videoDurationPlus, end: videoDurationPlus});
           
           that.video.addEventListener( "timeupdate", function( event ) {
 
             var currentTime    = this.currentTime,
-                previousTime   = that.data.tracks.previousUpdateTime
-                tracks         = that.data.tracks,
+                previousTime   = that.data.trackEvents.previousUpdateTime
+                tracks         = that.data.trackEvents,
                 tracksByEnd    = tracks.byEnd,
                 tracksByStart  = tracks.byStart;
 
     return dest;      
   };
 
-  Popcorn.addTrack = function( obj, track ) {
+  Popcorn.addTrackEvent = function( obj, track ) {
     console.log(obj);
     // Store this definition in an array sorted by times
-    obj.data.tracks.byStart.push( track );
-    obj.data.tracks.byEnd.push( track );
-    obj.data.tracks.byStart.sort( function( a, b ){
+    obj.data.trackEvents.byStart.push( track );
+    obj.data.trackEvents.byEnd.push( track );
+    obj.data.trackEvents.byStart.sort( function( a, b ){
       return ( a.start - b.start );
     });
-    obj.data.tracks.byEnd.sort( function( a, b ){
+    obj.data.trackEvents.byEnd.sort( function( a, b ){
       return ( a.end - b.end );
     });
   };
           this.video.addEventListener( type, function( event ) {
             
             Popcorn.forEach( self.data.events[type], function ( obj, key ) {
-
               if ( typeof obj === "function" ) {
                 obj.call(self, event);
               }
         play: function () {
           //  renders all of the interally stored track commands
         }
+      },
+      removePlugin: function( name ) {
+        
+        this[name] = undef;
+        
+        // remove plugin reference from registry
+        for (var i = 0, rl = Popcorn.registry.length; i < rl; i++) {
+          if (Popcorn.registry[i].type === name) {
+            Popcorn.registry.splice(i, 1);
+            break; // plugin found, stop checking
+          }
+        }
+
+        // remove all trackEvents
+        for (var trackEvent in this.data.trackEvents) {
+          // still working on this, need to clear tracks, failing one test because of it
+        }
+
       }
     }
   };
   
   //  Extend listen and trigger to all Popcorn instances
-  Popcorn.forEach( ["trigger", "listen", "unlisten"], function ( key ) {
+  Popcorn.forEach( ["trigger", "listen", "unlisten", "removePlugin"], function ( key ) {
     Popcorn.p[key] = Popcorn.events.fn[key];
   });  
   
     var natives = Popcorn.events.all, 
 
         reserved = [ "start", "end"], 
-        plugin = {},
+        plugin = {type: name},
         pluginFn, 
         setup;
     
         }
         
 
-        Popcorn.addTrack( this, options );
+        Popcorn.addTrackEvent( this, options );
 
         
         //  Future support for plugin event definitions 
index 699687bf97b0600e92300aa0e2237b4271a1a787..f60d9a9ec0503828e6b029e8042ab48f69c516d2 100644 (file)
@@ -39,7 +39,7 @@ test("API", function () {
 
 test("Utility", function () {
   
-  expect(6);
+  expect(7);
   //  TODO: comprehensive tests for these utilities
   
   equals( typeof Popcorn.forEach, "function" , "Popcorn.forEach is a provided utility function");
@@ -48,6 +48,7 @@ test("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");
   
   
   
@@ -82,8 +83,8 @@ test("Object", function () {
   ok( "data" in popped, "instance has `data` property" );
   ok( Object.prototype.toString.call(popped.data) === "[object Object]", "data property is an object" );
 
-  ok( "tracks" in popped.data, "instance has `tracks` property" );
-  ok( Object.prototype.toString.call(popped.data.tracks) === "[object Object]", "tracks property is an object" )
+  ok( "trackEvents" in popped.data, "instance has `trackEvents` property" );
+  ok( Object.prototype.toString.call(popped.data.trackEvents) === "[object Object]", "trackEvents property is an object" )
 
   
   popped.play();
@@ -158,8 +159,7 @@ test("Stored By Type", function () {
   var p = Popcorn("#video"), 
       count = 0,
       fired = 0, 
-      wants = 4
-      ;
+      wants = 4;
 
   function plus(){ 
 
@@ -365,6 +365,78 @@ test("UI/Mouse", function () {
 });
 
 module("Popcorn Plugin")
+
+test("Update Timer", function () {
+
+  QUnit.reset();
+
+  var p2 = Popcorn("#video"),                         
+      expects = 4, 
+      count   = 0,
+      // These make sure events are only fired once
+      // any second call will produce a failed test
+      forwardStart  = false,
+      forwardEnd    = false,
+      backwardStart = false,
+      backwardEnd   = false;
+
+  function plus() {
+    if ( ++count === expects ) {
+      start(); 
+      // clean up added events after tests
+      p2.removePlugin("forwards");
+      p2.removePlugin("backwards");
+    }
+  }
+  
+  stop();  
+
+  Popcorn.plugin("forwards", function () {
+    return {
+      start: function () {
+        forwardStart = !forwardStart;
+        ok( forwardStart, "forward's start fired");
+        plus();
+      },
+      end: function () {
+        forwardEnd = !forwardEnd;
+        p2.currentTime(1).play();
+        ok( forwardEnd, "forward's end fired");
+        plus();
+      }
+    };
+  });
+
+  p2.forwards({
+    start: 3, 
+    end: 4
+  });
+
+  Popcorn.plugin("backwards", function () {
+    return {
+      start: function () {
+        backwardStart = !backwardStart;
+        p2.currentTime(0).play();
+        ok( true, "backward's start fired");
+        plus();
+      },
+      end: function () {
+        backwardEnd = !backwardEnd;
+        ok( backwardEnd, "backward's end fired");
+        plus();
+      }
+    };
+  });
+
+  p2.backwards({
+    start: 1, 
+    end: 2
+  });
+
+  p2.currentTime(3).play();
+
+});
+
 test("Plugin Factory", function () {
   
   QUnit.reset();
@@ -412,9 +484,9 @@ test("Plugin Factory", function () {
         ok( Object.prototype.toString.call(popped.data) === "[object Object]", "data property is an object" );
         plus();
 
-        ok( "tracks" in this.data, "executor instance has `tracks` property" );
+        ok( "trackEvents" in this.data, "executor instance has `trackEvents` property" );
         plus();
-        ok( Object.prototype.toString.call(popped.data.tracks) === "[object Object]", "executor tracks property is an object" )      
+        ok( Object.prototype.toString.call(popped.data.trackEvents) === "[object Object]", "executor trackEvents property is an object" )      
         plus();      
       }, 
       end: function () {
@@ -461,9 +533,9 @@ test("Plugin Factory", function () {
       ok( Object.prototype.toString.call(popped.data) === "[object Object]", "complicator data property is an object" );
       plus();
 
-      ok( "tracks" in this.data, " complicatorinstance has `tracks` property" );
+      ok( "trackEvents" in this.data, " complicatorinstance has `trackEvents` property" );
       plus();
-      ok( Object.prototype.toString.call(popped.data.tracks) === "[object Object]", "complicator tracks property is an object" )      
+      ok( Object.prototype.toString.call(popped.data.trackEvents) === "[object Object]", "complicator trackEvents property is an object" )      
       plus();     
     },
     end: function () {