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(`(function test(){${testScript}})()\n//# sourceUR
L=${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(); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after 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); |
96 } finally { | 137 } finally { |
97 receiver[methodName] = original; | 138 receiver[methodName] = original; |
98 } | 139 } |
99 // In case of exception the override won't be called. | 140 // In case of exception the override won't be called. |
100 try { | 141 try { |
101 Array.prototype.push.call(arguments, result); | 142 Array.prototype.push.call(arguments, result); |
102 resolve.apply(this, arguments); | 143 resolve.apply(this, arguments); |
103 } catch (e) { | 144 } catch (e) { |
104 reject('Exception in overridden method \'' + methodName + '\': ' + e); | 145 reject('Exception in overridden method \'' + methodName + '\': ' + e); |
105 TestRunner.completeTest(); | 146 TestRunner.completeTest(); |
106 } | 147 } |
107 return result; | 148 return result; |
108 }; | 149 }; |
109 }); | 150 }); |
110 }; | 151 }; |
111 | 152 |
112 /** | 153 /** |
| 154 * @param {string} module |
| 155 * @return {!Promise<undefined>} |
| 156 */ |
| 157 TestRunner.loadModule = function(module) { |
| 158 return self.runtime.loadModulePromise(module); |
| 159 }; |
| 160 |
| 161 /** |
113 * @param {!Array<string>} lazyModules | 162 * @param {!Array<string>} lazyModules |
114 * @return {!Promise<!Array<undefined>>} | 163 * @return {!Promise<!Array<undefined>>} |
115 */ | 164 */ |
116 TestRunner.loadLazyModules = function(lazyModules) { | 165 TestRunner.loadLazyModules = function(lazyModules) { |
117 return Promise.all(lazyModules.map(lazyModule => self.runtime.loadModulePromis
e(lazyModule))); | 166 return Promise.all(lazyModules.map(lazyModule => self.runtime.loadModulePromis
e(lazyModule))); |
118 }; | 167 }; |
119 | 168 |
120 /** | 169 /** |
| 170 * @param {string} panel |
| 171 * @return {!Promise<!UI.Panel>} |
| 172 */ |
| 173 TestRunner.loadPanel = function(panel) { |
| 174 return UI.inspectorView.panel(panel); |
| 175 }; |
| 176 |
| 177 /** |
121 * @param {string} key | 178 * @param {string} key |
122 * @param {boolean=} ctrlKey | 179 * @param {boolean=} ctrlKey |
123 * @param {boolean=} altKey | 180 * @param {boolean=} altKey |
124 * @param {boolean=} shiftKey | 181 * @param {boolean=} shiftKey |
125 * @param {boolean=} metaKey | 182 * @param {boolean=} metaKey |
126 * @return {!KeyboardEvent} | 183 * @return {!KeyboardEvent} |
127 */ | 184 */ |
128 TestRunner.createKeyEvent = function(key, ctrlKey, altKey, shiftKey, metaKey) { | 185 TestRunner.createKeyEvent = function(key, ctrlKey, altKey, shiftKey, metaKey) { |
129 return new KeyboardEvent('keydown', { | 186 return new KeyboardEvent('keydown', { |
130 key: key, | 187 key: key, |
131 bubbles: true, | 188 bubbles: true, |
132 cancelable: true, | 189 cancelable: true, |
133 ctrlKey: !!ctrlKey, | 190 ctrlKey: !!ctrlKey, |
134 altKey: !!altKey, | 191 altKey: !!altKey, |
135 shiftKey: !!shiftKey, | 192 shiftKey: !!shiftKey, |
136 metaKey: !!metaKey | 193 metaKey: !!metaKey |
137 }); | 194 }); |
138 }; | 195 }; |
139 | 196 |
| 197 /** |
| 198 * @param {!Function} func |
| 199 * @param {!Function=} onexception |
| 200 * @return {!Function} |
| 201 */ |
| 202 TestRunner.safeWrap = function(func, onexception) { |
| 203 /** |
| 204 * @this {*} |
| 205 */ |
| 206 function result() { |
| 207 if (!func) |
| 208 return; |
| 209 var wrapThis = this; |
| 210 try { |
| 211 return func.apply(wrapThis, arguments); |
| 212 } catch (e) { |
| 213 TestRunner.addResult('Exception while running: ' + func + '\n' + (e.stack
|| e)); |
| 214 if (onexception) |
| 215 TestRunner.safeWrap(onexception)(); |
| 216 else |
| 217 TestRunner.completeTest(); |
| 218 } |
| 219 } |
| 220 return result; |
| 221 }; |
| 222 |
| 223 /** |
| 224 * @param {!Function} testFunction |
| 225 * @return {!Function} |
| 226 */ |
| 227 function debugTest(testFunction) { |
| 228 self.test = testFunction; |
| 229 TestRunner.addResult = console.log; |
| 230 TestRunner.completeTest = () => console.log('Test completed'); |
| 231 return () => {}; |
| 232 } |
| 233 |
140 (function() { | 234 (function() { |
141 /** | 235 /** |
142 * @param {string|!Event} message | 236 * @param {string|!Event} message |
143 * @param {string} source | 237 * @param {string} source |
144 * @param {number} lineno | 238 * @param {number} lineno |
145 * @param {number} colno | 239 * @param {number} colno |
146 * @param {!Error} error | 240 * @param {!Error} error |
147 */ | 241 */ |
148 function completeTestOnError(message, source, lineno, colno, error) { | 242 function completeTestOnError(message, source, lineno, colno, error) { |
149 TestRunner.addResult('TEST ENDED IN ERROR: ' + error.stack); | 243 TestRunner.addResult('TEST ENDED IN ERROR: ' + error.stack); |
150 TestRunner.completeTest(); | 244 TestRunner.completeTest(); |
151 } | 245 } |
152 | 246 |
153 self['onerror'] = completeTestOnError; | 247 self['onerror'] = completeTestOnError; |
154 })(); | 248 })(); |
OLD | NEW |