1 // ==========================================================================
  2 // Project:   SproutCore - JavaScript Application Framework
  3 // Copyright: ©2006-2011 Strobe Inc. and contributors.
  4 //            ©2008-2011 Apple Inc. All rights reserved.
  5 // License:   Licensed under MIT license (see license.js)
  6 // ==========================================================================
  7 // ========================================================================
  8 // SC.Timer.isPaused Tests
  9 // ========================================================================
 10 /*globals module test ok isObj equals expects */
 11 
 12 module("Timer.isPaused") ;
 13 
 14 test("setting isPaused should stop firing", function() {
 15   
 16   var firedCount = 0, f1, f2, f3 ;
 17   
 18   SC.RunLoop.begin() ;
 19   var start = SC.RunLoop.currentRunLoop.get('startTime') ;
 20   var t = SC.Timer.schedule({
 21     target: this,
 22     action: function() { firedCount++ ; },
 23     interval: 100,
 24     repeats: YES
 25   });
 26   SC.RunLoop.end() ;
 27   
 28   // wait for timer to fire twice, then pause it.
 29   var tries1 = 10 ;
 30   f1 = function f1() {
 31     if(firedCount<2) {
 32       if (--tries1 >= 0) {
 33         setTimeout(f1, 100) ;
 34       } else {
 35         equals(NO, YES, 'Timer never fired 2 times - f1') ;
 36         window.start() ; // starts the test runner
 37       }
 38     } else {
 39       equals(NO, t.get('isPaused'), 'should start with isPaused = NO');
 40       t.set('isPaused', YES) ;
 41       firedCount = 0 ; // Reset count here.
 42       setTimeout(f2, 300) ;
 43     }
 44   };
 45   
 46   // once timer paused, make sure it did not fire again.
 47   f2 = function f2() {
 48     equals(0, firedCount, 'timer kept firing!') ;
 49     equals(YES, t.get('isPaused'), 'timer is not paused') ;
 50     t.set('isPaused', NO) ;
 51     setTimeout(f3, 300) ;
 52   } ;
 53   
 54   // once timer has verified paused, unpause and make sure it fires again.
 55   var tries2 = 10 ;
 56   f3 = function f3() {
 57     if (firedCount <= 2) {
 58       if (--tries2 >= 0) {
 59         setTimeout(f3, 100) ;
 60       } else {
 61         equals(NO, YES, "Timer did not resume") ;
 62         window.start() ; // starts the test runner
 63       }
 64       
 65     // timer fired, clean up.
 66     } else {
 67       t.invalidate() ;
 68       equals(NO, t.get('isPaused'), 'timer did not unpause') ;
 69       window.start() ; // starts the test runner
 70     }
 71   };
 72   
 73   stop() ; // stops the test runner
 74   setTimeout(f1, 300) ;
 75 });
 76 
 77 // using invalidate on a repeating timer is tested in schedule().
 78