OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 var nativesPromise = requireAsync('testNatives'); | 5 var nativesPromise = requireAsync('testNatives'); |
| 6 var sendRequestNatives = requireNative('sendRequest'); |
6 | 7 |
7 function registerHooks(api) { | 8 function registerHooks(api) { |
8 var chromeTest = api.compiledApi; | 9 var chromeTest = api.compiledApi; |
9 var apiFunctions = api.apiFunctions; | 10 var apiFunctions = api.apiFunctions; |
10 | 11 |
11 apiFunctions.setHandleRequest('notifyPass', function() { | 12 apiFunctions.setHandleRequest('notifyPass', function() { |
12 nativesPromise.then(function(natives) { | 13 nativesPromise.then(function(natives) { |
13 natives.NotifyPass(); | 14 natives.NotifyPass(); |
14 }); | 15 }); |
15 }); | 16 }); |
(...skipping 18 matching lines...) Expand all Loading... |
34 Promise.resolve().then(function() { | 35 Promise.resolve().then(function() { |
35 runNextTest(); | 36 runNextTest(); |
36 }); | 37 }); |
37 } | 38 } |
38 | 39 |
39 function exportTests(tests, runTests, exports) { | 40 function exportTests(tests, runTests, exports) { |
40 $Array.forEach(tests, function(test) { | 41 $Array.forEach(tests, function(test) { |
41 exports[test.name] = function() { | 42 exports[test.name] = function() { |
42 runTests([test]); | 43 runTests([test]); |
43 return true; | 44 return true; |
44 } | 45 }; |
45 }); | 46 }); |
46 } | 47 } |
47 | 48 |
| 49 /** |
| 50 * A fake implementation of setTimeout and clearTimeout. |
| 51 * @constructor |
| 52 */ |
| 53 function TimeoutManager() { |
| 54 this.timeouts_ = {}; |
| 55 this.nextTimeoutId_ = 0; |
| 56 this.currentTime = 0; |
| 57 this.autorunEnabled_ = false; |
| 58 } |
| 59 |
| 60 /** |
| 61 * Installs setTimeout and clearTimeout into the global object. |
| 62 */ |
| 63 TimeoutManager.prototype.installGlobals = function() { |
| 64 var global = sendRequestNatives.GetGlobal({}); |
| 65 global.setTimeout = this.setTimeout_.bind(this); |
| 66 global.clearTimeout = this.clearTimeout_.bind(this); |
| 67 }; |
| 68 |
| 69 /** |
| 70 * Starts auto-running of timeout callbacks. Until |numCallbacksToRun| callbacks |
| 71 * have run, any timeout callbacks set by calls to setTimeout (including before |
| 72 * the call to run) will cause the currentTime to be advanced to the time of |
| 73 * the timeout. |
| 74 */ |
| 75 TimeoutManager.prototype.run = function(numCallbacksToRun) { |
| 76 this.numCallbacksToRun_ = numCallbacksToRun; |
| 77 Promise.resolve().then(this.autoRun_.bind(this)); |
| 78 }; |
| 79 |
| 80 /** |
| 81 * Runs timeout callbacks with earliest timeout. |
| 82 * @private |
| 83 */ |
| 84 TimeoutManager.prototype.autoRun_ = function() { |
| 85 if (this.numCallbacksToRun_ <= 0 || $Object.keys(this.timeouts_).length == 0) |
| 86 return; |
| 87 |
| 88 // Bucket the timeouts by their timeout time. |
| 89 var timeoutsByTimeout = {}; |
| 90 var timeoutIds = $Object.keys(this.timeouts_); |
| 91 for (var i = 0; i < timeoutIds.length; i++) { |
| 92 var timeout = this.timeouts_[timeoutIds[i]]; |
| 93 var timeMs = timeout.timeMs; |
| 94 if (!timeoutsByTimeout[timeMs]) |
| 95 timeoutsByTimeout[timeMs] = []; |
| 96 timeoutsByTimeout[timeMs].push(timeout); |
| 97 } |
| 98 this.currentTime = |
| 99 $Function.apply(Math.min, null, $Object.keys((timeoutsByTimeout))); |
| 100 // Run all timeouts in the earliest timeout bucket. |
| 101 var timeouts = timeoutsByTimeout[this.currentTime]; |
| 102 for (var i = 0; i < timeouts.length; i++) { |
| 103 var currentTimeout = timeouts[i]; |
| 104 if (!this.timeouts_[currentTimeout.id]) |
| 105 continue; |
| 106 this.numCallbacksToRun_--; |
| 107 delete this.timeouts_[currentTimeout.id]; |
| 108 try { |
| 109 currentTimeout.target(); |
| 110 } catch (e) { |
| 111 console.log('error calling timeout target ' + e.stack); |
| 112 } |
| 113 } |
| 114 // Continue running any later callbacks. |
| 115 Promise.resolve().then(this.autoRun_.bind(this)); |
| 116 }; |
| 117 |
| 118 /** |
| 119 * A fake implementation of setTimeout. This does not support passing callback |
| 120 * arguments. |
| 121 * @private |
| 122 */ |
| 123 TimeoutManager.prototype.setTimeout_ = function(target, timeoutMs) { |
| 124 var timeoutId = this.nextTimeoutId_++; |
| 125 this.timeouts_[timeoutId] = { |
| 126 id: timeoutId, |
| 127 target: target, |
| 128 timeMs: timeoutMs + this.currentTime, |
| 129 }; |
| 130 if (this.autorunEnabled_) |
| 131 Promise.resolve().then(this.autoRun_.bind(this)); |
| 132 return timeoutId; |
| 133 }; |
| 134 |
| 135 /** |
| 136 * A fake implementation of clearTimeout. |
| 137 * @private |
| 138 */ |
| 139 TimeoutManager.prototype.clearTimeout_ = function(timeoutId) { |
| 140 if (this.timeouts_[timeoutId]) |
| 141 delete this.timeouts_[timeoutId]; |
| 142 }; |
| 143 |
48 exports.registerHooks = registerHooks; | 144 exports.registerHooks = registerHooks; |
49 exports.testDone = testDone; | 145 exports.testDone = testDone; |
50 exports.exportTests = exportTests; | 146 exports.exportTests = exportTests; |
| 147 exports.TimeoutManager = TimeoutManager; |
OLD | NEW |