1 /** 2 * A block which waits for some condition to become true, with timeout. 3 * 4 * @constructor 5 * @extends jasmine.Block 6 * @param {jasmine.Env} env The Jasmine environment. 7 * @param {Number} timeout The maximum time in milliseconds to wait for the condition to become true. 8 * @param {Function} latchFunction A function which returns true when the desired condition has been met. 9 * @param {String} message The message to display if the desired condition hasn't been met within the given time period. 10 * @param {jasmine.Spec} spec The Jasmine spec. 11 */ 12 jasmine.WaitsForBlock = function(env, timeout, latchFunction, message, spec) { 13 this.timeout = timeout || env.defaultTimeoutInterval; 14 this.latchFunction = latchFunction; 15 this.message = message; 16 this.totalTimeSpentWaitingForLatch = 0; 17 jasmine.Block.call(this, env, null, spec); 18 }; 19 jasmine.util.inherit(jasmine.WaitsForBlock, jasmine.Block); 20 21 jasmine.WaitsForBlock.TIMEOUT_INCREMENT = 10; 22 23 jasmine.WaitsForBlock.prototype.execute = function(onComplete) { 24 this.env.reporter.log('>> Jasmine waiting for ' + (this.message || 'something to happen')); 25 var latchFunctionResult; 26 try { 27 latchFunctionResult = this.latchFunction.apply(this.spec); 28 } catch (e) { 29 this.spec.fail(e); 30 onComplete(); 31 return; 32 } 33 34 if (latchFunctionResult) { 35 onComplete(); 36 } else if (this.totalTimeSpentWaitingForLatch >= this.timeout) { 37 var message = 'timed out after ' + this.timeout + ' msec waiting for ' + (this.message || 'something to happen'); 38 this.spec.fail({ 39 name: 'timeout', 40 message: message 41 }); 42 43 this.abort = true; 44 onComplete(); 45 } else { 46 this.totalTimeSpentWaitingForLatch += jasmine.WaitsForBlock.TIMEOUT_INCREMENT; 47 var self = this; 48 this.env.setTimeout(function() { 49 self.execute(onComplete); 50 }, jasmine.WaitsForBlock.TIMEOUT_INCREMENT); 51 } 52 };