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

Side by Side Diff: third_party/WebKit/LayoutTests/http/tests/inspector-unit/inspector-unit-test.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 var UnitTest = {};
2 (function()
3 {
4 var lazyModules = [];
5 var oldLoadResourcePromise = Runtime.loadResourcePromise;
6 Runtime.loadResourcePromise = function(url)
7 {
8 if (url.startsWith("/"))
9 return oldLoadResourcePromise(url);
10
11 if (!url.startsWith("http://"))
12 return oldLoadResourcePromise("/inspector-debug/" + url);
13
14 var parsedURL = new URL(url, location.href);
15 var parsedLocation = new URL(location.href);
16
17 // hosted devtools is confused.
18 parsedURL.pathname = parsedURL.pathname.replace('/inspector-unit/', '/in spector-debug/');
19 return oldLoadResourcePromise(parsedURL.toString());
20 }
21
22 if (window.testRunner) {
23 testRunner.dumpAsText();
24 testRunner.waitUntilDone();
25 }
26
27 var results = [];
28 UnitTest.completeTest = function()
29 {
30 if (!window.testRunner) {
31 console.log("Test Done");
32 return;
33 }
34
35 document.documentElement.childNodes.forEach(x => x.remove());
36 var outputElement = document.createElement("div");
37 // Support for svg - add to document, not body, check for style.
38 if (outputElement.style) {
39 outputElement.style.whiteSpace = "pre";
40 outputElement.style.height = "10px";
41 outputElement.style.overflow = "hidden";
42 }
43 document.documentElement.appendChild(outputElement);
44 for (var i = 0; i < results.length; i++) {
45 outputElement.appendChild(document.createTextNode(results[i]));
46 outputElement.appendChild(document.createElement("br"));
47 }
48 results = [];
49 testRunner.notifyDone();
50 };
51
52 UnitTest.addResult = function(text)
53 {
54 if (window.testRunner)
55 results.push(String(text));
56 else
57 console.log(text);
58 };
59
60 UnitTest.runTests = function(tests)
61 {
62 nextTest();
63
64 function nextTest()
65 {
66 var test = tests.shift();
67 if (!test) {
68 UnitTest.completeTest();
69 return;
70 }
71 UnitTest.addResult("\ntest: " + test.name);
72 var testPromise = test();
73 if (!(testPromise instanceof Promise))
74 testPromise = Promise.resolve();
75 testPromise.then(nextTest);
76 }
77 };
78
79 UnitTest.addSniffer = function(receiver, methodName)
80 {
81 return new Promise(function(resolve, reject)
82 {
83 var original = receiver[methodName];
84 if (typeof original !== "function") {
85 reject("Cannot find method to override: " + methodName);
86 return;
87 }
88
89 receiver[methodName] = function(var_args)
90 {
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 overriden method '" + methodName + "': " + e);
102 UnitTest.completeTest();
103 }
104 return result;
105 };
106 });
107 };
108
109 UnitTest.addDependency = function(lazyModule)
110 {
111 lazyModules.push(lazyModule);
112 };
113
114 UnitTest.createKeyEvent = function(key, ctrlKey, altKey, shiftKey, metaKey)
115 {
116 return new KeyboardEvent("keydown", { key: key, bubbles: true, cancelabl e: true, ctrlKey: ctrlKey, altKey: altKey, shiftKey: shiftKey, metaKey: metaKey });
117 };
118
119 function completeTestOnError(message, source, lineno, colno, error)
120 {
121 UnitTest.addResult("TEST ENDED IN ERROR: " + error.stack);
122 UnitTest.completeTest();
123 }
124 window.onerror = completeTestOnError;
125
126 Runtime.startApplication("/inspector-unit/inspector-unit-test").then(runTest );
127
128 function runTest()
129 {
130 var description = document.body.textContent.trim();
131 if (description)
132 UnitTest.addResult(description);
133
134 Common.settings = new Common.Settings(new Common.SettingsStorage(
135 {},
136 InspectorFrontendHost.setPreference,
137 InspectorFrontendHost.removePreference,
138 InspectorFrontendHost.clearPreferences));
139
140
141 UI.viewManager = new UI.ViewManager();
142 UI.initializeUIUtils(document, Common.settings.createSetting("uiTheme", "default"));
143 UI.installComponentRootStyles(document.body);
144
145 UI.zoomManager = new UI.ZoomManager(window, InspectorFrontendHost);
146 UI.inspectorView = UI.InspectorView.instance();
147 UI.ContextMenu.initialize();
148 UI.ContextMenu.installHandler(document);
149 UI.Tooltip.installHandler(document);
150
151 var rootView = new UI.RootView();
152 UI.inspectorView.show(rootView.element);
153 rootView.attachToDocument(document);
154 Promise.all(lazyModules.map(lazyModule => window.runtime.loadModulePromi se(lazyModule))).then(test);
155 }
156 })();
157
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698