]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#140] Unit tests fixed
authorstevenaw <sweerdenburg@gmail.com>
Wed, 16 Mar 2011 00:25:36 +0000 (20:25 -0400)
committerstevenaw <sweerdenburg@gmail.com>
Wed, 16 Mar 2011 00:25:36 +0000 (20:25 -0400)
players/youtube/popcorn.youtube.html
players/youtube/popcorn.youtube.js
players/youtube/popcorn.youtube.unit.html
players/youtube/popcorn.youtube.unit.js

index c7af4175e4e27bca4d99df3df34b21367fcb559f..3b3b5db227d089f7d09828c2f44eb749dad56e06 100644 (file)
           Due to the Flash security model, this demo must be  run from a web server<br />
           From 5 to 40 seconds, the footnote 'The video is "RiP: A Remix Manifesto", by Brett Gaylor' will appear below 'Footnote Area'.
         <hr >
-          Vimeo does not support their player to be chromeless (hidden controls), but custsom controls have been developed and tied into their player.<br />
+          Youtube does not support their player to be chromeless (hidden controls), but custsom controls have been developed and tied into their player.<br />
           Clicking play/pause or seeking in either the video or via custsom controls will cause the other to update.<br />
           Clicking on the embed option in the Vimeo player while the video is playing will cause the video to pause and custom controls to update.<br />
