Web Inspector: inspector/page/setShowPaintRects.html is a flakey failure since introduction
https://bugs.webkit.org/show_bug.cgi?id=233047
rdar://85345706

Reviewed by Antoine Quint.

Adding test results to the page causes a paint to be scheduled, which can cause paint rects to be requested at times we
are not expecting them as part of the flow of the test. We expect only our explicit change to an element to cause paints
to occur, but currently those paints end up interleaved with paints for adding results to the test page.

To resolve this, we now defer writing results to the test page until the end of each test case. This allows us to wait
to start each test case until there are no paint rects, and to avoid accidently causing a paint rect to appear at
inopportune times for our testing.

* LayoutTests/inspector/page/setShowPaintRects-expected.txt:
* LayoutTests/inspector/page/setShowPaintRects.html:
* LayoutTests/platform/mac/TestExpectations:

Canonical link: https://commits.webkit.org/251585@main


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@295580 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/LayoutTests/inspector/page/setShowPaintRects-expected.txt b/LayoutTests/inspector/page/setShowPaintRects-expected.txt
index 1e45c31..bbba181 100644
--- a/LayoutTests/inspector/page/setShowPaintRects-expected.txt
+++ b/LayoutTests/inspector/page/setShowPaintRects-expected.txt
@@ -2,12 +2,16 @@
 
 
 == Running test suite: Page.setShowPaintRects
+-- Running test setup.
 -- Running test case: Page.setShowPaintRects.Enabled
 PASS: Should not have paint rects displayed.
 PASS: Should have paint rects displayed.
 PASS: Should not have paint rects displayed.
+-- Running test teardown.
 
+-- Running test setup.
 -- Running test case: Page.setShowPaintRects.Disabled
 PASS: Should not have paint rects displayed.
 PASS: Should not have had any paint rects displayed.
+-- Running test teardown.
 
diff --git a/LayoutTests/inspector/page/setShowPaintRects.html b/LayoutTests/inspector/page/setShowPaintRects.html
index a0c840d..e29fcd9 100644
--- a/LayoutTests/inspector/page/setShowPaintRects.html
+++ b/LayoutTests/inspector/page/setShowPaintRects.html
@@ -17,6 +17,21 @@
 
 function test()
 {
+    // Because adding test results to the page also causes painting (of the new result elements) we need to defer adding
+    // results to the page until we are between test cases.
+    let deferredTestResults = [];
+
+    const inspectorTestOriginalAddResult = InspectorTest.addResult.bind(InspectorTest);
+    InspectorTest.addResult = function(result) {
+        deferredTestResults.push(result);
+    }
+
+    function writeDeferredTestOutputToPage() {
+        for (let result of deferredTestResults)
+            inspectorTestOriginalAddResult(result);
+        deferredTestResults = [];
+    }
+
     async function expectHasPaintRectsDidChangeTo(expected) {
         await InspectorTest.awaitEvent("TestHasPaintRectsDidChange");
         await expectHasPaintRects(expected);
@@ -27,11 +42,27 @@
         InspectorTest.expectEqual(hasPaintRects, expected, `Should ${expected ? "" : "not "}have paint rects displayed.`);
     }
 
+    async function setup() {
+        await PageAgent.setShowPaintRects(false);
+
+        // Existing paint rects still need to fade out, so it isn't safe to assume we will immediately have zero paint
+        // rects visible.
+        if (!(await InspectorTest.evaluateInPage(`window._test_hasPaintRects`)))
+            return;
+        await InspectorTest.awaitEvent("TestHasPaintRectsDidChange");
+    }
+
+    async function teardown() {
+        writeDeferredTestOutputToPage();
+    }
+
     let suite = InspectorTest.createAsyncSuite("Page.setShowPaintRects");
 
     suite.addTestCase({
         name: "Page.setShowPaintRects.Enabled",
         description: "Test that paint rects are correctly created and removed while enabled.",
+        setup,
+        teardown,
         async test() {
             await PageAgent.setShowPaintRects(true);
             await expectHasPaintRects(false);
@@ -47,6 +78,8 @@
     suite.addTestCase({
         name: "Page.setShowPaintRects.Disabled",
         description: "Test that paint rects are not created while disabled.",
+        setup,
+        teardown,
         async test() {
             await PageAgent.setShowPaintRects(false);
             await expectHasPaintRects(false);
diff --git a/LayoutTests/platform/mac/TestExpectations b/LayoutTests/platform/mac/TestExpectations
index b847ebe..cb89b02 100644
--- a/LayoutTests/platform/mac/TestExpectations
+++ b/LayoutTests/platform/mac/TestExpectations
@@ -2245,8 +2245,6 @@
 
 webkit.org/b/237172 imported/w3c/web-platform-tests/speech-api/SpeechSynthesis-speak-twice.html [ Pass Failure Crash ]
 
-webkit.org/b/233047 inspector/page/setShowPaintRects.html [ Pass Failure ]
-
 webkit.org/b/237680 imported/w3c/web-platform-tests/service-workers/service-worker/partitioned-service-worker-matchAll.tentative.https.html [ Pass Failure ]
 
 # Plugins