]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#140] Unit tests fixed
authorstevenaw <sweerdenburg@gmail.com>
Tue, 15 Mar 2011 18:22:08 +0000 (14:22 -0400)
committerstevenaw <sweerdenburg@gmail.com>
Tue, 15 Mar 2011 18:22:08 +0000 (14:22 -0400)
players/youtube/popcorn.youtube.js
players/youtube/popcorn.youtube.unit.js

index b9aa7d57b9130ebb4dc287b90bc48a8637e8659f..26b345c60bc2aaba449c0aac189b476007f2cd4c 100644 (file)
@@ -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. */
 
index 29e4b6aca8a90bdd3733c835fc0e80f67ffe3373..7a8eedfa8c2b87b0d92bd506c5fa912fbe1126cc 100644 (file)
@@ -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