From: rwldrn Date: Thu, 24 Mar 2011 14:09:20 +0000 (-0400) Subject: [#450] Audio support; 33 Unit tests X-Git-Tag: v0.5~38^2~2^2~4 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=a196544a2377820cb8ae506eee2628987b408770;p=popcorn-js.git [#450] Audio support; 33 Unit tests --- diff --git a/popcorn.js b/popcorn.js index 86aaa70..92fcc53 100644 --- a/popcorn.js +++ b/popcorn.js @@ -55,9 +55,9 @@ Popcorn.addInstance = function( instance ) { var instanceLen = Popcorn.instances.length, - instanceId = instance.video.id && instance.video.id; + instanceId = instance.media.id && instance.media.id; - // If the video element has its own `id` use it, otherwise provide one + // If the media element has its own `id` use it, otherwise provide one // Ensure that instances have unique ids and unique entries // Uses `in` operator to avoid false positives on 0 instance.id = !( instanceId in Popcorn.instanceIds ) && instanceId || @@ -139,11 +139,15 @@ // Check if entity is a valid string id matches = rIdExp.exec( entity ); - // Get video element by id or object reference - this.video = matches && matches.length && matches[ 2 ] ? + // Get media element by id or object reference + this.media = matches && matches.length && matches[ 2 ] ? document.getElementById( matches[ 2 ] ) : entity; + // Create an audio or video element property reference + this[ this.media.tagName.toLowerCase() ] = this.media; + + // Register new instance Popcorn.addInstance( this ); @@ -168,11 +172,11 @@ // Wrap true ready check var isReady = function( that ) { - if ( that.video.readyState >= 2 ) { + 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.video.duration; + var duration = that.media.duration; // Check for no duration info (NaN) var videoDurationPlus = duration != duration ? Number.MAX_VALUE : duration + 1; @@ -181,7 +185,7 @@ end: videoDurationPlus }); - that.video.addEventListener( "timeupdate", function( event ) { + that.media.addEventListener( "timeupdate", function( event ) { var currentTime = this.currentTime, previousTime = that.data.trackEvents.previousUpdateTime, @@ -347,8 +351,8 @@ ret[ name ] = function( arg ) { - if ( typeof this.video[name] === "function" ) { - this.video[ name ](); + if ( typeof this.media[name] === "function" ) { + this.media[ name ](); return this; } @@ -356,12 +360,12 @@ if ( arg !== false && arg !== null && typeof arg !== "undefined" ) { - this.video[ name ] = arg; + this.media[ name ] = arg; return this; } - return this.video[ name ]; + return this.media[ name ]; }; }); @@ -374,7 +378,7 @@ // Rounded currentTime roundTime: function () { - return -~this.video.currentTime; + return -~this.media.currentTime; }, // Attach an event to a single point in time @@ -456,7 +460,7 @@ var evt = document.createEvent( eventInterface ); evt.initEvent(type, true, true, global, 1); - this.video.dispatchEvent(evt); + this.media.dispatchEvent(evt); return this; } @@ -487,7 +491,7 @@ // only attach one event of any type if ( !hasEvents && Popcorn.events.all.indexOf( type ) > -1 ) { - this.video.addEventListener( type, function( event ) { + this.media.addEventListener( type, function( event ) { Popcorn.forEach( self.data.events[type], function ( obj, key ) { if ( typeof obj === "function" ) { @@ -1097,31 +1101,34 @@ document.addEventListener( "DOMContentLoaded", function () { - var videos = document.getElementsByTagName( "video" ); + var medias = document.querySelectorAll( "video,audio" ); - Popcorn.forEach( videos, function ( iter, key ) { + + Popcorn.forEach( medias, function( idx, key ) { - var video = videos[ key ], + var media = medias[ key ], hasDataSources = false, - dataSources, data, popcornVideo; + dataSources, data, popcornMedia; // Ensure that the DOM has an id - if ( !video.id ) { + if ( !media.id ) { - video.id = Popcorn.guid( "__popcorn" ); + media.id = Popcorn.guid( "__popcorn" ); } // Ensure we're looking at a dom node - if ( video.nodeType && video.nodeType === 1 ) { + if ( media.nodeType && media.nodeType === 1 ) { - popcornVideo = Popcorn( "#" + video.id ); + popcornMedia = Popcorn( "#" + media.id ); - dataSources = ( video.getAttribute( "data-timeline-sources" ) || "" ).split(","); + dataSources = ( media.getAttribute( "data-timeline-sources" ) || "" ).split(","); + + if ( dataSources[ 0 ] ) { - Popcorn.forEach( dataSources, function ( source ) { + Popcorn.forEach( dataSources, function( source ) { // split the parser and data as parser:file data = source.split( ":" ); @@ -1135,20 +1142,20 @@ } - // If the video has data sources and the correct parser is registered, continue to load - if ( dataSources[ 0 ] && popcornVideo[ data[ 0 ] ] ) { + // If the media has data sources and the correct parser is registered, continue to load + if ( dataSources[ 0 ] && popcornMedia[ data[ 0 ] ] ) { - // Set up the video and load in the datasources - popcornVideo[ data[ 0 ] ]( data[ 1 ] ); + // Set up the media and load in the datasources + popcornMedia[ data[ 0 ] ]( data[ 1 ] ); } }); } - // Only play the video if it was specified to do so - if ( !!popcornVideo.autoplay ) { - popcornVideo.play(); + // Only play the media if it was specified to do so + if ( !!popcornMedia.autoplay ) { + popcornMedia.play(); } } diff --git a/test/data/parserAudio.json b/test/data/parserAudio.json new file mode 100644 index 0000000..984eabe --- /dev/null +++ b/test/data/parserAudio.json @@ -0,0 +1,10 @@ +{ + "data": [ + { + "testAudioParser": { + "start": 5, + "end": 6 + } + } + ] +} diff --git a/test/index.html b/test/index.html index 52a9fca..c075015 100644 --- a/test/index.html +++ b/test/index.html @@ -30,7 +30,8 @@ - + + + diff --git a/test/italia.ogg b/test/italia.ogg new file mode 100644 index 0000000..aa6102e Binary files /dev/null and b/test/italia.ogg differ diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index 32294c6..cd32b53 100755 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -111,7 +111,7 @@ test("Instances", function() { ok( instance instanceof Popcorn, "Instance instanceof Popcorn" ); plus(); - equal( Popcorn.instances.length, 1, "There are the correct number of Popcorn instances" ); + equal( Popcorn.instances.length, 2, "There are the correct number of Popcorn instances" ); plus(); // Create another instance @@ -121,7 +121,7 @@ test("Instances", function() { var remove = Popcorn.instances[1]; // Remove and check the length of the currently cached instances - equal( Popcorn.removeInstanceById( remove.id ).length, 1, "Removing an instance by id: 1 instance remains" ); + equal( Popcorn.removeInstanceById( remove.id ).length, 2, "Removing an instance by id: 1 instance remains" ); plus(); }); @@ -1816,6 +1816,122 @@ test("Parsing Handler - References unavailable plugin", function () { }); +module("Audio"); +test( "Basic Audio Support", function () { + + var popped = Popcorn( "#audio" ), + popObj = Popcorn( document.getElementById( "audio" ) ), + 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, + count = 0, + timeOut = 0, + interval, + audiocorn = Popcorn( "#audio" ); + + function plus() { + if ( ++count === expects ) { + start(); + + Popcorn.removePlugin( "testAudioParser" ); + } + } + + expect(expects); + stop(5000); + + + Popcorn.plugin("testAudioParser", { + + start: function() { + ok(true, "testAudioParser started: " + Math.round(this.currentTime()) ); + plus(); + }, + end: function() { + ok(true, "testAudioParser ended: " + Math.round(this.currentTime()) ); + plus(); + } + }); + + Popcorn.parser( "parseAudio", function( data ){ + ok( typeof data.json === "object", "data.json exists"); + plus(); + return data.json; + }); + + audiocorn.parseAudio("data/parserAudio.json", function() { + + audiocorn.currentTime(4).play(); + + }); +}); + + module("Popcorn Test Runner End"); test("Last Check", function () {