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

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

Powered by Google App Engine
This is Rietveld 408576698