Blame view

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