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

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

Issue 2513423003: DevTools: Convert inspector-unit tests to use reusable test harness (Closed)
Patch Set: Moved type definition out of externs Created 4 years 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
(Empty)
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
3 // found in the LICENSE file.
4
5 /** @type {!{notifyDone: function()}|undefined} */
6 self.testRunner;
chenwilliam 2016/11/30 23:06:04 It doesn't seem like closure compiler enforces an
einbinder 2016/12/01 02:07:51 Looks good enough to me.
7
8 TestRunner.executeTestScript = function() {
9 fetch(`${Runtime.queryParam('test')}`)
10 .then((data) => data.text())
11 .then((testScript) => eval(`(function(){${testScript}})()`))
12 .catch((error) => {
13 TestRunner.addResult(`Unable to execute test script because of error: ${ error}`);
14 TestRunner.completeTest();
15 });
16 };
17
18 /** @type {!Array<string>} */
19 TestRunner._results = [];
20
21 /**
22 * @suppressGlobalPropertiesCheck
23 */
24 TestRunner.completeTest = function() {
25 if (!self.testRunner) {
26 console.log('Test Done');
27 return;
28 }
29
30 Array.prototype.forEach.call(document.documentElement.childNodes, x => x.remov e());
31 var outputElement = document.createElement('div');
32 // Support for svg - add to document, not body, check for style.
33 if (outputElement.style) {
34 outputElement.style.whiteSpace = 'pre';
35 outputElement.style.height = '10px';
36 outputElement.style.overflow = 'hidden';
37 }
38 document.documentElement.appendChild(outputElement);
39 for (var i = 0; i < TestRunner._results.length; i++) {
40 outputElement.appendChild(document.createTextNode(TestRunner._results[i]));
41 outputElement.appendChild(document.createElement('br'));
42 }
43 TestRunner._results = [];
44 self.testRunner.notifyDone();
45 };
46
47 /**
48 * @param {*} text
49 */
50 TestRunner.addResult = function(text) {
51 if (self.testRunner)
52 TestRunner._results.push(String(text));
53 else
54 console.log(text);
55 };
56
57 /**
58 * @param {!Array<function()>} tests
59 */
60 TestRunner.runTests = function(tests) {
61 nextTest();
62
63 function nextTest() {
64 var test = tests.shift();
65 if (!test) {
66 TestRunner.completeTest();
67 return;
68 }
69 TestRunner.addResult('\ntest: ' + test.name);
70 var testPromise = test();
71 if (!(testPromise instanceof Promise))
72 testPromise = Promise.resolve();
73 testPromise.then(nextTest);
74 }
75 };
76
77 /**
78 * @param {!Object} receiver
79 * @param {string} methodName
80 * @return {!Promise<*>}
81 */
82 TestRunner.addSniffer = function(receiver, methodName) {
83 return new Promise(function(resolve, reject) {
84 var original = receiver[methodName];
85 if (typeof original !== 'function') {
86 reject('Cannot find method to override: ' + methodName);
87 return;
88 }
89
90 receiver[methodName] = function(var_args) {
91 try {
92 var result = original.apply(this, arguments);
93 } finally {
94 receiver[methodName] = original;
95 }
96 // In case of exception the override won't be called.
97 try {
98 Array.prototype.push.call(arguments, result);
99 resolve.apply(this, arguments);
100 } catch (e) {
101 reject('Exception in overridden method \'' + methodName + '\': ' + e);
102 TestRunner.completeTest();
103 }
104 return result;
105 };
106 });
107 };
108
109 /**
110 * @param {!Array<string>} lazyModules
111 * @return {!Promise<!Array<undefined>>}
112 */
113 TestRunner.loadLazyModules = function(lazyModules) {
114 return Promise.all(lazyModules.map(lazyModule => self.runtime.loadModulePromis e(lazyModule)));
115 };
116
117 /**
118 * @param {string} key
119 * @param {boolean} ctrlKey
120 * @param {boolean} altKey
121 * @param {boolean} shiftKey
122 * @param {boolean} metaKey
123 * @return {!KeyboardEvent}
124 */
125 TestRunner.createKeyEvent = function(key, ctrlKey, altKey, shiftKey, metaKey) {
126 return new KeyboardEvent('keydown', {
127 key: key,
128 bubbles: true,
129 cancelable: true,
130 ctrlKey: ctrlKey,
131 altKey: altKey,
132 shiftKey: shiftKey,
133 metaKey: metaKey
134 });
135 };
136
137 (function() {
138 /**
139 * @param {string|!Event} message
140 * @param {string} source
141 * @param {number} lineno
142 * @param {number} colno
143 * @param {!Error} error
144 */
145 function completeTestOnError(message, source, lineno, colno, error) {
146 TestRunner.addResult('TEST ENDED IN ERROR: ' + error.stack);
147 TestRunner.completeTest();
148 }
149
150 self['onerror'] = completeTestOnError;
151 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698