-          The vimeo video can be specified in the HTML source by giving the vimeo web site url (http://vimeo.com/VIDEOID) to either the VimeoEngine or as the iframe src attribute.<br/>
+          The video can be specified in the HTML source by giving the vimeo web site url (http://www.youtube.com/watch?v=VIDEOID) to either Popcorn.youtube or as the div src attribute.<br/>
         </p>
         <br />
         <br />
index 473a43cd1e192269833f303de5d85c0df816b5e0..192c46f07ab7437e689e89f5fc49c5b6575fadb5 100644 (file)
@@ -59,8 +59,12 @@ var onYouTubePlayerReady;
     
     var matches = url.match( /((http:\/\/)?www\.)?youtube\.[a-z]+\/watch\?v\=[a-z0-9]+/i );
     
+    if ( !matches ) {
+      return;
+    }
+    
     // Return id, which comes after first equals sign
-    return !matches || matches[0].split( "=" )[1];
+    return matches[0].split( "=" )[1];
   };
   
   // Extract the id from a player url
@@ -71,8 +75,12 @@ var onYouTubePlayerReady;
     
     var matches = url.match( /^http:\/\/?www\.youtube\.[a-z]+\/e\/[a-z0-9]+/i );
     
+    if ( !matches ) {
+      return;
+    }
+    
     // Return id, which comes after first equals sign
-    return !matches || matches[0].split( "/e/" )[1];
+    return matches[0].split( "/e/" )[1];
   };
   
   function cueDelayedDurationCheck ( evt ) {
@@ -160,20 +168,10 @@ var onYouTubePlayerReady;
     
     var self = this,
         container = document.getElementById( elementId ),
+        src,
         durEvtType;
-        
-    if ( !container ) {
-      throw "Container ID could not be found";
-    }
     
-    // The video id for youtube (web or player formats)
-    this.vidId = extractIdFromUrl( url ) || extractIdFromUri( url );
-    
-    if ( !this.vidId ) {
-      throw "Must supply a video url!";
-    }
-    
-    this.playerId = Popcorn.guid( elementId );
+    this.playerId = elementId;
     this.readyState = READY_STATE_HAVE_NOTHING;
     this.eventListeners = {};
     this.loadStarted = false;
@@ -189,19 +187,62 @@ var onYouTubePlayerReady;
     this.previousVolume = 0;    
     this.duration = Number.MAX_VALUE;
     
-    this.addEventListener( "load", function() {
-      self.video.cueVideoByUrl( getPlayerAddress( self.vidId ) );
-      cueDelayedDurationCheck.call( self, "playing" );
-    });
+    // The video id for youtube (web or player formats)
+    this.vidId = extractIdFromUrl( url ) || extractIdFromUri( url );
+    
+    var loadByctor = !!this.vidId;
     
-    this.video = createSWFObject({
-      playerId: this.playerId,
-      videoId: this.vidId,
-      width: container.getAttribute("width") || 460,
-      height: container.getAttribute("height") || 350
-    });
+    // Container not yet loaded, load it on DOMDontentLoad
+    if ( container ) {
+      this.vidId = this.vidId || extractIdFromUrl( container.getAttribute( "src" ) ) || extractIdFromUri( container.getAttribute( "src" ) );
+      
+      if ( !this.vidId ) {
+        throw "Could not find video id";
+      }
+      
+      if ( container.children.length ) {
+        this.video = container.firstChild;
+      } else {
+        this.video = createSWFObject({
+          playerId: this.playerId,
+          videoId: this.vidId,
+          width: container.getAttribute("width") || 460,
+          height: container.getAttribute("height") || 350
+        });
+        
+        container.appendChild( this.video );
+      }
+    } else {
+      document.addEventListener( "DOMContentLoaded", function() {
+        container = document.getElementById( elementId );
+        
+        if ( !container ) {
+          throw "Could not find container!";
+        }
+        
+        self.vidId = self.vidId || extractIdFromUrl( container.getAttribute( "src" ) ) || extractIdFromUri( container.getAttribute( "src" ) );
+        
+        if ( !self.vidId ) {
+          throw "Could not find video id";
+        }
+        
+        self.video = createSWFObject({
+          playerId: self.playerId,
+          videoId: self.vidId,
+          width: container.getAttribute("width") || 460,
+          height: container.getAttribute("height") || 350
+        });
+        
+        container.appendChild( self.video );
+      }, false);
+    }
     
-    container.appendChild( this.video );
+    if ( loadByctor ) {
+      this.addEventListener( "load", function() {
+        self.video.cueVideoByUrl( getPlayerAddress( self.vidId ) );
+        cueDelayedDurationCheck.call( self, "playing" );
+      });
+    }
     
     registry[this.playerId] = this;
     
index 93d5b1d05e251139797dd3bd9f62736963e5c14e..b6a1a4e739490b79afb3a592dd23a3f5cdb1a200 100644 (file)
     Therefore, if failures occur for the first time, just run it again. Likely,
     failures would disappear the second time.
   </p>
-  <div>
-    <object id="video" type="application/x-shockwave-flash"
-      data="http://www.youtube.com/e/ac7KhViaVqc?enablejsapi=1&playerapiid=video&version=3"
-      width="250" height="200">
-      <param name="allowFullScreen" value="true" />
-      <param name="allowscriptaccess" value="always" />
-    </object>
-  </div>
+  <div id="video" width="360" height="300" src="http://www.youtube.com/e/ac7KhViaVqc" ></div>
 </body>
 </html>
 
index 7a8eedfa8c2b87b0d92bd506c5fa912fbe1126cc..bbfececac43ae12387da160fba060b282e3de7de 100644 (file)
@@ -45,10 +45,34 @@ test( "Popcorn YouTube Plugin Event Tests", function() {
   
   QUnit.reset();
   
+  // events must be fired in this order
+  var expectedEvents = [
+    'play',
+    'loadeddata',
+    'volumechange',
+    'playing',
+    'volumechange',
+    'pause',
+    'play',
+    'playing',
+    'seeked',
+    'volumechange',
+    'volumechange',
+    'playing',
+    'pause',
+    'ended'
+  ];
+  
   var count = 0,
-      expects = 19;
+      eventCount = 0,
+      added = [],
+      set1Executed = false,
+      set2Executed = false,
+      set3Executed = false,
+      expects = expectedEvents.length + 5;
       
   stop( 15000 );
+  expect(expects);
   
   popcorn.volume(1); // is muted later
   
@@ -65,30 +89,8 @@ test( "Popcorn YouTube Plugin Event Tests", function() {
     ok( false, "This should not be run." );
   });
 
-  // events must be fired in this order
-  var expectedEvents = [
-    'play',
-    'loadeddata',
-    'volumechange',
-    'playing',
-    'volumechange',
-    'pause',
-    'play',
-    'playing',
-    'seeked',
-    'volumechange',
-    'volumechange',
-    'playing',
-    'pause',
-    'ended'
-  ];
-
-  var expectedEventCount = expectedEvents.length;
-  expect(expectedEventCount + 5);
-
+  
   // register each events
-  var eventCount = 0;
-  var added = [];
   for ( var i in expectedEvents ) {
     (function( event ) {
       // skip same listeners already added
@@ -113,7 +115,6 @@ test( "Popcorn YouTube Plugin Event Tests", function() {
   }
 
   // operations set1
-  var set1Executed = false;
   popcorn.listen( 'playing', function() {
     // prevent double calling
     if ( set1Executed ) {
@@ -137,7 +138,6 @@ test( "Popcorn YouTube Plugin Event Tests", function() {
   popcorn.play();
   
   // operations set2
-  var set2Executed = false;
   popcorn.listen( 'pause', function() {
     if ( set2Executed ) {
       return;
@@ -157,7 +157,6 @@ test( "Popcorn YouTube Plugin Event Tests", function() {
   });
   
   // operations set3
-  var set3Executed = false;
   popcorn.listen( 'seeked', function() {
     if ( set3Executed ) {
       return;
@@ -202,7 +201,7 @@ test( "Popcorn YouTube Plugin Url and Duration Tests", function() {
   equals( rawTube.duration, Number.MAX_VALUE, 'Duration starts as Max Value');
   plus();
   
-  rawTube.addEventListener( "playing", function() {
+  rawTube.addEventListener( "durationchange", function() {
     notEqual( rawTube.duration, Number.MAX_VALUE, "Duration has been changed from max value" );
     plus();
     notEqual( rawTube.duration, 0, "Duration is non-zero" );