]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#494] Adds initial set of tests for frameAnimation option
authorRick Waldron <waldron.rick@gmail.com>
Wed, 7 Sep 2011 17:36:45 +0000 (13:36 -0400)
committerRick Waldron <waldron.rick@gmail.com>
Wed, 7 Sep 2011 17:36:45 +0000 (13:36 -0400)
popcorn.js
test/popcorn.unit.js

index 664acc327feacd3659c2d47267064bf3364d368b..e70d8db5721222793149e8123128f85681e062bd 100644 (file)
     }
   },
 
+  //  Non-public `requestAnimFrame`
+  //  http://paulirish.com/2011/requestanimationframe-for-smart-animating/
   requestAnimFrame = (function(){
-    // shim from http://paulirish.com/2011/requestanimationframe-for-smart-animating/
-    return global.requestAnimationFrame       || 
-      global.webkitRequestAnimationFrame || 
-      global.mozRequestAnimationFrame    || 
-      global.oRequestAnimationFrame      || 
-      global.msRequestAnimationFrame     || 
-      function( callback, element){
+    return global.requestAnimationFrame ||
+      global.webkitRequestAnimationFrame ||
+      global.mozRequestAnimationFrame ||
+      global.oRequestAnimationFrame ||
+      global.msRequestAnimationFrame ||
+      function( callback, element ) {
         global.setTimeout( callback, 16 );
       };
   }()),
       //  Wrap true ready check
       var isReady = function( that ) {
 
+        var duration, videoDurationPlus, animate;
+
         if ( that.media.readyState >= 2 ) {
           //  Adding padding to the front and end of the arrays
           //  this is so we do not fall off either end
 
-          var duration = that.media.duration,
-              //  Check for no duration info (NaN)
-              videoDurationPlus = duration != duration ? Number.MAX_VALUE : duration + 1;
+          duration = that.media.duration;
+          //  Check for no duration info (NaN)
+          videoDurationPlus = duration != duration ? Number.MAX_VALUE : duration + 1;
 
           Popcorn.addTrackEvent( that, {
             start: videoDurationPlus,
           });
 
           if ( that.options.frameAnimation ) {
-            // if Popcorn is created with frameAnimation option set to true,
-            // requestAnimFrame is used instead of "timeupdate" media event.
-            // This is for greater frame time accuracy, theoretically up to
-            // 60 frames per second as opposed to 4
+            //  if Popcorn is created with frameAnimation option set to true,
+            //  requestAnimFrame is used instead of "timeupdate" media event.
+            //  This is for greater frame time accuracy, theoretically up to
+            //  60 frames per second as opposed to ~4 ( ~every 15-250ms)
+            animate = function () {
 
-            var animate = function () {
               Popcorn.timeUpdate( that, event );
+
               that.trigger( "timeupdate" );
 
               requestAnimFrame( animate );
 
               Popcorn.timeUpdate( that, event );
             }, false );
-
           }
-
         } else {
           global.setTimeout(function() {
             isReady( that );
 
     timeUpdate: function( event ) {
       Popcorn.timeUpdate.call( null, this, event );
-      return this; 
+      return this;
     }
   });
 
index 605922acebd1636efe4639e226d3c92ba41851ed..38f3b56d14097655ae0a9b3064d8cf6f9034c47d 100644 (file)
@@ -1479,7 +1479,7 @@ test("Start Zero Immediately", function () {
     end: 2
   });
 });
-test("Update Timer", function () {
+test("Update Timer (timeupdate)", function () {
 
   QUnit.reset();
 
@@ -1647,7 +1647,176 @@ test("Update Timer", function () {
 
 });
 
-test("Plugin Factory", function () {
+test("Update Timer (frameAnimation)", function () {
+
+  QUnit.reset();
+
+  var p2 = Popcorn("#video", { frameAnimation: true }),
+      expects = 12,
+      count   = 0,
+      execCount = 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,
+      wrapperRunning = { one: false, two: false, };
+
+  function plus() {
+    if ( ++count === expects ) {
+      // clean up added events after tests
+      Popcorn.removePlugin( "forwards" );
+      Popcorn.removePlugin( "backwards" );
+      Popcorn.removePlugin( "wrapper" );
+      p2.removePlugin( "exec" );
+      start();
+    }
+  }
+
+  // These tests come close to 10 seconds on chrome, increasing to 15
+  stop( 15000 );
+
+  Popcorn.plugin( "forwards", function () {
+    return {
+      start: function ( event, options ) {
+
+        if ( !options.startFired ) {
+
+          options.startFired = true;
+          forwardStart = !forwardStart;
+          ok( forwardStart, "forward's start fired" );
+          plus();
+        }
+      },
+      end: function ( event, options ) {
+
+        if ( !options.endFired ) {
+
+          options.endFired = true;
+          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 ( event, options ) {
+
+        if ( !options.startFired ) {
+
+          options.startFired = true;
+          backwardStart = !backwardStart;
+          p2.currentTime(0).play();
+          ok( true, "backward's start fired" );
+          plus();
+        }
+      },
+      end: function ( event, options ) {
+
+        if ( !options.endFired ) {
+
+          options.endFired = true;
+          backwardEnd = !backwardEnd;
+          ok( backwardEnd, "backward's end fired" );
+          plus();
+          p2.currentTime( 5 ).play();
+        }
+      }
+    };
+  });
+
+  p2.backwards({
+    start: 1,
+    end: 2
+  });
+
+  Popcorn.plugin( "wrapper", {
+    start: function ( event, options ) {
+
+      wrapperRunning[ options.wrapper ] = true;
+    },
+    end: function ( event, options ) {
+
+      wrapperRunning[ options.wrapper ] = false;
+    }
+  });
+
+  // second instance of wrapper is wrapping the first
+  p2.wrapper({
+    start: 6,
+    end: 7,
+    wrapper: "one"
+  })
+  .wrapper({
+    start: 5,
+    end: 8,
+    wrapper: "two"
+  })
+  // checking wrapper 2's start
+  .exec( 5, function() {
+
+    if ( execCount === 0 ) {
+
+      execCount++;
+      ok( wrapperRunning.two, "wrapper two is running at second 5" );
+      plus();
+      ok( !wrapperRunning.one, "wrapper one is stopped at second 5" );
+      plus();
+    }
+  })
+  // checking wrapper 1's start
+  .exec( 6, function() {
+
+    if ( execCount === 1 ) {
+
+      execCount++;
+      ok( wrapperRunning.two, "wrapper two is running at second 6" );
+      plus();
+      ok( wrapperRunning.one, "wrapper one is running at second 6" );
+      plus();
+    }
+  })
+  // checking wrapper 1's end
+  .exec( 7, function() {
+
+    if ( execCount === 2 ) {
+
+      execCount++;
+      ok( wrapperRunning.two, "wrapper two is running at second 7" );
+      plus();
+      ok( !wrapperRunning.one, "wrapper one is stopped at second 7" );
+      plus();
+    }
+  })
+  // checking wrapper 2's end
+  .exec( 8, function() {
+
+    if ( execCount === 3 ) {
+
+      execCount++;
+      ok( !wrapperRunning.two, "wrapper two is stopped at second 9" );
+      plus();
+      ok( !wrapperRunning.one, "wrapper one is stopped at second 9" );
+      plus();
+    }
+  });
+
+  p2.currentTime(3).play();
+
+});
+
+
+test("Plugin Factory (timeupdate)", function () {
 
   QUnit.reset();
 
@@ -1766,6 +1935,125 @@ test("Plugin Factory", function () {
 
 });
 
+test("Plugin Factory (frameAnimation)", function () {
+
+  QUnit.reset();
+
+  var popped = Popcorn("#video", { frameAnimation: true }),
+      methods = "load play pause currentTime mute volume roundTime exec removePlugin",
+      expects = 34, // 15*2+2+2. executor/complicator each do 15
+      count = 0;
+
+  function plus() {
+    if ( ++count == expects ) {
+      Popcorn.removePlugin("executor");
+      Popcorn.removePlugin("complicator");
+      start();
+    }
+  }
+
+  expect( expects );
+  stop( 15000 );
+
+  Popcorn.plugin("executor", function () {
+
+    return {
+
+      start: function () {
+        var self = this;
+
+        // These ensure that a popcorn instance is the value of `this` inside a plugin definition
+
+        methods.split(/\s+/g).forEach(function (k,v) {
+          ok( k in self, "executor instance has method: " + k );
+
+          plus();
+        });
+
+        ok( "video" in this, "executor instance has `video` property" );
+        plus();
+        ok( Object.prototype.toString.call(popped.video) === "[object HTMLVideoElement]", "video property is a HTMLVideoElement" );
+        plus();
+
+        ok( "data" in this, "executor instance has `data` property" );
+        plus();
+        ok( Object.prototype.toString.call(popped.data) === "[object Object]", "data property is an object" );
+        plus();
+
+        ok( "trackEvents" in this.data, "executor instance has `trackEvents` property" );
+        plus();
+        ok( Object.prototype.toString.call(popped.data.trackEvents) === "[object Object]", "executor trackEvents property is an object" )
+        plus();
+      },
+      end: function () {
+
+      }
+    };
+
+  });
+
+  ok( "executor" in popped, "executor plugin is now available to instance" );
+  plus();
+  equals( Popcorn.registry.length, 1, "One item in the registry");
+  plus();
+
+  popped.executor({
+    start: 1,
+    end: 2
+  });
+
+  Popcorn.plugin("complicator", {
+
+    start: function ( event ) {
+
+      var self = this;
+
+      // These ensure that a popcorn instance is the value of `this` inside a plugin definition
+
+      methods.split(/\s+/g).forEach(function (k,v) {
+        ok( k in self, "complicator instance has method: " + k );
+
+        plus();
+      });
+
+      ok( "video" in this, "complicator instance has `video` property" );
+      plus();
+      ok( Object.prototype.toString.call(popped.video) === "[object HTMLVideoElement]", "video property is a HTMLVideoElement" );
+      plus();
+
+      ok( "data" in this, "complicator instance has `data` property" );
+      plus();
+      ok( Object.prototype.toString.call(popped.data) === "[object Object]", "complicator data property is an object" );
+      plus();
+
+      ok( "trackEvents" in this.data, " complicatorinstance has `trackEvents` property" );
+      plus();
+      ok( Object.prototype.toString.call(popped.data.trackEvents) === "[object Object]", "complicator trackEvents property is an object" )
+      plus();
+    },
+    end: function () {
+
+      //start();
+
+    },
+    timeupdate: function () {
+    }
+  });
+
+  ok( "complicator" in popped, "complicator plugin is now available to instance" );
+  plus();
+  equals( Popcorn.registry.length, 2, "Two items in the registry");
+  plus();
+
+  popped.complicator({
+    start: 4,
+    end: 5
+  });
+
+  popped.currentTime(0).play();
+
+});
+
 test( "Popcorn Compose", function () {
 
   QUnit.reset();
@@ -2625,7 +2913,7 @@ test("Index Integrity", function () {
 
 });
 
-test("Popcorn.disable/enable/toggle", function() {
+test("Popcorn.disable/enable/toggle (timeupdate)", function() {
 
 
   var $pop = Popcorn( "#video" ),
@@ -2639,6 +2927,7 @@ test("Popcorn.disable/enable/toggle", function() {
       start();
 
       Popcorn.removeInstance( $pop );
+                       Popcorn.removePlugin( "toggler" );
     }
   }
 
@@ -3239,7 +3528,7 @@ test("Parsing Handler - References unavailable plugin", function () {
 });
 
 module("Audio");
-test( "Basic Audio Support", function () {
+test( "Basic Audio Support (timeupdate)", function () {
 
   var popped = Popcorn( "#audio" ),
       popObj = Popcorn( document.getElementById( "audio" ) ),
@@ -3308,6 +3597,75 @@ test( "Basic Audio Support", function () {
   });
 });
 
+test( "Basic Audio Support (frameAnimation)", function () {
+
+  var popped = Popcorn( "#audio", { frameAnimation: true }),
+      popObj = Popcorn( document.getElementById( "audio" ), { frameAnimation: true }),
+      methods = "load play pause currentTime mute volume roundTime exec removePlugin",
+      count = 0,
+      expects = 30;
+
+  expect( expects );
+
+  function plus() {
+
+    if ( ++count === expects ) {
+
+      start();
+    }
+  }
+
+  stop( 10000 );
+
+  // testing element passed by id
+  ok( "audio" in popped, "instance by id has `media` property" );
+  plus();
+  equal( Object.prototype.toString.call( popped.media ), "[object HTMLAudioElement]", "instance by id media property is a HTMLAudioElement" );
+  plus();
+
+  ok( "data" in popped, "instance by id has `data` property" );
+  plus();
+  equal( Object.prototype.toString.call( popped.data ), "[object Object]", "instance by id data property is an object" );
+  plus();
+
+  ok( "trackEvents" in popped.data, "instance by id has `trackEvents` property" );
+  plus();
+  equal( Object.prototype.toString.call( popped.data.trackEvents ), "[object Object]", "instance by id trackEvents property is an object" );
+  plus();
+
+  popped.play();
+
+  methods.split( /\s+/g ).forEach(function ( k,v ) {
+
+    ok( k in popped, "instance by id has method: " + k );
+    plus();
+  });
+
+  // testing element passed by reference
+  ok( "media" in popObj, "instance by reference has `media` property" );
+  plus();
+  equal( Object.prototype.toString.call( popObj.media ), "[object HTMLAudioElement]", "instance by reference media property is a HTMLAudioElement" );
+  plus();
+
+  ok( "data" in popObj, "instance by reference has `data` property" );
+  plus();
+  equal( Object.prototype.toString.call( popObj.data ), "[object Object]", "instance by reference data property is an object" );
+  plus();
+
+  ok( "trackEvents" in popObj.data, "instance by reference has `trackEvents` property" );
+  plus();
+  equal( Object.prototype.toString.call( popObj.data.trackEvents ), "[object Object]", "instance by reference trackEvents property is an object" );
+  plus();
+
+  popObj.play();
+
+  methods.split( /\s+/g ).forEach(function ( k,v ) {
+
+    ok( k in popObj, "instance by reference has method: " + k );
+    plus();
+  });
+});
+
 test("Parser Support", function () {
 
   var expects = 3,