From: stevenaw Date: Wed, 16 Mar 2011 00:25:36 +0000 (-0400) Subject: [#140] Unit tests fixed X-Git-Tag: v0.4~7^2~3 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=3e353300155fd348a49468f97eab2c22b1d27975;p=popcorn-js.git [#140] Unit tests fixed --- diff --git a/players/youtube/popcorn.youtube.html b/players/youtube/popcorn.youtube.html index c7af417..3b3b5db 100644 --- a/players/youtube/popcorn.youtube.html +++ b/players/youtube/popcorn.youtube.html @@ -81,10 +81,10 @@ Due to the Flash security model, this demo must be run from a web server
From 5 to 40 seconds, the footnote 'The video is "RiP: A Remix Manifesto", by Brett Gaylor' will appear below 'Footnote Area'.
- Vimeo does not support their player to be chromeless (hidden controls), but custsom controls have been developed and tied into their player.
+ Youtube does not support their player to be chromeless (hidden controls), but custsom controls have been developed and tied into their player.
Clicking play/pause or seeking in either the video or via custsom controls will cause the other to update.
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.
- 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.
+ 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.



diff --git a/players/youtube/popcorn.youtube.js b/players/youtube/popcorn.youtube.js index 473a43c..192c46f 100644 --- a/players/youtube/popcorn.youtube.js +++ b/players/youtube/popcorn.youtube.js @@ -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; diff --git a/players/youtube/popcorn.youtube.unit.html b/players/youtube/popcorn.youtube.unit.html index 93d5b1d..b6a1a4e 100644 --- a/players/youtube/popcorn.youtube.unit.html +++ b/players/youtube/popcorn.youtube.unit.html @@ -28,14 +28,7 @@ Therefore, if failures occur for the first time, just run it again. Likely, failures would disappear the second time.

-
- - - - -
+
diff --git a/players/youtube/popcorn.youtube.unit.js b/players/youtube/popcorn.youtube.unit.js index 7a8eedf..bbfecec 100644 --- a/players/youtube/popcorn.youtube.unit.js +++ b/players/youtube/popcorn.youtube.unit.js @@ -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" );