Flaky API Test: TestWebKitAPI.ProcessSwap.PageZoomLevelAfterSwap
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 28 Feb 2019 18:51:55 +0000 (18:51 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 28 Feb 2019 18:51:55 +0000 (18:51 +0000)
https://bugs.webkit.org/show_bug.cgi?id=195107

Reviewed by Alex Christensen.

Give some time for the zoom level to get restored.

* TestWebKitAPI/Tests/WebKitCocoa/ProcessSwapOnNavigation.mm:

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

Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WebKitCocoa/ProcessSwapOnNavigation.mm

index 8836d4b..a416ffb 100644 (file)
@@ -1,3 +1,14 @@
+2019-02-28  Chris Dumez  <cdumez@apple.com>
+
+        Flaky API Test: TestWebKitAPI.ProcessSwap.PageZoomLevelAfterSwap
+        https://bugs.webkit.org/show_bug.cgi?id=195107
+
+        Reviewed by Alex Christensen.
+
+        Give some time for the zoom level to get restored.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/ProcessSwapOnNavigation.mm:
+
 2019-02-28  Sihui Liu  <sihui_liu@apple.com>
 
         Stop using legacy IDB path by default when creating WebProcessPool from websiteDataStore
index 47e9f5b..de44eca 100644 (file)
@@ -2765,20 +2765,39 @@ TEST(ProcessSwap, MainFramesOnly)
 
 #if PLATFORM(MAC)
 
-static const char* sendBodyClientWidth = R"PSONRESOURCE(
+static const char* getClientWidthBytes = R"PSONRESOURCE(
 <body>
 TEST
 <script>
-onload = () => {
-    document.body.offsetTop;    // force layout
-    setTimeout(() => {
-        window.webkit.messageHandlers.pson.postMessage("" + document.body.clientWidth);
-    });
+function getClientWidth()
+{
+    document.body.offsetTop; // Force layout.
+    return document.body.clientWidth;
 }
 </script>
 </body>
 )PSONRESOURCE";
 
+static unsigned waitUntilClientWidthIs(WKWebView *webView, unsigned expectedClientWidth)
+{
+    int timeout = 10;
+    unsigned clientWidth = 0;
+    do {
+        if (timeout != 10)
+            TestWebKitAPI::Util::sleep(0.1);
+
+        [webView evaluateJavaScript:@"getClientWidth()" completionHandler: [&] (id result, NSError *error) {
+            clientWidth = [result integerValue];
+            done = true;
+        }];
+        TestWebKitAPI::Util::run(&done);
+        done = false;
+        --timeout;
+    } while (clientWidth != expectedClientWidth && timeout >= 0);
+
+    return clientWidth;
+}
+
 TEST(ProcessSwap, PageZoomLevelAfterSwap)
 {
     auto processPoolConfiguration = psonProcessPoolConfiguration();
@@ -2787,13 +2806,10 @@ TEST(ProcessSwap, PageZoomLevelAfterSwap)
     auto webViewConfiguration = adoptNS([[WKWebViewConfiguration alloc] init]);
     [webViewConfiguration setProcessPool:processPool.get()];
     auto handler = adoptNS([[PSONScheme alloc] init]);
-    [handler addMappingFromURLString:@"pson://www.webkit.org/main.html" toData:sendBodyClientWidth];
-    [handler addMappingFromURLString:@"pson://www.apple.com/main.html" toData:sendBodyClientWidth];
+    [handler addMappingFromURLString:@"pson://www.webkit.org/main.html" toData:getClientWidthBytes];
+    [handler addMappingFromURLString:@"pson://www.apple.com/main.html" toData:getClientWidthBytes];
     [webViewConfiguration setURLSchemeHandler:handler.get() forURLScheme:@"PSON"];
 
-    auto messageHandler = adoptNS([[PSONMessageHandler alloc] init]);
-    [[webViewConfiguration userContentController] addScriptMessageHandler:messageHandler.get() name:@"pson"];
-
     auto webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:webViewConfiguration.get()]);
     auto delegate = adoptNS([[PSONNavigationDelegate alloc] init]);
     [webView setNavigationDelegate:delegate.get()];
@@ -2806,35 +2822,29 @@ TEST(ProcessSwap, PageZoomLevelAfterSwap)
     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"pson://www.webkit.org/main.html"]];
     [webView loadRequest:request];
 
-    TestWebKitAPI::Util::run(&receivedMessage);
-    receivedMessage = false;
-
-    EXPECT_WK_STREQ(@"400", receivedMessages.get()[0]);
-
     TestWebKitAPI::Util::run(&done);
     done = false;
 
+    unsigned clientWidth = waitUntilClientWidthIs(webView.get(), 400);
+    EXPECT_EQ(400U, clientWidth);
+
     request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"pson://www.apple.com/main.html"]];
     [webView loadRequest:request];
 
-    TestWebKitAPI::Util::run(&receivedMessage);
-    receivedMessage = false;
-
-    EXPECT_WK_STREQ(@"400", receivedMessages.get()[1]);
-
     TestWebKitAPI::Util::run(&done);
     done = false;
 
+    clientWidth = waitUntilClientWidthIs(webView.get(), 400);
+    EXPECT_EQ(400U, clientWidth);
+
     // Kill the WebProcess, the page should reload automatically and the page zoom level should be maintained.
     kill([webView _webProcessIdentifier], 9);
 
-    TestWebKitAPI::Util::run(&receivedMessage);
-    receivedMessage = false;
-
-    EXPECT_WK_STREQ(@"400", receivedMessages.get()[2]);
-
     TestWebKitAPI::Util::run(&done);
     done = false;
+
+    clientWidth = waitUntilClientWidthIs(webView.get(), 400);
+    EXPECT_EQ(400U, clientWidth);
 }
 
 #endif // PLATFORM(MAC)