Web Inspector: support `console.screenshot` with dataURL strings
authordrousso@apple.com <drousso@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 14 Aug 2019 21:59:24 +0000 (21:59 +0000)
committerdrousso@apple.com <drousso@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 14 Aug 2019 21:59:24 +0000 (21:59 +0000)
https://bugs.webkit.org/show_bug.cgi?id=199307

Reviewed by Joseph Pecoraro.

Source/WebCore:

Test: inspector/console/console-screenshot.html

* page/PageConsoleClient.cpp:
(WebCore::PageConsoleClient::screenshot):

LayoutTests:

* inspector/console/console-screenshot.html:
* inspector/console/console-screenshot-expected.txt:

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

LayoutTests/ChangeLog
LayoutTests/inspector/console/console-screenshot-expected.txt
LayoutTests/inspector/console/console-screenshot.html
Source/WebCore/ChangeLog
Source/WebCore/page/PageConsoleClient.cpp

index 32c36a2..6625a84 100644 (file)
@@ -1,3 +1,13 @@
+2019-08-14  Devin Rousso  <drousso@apple.com>
+
+        Web Inspector: support `console.screenshot` with dataURL strings
+        https://bugs.webkit.org/show_bug.cgi?id=199307
+
+        Reviewed by Joseph Pecoraro.
+
+        * inspector/console/console-screenshot.html:
+        * inspector/console/console-screenshot-expected.txt:
+
 2019-08-14  Ryan Haddad  <ryanhaddad@apple.com>
 
         [WebAuthN] Enable LocalAuthenticator for macOS
index 3c020ea..af8acf7 100644 (file)
@@ -6,6 +6,9 @@ CONSOLE MESSAGE: [object HTMLDivElement]
 CONSOLE MESSAGE: [object ImageData]
 CONSOLE MESSAGE: [object ImageBitmap]
 CONSOLE MESSAGE: [object CanvasRenderingContext2D]
+CONSOLE MESSAGE: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAAXNSR0IArs4c6QAAABNJREFUCB1j/M/AAEQMDEwgAgQAHxcCAmtAm/sAAAAASUVORK5CYII=
+CONSOLE MESSAGE: Viewport
+CONSOLE MESSAGE: Viewport
 CONSOLE MESSAGE: Viewport
 CONSOLE MESSAGE: Viewport
 Tests for the console.screenshot API.
@@ -57,6 +60,24 @@ PASS: The image should not be empty.
 PASS: The image width should be 2px.
 PASS: The image height should be 2px.
 
+-- Running test case: console.screenshot.String.Base64.Valid
+PASS: The added message should be an image.
+PASS: The image should not be empty.
+PASS: The image width should be 2px.
+PASS: The image height should be 2px.
+
+-- Running test case: console.screenshot.String.Base64.NoContent
+PASS: The added message should be an image.
+PASS: The image should not be empty.
+PASS: The image width should be greater than 2px.
+PASS: The image height should be greater than 2px.
+
+-- Running test case: console.screenshot.String.Base64.Invalid
+PASS: The added message should be an image.
+PASS: The image should not be empty.
+PASS: The image width should be greater than 2px.
+PASS: The image height should be greater than 2px.
+
 -- Running test case: console.screenshot.NonScreenshotableTarget
 PASS: The added message should be an image.
 PASS: The image should not be empty.
