Web Inspector: inspector tests should redirect and log console.trace() calls in test...
authorbburg@apple.com <bburg@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 17 May 2016 22:04:48 +0000 (22:04 +0000)
committerbburg@apple.com <bburg@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 17 May 2016 22:04:48 +0000 (22:04 +0000)
https://bugs.webkit.org/show_bug.cgi?id=157802
<rdar://problem/26325671>

Reviewed by Timothy Hatcher.

* UserInterface/Test/FrontendTestHarness.js:
(FrontendTestHarness.prototype.redirectConsoleToTestOutput.createProxyConsoleHandler):
Remove bind() that is now unnecessary. Also redirect console.warn.

(FrontendTestHarness.prototype.redirectConsoleToTestOutput):
For console.trace(), throw and catch a dummy Error to get a stack trace.
Do some post processing on it to remove useless frames and sanitize file paths.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@201048 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebInspectorUI/ChangeLog
Source/WebInspectorUI/UserInterface/Test/FrontendTestHarness.js

index d83a55d..e51ce36 100644 (file)
@@ -1,5 +1,21 @@
 2016-05-17  Brian Burg  <bburg@apple.com>
 
+        Web Inspector: inspector tests should redirect and log console.trace() calls in test output
+        https://bugs.webkit.org/show_bug.cgi?id=157802
+        <rdar://problem/26325671>
+
+        Reviewed by Timothy Hatcher.
+
+        * UserInterface/Test/FrontendTestHarness.js:
+        (FrontendTestHarness.prototype.redirectConsoleToTestOutput.createProxyConsoleHandler):
+        Remove bind() that is now unnecessary. Also redirect console.warn.
+
+        (FrontendTestHarness.prototype.redirectConsoleToTestOutput):
+        For console.trace(), throw and catch a dummy Error to get a stack trace.
+        Do some post processing on it to remove useless frames and sanitize file paths.
+
+2016-05-17  Brian Burg  <bburg@apple.com>
+
         Web Inspector: Filtering huge data grids should yield occasionally so the UI remains responsive
         https://bugs.webkit.org/show_bug.cgi?id=157702
         <rdar://problem/26282898>
index 3a1dee1..d3b78a5 100644 (file)
@@ -134,17 +134,51 @@ FrontendTestHarness = class FrontendTestHarness extends TestHarness
         let self = this;
         function createProxyConsoleHandler(type) {
             return function() {
-                self.addResult(`${type}: ` + Array.from(arguments).join(" ")); 
+                self.addResult(`${type}: ` + Array.from(arguments).join(" "));
             };
         }
 
+        function createProxyConsoleTraceHandler(){
+            return function() {
+                try {
+                    throw new Exception();
+                } catch (e) {
+                    // Skip the first frame which is added by this function.
+                    let frames = e.stack.split("\n").slice(1);
+                    let sanitizedFrames = frames.map((frame, i) => {
+                        // Most frames are of the form "functionName@file:///foo/bar/File.js:345".
+                        // But, some frames do not have a functionName. Get rid of the file path.
+                        let nameAndURLSeparator = frame.indexOf("@");
+                        let frameName = (nameAndURLSeparator > 0) ? frame.substr(0, nameAndURLSeparator) : "(anonymous)";
+
+                        let lastPathSeparator = Math.max(frame.lastIndexOf("/"), frame.lastIndexOf("\\"));
+                        let frameLocation = (lastPathSeparator > 0) ? frame.substr(lastPathSeparator + 1) : frame;
+                        if (!frameLocation.length)
+                            frameLocation = "unknown";
+
+                        // Clean up the location so it is bracketed or in parenthesis.
+                        if (frame.indexOf("[native code]") !== -1)
+                            frameLocation = "[native code]";
+                        else
+                            frameLocation = "(" + frameLocation + ")"
+
+                        return `#${i}: ${frameName} ${frameLocation}`;
+                    });
+                    self.addResult("TRACE: " + Array.from(arguments).join(" "));
+                    self.addResult(sanitizedFrames.join("\n"));
+                }
+            }
+        }
+
         let redirectedMethods = {};
-        for (let key in window.console) 
-            redirectedMethods[key] = window.console[key].bind(window.console);
+        for (let key in window.console)
+            redirectedMethods[key] = window.console[key];
 
-        for (let type of ["log", "error", "info"])
+        for (let type of ["log", "error", "info", "warn"])
             redirectedMethods[type] = createProxyConsoleHandler(type.toUpperCase());
 
+        redirectedMethods["trace"] = createProxyConsoleTraceHandler();
+
         this._originalConsole = window.console;
         window.console = redirectedMethods;
     }