From: rwldrn Date: Fri, 3 Dec 2010 23:38:07 +0000 (-0500) Subject: added unit tests to check plugin scope X-Git-Tag: v0.2~76 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=09e447f54d76a2226b6c9783d054f41bd9794b0d;p=popcorn-js.git added unit tests to check plugin scope --- diff --git a/popcorn-api-draft.js b/popcorn-api-draft.js index 9876d38..1c506c0 100644 --- a/popcorn-api-draft.js +++ b/popcorn-api-draft.js @@ -41,8 +41,8 @@ this.video = elem ? elem : null; this.data = { - events: {}, - timelines: [] + events: {}, + tracks: [] }; return this; @@ -180,7 +180,7 @@ this.data.events[type] = {}; } - // setup for custom event system + // Register this.data.events[type][ fn.toString() + Popcorn.guid() ] = fn; if ( Popcorn.events.all.indexOf( type ) > -1 ) { @@ -205,6 +205,8 @@ Popcorn.p[key] = Popcorn.events.fn[key]; }); + // Plugins are registered + Popcorn.registry = []; // An interface for extending Popcorn // with plugin functionality Popcorn.plugin = function( name, definition ) { @@ -219,13 +221,16 @@ Popcorn.extend( Popcorn.p, plugin ); - - - + if ( Popcorn.registry ) { + Popcorn.registry.push({ + name: name, + plugin: plugin + }); + } // within the context of a plugin, the `timeupdate` or any of the events can be listened to - }; + global.Popcorn = Popcorn; diff --git a/test/popcorn-api-draft-unit.js b/test/popcorn-api-draft-unit.js index 6d205fb..e18d2f0 100644 --- a/test/popcorn-api-draft-unit.js +++ b/test/popcorn-api-draft-unit.js @@ -71,38 +71,57 @@ test("Popcorn 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( "timelines" in popped.data, "instance has `timelines` property" ); - ok( Object.prototype.toString.call(popped.data.timelines) === "[object Array]", "timelines property is an array" ) + ok( "tracks" in popped.data, "instance has `tracks` property" ); + ok( Object.prototype.toString.call(popped.data.tracks) === "[object Array]", "tracks property is an array" ) }); test("Popcorn Plugin", function () { - - - Popcorn.plugin("subtitles", function ( ) { - - console.log(this); + // needs expectation + + var popped = Popcorn("#video"), + methods = "load play pause currentTime mute volume"; + Popcorn.plugin("subtitles", 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, "instance has method: " + k ); + }); + + ok( "video" in this, "instance has `video` property" ); + ok( Object.prototype.toString.call(popped.video) === "[object HTMLVideoElement]", "video property is a HTMLVideoElement" ); + + ok( "data" in this, "instance has `data` property" ); + ok( Object.prototype.toString.call(popped.data) === "[object Object]", "data property is an object" ); + + ok( "tracks" in this.data, "instance has `tracks` property" ); + ok( Object.prototype.toString.call(popped.data.tracks) === "[object Array]", "tracks property is an array" ) }); - var popped = Popcorn("#video"); ok( "subtitles" in popped, "plugin is now available to instance" ); + popped.subtitles(); - + ok( Popcorn.registry.length === 1, "One item in the registry"); }); -test("Popcorn Events", function () { + + +test("Popcorn Events Simulated", function () { var p = Popcorn("#video"), @@ -110,10 +129,51 @@ test("Popcorn Events", function () { eventtest = "loadstart progress suspend emptied stalled play pause " + "loadedmetadata loadeddata waiting playing canplay canplaythrough " + "seeking seeked timeupdate ended ratechange durationchange volumechange", + events = eventtest.split(/\s+/g); + + + var expects = events.length, + count = 0; + + expect(expects); + + function plus(){ + if ( ++count == expects ) start(); + } + + stop(); + events.forEach(function ( name ) { + p.listen( name, function (event) { + + if ( completed.indexOf(name) === -1 ) { + ok(true, name + " fired"); + plus(); + + completed.push(name); + } + + + }); + }); + + events.forEach(function ( name ) { + p.trigger( name ); + }); +}); + + +test("Popcorn Events Real", function () { + + + var p = Popcorn("#video"), + completed = [], + eventtest = "loadstart progress suspend emptied stalled play pause " + + "loadedmetadata loadeddata waiting playing canplay canplaythrough " + + "seeking seeked timeupdate ended ratechange durationchange volumechange", events = eventtest.split(/\s+/g);