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

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: refactor into test app 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 /**
6 * @suppressGlobalPropertiesCheck
7 */
8 TestRunner.initializeUnitTest = function() {
9 var globalStorage = new Common.SettingsStorage(
10 {}, InspectorFrontendHost.setPreference, InspectorFrontendHost.removePrefe rence,
11 InspectorFrontendHost.clearPreferences);
12 var storagePrefix = '';
13 var localStorage = new Common.SettingsStorage({}, undefined, undefined, undefi ned, storagePrefix);
14 Common.settings = new Common.Settings(globalStorage, localStorage);
15
16 UI.viewManager = new UI.ViewManager();
17 UI.initializeUIUtils(document, Common.settings.createSetting('uiTheme', 'defau lt'));
18 UI.installComponentRootStyles(/** @type {!Element} */ (document.body));
19
20 UI.zoomManager = new UI.ZoomManager(self, InspectorFrontendHost);
21 UI.inspectorView = UI.InspectorView.instance();
22 UI.ContextMenu.initialize();
23 UI.ContextMenu.installHandler(document);
24 UI.Tooltip.installHandler(document);
25
26 var rootView = new UI.RootView();
27 UI.inspectorView.show(rootView.element);
28 rootView.attachToDocument(document);
29 TestRunner.executeTestScript();
30 };
31
32 TestRunner.executeTestScript = function() {
33 fetch(`${Runtime.queryParam('test')}`)
34 .then((data) => data.text())
35 .then((testScript) => eval(`(function(){${testScript}})()`))
36 .catch((error) => {
37 TestRunner.addResult(`Unable to execute test script because of error: ${ error}`);
38 TestRunner.completeTest();
39 });
40 };
41
42 /** @type {!Array<string>} */
43 TestRunner.results = [];
44
45 /**
46 * @suppressGlobalPropertiesCheck
47 */
48 TestRunner.completeTest = function() {
49 if (!self.testRunner) {
50 console.log('Test Done');
51 return;
52 }
53
54 Array.prototype.forEach.call(document.documentElement.childNodes, x => x.remov e());
55 var outputElement = document.createElement('div');
56 // Support for svg - add to document, not body, check for style.
57 if (outputElement.style) {
58 outputElement.style.whiteSpace = 'pre';
59 outputElement.style.height = '10px';
60 outputElement.style.overflow = 'hidden';
61 }
62 document.documentElement.appendChild(outputElement);
63 for (var i = 0; i < TestRunner.results.length; i++) {
64 outputElement.appendChild(document.createTextNode(TestRunner.results[i]));
65 outputElement.appendChild(document.createElement('br'));
66 }
67 TestRunner.results = [];
68 testRunner.notifyDone();
69 };
70
71 TestRunner.addResult = function(text) {
72 if (self.testRunner)
73 TestRunner.results.push(String(text));
74 else
75 console.log(text);
76 };
77
78 TestRunner.runTests = function(tests) {
79 nextTest();
80
81 function nextTest() {
82 var test = tests.shift();
83 if (!test) {
84 TestRunner.completeTest();
85 return;
86 }
87 TestRunner.addResult('\ntest: ' + test.name);
88 var testPromise = test();
89 if (!(testPromise instanceof Promise))
90 testPromise = Promise.resolve();
91 testPromise.then(nextTest);
92 }
93 };
94
95 /**
96 * @param {!Object} receiver
97 * @param {string} methodName
98 * @return {!Promise<*>}
99 */
100 TestRunner.addSniffer = function(receiver, methodName) {
101 return new Promise(function(resolve, reject) {
102 var original = receiver[methodName];
103 if (typeof original !== 'function') {
104 reject('Cannot find method to override: ' + methodName);
105 return;
106 }
107
108 receiver[methodName] = function(var_args) {
109 try {
110 var result = original.apply(this, arguments);
111 } finally {
112 receiver[methodName] = original;
113 }
114 // In case of exception the override won't be called.
115 try {
116 Array.prototype.push.call(arguments, result);
117 resolve.apply(this, arguments);
118 } catch (e) {
119 reject('Exception in overridden method \'' + methodName + '\': ' + e);
120 TestRunner.completeTest();
121 }
122 return result;
123 };
124 });
125 };
126
127 /**
128 * @param {!Array<string>} lazyModules
129 * @return {!Promise<!Array<undefined>>}
130 */
131 TestRunner.loadLazyModules = function(lazyModules) {
132 return Promise.all(lazyModules.map(lazyModule => self.runtime.loadModulePromis e(lazyModule)));
133 };
134
135 /**
136 * @param {string} key
137 * @param {boolean} ctrlKey
138 * @param {boolean} altKey
139 * @param {boolean} shiftKey
140 * @param {boolean} metaKey
141 * @return {!KeyboardEvent}
142 */
143 TestRunner.createKeyEvent = function(key, ctrlKey, altKey, shiftKey, metaKey) {
144 return new KeyboardEvent('keydown', {
145 key: key,
146 bubbles: true,
147 cancelable: true,
148 ctrlKey: ctrlKey,
149 altKey: altKey,
150 shiftKey: shiftKey,
151 metaKey: metaKey
152 });
153 };
154
155 /**
156 * @param {string|!Event} message
157 * @param {string} source
158 * @param {number} lineno
159 * @param {number} colno
160 * @param {!Error} error
161 */
162 function completeTestOnError(message, source, lineno, colno, error) {
163 TestRunner.addResult('TEST ENDED IN ERROR: ' + error.stack);
164 TestRunner.completeTest();
165 }
166
167 self['onerror'] = completeTestOnError;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698