From: ScottDowne Date: Mon, 20 Jun 2011 17:43:58 +0000 (-0400) Subject: [#t366] effects framework renamed to compose and 40 tests added X-Git-Tag: v0.7^2~22^2~12 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=50db6680a69ffc506bf2d36486e07f694eb024fc;p=popcorn-js.git [#t366] effects framework renamed to compose and 40 tests added --- diff --git a/effects/redflash/popcorn.redflash.html b/effects/redflash/popcorn.redflash.html index 2d74184..db84806 100755 --- a/effects/redflash/popcorn.redflash.html +++ b/effects/redflash/popcorn.redflash.html @@ -45,7 +45,7 @@ start: 5, // seconds end: 15, // seconds text: 'This video made exclusively for drumbeat.org', - effect: "redflash redflash2" + compose: "redflash redflash2" }) .test({ start: 20, // seconds diff --git a/effects/redflash/popcorn.redflash.js b/effects/redflash/popcorn.redflash.js index 0175602..5e242cf 100755 --- a/effects/redflash/popcorn.redflash.js +++ b/effects/redflash/popcorn.redflash.js @@ -24,7 +24,7 @@ } ) * */ - Popcorn.effect( "redflash" , { + Popcorn.compose( "redflash" , { manifest: { about:{ @@ -54,35 +54,4 @@ console.log( "in effect teardown" ); } }); - Popcorn.effect( "redflash2" , { - - manifest: { - about:{ - name: "Popcorn Redflash Effect", - version: "0.1", - author: "@scottdowne", - website: "scottdowne.wordpress.com" - }, - options:{ - effect: true - } - }, - _setup: function( options ) { - - console.log( "in effect 2 setup" ); - }, - start: function( event, options ){ - - console.log( "in effect start" ); - }, - end: function( event, options ){ - - console.log( "in effect end" ); - }, - _teardown: function( options ) { - - console.log( "in effect teardown" ); - } - }); - })( Popcorn ); diff --git a/popcorn.js b/popcorn.js index d64682e..b203c70 100644 --- a/popcorn.js +++ b/popcorn.js @@ -655,8 +655,8 @@ // Push track event ids into the history obj.data.history.push( track._id ); - track._natives.start = track._natives.start || Popcorn.nop; - track._natives.end = track._natives.end || Popcorn.nop; + //track._natives.start = track._natives.start || Popcorn.nop; + //track._natives.end = track._natives.end || Popcorn.nop; } track.start = Popcorn.util.toSeconds( track.start, obj.options.framerate ); @@ -816,6 +816,7 @@ }, removeTrackEvent: function( id ) { + Popcorn.removeTrackEvent.call( null, this, id ); return this; }, @@ -847,7 +848,11 @@ setup, isfn = typeof definition === "function"; - var extendFunction = function( first, second ) { + // combines calls of two function calls into one + var combineFn = function( first, second ) { + + first = first || Popcorn.nop; + second = second || Popcorn.nop; return function() { @@ -862,6 +867,12 @@ manifest = definition.manifest || {}; } + // apply safe, and empty default functions + definition._setup = definition._setup || Popcorn.nop; + definition._teardown = definition._teardown || Popcorn.nop; + definition.start = definition.start || Popcorn.nop; + definition.end = definition.end || Popcorn.nop; + var pluginFn = function( setup, options ) { if ( !options ) { @@ -869,23 +880,32 @@ } // Storing the plugin natives - options._natives = {}; //setup; + var natives = options._natives = {}, + compose = ""; + + Popcorn.extend( natives, setup ); - Popcorn.extend( options._natives, setup ); - options._natives.type = name; options._running = false; // default to an empty string if no effect exists // split string into an array of effects + options.compose = options.compose && options.compose.split( " " ) || []; options.effect = options.effect && options.effect.split( " " ) || []; - for ( var i = 0; i < options.effect.length; i++ ) { + // join the two arrays together + options.compose = options.compose.concat( options.effect ); + + for ( var i = 0, l = options.compose.length; i < l; i++ ) { - options._natives._setup = extendFunction( options._natives._setup, Popcorn.effects[ options.effect[ i ] ]._setup ); - options._natives._teardown = extendFunction( options._natives._teardown, Popcorn.effects[ options.effect[ i ] ]._teardown ); - options._natives.start = extendFunction( options._natives.start, Popcorn.effects[ options.effect[ i ] ].start ); - options._natives.end = extendFunction( options._natives.end, Popcorn.effects[ options.effect[ i ] ].end ); + // if the requested compose is garbage, throw it away + compose = Popcorn.compositions[ options.compose[ i ] ] || {}; + + // extends previous functions with compose function + natives._setup = combineFn( natives._setup, compose._setup ); + natives._teardown = combineFn( natives._teardown, compose._teardown ); + natives.start = combineFn( natives.start, compose.start ); + natives.end = combineFn( natives.end, compose.end ); } // Ensure a manifest object, an empty object is a sufficient fallback @@ -1024,19 +1044,21 @@ } }; - Popcorn.effects = {}; + Popcorn.compositions = {}; // Plugin inheritance - Popcorn.effect = function( name, definition, manifest ) { + Popcorn.compose = function( name, definition, manifest ) { // If `manifest` arg is undefined, check for manifest within the `definition` object // If no `definition.manifest`, an empty object is a sufficient fallback Popcorn.manifest[ name ] = manifest = manifest || definition.manifest || {}; // register the effect by name - Popcorn.effects[ name ] = definition; + Popcorn.compositions[ name ] = definition; }; + Popcorn.plugin.effect = Popcorn.effect = Popcorn.compose; + // stores parsers keyed on filetype Popcorn.parsers = {}; diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index 7c4a603..bb2f8f3 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -1156,23 +1156,13 @@ test("Plugin Factory", function () { var popped = Popcorn("#video"), methods = "load play pause currentTime mute volume roundTime exec removePlugin", - expects = 96, // 15*2+16*2+6+12+16. executor/complicator each do 15 + expects = 34, // 15*2+2+2. executor/complicator each do 15 count = 0; function plus() { if ( ++count == expects ) { Popcorn.removePlugin("executor"); Popcorn.removePlugin("complicator"); - Popcorn.removePlugin("executor_inherits"); - Popcorn.removePlugin("complicator_inherits"); - Popcorn.removePlugin("A"); - Popcorn.removePlugin("B"); - Popcorn.removePlugin("C"); - Popcorn.removePlugin("D"); - Popcorn.removePlugin("E"); - Popcorn.removePlugin("F"); - Popcorn.removePlugin("optionTest1"); - Popcorn.removePlugin("optionTest2"); start(); } } @@ -1217,27 +1207,15 @@ test("Plugin Factory", function () { }); - Popcorn.inherit("executor_inherits", "executor", function (options) { - return { - start: function() { ok(true, "child class method executed"); plus(); } - }; - }); - ok( "executor" in popped, "executor plugin is now available to instance" ); plus(); - ok( "executor_inherits" in popped, "executor-inerits plugin is now available to instance" ); - plus(); - equals( Popcorn.registry.length, 2, "One item in the registry"); + equals( Popcorn.registry.length, 1, "One item in the registry"); plus(); popped.executor({ start: 1, end: 2 }); - popped.executor_inherits({ - start: 2, - end: 3 - }); Popcorn.plugin("complicator", { @@ -1277,133 +1255,260 @@ test("Plugin Factory", function () { } }); - Popcorn.inherit("complicator_inherits", "executor", { - start: function() { ok(true, "child class method executed"); plus(); } - }); - ok( "complicator" in popped, "complicator plugin is now available to instance" ); plus(); - ok( "complicator_inherits" in popped, "complicator plugin is now available to instance" ); - plus(); - equals( Popcorn.registry.length, 4, "Two items in the registry"); + equals( Popcorn.registry.length, 2, "Two items in the registry"); plus(); popped.complicator({ start: 4, end: 5 - }); - popped.complicator_inherits({ - start: 5, - end: 6 - }); - - var counts = { a: 0, b: 0, c: 0, d: 0, e: 0, f: 0 }; - Popcorn.plugin("A", function(options) { - return { start: function() { counts.a++; } }; }); - Popcorn.plugin("B", function(options) { - return { start: function() { counts.b++; } }; + + popped.currentTime(0).play(); + +}); + +test( "Plugin extend", function () { + + QUnit.reset(); + + var popped = Popcorn("#video"), + expects = 40, + count = 0, + effectTrackOne, + effectTrackTwo, + effectTrackThree, + composeOptionsOne, + composeOptionsTwo, + test = { + one: { + setup: 0, + running: 0 + }, + two: { + setup: 0, + running: 0 + } + }; + + function plus() { + if ( ++count == expects ) { + Popcorn.removePlugin("testPlugin"); + Popcorn.removePlugin("pluginOptions1"); + Popcorn.removePlugin("pluginOptions2"); + start(); + } + } + + expect( expects ); + stop( 15000 ); + + Popcorn.plugin( "testPlugin", {}); + + Popcorn.compose( "testCompose1", { + start: function() { + test.one.running++; + }, + end: function() { + test.one.running--; + }, + _setup: function() { + test.one.setup++; + }, + _teardown: function() { + test.one.setup--; + } }); - Popcorn.inherit("C", "B", function(options) { - return { start: function() { counts.c++; } }; + + Popcorn.effect( "testEffect2", { + start: function() { + test.two.running++; + }, + end: function() { + test.two.running--; + }, + _setup: function() { + test.two.setup++; + }, + _teardown: function() { + test.two.setup--; + } }); - Popcorn.inherit("D", ["A", "B"], function(options) { - return { start: function() { counts.d++; } }; + + popped.testPlugin({ + start: 0, + end: 1, + compose: "testCompose1", + effect: "testEffect2" }); - Popcorn.inherit("E", ["B"], function(options) { - return { start: function() { counts.e++; } }; + + effectTrackOne = popped.getLastTrackEventId(); + + popped.testPlugin({ + start: 1, + end: 2 + }) + .testPlugin({ + start: 2, + end: 4, + compose: "testCompose1" + }) + .testPlugin({ + start: 3, + end: 4, + compose: "testCompose1 testEffect2" }); - Popcorn.inherit("F", ["E", "C"], function(options) { - return { start: function() { counts.f++; } }; + + effectTrackTwo = popped.getLastTrackEventId(); + + popped.testPlugin({ + start: 5, + end: 6, + effect: "testCompose1" + }) + .testPlugin({ + start: 6, + end: 7, + effect: "testCompose1 testEffect2" }); - equals(counts.a, 0, "plugin A should not have been run yet"); - plus(); - equals(counts.b, 0, "plugin B should not have been run yet"); - plus(); - equals(counts.c, 0, "plugin C should not have been run yet"); + effectTrackThree = popped.getLastTrackEventId(); + + equals( test.one.running, 0, "no compose one running" ); plus(); - equals(counts.d, 0, "plugin D should not have been run yet"); + equals( test.one.setup, 5, "five compose one setup" ); plus(); - equals(counts.e, 0, "plugin E should not have been run yet"); + equals( test.two.running, 0, "no compose two running" ); plus(); - equals(counts.f, 0, "plugin F should not have been run yet"); + equals( test.two.setup, 3, "three compose two setup" ); plus(); - popped.C({ start: 6, end: 7 }); - popped.D({ start: 7, end: 8 }); - popped.F({ start: 8, end: 9 }); - - setTimeout(function() { - equals(counts.a, 1, "plugin A should have been run once"); + popped.exec( 0, function() { + equals( test.one.running, 1, "one compose running" ); + plus(); + equals( test.two.running, 1, "one effect running" ); + plus(); + }) + .exec( 1, function() { + equals( test.one.running, 0, "no compose running" ); + plus(); + equals( test.two.running, 0, "no effect running" ); + plus(); + }) + .exec( 2, function() { + equals( test.one.running, 1, "one compose running" ); + plus(); + equals( test.two.running, 0, "no effect running" ); + plus(); + }) + .exec( 3, function() { + equals( test.one.running, 2, "two compose one running" ); + plus(); + equals( test.two.running, 1, "one compose two running" ); plus(); - equals(counts.b, 3, "plugin B should have been run thrice"); + }) + .exec( 4, function() { + equals( test.one.running, 0, "no compose one running" ); + plus(); + equals( test.two.running, 0, "no compose two running" ); + plus(); + }) + .exec( 5, function() { + equals( test.one.running, 1, "one effect running" ); + plus(); + equals( test.two.running, 0, "no compose running" ); plus(); - equals(counts.c, 2, "plugin C should have been run twice"); + }) + .exec( 6, function() { + equals( test.one.running, 1, "one effect one running" ); plus(); - equals(counts.d, 1, "plugin D should have been run once"); - plus(); - equals(counts.e, 1, "plugin E should have been run once"); - plus(); - equals(counts.f, 1, "plugin F should have been run once"); - plus(); - }, 10000); - - Popcorn.plugin( "optionTest1", { + equals( test.two.running, 1, "one effect two running" ); + plus(); + }) + .exec( 7, function() { + popped.removeTrackEvent( effectTrackOne ); + popped.removeTrackEvent( effectTrackTwo ); + popped.removeTrackEvent( effectTrackThree ); + popped.removeTrackEvent( composeOptionsOne ); + popped.removeTrackEvent( composeOptionsTwo ); + equals( test.one.setup, 2, "three compose one teardowns called. 5 - 3 = 2" ); + plus(); + equals( test.two.setup, 0, "three compose two teardowns called. 3 - 3 = 0" ); + plus(); + }); + + // runs once, 2 tests + Popcorn.plugin( "pluginOptions1", { _setup: function( options ) { - options.item = "exist"; - equals( options.data[ 0 ], "parent", "parent plugin _setup options.data" ); + ok( options.pluginoption, "plugin option one exists at setup" ); plus(); - }, - start: function( event, options ) { - equals( options.item, "exist", "parent plugin start options.item" ); + ok( !options.composeoption, "compose option one does not exist at setup" ); plus(); - equals( options.data[ 0 ], "parent", "parent plugin start options.data" ); - plus(); - }, - end: function( event, options ) { - equals( options.item, "exist", "parent plugin end options.item" ); + // check to test plugin to effect call order + options.composeoption = true; + } + }); + + // runs once, 2 tests + Popcorn.plugin( "pluginOptions2", { + _setup: function( options ) { + ok( !options.pluginoption, "plugin option two does not exist at setup" ); plus(); - equals( options.data[ 0 ], "parent", "parent plugin end options.data" ); + ok( options.composeoption, "compose option two exists at setup" ); plus(); + // check to test plugin to effect call order + options.pluginoption = true; } }); - Popcorn.inherit( "optionTest2", "optionTest1", { + // runs twice, 8 tests * 2 runs = 16 tests + Popcorn.plugin.effect( "composeOptions", { _setup: function( options ) { - equals( options.item, "exist", "child plugin _setup options.item" ); + ok( options.pluginoption, "plugin option exists at setup" ); plus(); - equals( options.data[ 1 ], "child", "child plugin _setup options.data" ); + ok( options.composeoption, "compose option exists at setup" ); + plus(); + }, + _teardown: function( options ) { + ok( options.pluginoption, "plugin option exists at teardown" ); + plus(); + ok( options.composeoption, "compose option exists at teardown" ); plus(); }, start: function( event, options ) { - equals( options.item, "exist", "child plugin start options.item" ); + ok( options.pluginoption, "plugin option exists at start" ); plus(); - equals( options.data[ 1 ], "child", "child plugin start options.data" ); + ok( options.composeoption, "compose option exists at start" ); plus(); }, end: function( event, options ) { - equals( options.item, "exist", "child plugin end options.item" ); + ok( options.pluginoption, "plugin option exists at end" ); plus(); - equals( options.data[ 1 ], "child", "child plugin end options.data" ); + ok( options.composeoption, "compose option exists at end" ); plus(); } }); - popped.optionTest1({ - start: 6, - end: 7, - data: [ "parent" ] - }) - .optionTest2({ - start: 7, - end: 8, - data: [ "parent", "child" ] - }) - + popped.pluginOptions1({ + start: 0, + end: 1, + compose: "composeOptions", + pluginoption: true + }); - popped.currentTime(0).play(); + composeOptionsOne = popped.getLastTrackEventId(); + popped.pluginOptions2({ + start: 0, + end: 1, + compose: "composeOptions", + composeoption: true + }); + + composeOptionsTwo = popped.getLastTrackEventId(); + + popped.currentTime( 0 ).play(); }); test("Plugin Breaker", function () { @@ -1476,7 +1581,7 @@ test("Plugin Empty", function () { QUnit.reset(); var popped = Popcorn("#video"), - expects = 2, + expects = 4, testObj = {}, count = 0; @@ -1498,6 +1603,10 @@ test("Plugin Empty", function () { plus(); ok( testObj.end, "default end function is generated" ); plus(); + ok( testObj._setup, "default _setup function is generated" ); + plus(); + ok( testObj._teardown, "default _teardown function is generated" ); + plus(); popped.currentTime(0).play();