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

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

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

Powered by Google App Engine
This is Rietveld 408576698