From: stevenaw Date: Tue, 15 Mar 2011 18:22:08 +0000 (-0400) Subject: [#140] Unit tests fixed X-Git-Tag: v0.4~7^2~5 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=12805a7aec3a48646e7c52933579cf40dc1ebbe0;p=popcorn-js.git [#140] Unit tests fixed --- diff --git a/players/youtube/popcorn.youtube.js b/players/youtube/popcorn.youtube.js index b9aa7d5..26b345c 100644 --- a/players/youtube/popcorn.youtube.js +++ b/players/youtube/popcorn.youtube.js @@ -126,6 +126,11 @@ var onYouTubePlayerReady; this.timeUpdater = null; this.progressUpdater = null; + this.currentTime = 0; + this.previousCurrentTime = 0; + this.volume = 0; + this.previousVolume = 0; + this.playerId = elementId; this.duration = Number.MAX_VALUE; @@ -202,10 +207,10 @@ var onYouTubePlayerReady; // Start current time and loading progress syncing intervals. registerInternalEventHandlers: function() { this.addEventListener( 'playing', function() { - startTimeUpdater( this ); + this.startTimeUpdater(); }); this.addEventListener( 'loadedmetadata', function() { - startProgressUpdater( this ); + this.startProgressUpdater(); }); }, @@ -223,9 +228,6 @@ var onYouTubePlayerReady; this.video.playVideo(); this.video.pauseVideo(); }, - - currentTime: 0, - previousCurrentTime: 0, seekTo: function( time ) { var playing = this.video.getPlayerState() == YOUTUBE_STATE_PLAYING; @@ -253,15 +255,14 @@ var onYouTubePlayerReady; mute: function() { if ( this.video.isMuted() ) { this.video.unMute(); + this.volume = this.video.getVolume() / 100; this.dispatchEvent( 'volumechange' ); } else { this.video.mute(); + this.volume = 0; this.dispatchEvent( 'volumechange' ); } }, - - volume: 0, - previousVolume: 0, // Expects beteween 0 and 1 setVolume: function( vol ) { @@ -302,40 +303,42 @@ var onYouTubePlayerReady; }, playbackRate: function( arg ) { - } - - }); // end Popcorn.extend - - function startTimeUpdater( youcorn ) { - youcorn.timeUpdater = setInterval(function() { - - var state = youcorn.video.getPlayerState(); - + }, + + startTimeUpdater: function() { + var state = this.video.getPlayerState(), + self = this, + seeked = 0; + if ( abs( this.currentTime - this.previousCurrentTime ) > timeCheckInterval ) { + // Has programatically set the currentTime + this.previousCurrentTime = this.currentTime - timeCheckInterval; + this.seekTo( this.currentTime ); + seeked = 1; + } else { + this.previousCurrentTime = this.currentTime; + this.currentTime = this.video.getCurrentTime(); + } - if ( state !== YOUTUBE_STATE_ENDED && state !== YOUTUBE_STATE_PAUSED ) { - if ( abs( youcorn.currentTime - youcorn.previousCurrentTime ) > timeCheckInterval ) { - // Has programatically set the currentTime - youcorn.previousCurrentTime = youcorn.currentTime - timeCheckInterval; - youcorn.seekTo( youcorn.currentTime ); - } else { - youcorn.previousCurrentTime = youcorn.currentTime; - youcorn.currentTime = youcorn.video.getCurrentTime(); - } + if ( this.volume !== this.previousVolume ) { + this.setVolume( this.volume ); } - if ( youcorn.volume !== youcorn.previousVolume ) { - youcorn.setVolume( youcorn.volume ); + if ( state !== YOUTUBE_STATE_ENDED && state !== YOUTUBE_STATE_PAUSED || seeked ) { + this.dispatchEvent( 'timeupdate' ); } - youcorn.dispatchEvent( 'timeupdate' ); - }, timeupdateInterval); - } - - function startProgressUpdater( youcorn ) { - youcorn.progressUpdater = setInterval(function() { - var bytesLoaded = youcorn.video.getVideoBytesLoaded(), - bytesToLoad = youcorn.video.getVideoBytesTotal(); + if( state !== YOUTUBE_STATE_ENDED ) { + setTimeout( function() { + self.startTimeUpdater.call(self); + }, timeupdateInterval); + } + }, + + startProgressUpdater: function() { + var bytesLoaded = this.video.getVideoBytesLoaded(), + bytesToLoad = this.video.getVideoBytesTotal(), + self = this; // do nothing if size is not yet determined if ( bytesToLoad == 0 ) { @@ -343,23 +346,26 @@ var onYouTubePlayerReady; } // raise an event if load has just started - if ( !youcorn.loadStarted ) { - youcorn.loadStarted = true; - youcorn.dispatchEvent( 'loadstart' ); + if ( !this.loadStarted ) { + this.loadStarted = true; + this.dispatchEvent( 'loadstart' ); } // fully loaded if ( bytesLoaded >= bytesToLoad ) { - youcorn.fullyLoaded = true; - youcorn.readyState = READY_STATE_HAVE_ENOUGH_DATA; - youcorn.dispatchEvent( 'canplaythrough' ); - clearInterval( youcorn.progressUpdater ); + this.fullyLoaded = true; + this.readyState = READY_STATE_HAVE_ENOUGH_DATA; + this.dispatchEvent( 'canplaythrough' ); return; } - youcorn.dispatchEvent( 'progress' ); - }, progressInterval); - } + this.dispatchEvent( 'progress' ); + + setTimeout( function() { + self.startTimeUpdater.call( self ); + }, progressInterval); + } + }); // end Popcorn.extend /* Unsupported properties and events. */ diff --git a/players/youtube/popcorn.youtube.unit.js b/players/youtube/popcorn.youtube.unit.js index 29e4b6a..7a8eedf 100644 --- a/players/youtube/popcorn.youtube.unit.js +++ b/players/youtube/popcorn.youtube.unit.js @@ -37,14 +37,29 @@ test( "Popcorn YouTube Plugin Event Tests", function() { return; } + function plus(){ + if ( ++count == expects ) { + start(); + } + } + + QUnit.reset(); + + var count = 0, + expects = 19; + + stop( 15000 ); + popcorn.volume(1); // is muted later // check time sync popcorn.exec(2, function() { ok( popcorn.currentTime() >= 2, "Check time synchronization." ); + plus(); }); popcorn.exec(49, function() { ok( popcorn.currentTime() >= 49, "Check time synchronization." ); + plus(); }); popcorn.exec(40, function() { ok( false, "This should not be run." ); @@ -54,6 +69,7 @@ test( "Popcorn YouTube Plugin Event Tests", function() { var expectedEvents = [ 'play', 'loadeddata', + 'volumechange', 'playing', 'volumechange', 'pause', @@ -62,7 +78,6 @@ test( "Popcorn YouTube Plugin Event Tests", function() { 'seeked', 'volumechange', 'volumechange', - 'volumechange', 'playing', 'pause', 'ended' @@ -87,9 +102,10 @@ test( "Popcorn YouTube Plugin Event Tests", function() { eventCount++; var expected = expectedEvents.shift(); if ( expected == event ) { - ok( true, event + " is fired." ); + ok( true, "Event: "+event + " is fired." ); + plus(); } else { - ok( false, event + " is fired, expecting: " + expected ); + ok( false, event + " is fired unexpectedly, expecting: " + expected ); } }); added.push( event ); @@ -147,34 +163,21 @@ test( "Popcorn YouTube Plugin Event Tests", function() { return; } - popcorn.volume(1); + popcorn.volume(0.5); popcorn.mute(); equals( popcorn.volume(), 0, "Muted" ); + plus(); popcorn.mute(); ok( popcorn.volume() !== 0, "Not Muted" ); - equals( popcorn.volume(), 1, "Back to volume of 1" ); + plus(); + + equals( popcorn.volume(), 0.5, "Back to volume of 1" ); + plus(); set3Executed = true; }); - - var time = 0, - wait = 100, - timeout = 15000; - - stop( timeout + wait ); - var interval = setInterval( function() { - time += wait; - if ( time > timeout ) { - clearInterval( interval ); - return; - } - if ( eventCount == expectedEventCount ) { - start(); - clearInterval( interval ); - } - }, wait ); }); test( "Popcorn YouTube Plugin Url and Duration Tests", function() { @@ -209,5 +212,4 @@ test( "Popcorn YouTube Plugin Url and Duration Tests", function() { }); rawTube.play(); - }); \ No newline at end of file