index b56b3c9..b64fcc7 100644 (file)
@@ -52,32 +52,33 @@ function test()
 {
     let suite = InspectorTest.createAsyncSuite("console.screenshot");
 
-    function addTest({name, expression, imageMessageAddedCallback, shouldError}) {
+    function addTest({name, expression, imageMessageAddedCallback, shouldCaptureViewport, shouldError}) {
         suite.addTestCase({
             name,
-            test(resolve, reject) {
-                let listener = WI.consoleManager.addEventListener(WI.ConsoleManager.Event.MessageAdded, async (event) => {
-                    let {message} = event.data;
-
-                    let isError = message.level === WI.ConsoleMessage.MessageLevel.Error;
-                    if (isError || message.type === WI.ConsoleMessage.MessageType.Image) {
-                        WI.consoleManager.removeEventListener(WI.ConsoleManager.Event.MessageAdded, listener);
-
-                        if (isError)
-                            InspectorTest.expectThat(shouldError, message.messageText);
-                        else
-                            InspectorTest.expectEqual(message.type, WI.ConsoleMessage.MessageType.Image, "The added message should be an image.");
-
-                        if (imageMessageAddedCallback)
-                            await imageMessageAddedCallback(message);
-
-                        resolve();
-                        return;
-                    }
-                });
-
-                InspectorTest.evaluateInPage(expression)
-                .catch(reject);
+            async test() {
+                let [event] = await Promise.all([
+                    WI.consoleManager.awaitEvent(WI.ConsoleManager.Event.MessageAdded),
+                    InspectorTest.evaluateInPage(expression),
+                ]);
+
+                let {message} = event.data;
+
+                if (message.level === WI.ConsoleMessage.MessageLevel.Error) {
+                    InspectorTest.expectThat(shouldError, message.messageText);
+                    return;
+                }
+
+                InspectorTest.expectEqual(message.type, WI.ConsoleMessage.MessageType.Image, "The added message should be an image.");
+                InspectorTest.expectNotEqual(message.messageText, "data:", "The image should not be empty.");
+
+                let img = await WI.ImageUtilities.promisifyLoad(message.messageText);
+                if (shouldCaptureViewport) {
+                    InspectorTest.expectGreaterThan(img.width, 2, "The image width should be greater than 2px.");
+                    InspectorTest.expectGreaterThan(img.height, 2, "The image height should be greater than 2px.");
+                } else {
+                    InspectorTest.expectEqual(img.width, 2, "The image width should be 2px.");
+                    InspectorTest.expectEqual(img.height, 2, "The image height should be 2px.");
+                }
             },
         });
     }
@@ -85,49 +86,21 @@ function test()
     addTest({
         name: "console.screenshot.Node.SingleArgument",
         expression: `console.screenshot(document.querySelector("#testNode"))`,
-        async imageMessageAddedCallback(message) {
-            InspectorTest.expectNotEqual(message.messageText, "data:", "The image should not be empty.");
-
-            let img = await WI.ImageUtilities.promisifyLoad(message.messageText);
-            InspectorTest.expectEqual(img.width, 2, "The image width should be 2px.");
-            InspectorTest.expectEqual(img.height, 2, "The image height should be 2px.");
-        },
     });
 
     addTest({
         name: "console.screenshot.Node.MultipleArguments",
         expression: `console.screenshot(document.querySelector("#testNode"), "test")`,
-        async imageMessageAddedCallback(message) {
-            InspectorTest.expectNotEqual(message.messageText, "data:", "The image should not be empty.");
-
-            let img = await WI.ImageUtilities.promisifyLoad(message.messageText);
-            InspectorTest.expectEqual(img.width, 2, "The image width should be 2px.");
-            InspectorTest.expectEqual(img.height, 2, "The image height should be 2px.");
-        },
     });
 
     addTest({
         name: "console.screenshot.Node.DetachedScreenshotable.Image",
         expression: `testHTMLImageElement()`,
-        async imageMessageAddedCallback(message) {
-            InspectorTest.expectNotEqual(message.messageText, "data:", "The image should not be empty.");
-
-            let img = await WI.ImageUtilities.promisifyLoad(message.messageText);
-            InspectorTest.expectEqual(img.width, 2, "The image width should be 2px.");
-            InspectorTest.expectEqual(img.height, 2, "The image height should be 2px.");
-        },
     });
 
     addTest({
         name: "console.screenshot.Node.DetachedScreenshotable.Picture",
         expression: `testHTMLPictureElement()`,
-        async imageMessageAddedCallback(message) {
-            InspectorTest.expectNotEqual(message.messageText, "data:", "The image should not be empty.");
-
-            let img = await WI.ImageUtilities.promisifyLoad(message.messageText);
-            InspectorTest.expectEqual(img.width, 2, "The image width should be 2px.");
-            InspectorTest.expectEqual(img.height, 2, "The image height should be 2px.");
-        },
     });
 
     addTest({
@@ -139,61 +112,45 @@ function test()
     addTest({
         name: "console.screenshot.ImageData",
         expression: `console.screenshot(new ImageData(2, 2))`,
-        async imageMessageAddedCallback(message) {
-            InspectorTest.expectNotEqual(message.messageText, "data:", "The image should not be empty.");
-
-            let img = await WI.ImageUtilities.promisifyLoad(message.messageText);
-            InspectorTest.expectEqual(img.width, 2, "The image width should be 2px.");
-            InspectorTest.expectEqual(img.height, 2, "The image height should be 2px.");
-        },
     });
 
     addTest({
         name: "console.screenshot.ImageBitmap",
         expression: `testImageBitmap()`,
-        async imageMessageAddedCallback(message) {
-            InspectorTest.expectNotEqual(message.messageText, "data:", "The image should not be empty.");
-
-            let img = await WI.ImageUtilities.promisifyLoad(message.messageText);
-            InspectorTest.expectEqual(img.width, 2, "The image width should be 2px.");
-            InspectorTest.expectEqual(img.height, 2, "The image height should be 2px.");
-        },
     });
 
     addTest({
         name: "console.screenshot.CanvasRenderingContext2D",
         expression: `console.screenshot(createCanvas2DTest())`,
-        async imageMessageAddedCallback(message) {
-            InspectorTest.expectNotEqual(message.messageText, "data:", "The image should not be empty.");
+    });
+
+    addTest({
+        name: "console.screenshot.String.Base64.Valid",
+        expression: `console.screenshot("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAAXNSR0IArs4c6QAAABNJREFUCB1j/M/AAEQMDEwgAgQAHxcCAmtAm/sAAAAASUVORK5CYII=")`, // 2x2 red square
+    });
+
+    addTest({
+        name: "console.screenshot.String.Base64.NoContent",
+        expression: `console.screenshot("data:image/png;a1=b2;base64,")`,
+        shouldCaptureViewport: true,
+    });
 
-            let img = await WI.ImageUtilities.promisifyLoad(message.messageText);
-            InspectorTest.expectEqual(img.width, 2, "The image width should be 2px.");
-            InspectorTest.expectEqual(img.height, 2, "The image height should be 2px.");
-        },
+    addTest({
+        name: "console.screenshot.String.Base64.Invalid",
+        expression: `console.screenshot("invalid")`,
+        shouldCaptureViewport: true,
     });
 
     addTest({
         name: "console.screenshot.NonScreenshotableTarget",
         expression: `console.screenshot(42)`,
-        async imageMessageAddedCallback(message) {
-            InspectorTest.expectNotEqual(message.messageText, "data:", "The image should not be empty.");
-
-            let img = await WI.ImageUtilities.promisifyLoad(message.messageText);
-            InspectorTest.expectGreaterThan(img.width, 2, "The image width should be greater than 2px.");
-            InspectorTest.expectGreaterThan(img.height, 2, "The image height should be greater than 2px.");
-        },
+        shouldCaptureViewport: true,
     });
 
     addTest({
         name: "console.screenshot.NoArguments",
         expression: `console.screenshot()`,
-        async imageMessageAddedCallback(message) {
-            InspectorTest.expectNotEqual(message.messageText, "data:", "The image should not be empty.");
-
-            let img = await WI.ImageUtilities.promisifyLoad(message.messageText);
-            InspectorTest.expectGreaterThan(img.width, 2, "The image width should be greater than 2px.");
-            InspectorTest.expectGreaterThan(img.height, 2, "The image height should be greater than 2px.");
-        },
+        shouldCaptureViewport: true,
     });
 
     suite.runTestCasesAndFinish();
index 0e8919d..0875f1f 100644 (file)
@@ -1,3 +1,15 @@
+2019-08-14  Devin Rousso  <drousso@apple.com>
+
+        Web Inspector: support `console.screenshot` with dataURL strings
+        https://bugs.webkit.org/show_bug.cgi?id=199307
+
+        Reviewed by Joseph Pecoraro.
+
+        Test: inspector/console/console-screenshot.html
+
+        * page/PageConsoleClient.cpp:
+        (WebCore::PageConsoleClient::screenshot):
+
 2019-08-14  Zalan Bujtas  <zalan@apple.com>
 
         [ContentChangeObserver] Stop content change observation explicitly.
index d032fcf..1891e95 100644 (file)
@@ -64,6 +64,7 @@
 #include "Settings.h"
 #include <JavaScriptCore/ConsoleMessage.h>
 #include <JavaScriptCore/JSCInlines.h>
+#include <JavaScriptCore/RegularExpression.h>
 #include <JavaScriptCore/ScriptArguments.h>
 #include <JavaScriptCore/ScriptCallStack.h>
 #include <JavaScriptCore/ScriptCallStackFactory.h>
@@ -367,6 +368,15 @@ void PageConsoleClient::screenshot(JSC::ExecState* state, Ref<ScriptArguments>&&
             }
 
             // FIXME: <https://webkit.org/b/180833> Web Inspector: support OffscreenCanvas for Canvas related operations
+        } else {
+            String base64;
+            if (possibleTarget.getString(state, base64)) {
+                JSC::Yarr::RegularExpression regex("^data:image/(?:[^;]+;)+base64,.+$"_s, JSC::Yarr::TextCaseSensitivity::TextCaseInsensitive);
+                if (regex.match(base64) != -1) {
+                    target = possibleTarget;
+                    dataURL = base64;
+                }
+            }
         }
     }