| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 /* eslint-disable no-console */ | 5 /* eslint-disable no-console */ |
| 6 | 6 |
| 7 /** @type {!{notifyDone: function()}|undefined} */ | 7 /** @type {!{logToStderr: function(), notifyDone: function()}|undefined} */ |
| 8 self.testRunner; | 8 self.testRunner; |
| 9 | 9 |
| 10 TestRunner.executeTestScript = function() { | 10 TestRunner.executeTestScript = function() { |
| 11 const testScriptURL = /** @type {string} */ (Runtime.queryParam('test')); | 11 const testScriptURL = /** @type {string} */ (Runtime.queryParam('test')); |
| 12 fetch(testScriptURL) | 12 fetch(testScriptURL) |
| 13 .then(data => data.text()) | 13 .then(data => data.text()) |
| 14 .then(testScript => eval(`(function test(){${testScript}})()\n//# sourceUR
L=${testScriptURL}`)) | 14 .then(testScript => eval(`(async function test(){${testScript}})()\n//# so
urceURL=${testScriptURL}`)) |
| 15 .catch(error => { | 15 .catch(error => { |
| 16 TestRunner.addResult(`Unable to execute test script because of error: ${
error}`); | 16 TestRunner.addResult(`Unable to execute test script because of error: ${
error}`); |
| 17 TestRunner.completeTest(); | 17 TestRunner.completeTest(); |
| 18 }); | 18 }); |
| 19 }; | 19 }; |
| 20 | 20 |
| 21 /** @type {!Array<string>} */ | 21 /** @type {!Array<string>} */ |
| 22 TestRunner._results = []; | 22 TestRunner._results = []; |
| 23 | 23 |
| 24 /** | 24 /** |
| (...skipping 26 matching lines...) Expand all Loading... |
| 51 * @param {*} text | 51 * @param {*} text |
| 52 */ | 52 */ |
| 53 TestRunner.addResult = function(text) { | 53 TestRunner.addResult = function(text) { |
| 54 if (self.testRunner) | 54 if (self.testRunner) |
| 55 TestRunner._results.push(String(text)); | 55 TestRunner._results.push(String(text)); |
| 56 else | 56 else |
| 57 console.log(text); | 57 console.log(text); |
| 58 }; | 58 }; |
| 59 | 59 |
| 60 /** | 60 /** |
| 61 * @param {!Array<string>} textArray |
| 62 */ |
| 63 TestRunner.addResults = function(textArray) { |
| 64 if (!textArray) |
| 65 return; |
| 66 for (var i = 0, size = textArray.length; i < size; ++i) |
| 67 TestRunner.addResult(textArray[i]); |
| 68 }; |
| 69 |
| 70 /** |
| 61 * @param {!Array<function()>} tests | 71 * @param {!Array<function()>} tests |
| 62 */ | 72 */ |
| 63 TestRunner.runTests = function(tests) { | 73 TestRunner.runTests = function(tests) { |
| 64 nextTest(); | 74 nextTest(); |
| 65 | 75 |
| 66 function nextTest() { | 76 function nextTest() { |
| 67 var test = tests.shift(); | 77 var test = tests.shift(); |
| 68 if (!test) { | 78 if (!test) { |
| 69 TestRunner.completeTest(); | 79 TestRunner.completeTest(); |
| 70 return; | 80 return; |
| 71 } | 81 } |
| 72 TestRunner.addResult('\ntest: ' + test.name); | 82 TestRunner.addResult('\ntest: ' + test.name); |
| 73 var testPromise = test(); | 83 var testPromise = test(); |
| 74 if (!(testPromise instanceof Promise)) | 84 if (!(testPromise instanceof Promise)) |
| 75 testPromise = Promise.resolve(); | 85 testPromise = Promise.resolve(); |
| 76 testPromise.then(nextTest); | 86 testPromise.then(nextTest); |
| 77 } | 87 } |
| 78 }; | 88 }; |
| 79 | 89 |
| 80 /** | 90 /** |
| 81 * @param {!Object} receiver | 91 * @param {!Object} receiver |
| 82 * @param {string} methodName | 92 * @param {string} methodName |
| 93 * @param {!Function} override |
| 94 * @param {boolean} opt_sticky |
| 95 */ |
| 96 TestRunner.addSniffer = function(receiver, methodName, override, opt_sticky) { |
| 97 override = TestRunner.safeWrap(override); |
| 98 |
| 99 var original = receiver[methodName]; |
| 100 if (typeof original !== 'function') |
| 101 throw new Error('Cannot find method to override: ' + methodName); |
| 102 |
| 103 receiver[methodName] = function(var_args) { |
| 104 try { |
| 105 var result = original.apply(this, arguments); |
| 106 } finally { |
| 107 if (!opt_sticky) |
| 108 receiver[methodName] = original; |
| 109 } |
| 110 // In case of exception the override won't be called. |
| 111 try { |
| 112 Array.prototype.push.call(arguments, result); |
| 113 override.apply(this, arguments); |
| 114 } catch (e) { |
| 115 throw new Error('Exception in overriden method \'' + methodName + '\': ' +
e); |
| 116 } |
| 117 return result; |
| 118 }; |
| 119 }; |
| 120 |
| 121 /** |
| 122 * @param {!Object} receiver |
| 123 * @param {string} methodName |
| 83 * @return {!Promise<*>} | 124 * @return {!Promise<*>} |
| 84 */ | 125 */ |
| 85 TestRunner.addSniffer = function(receiver, methodName) { | 126 TestRunner.addSnifferPromise = function(receiver, methodName) { |
| 86 return new Promise(function(resolve, reject) { | 127 return new Promise(function(resolve, reject) { |
| 87 var original = receiver[methodName]; | 128 var original = receiver[methodName]; |
| 88 if (typeof original !== 'function') { | 129 if (typeof original !== 'function') { |
| 89 reject('Cannot find method to override: ' + methodName); | 130 reject('Cannot find method to override: ' + methodName); |
| 90 return; | 131 return; |
| 91 } | 132 } |
| 92 | 133 |
| 93 receiver[methodName] = function(var_args) { | 134 receiver[methodName] = function(var_args) { |
| 94 try { | 135 try { |
| 95 var result = original.apply(this, arguments); | 136 var result = original.apply(this, arguments); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 111 | 152 |
| 112 /** | 153 /** |
| 113 * @param {!Array<string>} lazyModules | 154 * @param {!Array<string>} lazyModules |
| 114 * @return {!Promise<!Array<undefined>>} | 155 * @return {!Promise<!Array<undefined>>} |
| 115 */ | 156 */ |
| 116 TestRunner.loadLazyModules = function(lazyModules) { | 157 TestRunner.loadLazyModules = function(lazyModules) { |
| 117 return Promise.all(lazyModules.map(lazyModule => self.runtime.loadModulePromis
e(lazyModule))); | 158 return Promise.all(lazyModules.map(lazyModule => self.runtime.loadModulePromis
e(lazyModule))); |
| 118 }; | 159 }; |
| 119 | 160 |
| 120 /** | 161 /** |
| 162 * @param {string} panel |
| 163 * @return {!Promise<!UI.Panel>} |
| 164 */ |
| 165 TestRunner.loadPanel = function(panel) { |
| 166 return UI.inspectorView.panel(panel); |
| 167 }; |
| 168 |
| 169 /** |
| 121 * @param {string} key | 170 * @param {string} key |
| 122 * @param {boolean=} ctrlKey | 171 * @param {boolean=} ctrlKey |
| 123 * @param {boolean=} altKey | 172 * @param {boolean=} altKey |
| 124 * @param {boolean=} shiftKey | 173 * @param {boolean=} shiftKey |
| 125 * @param {boolean=} metaKey | 174 * @param {boolean=} metaKey |
| 126 * @return {!KeyboardEvent} | 175 * @return {!KeyboardEvent} |
| 127 */ | 176 */ |
| 128 TestRunner.createKeyEvent = function(key, ctrlKey, altKey, shiftKey, metaKey) { | 177 TestRunner.createKeyEvent = function(key, ctrlKey, altKey, shiftKey, metaKey) { |
| 129 return new KeyboardEvent('keydown', { | 178 return new KeyboardEvent('keydown', { |
| 130 key: key, | 179 key: key, |
| 131 bubbles: true, | 180 bubbles: true, |
| 132 cancelable: true, | 181 cancelable: true, |
| 133 ctrlKey: !!ctrlKey, | 182 ctrlKey: !!ctrlKey, |
| 134 altKey: !!altKey, | 183 altKey: !!altKey, |
| 135 shiftKey: !!shiftKey, | 184 shiftKey: !!shiftKey, |
| 136 metaKey: !!metaKey | 185 metaKey: !!metaKey |
| 137 }); | 186 }); |
| 138 }; | 187 }; |
| 139 | 188 |
| 189 /** |
| 190 * @param {!Function} func |
| 191 * @param {!Function=} onexception |
| 192 * @return {!Function} |
| 193 */ |
| 194 TestRunner.safeWrap = function(func, onexception) { |
| 195 /** |
| 196 * @this {*} |
| 197 */ |
| 198 function result() { |
| 199 if (!func) |
| 200 return; |
| 201 var wrapThis = this; |
| 202 try { |
| 203 return func.apply(wrapThis, arguments); |
| 204 } catch (e) { |
| 205 TestRunner.addResult('Exception while running: ' + func + '\n' + (e.stack
|| e)); |
| 206 if (onexception) |
| 207 TestRunner.safeWrap(onexception)(); |
| 208 else |
| 209 TestRunner.completeTest(); |
| 210 } |
| 211 } |
| 212 return result; |
| 213 }; |
| 214 |
| 140 (function() { | 215 (function() { |
| 141 /** | 216 /** |
| 142 * @param {string|!Event} message | 217 * @param {string|!Event} message |
| 143 * @param {string} source | 218 * @param {string} source |
| 144 * @param {number} lineno | 219 * @param {number} lineno |
| 145 * @param {number} colno | 220 * @param {number} colno |
| 146 * @param {!Error} error | 221 * @param {!Error} error |
| 147 */ | 222 */ |
| 148 function completeTestOnError(message, source, lineno, colno, error) { | 223 function completeTestOnError(message, source, lineno, colno, error) { |
| 149 TestRunner.addResult('TEST ENDED IN ERROR: ' + error.stack); | 224 TestRunner.addResult('TEST ENDED IN ERROR: ' + error.stack); |
| 150 TestRunner.completeTest(); | 225 TestRunner.completeTest(); |
| 151 } | 226 } |
| 152 | 227 |
| 153 self['onerror'] = completeTestOnError; | 228 self['onerror'] = completeTestOnError; |
| 154 })(); | 229 })(); |
| OLD | NEW |