Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / inspector / console-test.js
blob74da429e4036bfa18f1ceb7ff523bfddff62ee93
1 var initialize_ConsoleTest = function() {
3 InspectorTest.preloadModule("source_frame");
4 InspectorTest.preloadPanel("console");
6 InspectorTest.evaluateInConsole = function(code, callback)
8 callback = InspectorTest.safeWrap(callback);
10 var consoleView = WebInspector.ConsolePanel._view();
11 consoleView.visible = true;
12 consoleView._prompt.setText(code);
13 var event = document.createEvent("KeyboardEvent");
14 event.initKeyboardEvent("keydown", true, true, null, "Enter", "");
15 consoleView._prompt.proxyElementForTests().dispatchEvent(event);
16 InspectorTest.addConsoleViewSniffer(function(commandResult) {
17 callback(commandResult.toMessageElement().deepTextContent());
18 });
21 InspectorTest.addConsoleViewSniffer = function(override, opt_sticky)
23 var sniffer = function (viewMessage) {
24 override(viewMessage);
27 InspectorTest.addSniffer(WebInspector.ConsoleView.prototype, "_consoleMessageAddedForTest", sniffer, opt_sticky);
30 InspectorTest.evaluateInConsoleAndDump = function(code, callback)
32 callback = InspectorTest.safeWrap(callback);
34 function mycallback(text)
36 InspectorTest.addResult(code + " = " + text.replace(/\bVM\d+/g, "VM").replace(/InjectedScript\.\w+ @ VM:\d+/g, ""));
37 callback(text);
39 InspectorTest.evaluateInConsole(code, mycallback);
42 InspectorTest.prepareConsoleMessageText = function(messageElement, consoleMessage)
44 var messageText = messageElement.deepTextContent().replace(/\u200b/g, "");
45 // Replace scriptIds with generic scriptId string to avoid flakiness.
46 messageText = messageText.replace(/VM\d+/g, "VM");
47 // Strip out InjectedScript line numbers from stack traces to avoid rebaselining each time InjectedScriptSource is edited.
48 messageText = messageText.replace(/InjectedScript\.\w+ @ VM:\d+/g, "");
49 // Strip out InjectedScript line numbers from console message anchor.
50 var functionName = consoleMessage && consoleMessage.stackTrace && consoleMessage.stackTrace[0] && consoleMessage.stackTrace[0].functionName || "";
51 if (functionName.indexOf("InjectedScript") !== -1)
52 messageText = messageText.replace(/\bVM:\d+/, ""); // Only first replace.
53 if (messageText.startsWith("Navigated to")) {
54 var fileName = messageText.split(" ").pop().split("/").pop();
55 messageText = "Navigated to " + fileName;
57 // The message might be extremely long in case of dumping stack overflow message.
58 messageText = messageText.substring(0, 1024);
59 return messageText;
62 InspectorTest.disableConsoleViewport = function()
64 InspectorTest.fixConsoleViewportDimensions(600, 2000);
67 InspectorTest.fixConsoleViewportDimensions = function(width, height)
69 var viewport = WebInspector.ConsolePanel._view()._viewport;
70 viewport.element.style.width = width + "px";
71 viewport.element.style.height = height + "px";
72 viewport.element.style.position = "absolute";
73 viewport.invalidate();
76 InspectorTest.consoleMessagesCount = function()
78 var consoleView = WebInspector.ConsolePanel._view();
79 return consoleView._consoleMessages.length;
82 InspectorTest.dumpConsoleMessages = function(printOriginatingCommand, dumpClassNames, formatter)
84 InspectorTest.addResults(InspectorTest.dumpConsoleMessagesIntoArray(printOriginatingCommand, dumpClassNames, formatter));
87 InspectorTest.dumpConsoleMessagesIntoArray = function(printOriginatingCommand, dumpClassNames, formatter)
89 formatter = formatter || InspectorTest.prepareConsoleMessageText;
90 var result = [];
91 InspectorTest.disableConsoleViewport();
92 var consoleView = WebInspector.ConsolePanel._view();
93 if (consoleView._needsFullUpdate)
94 consoleView._updateMessageList();
95 var viewMessages = consoleView._visibleViewMessages;
96 for (var i = 0; i < viewMessages.length; ++i) {
97 var uiMessage = viewMessages[i];
98 var message = uiMessage.consoleMessage();
99 var element = uiMessage.contentElement();
101 if (dumpClassNames) {
102 var classNames = [];
103 for (var node = element.firstChild; node; node = node.traverseNextNode(element)) {
104 if (node.nodeType === Node.ELEMENT_NODE && node.className)
105 classNames.push(node.className.replace("platform-linux", "platform-*").replace("platform-mac", "platform-*").replace("platform-windows", "platform-*"));
109 if (InspectorTest.dumpConsoleTableMessage(uiMessage, false, result)) {
110 if (dumpClassNames)
111 result.push(classNames.join(" > "));
112 } else {
113 var messageText = formatter(element, message);
114 result.push(messageText + (dumpClassNames ? " " + classNames.join(" > ") : ""));
117 if (printOriginatingCommand && uiMessage.consoleMessage().originatingMessage())
118 result.push("Originating from: " + uiMessage.consoleMessage().originatingMessage().messageText);
120 return result;
123 InspectorTest.formatterIgnoreStackFrameUrls = function(messageFormatter, node)
125 function isNotEmptyLine(string)
127 return string.trim().length > 0;
130 function ignoreStackFrameAndMutableData(string)
132 var buffer = string.replace(/\u200b/g, "");
133 buffer = buffer.replace(/VM\d+/g, "VM");
134 buffer = buffer.replace(/InjectedScript\.\w+ @ VM:\d+/g, "");
135 return buffer.replace(/^\s+at [^\]]+(]?)$/, "$1");
138 messageFormatter = messageFormatter || InspectorTest.textContentWithLineBreaks;
139 var buffer = messageFormatter(node);
140 return buffer.split("\n").map(ignoreStackFrameAndMutableData).filter(isNotEmptyLine).join("\n");
143 InspectorTest.dumpConsoleMessagesIgnoreErrorStackFrames = function(printOriginatingCommand, dumpClassNames, messageFormatter)
145 InspectorTest.addResults(InspectorTest.dumpConsoleMessagesIntoArray(printOriginatingCommand, dumpClassNames, InspectorTest.formatterIgnoreStackFrameUrls.bind(this, messageFormatter)));
148 InspectorTest.dumpConsoleTableMessage = function(viewMessage, forceInvalidate, results)
150 if (forceInvalidate)
151 WebInspector.ConsolePanel._view()._viewport.invalidate();
152 var table = viewMessage.contentElement();
153 var headers = table.querySelectorAll("th > div:first-child");
154 if (!headers.length)
155 return false;
157 var headerLine = "";
158 for (var i = 0; i < headers.length; i++)
159 headerLine += headers[i].textContent + " | ";
161 addResult("HEADER " + headerLine);
163 var rows = table.querySelectorAll(".data-container tr");
165 for (var i = 0; i < rows.length; i++) {
166 var row = rows[i];
167 var rowLine = "";
168 var items = row.querySelectorAll("td > span");
169 for (var j = 0; j < items.length; j++)
170 rowLine += items[j].textContent + " | ";
172 if (rowLine.trim())
173 addResult("ROW " + rowLine);
176 function addResult(x)
178 if (results)
179 results.push(x);
180 else
181 InspectorTest.addResult(x);
184 return true;
187 InspectorTest.dumpConsoleMessagesWithStyles = function(sortMessages)
189 var result = [];
190 var messageViews = WebInspector.ConsolePanel._view()._visibleViewMessages;
191 for (var i = 0; i < messageViews.length; ++i) {
192 var element = messageViews[i].contentElement();
193 var messageText = InspectorTest.prepareConsoleMessageText(element)
194 InspectorTest.addResult(messageText);
195 var spans = element.querySelectorAll(".console-message-text > span *");
196 for (var j = 0; j < spans.length; ++j)
197 InspectorTest.addResult("Styled text #" + j + ": " + (spans[j].style.cssText || "NO STYLES DEFINED"));
201 InspectorTest.dumpConsoleMessagesWithClasses = function(sortMessages) {
202 var result = [];
203 var messageViews = WebInspector.ConsolePanel._view()._visibleViewMessages;
204 for (var i = 0; i < messageViews.length; ++i) {
205 var element = messageViews[i].contentElement();
206 var messageText = InspectorTest.prepareConsoleMessageText(element);
207 result.push(messageText + " " + messageViews[i].toMessageElement().getAttribute("class") + " > " + element.getAttribute("class"));
209 if (sortMessages)
210 result.sort();
211 for (var i = 0; i < result.length; ++i)
212 InspectorTest.addResult(result[i]);
215 InspectorTest.dumpConsoleClassesBrief = function()
217 var messageViews = WebInspector.ConsolePanel._view()._visibleViewMessages;
218 for (var i = 0; i < messageViews.length; ++i)
219 InspectorTest.addResult(messageViews[i].toMessageElement().className);
222 InspectorTest.expandConsoleMessages = function(callback, deepFilter, sectionFilter)
224 var messageViews = WebInspector.ConsolePanel._view()._visibleViewMessages;
226 // Initiate round-trips to fetch necessary data for further rendering.
227 for (var i = 0; i < messageViews.length; ++i)
228 messageViews[i].contentElement();
230 InspectorTest.runAfterPendingDispatches(expandTreeElements);
232 function expandTreeElements()
234 for (var i = 0; i < messageViews.length; ++i) {
235 var element = messageViews[i].contentElement();
236 for (var node = element; node; node = node.traverseNextNode(element)) {
237 if (node.treeElementForTest)
238 node.treeElementForTest.expand();
239 if (!node._section)
240 continue;
241 if (sectionFilter && !sectionFilter(node._section))
242 continue;
243 node._section.expand();
245 if (!deepFilter)
246 continue;
247 var treeElements = node._section.rootElement().children();
248 for (var j = 0; j < treeElements.length; ++j) {
249 for (var treeElement = treeElements[j]; treeElement; treeElement = treeElement.traverseNextTreeElement(true, null, true)) {
250 if (deepFilter(treeElement))
251 treeElement.expand();
256 InspectorTest.runAfterPendingDispatches(callback);
260 InspectorTest.expandConsoleMessagesErrorParameters = function(callback)
262 var messageViews = WebInspector.ConsolePanel._view()._visibleViewMessages;
263 // Initiate round-trips to fetch necessary data for further rendering.
264 for (var i = 0; i < messageViews.length; ++i)
265 messageViews[i].contentElement();
266 InspectorTest.runAfterPendingDispatches(expandErrorParameters);
267 function expandErrorParameters()
269 for (var i = 0; i < messageViews.length; ++i) {
270 var element = messageViews[i].contentElement();
271 var spans = element.querySelectorAll("span.object-value-error");
272 for (var j = 0; j < spans.length; ++j) {
273 var links = spans[j].querySelectorAll("a");
274 for (var k = 0; k < links.length; ++k) {
275 var link = links[k];
276 if (link && link._showDetailedForTest)
277 link._showDetailedForTest();
281 callback();
285 InspectorTest.waitForRemoteObjectsConsoleMessages = function(callback)
287 var messages = WebInspector.ConsolePanel._view()._visibleViewMessages;
288 for (var i = 0; i < messages.length; ++i)
289 messages[i].toMessageElement();
290 InspectorTest.runAfterPendingDispatches(callback);
293 InspectorTest.checkConsoleMessagesDontHaveParameters = function()
295 var messageViews = WebInspector.ConsolePanel._view()._visibleViewMessages;
296 for (var i = 0; i < messageViews.length; ++i) {
297 var m = messageViews[i].consoleMessage();
298 InspectorTest.addResult("Message[" + i + "]:");
299 InspectorTest.addResult("Message: " + WebInspector.displayNameForURL(m.url) + ":" + m.line + " " + m.message);
300 if ("_parameters" in m) {
301 if (m._parameters)
302 InspectorTest.addResult("FAILED: message parameters list is not empty: " + m.parameters);
303 else
304 InspectorTest.addResult("SUCCESS: message parameters list is empty. ");
305 } else {
306 InspectorTest.addResult("FAILED: didn't find _parameters field in the message.");
311 InspectorTest.waitUntilMessageReceived = function(callback)
313 InspectorTest.addSniffer(InspectorTest.consoleModel, "addMessage", callback, false);
316 InspectorTest.waitUntilNthMessageReceived = function(count, callback)
318 function override()
320 if (--count === 0)
321 InspectorTest.safeWrap(callback)();
322 else
323 InspectorTest.addSniffer(InspectorTest.consoleModel, "addMessage", override, false);
325 InspectorTest.addSniffer(InspectorTest.consoleModel, "addMessage", override, false);
328 InspectorTest.changeExecutionContext = function(namePrefix)
330 var selector = WebInspector.ConsolePanel._view()._executionContextModel._selectElement;
331 var option = selector.firstChild;
332 while (option) {
333 if (option.textContent && option.textContent.startsWith(namePrefix))
334 break;
335 option = option.nextSibling;
337 if (!option) {
338 InspectorTest.addResult("FAILED: context with prefix: " + namePrefix + " not found in the context list");
339 return;
341 option.selected = true;
342 WebInspector.ConsolePanel._view()._executionContextModel._executionContextChanged();
345 InspectorTest.waitForConsoleMessages = function(expectedCount, callback)
347 var consoleView = WebInspector.ConsolePanel._view();
348 checkAndReturn();
350 function checkAndReturn()
352 if (consoleView._visibleViewMessages.length === expectedCount) {
353 InspectorTest.addResult("Message count: " + expectedCount);
354 callback();
355 } else {
356 InspectorTest.addSniffer(consoleView, "_messageAppendedForTests", checkAndReturn);