From: stevenaw Date: Thu, 17 Mar 2011 06:31:15 +0000 (-0400) Subject: [#140] Unit test bug fixed, tests now mutually independant X-Git-Tag: v0.4~7^2 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=df7de37de11e33590374dcaee13c83e4b25c97ff;p=popcorn-js.git [#140] Unit test bug fixed, tests now mutually independant --- diff --git a/players/youtube/popcorn.youtube.js b/players/youtube/popcorn.youtube.js index 07cd4c7..1790ef7 100644 --- a/players/youtube/popcorn.youtube.js +++ b/players/youtube/popcorn.youtube.js @@ -857,7 +857,7 @@ var onYouTubePlayerReady; return; } - return "http://www.youtube.com/e/" + id+ "?enablejsapi=1&playerapiid=" + playerId + "&verion=3"; + return "http://www.youtube.com/e/" + id; } function makeSWF( url, container ) { @@ -867,8 +867,11 @@ var onYouTubePlayerReady; // The video id for youtube (web or player formats) // First check manually given url, if that doesn't work resort to "src" - this.vidId = extractIdFromUrl( url ) || extractIdFromUri( url ) - || extractIdFromUrl( container.getAttribute( "src" ) ) || extractIdFromUri( container.getAttribute( "src" ) ); + this.vidId = this.vidId || extractIdFromUrl( container.getAttribute( "src" ) ) || extractIdFromUri( container.getAttribute( "src" ) ); + + if ( !this.vidId ) { + throw "Could not find video id"; + } // Determine width/height/etc based on container this.width = container.getAttribute("width") || 460; @@ -884,10 +887,6 @@ var onYouTubePlayerReady; this.offsetLeft = container.offsetLeft; this.offsetTop = container.offsetTop; - if ( !this.vidId ) { - throw "Could not find video id"; - } - flashvars = { playerapiid: this.playerId }; @@ -903,7 +902,7 @@ var onYouTubePlayerReady; }; swfobject.embedSWF( "http://www.youtube.com/e/" + this.vidId +"?enablejsapi=1&playerapiid=" + this.playerId + "&verion=3", - this.playerId, this.width, this.height, "9.0.0", "expressInstall.swf", flashvars, params, attributes ); + this.playerId, this.width, this.height, "8", null, flashvars, params, attributes ); } // Called when a player is loaded @@ -915,20 +914,8 @@ var onYouTubePlayerReady; // Video hadn't loaded yet when ctor was called vid.video = document.getElementById( playerId ); - - // For calculating position relative to video (like subtitles) - vid.offsetWidth = vid.video.offsetWidth; - vid.offsetHeight = vid.video.offsetHeight; - vid.offsetParent = vid.video.offsetParent; - vid.offsetLeft = vid.video.offsetLeft; - vid.offsetTop = vid.video.offsetTop; - vid.duration = vid.video.getDuration(); - // Set up stuff that requires the API to be loaded - vid.registerYoutubeEventHandlers(); - vid.registerInternalEventHandlers(); - // Issue load event vid.dispatchEvent( 'load' ); vid.dispatchEvent( "durationchange" ); @@ -959,9 +946,52 @@ var onYouTubePlayerReady; this.currentTime = this.previousCurrentTime = 0; this.volume = this.previousVolume = this.preMuteVol = 1; - this.duration = Number.MAX_VALUE; + this.duration = 0; + + this.vidId = extractIdFromUrl( url ) || extractIdFromUri( url ); - if ( container ) { + this.addEventListener( "load", function() { + // For calculating position relative to video (like subtitles) + this.offsetWidth = this.video.offsetWidth; + this.offsetHeight = this.video.offsetHeight; + this.offsetParent = this.video.offsetParent; + this.offsetLeft = this.video.offsetLeft; + this.offsetTop = this.video.offsetTop; + + // Set up stuff that requires the API to be loaded + this.registerYoutubeEventHandlers(); + this.registerInternalEventHandlers(); + }); + + (function() { + var hasBeenCalled = 0; + + self.addEventListener( "playing", function() { + if (hasBeenCalled) { + return; + } + + hasBeenCalled = 1; + self.duration = self.video.getDuration(); + self.dispatchEvent( "durationchange" ); + + }); + })(); + + if ( loadedPlayers[this.playerId] ) { + this.video = registry[this.playerId].video; + + this.vidId = this.vidId || extractIdFromUrl( container.getAttribute( "src" ) ) || extractIdFromUri( container.getAttribute( "src" ) ); + + if (this.vidId !== registry[this.playerId].vidId ) { + this.video.cueVideoById( this.vidId ); + } else { + // Same video, new ctor. Force a seek to the beginning + this.previousCurrentTime = 1; + } + + this.dispatchEvent( 'load' ); + } else if ( container ) { makeSWF.call( this, url, container ); } else { // Container not yet loaded, get it on DOMDontentLoad @@ -977,10 +1007,6 @@ var onYouTubePlayerReady; } registry[this.playerId] = this; - - if ( loadedPlayers[this.playerId] ) { - this.dispatchEvent( "load" ); - } }; // end Popcorn.youtube.init @@ -1047,7 +1073,9 @@ var onYouTubePlayerReady; play: function() { // In case called before video is loaded, defer acting if ( !loadedPlayers[this.playerId] ) { - this.addEventListener( "load", this.play ); + this.addEventListener( "load", function() { + this.play(); + }); return; } @@ -1069,7 +1097,9 @@ var onYouTubePlayerReady; load: function() { // In case called before video is loaded, defer acting if ( !loadedPlayers[this.playerId] ) { - this.addEventListener( "load", this.load ); + this.addEventListener( "load", function() { + this.load(); +} ); return; } @@ -1209,7 +1239,7 @@ var onYouTubePlayerReady; this.dispatchEvent( 'progress' ); setTimeout( function() { - self.startTimeUpdater.call( self ); + self.startProgressUpdater.call( self ); }, progressInterval); } }); // end Popcorn.extend diff --git a/players/youtube/popcorn.youtube.unit.html b/players/youtube/popcorn.youtube.unit.html index b6a1a4e..fb2195e 100644 --- a/players/youtube/popcorn.youtube.unit.html +++ b/players/youtube/popcorn.youtube.unit.html @@ -29,6 +29,7 @@ failures would disappear the second time.

+
diff --git a/players/youtube/popcorn.youtube.unit.js b/players/youtube/popcorn.youtube.unit.js index c7e7b6a..18d9cdf 100644 --- a/players/youtube/popcorn.youtube.unit.js +++ b/players/youtube/popcorn.youtube.unit.js @@ -68,7 +68,8 @@ test( "Popcorn YouTube Plugin Event Tests", function() { set1Executed = false, set2Executed = false, set3Executed = false, - expects = expectedEvents.length + 5; + expects = expectedEvents.length + 5, + listeners = []; stop( 15000 ); expect(expects); @@ -99,19 +100,29 @@ test( "Popcorn YouTube Plugin Event Tests", function() { } } - popcorn.listen( event, function() { - eventCount++; - var expected = expectedEvents.shift(); - if ( expected == event ) { - ok( true, "Event: "+event + " is fired." ); - plus(); - } else { - ok( false, event + " is fired unexpectedly, expecting: " + expected ); - } + listeners.push( { + evt: event, + fn: popcorn.listen( event, function() { + eventCount++; + var expected = expectedEvents.shift(); + if ( expected == event ) { + ok( true, "Event: "+event + " is fired." ); + plus(); + } else { + ok( false, event + " is fired unexpectedly, expecting: " + expected ); + } + }) }); added.push( event ); })( expectedEvents[i] ); } + + // Cleanup + listeners.push( popcorn.listen( "ended", function() { + Popcorn.forEach( listeners, function ( obj ) { + popcorn.unlisten( obj.evt, obj.fn ); + }); + })); // operations set1 popcorn.listen( 'playing', function() { @@ -188,26 +199,24 @@ test( "Popcorn YouTube Plugin Url and Duration Tests", function() { QUnit.reset(); var count = 0, - expects = 4, - rawTube = Popcorn.youtube( 'video', 'http://www.youtube.com/watch?v=9oar9glUCL0' ); + expects = 3, + popcorn = Popcorn( Popcorn.youtube( 'video2', 'http://www.youtube.com/watch?v=9oar9glUCL0' ) ); expect( expects ); - stop( 5000 ); + stop( 10000 ); - equals( rawTube.vidId, '9oar9glUCL0', 'Video id set' ); + equals( popcorn.video.vidId, '9oar9glUCL0', 'Video id set' ); plus(); - equals( rawTube.duration, Number.MAX_VALUE, 'Duration starts as Max Value'); + equals( popcorn.duration(), 0, 'Duration starts as 0'); plus(); - rawTube.addEventListener( "playing", function() { - notEqual( rawTube.duration, Number.MAX_VALUE, "Duration has been changed from max value" ); - plus(); - notEqual( rawTube.duration, 0, "Duration is non-zero" ); + popcorn.listen( "durationchange", function() { + notEqual( 0, popcorn.duration(), "Duration has been changed from 0" ); plus(); - rawTube.pause(); + popcorn.pause(); }); - rawTube.play(); + popcorn.play(); }); \ No newline at end of file