summaryrefslogtreecommitdiffstats
path: root/extensions/test/data/unit_test_environment_specific_bindings.js
blob: 2fbcc3e09be09f5543e126ded5d20781491f7010 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var sendRequestNatives = requireNative('sendRequest');

function registerHooks(api) {
  var chromeTest = api.compiledApi;
  var apiFunctions = api.apiFunctions;

  apiFunctions.setHandleRequest('notifyPass', function() {
    requireAsync('testNatives').then(function(natives) {
      natives.NotifyPass();
    });
  });

  apiFunctions.setHandleRequest('notifyFail', function(message) {
    requireAsync('testNatives').then(function(natives) {
      natives.NotifyFail(message);
    });
  });

  apiFunctions.setHandleRequest('log', function() {
    requireAsync('testNatives').then(function(natives) {
      natives.Log($Array.join(arguments, ' '));
    });
  });

}

function testDone(runNextTest) {
    // Use a promise here to allow previous test contexts to be eligible for
    // garbage collection.
    Promise.resolve().then(function() {
      runNextTest();
    });
}

function exportTests(tests, runTests, exports) {
  $Array.forEach(tests, function(test) {
    exports[test.name] = function() {
      runTests([test]);
      return true;
    };
  });
}

/**
 * A fake implementation of setTimeout and clearTimeout.
 * @constructor
 */
function TimeoutManager() {
  this.timeouts_ = {};
  this.nextTimeoutId_ = 0;
  this.currentTime = 0;
  this.autorunEnabled_ = false;
}

/**
 * Installs setTimeout and clearTimeout into the global object.
 */
TimeoutManager.prototype.installGlobals = function() {
  var global = sendRequestNatives.GetGlobal({});
  global.setTimeout = this.setTimeout_.bind(this);
  global.clearTimeout = this.clearTimeout_.bind(this);
};

/**
 * Starts auto-running of timeout callbacks. Until |numCallbacksToRun| callbacks
 * have run, any timeout callbacks set by calls to setTimeout (including before
 * the call to run) will cause the currentTime to be advanced to the time of
 * the timeout.
 */
TimeoutManager.prototype.run = function(numCallbacksToRun) {
  this.numCallbacksToRun_ = numCallbacksToRun;
  Promise.resolve().then(this.autoRun_.bind(this));
};

/**
 * Runs timeout callbacks with earliest timeout.
 * @private
 */
TimeoutManager.prototype.autoRun_ = function() {
  if (this.numCallbacksToRun_ <= 0 || $Object.keys(this.timeouts_).length == 0)
    return;

  // Bucket the timeouts by their timeout time.
  var timeoutsByTimeout = {};
  var timeoutIds = $Object.keys(this.timeouts_);
  for (var i = 0; i < timeoutIds.length; i++) {
    var timeout = this.timeouts_[timeoutIds[i]];
    var timeMs = timeout.timeMs;
    if (!timeoutsByTimeout[timeMs])
      timeoutsByTimeout[timeMs] = [];
    timeoutsByTimeout[timeMs].push(timeout);
  }
  this.currentTime =
      $Function.apply(Math.min, null, $Object.keys((timeoutsByTimeout)));
  // Run all timeouts in the earliest timeout bucket.
  var timeouts = timeoutsByTimeout[this.currentTime];
  for (var i = 0; i < timeouts.length; i++) {
    var currentTimeout = timeouts[i];
    if (!this.timeouts_[currentTimeout.id])
      continue;
    this.numCallbacksToRun_--;
    delete this.timeouts_[currentTimeout.id];
    try {
      currentTimeout.target();
    } catch (e) {
      console.log('error calling timeout target ' + e.stack);
    }
  }
  // Continue running any later callbacks.
  Promise.resolve().then(this.autoRun_.bind(this));
};

/**
 * A fake implementation of setTimeout. This does not support passing callback
 * arguments.
 * @private
 */
TimeoutManager.prototype.setTimeout_ = function(target, timeoutMs) {
  var timeoutId = this.nextTimeoutId_++;
  this.timeouts_[timeoutId] = {
    id: timeoutId,
    target: target,
    timeMs: timeoutMs + this.currentTime,
  };
  if (this.autorunEnabled_)
    Promise.resolve().then(this.autoRun_.bind(this));
  return timeoutId;
};

/**
 * A fake implementation of clearTimeout.
 * @private
 */
TimeoutManager.prototype.clearTimeout_ = function(timeoutId) {
  if (this.timeouts_[timeoutId])
    delete this.timeouts_[timeoutId];
};

exports.registerHooks = registerHooks;
exports.testDone = testDone;
exports.exportTests = exportTests;
exports.TimeoutManager = TimeoutManager;