]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#450] Audio support; 33 Unit tests
authorrwldrn <waldron.rick@gmail.com>
Thu, 24 Mar 2011 14:09:20 +0000 (10:09 -0400)
committerrwldrn <waldron.rick@gmail.com>
Thu, 24 Mar 2011 14:09:20 +0000 (10:09 -0400)
popcorn.js
test/data/parserAudio.json [new file with mode: 0644]
test/index.html
test/italia.ogg [new file with mode: 0644]
test/popcorn.unit.js

index 86aaa70c76920b4057f9e148d6c8ff6fb7144602..92fcc5315674cfec75a95d63c1cec6d0490e2b5b 100644 (file)
@@ -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 ||
       //  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 );
 
       //  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;
 
             end: videoDurationPlus
           });
 
-          that.video.addEventListener( "timeupdate", function( event ) {
+          that.media.addEventListener( "timeupdate", function( event ) {
 
             var currentTime    = this.currentTime,
                 previousTime   = that.data.trackEvents.previousUpdateTime,
 
         ret[ name ] = function( arg ) {
 
-          if ( typeof this.video[name] === "function" ) {
-            this.video[ name ]();
+          if ( typeof this.media[name] === "function" ) {
+            this.media[ name ]();
 
             return this;
           }
 
           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 ];
         };
       });
 
 
     //  Rounded currentTime
     roundTime: function () {
-      return -~this.video.currentTime;
+      return -~this.media.currentTime;
     },
 
     //  Attach an event to a single point in time
             var evt = document.createEvent( eventInterface );
                 evt.initEvent(type, true, true, global, 1);
 
-            this.video.dispatchEvent(evt);
+            this.media.dispatchEvent(evt);
 
             return this;
           }
         // 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" ) {
 
   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( ":" );
 
             }
 
-            //  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 (file)
index 0000000..984eabe
--- /dev/null
@@ -0,0 +1,10 @@
+{\r
+  "data": [\r
+    {\r
+      "testAudioParser": {\r
+        "start": 5,\r
+        "end": 6\r
+      }\r
+    }\r
+  ]\r
+}\r
index 52a9fca4d185caef360f77e02636f4528aa7e1ce..c075015ba2a4292f65d750364e83ca80044826e1 100644 (file)
@@ -30,7 +30,8 @@
   
   </div>
   
-  <video id='video'style="display:" 
+  <video id='video'
+       style="display:;width:300px" 
     controls 
     poster="poster.png"> 
 
@@ -44,6 +45,9 @@
 
     <p>Your user agent does not support the HTML5 Video element.</p> 
 
-  </video>          
+  </video>
+
+       <audio id='audio' src="italia.ogg" data-timeline-sources="data/parserData.json" controls> 
+       </audio>
 </body>
 </html>
diff --git a/test/italia.ogg b/test/italia.ogg
new file mode 100644 (file)
index 0000000..aa6102e
Binary files /dev/null and b/test/italia.ogg differ
index 32294c6fadc44643235b345328e61c83c63104ba..cd32b5355fcebfdbe63ca31a0f9d52b837e78a21 100755 (executable)
@@ -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 () {