From 99fa0390028f728a62209c44d9022e2543d77f39 Mon Sep 17 00:00:00 2001 From: Rick Waldron Date: Wed, 7 Sep 2011 13:36:45 -0400 Subject: [PATCH] [#494] Adds initial set of tests for frameAnimation option --- popcorn.js | 38 ++--- test/popcorn.unit.js | 366 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 382 insertions(+), 22 deletions(-) diff --git a/popcorn.js b/popcorn.js index 664acc3..e70d8db 100644 --- a/popcorn.js +++ b/popcorn.js @@ -28,14 +28,15 @@ } }, + // 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 ); }; }()), @@ -212,13 +213,15 @@ // 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, @@ -226,13 +229,14 @@ }); 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 ); @@ -246,9 +250,7 @@ Popcorn.timeUpdate( that, event ); }, false ); - } - } else { global.setTimeout(function() { isReady( that ); @@ -1088,7 +1090,7 @@ timeUpdate: function( event ) { Popcorn.timeUpdate.call( null, this, event ); - return this; + return this; } }); diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index 605922a..38f3b56 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -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, -- 2.43.0