Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/test_runner/TestRunner.js

Issue 2837083003: DevTools: create test infrastructure so devtools drives the test (Closed)
Patch Set: all Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 Host.isUnderTest = () => true;
dgozman 2017/06/19 22:55:52 Why this?
chenwilliam 2017/06/19 23:48:18 I accidentally ran unit tests w/ integration test
11
10 TestRunner.executeTestScript = function() { 12 TestRunner.executeTestScript = function() {
11 const testScriptURL = /** @type {string} */ (Runtime.queryParam('test')); 13 const testScriptURL = /** @type {string} */ (Runtime.queryParam('test'));
12 fetch(testScriptURL) 14 fetch(testScriptURL)
13 .then(data => data.text()) 15 .then(data => data.text())
14 .then(testScript => eval(`(function test(){${testScript}})()\n//# sourceUR L=${testScriptURL}`)) 16 .then(testScript => eval(`(function test(){${testScript}})()\n//# sourceUR L=${testScriptURL}`))
15 .catch(error => { 17 .catch(error => {
16 TestRunner.addResult(`Unable to execute test script because of error: ${ error}`); 18 TestRunner.addResult(`Unable to execute test script because of error: ${ error}`);
17 TestRunner.completeTest(); 19 TestRunner.completeTest();
18 }); 20 });
19 }; 21 };
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 * @param {*} text 53 * @param {*} text
52 */ 54 */
53 TestRunner.addResult = function(text) { 55 TestRunner.addResult = function(text) {
54 if (self.testRunner) 56 if (self.testRunner)
55 TestRunner._results.push(String(text)); 57 TestRunner._results.push(String(text));
56 else 58 else
57 console.log(text); 59 console.log(text);
58 }; 60 };
59 61
60 /** 62 /**
63 * @param {!Array<string>} textArray
64 */
65 TestRunner.addResults = function(textArray) {
66 if (!textArray)
67 return;
68 for (var i = 0, size = textArray.length; i < size; ++i)
69 TestRunner.addResult(textArray[i]);
70 };
71
72 /**
61 * @param {!Array<function()>} tests 73 * @param {!Array<function()>} tests
62 */ 74 */
63 TestRunner.runTests = function(tests) { 75 TestRunner.runTests = function(tests) {
64 nextTest(); 76 nextTest();
65 77
66 function nextTest() { 78 function nextTest() {
67 var test = tests.shift(); 79 var test = tests.shift();
68 if (!test) { 80 if (!test) {
69 TestRunner.completeTest(); 81 TestRunner.completeTest();
70 return; 82 return;
71 } 83 }
72 TestRunner.addResult('\ntest: ' + test.name); 84 TestRunner.addResult('\ntest: ' + test.name);
73 var testPromise = test(); 85 var testPromise = test();
74 if (!(testPromise instanceof Promise)) 86 if (!(testPromise instanceof Promise))
75 testPromise = Promise.resolve(); 87 testPromise = Promise.resolve();
76 testPromise.then(nextTest); 88 testPromise.then(nextTest);
77 } 89 }
78 }; 90 };
79 91
80 /** 92 /**
81 * @param {!Object} receiver 93 * @param {!Object} receiver
82 * @param {string} methodName 94 * @param {string} methodName
95 * @param {!Function} override
96 * @param {boolean} opt_sticky
97 */
98 TestRunner.addSniffer = function(receiver, methodName, override, opt_sticky) {
99 override = TestRunner.safeWrap(override);
100
101 var original = receiver[methodName];
102 if (typeof original !== 'function')
103 throw new Error('Cannot find method to override: ' + methodName);
104
105 receiver[methodName] = function(var_args) {
106 try {
107 var result = original.apply(this, arguments);
108 } finally {
109 if (!opt_sticky)
110 receiver[methodName] = original;
111 }
112 // In case of exception the override won't be called.
113 try {
114 Array.prototype.push.call(arguments, result);
115 override.apply(this, arguments);
116 } catch (e) {
117 throw new Error('Exception in overriden method \'' + methodName + '\': ' + e);
118 }
119 return result;
120 };
121 };
122
123 /**
124 * @param {!Object} receiver
125 * @param {string} methodName
83 * @return {!Promise<*>} 126 * @return {!Promise<*>}
84 */ 127 */
85 TestRunner.addSniffer = function(receiver, methodName) { 128 TestRunner.addSnifferPromise = function(receiver, methodName) {
86 return new Promise(function(resolve, reject) { 129 return new Promise(function(resolve, reject) {
87 var original = receiver[methodName]; 130 var original = receiver[methodName];
88 if (typeof original !== 'function') { 131 if (typeof original !== 'function') {
89 reject('Cannot find method to override: ' + methodName); 132 reject('Cannot find method to override: ' + methodName);
90 return; 133 return;
91 } 134 }
92 135
93 receiver[methodName] = function(var_args) { 136 receiver[methodName] = function(var_args) {
94 try { 137 try {
95 var result = original.apply(this, arguments); 138 var result = original.apply(this, arguments);
(...skipping 15 matching lines...) Expand all
111 154
112 /** 155 /**
113 * @param {!Array<string>} lazyModules 156 * @param {!Array<string>} lazyModules
114 * @return {!Promise<!Array<undefined>>} 157 * @return {!Promise<!Array<undefined>>}
115 */ 158 */
116 TestRunner.loadLazyModules = function(lazyModules) { 159 TestRunner.loadLazyModules = function(lazyModules) {
117 return Promise.all(lazyModules.map(lazyModule => self.runtime.loadModulePromis e(lazyModule))); 160 return Promise.all(lazyModules.map(lazyModule => self.runtime.loadModulePromis e(lazyModule)));
118 }; 161 };
119 162
120 /** 163 /**
164 * @param {string} panel
165 * @return {!Promise<!UI.Panel>}
166 */
167 TestRunner.loadPanel = function(panel) {
168 return UI.inspectorView.panel(panel);
169 };
170
171 /**
121 * @param {string} key 172 * @param {string} key
122 * @param {boolean=} ctrlKey 173 * @param {boolean=} ctrlKey
123 * @param {boolean=} altKey 174 * @param {boolean=} altKey
124 * @param {boolean=} shiftKey 175 * @param {boolean=} shiftKey
125 * @param {boolean=} metaKey 176 * @param {boolean=} metaKey
126 * @return {!KeyboardEvent} 177 * @return {!KeyboardEvent}
127 */ 178 */
128 TestRunner.createKeyEvent = function(key, ctrlKey, altKey, shiftKey, metaKey) { 179 TestRunner.createKeyEvent = function(key, ctrlKey, altKey, shiftKey, metaKey) {
129 return new KeyboardEvent('keydown', { 180 return new KeyboardEvent('keydown', {
130 key: key, 181 key: key,
131 bubbles: true, 182 bubbles: true,
132 cancelable: true, 183 cancelable: true,
133 ctrlKey: !!ctrlKey, 184 ctrlKey: !!ctrlKey,
134 altKey: !!altKey, 185 altKey: !!altKey,
135 shiftKey: !!shiftKey, 186 shiftKey: !!shiftKey,
136 metaKey: !!metaKey 187 metaKey: !!metaKey
137 }); 188 });
138 }; 189 };
139 190
191 /**
192 * @param {!Function} func
193 * @param {!Function=} onexception
194 * @return {!Function}
195 */
196 TestRunner.safeWrap = function(func, onexception) {
197 /**
198 * @this {*}
199 */
200 function result() {
201 if (!func)
202 return;
203 var wrapThis = this;
204 try {
205 return func.apply(wrapThis, arguments);
206 } catch (e) {
207 TestRunner.addResult('Exception while running: ' + func + '\n' + (e.stack || e));
208 if (onexception)
209 TestRunner.safeWrap(onexception)();
210 else
211 TestRunner.completeTest();
212 }
213 }
214 return result;
215 };
216
217 /**
218 * @param {!Function} testFunction
219 * @return {!Function}
220 */
221 TestRunner.debugTest = function(testFunction) {
222 self.test = testFunction;
223 TestRunner.addResult = console.log;
224 TestRunner.completeTest = () => console.log('Test completed');
225 return () => {};
226 };
227
140 (function() { 228 (function() {
141 /** 229 /**
142 * @param {string|!Event} message 230 * @param {string|!Event} message
143 * @param {string} source 231 * @param {string} source
144 * @param {number} lineno 232 * @param {number} lineno
145 * @param {number} colno 233 * @param {number} colno
146 * @param {!Error} error 234 * @param {!Error} error
147 */ 235 */
148 function completeTestOnError(message, source, lineno, colno, error) { 236 function completeTestOnError(message, source, lineno, colno, error) {
149 TestRunner.addResult('TEST ENDED IN ERROR: ' + error.stack); 237 TestRunner.addResult('TEST ENDED IN ERROR: ' + error.stack);
150 TestRunner.completeTest(); 238 TestRunner.completeTest();
151 } 239 }
152 240
153 self['onerror'] = completeTestOnError; 241 self['onerror'] = completeTestOnError;
154 })(); 242 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698