Title: [294975] trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/TestSOAuthorization.mm
Revision
294975
Author
j_pas...@apple.com
Date
2022-05-27 18:46:33 -0700 (Fri, 27 May 2022)

Log Message

[ iOS ] TestWebKitAPI.SOAuthorizationSubFrame.InterceptionErrorWithReferrer is a flaky timeout
https://bugs.webkit.org/show_bug.cgi?id=239311
<rdar://91723056>

Reviewed by Brent Fulgham.

There is a race condition when using waitForMessage that we hit here, causing flaky tests on
some bots. This patch avoids this by specifying the messages waited for before loading the
request.

* Tools/TestWebKitAPI/Tests/WebKitCocoa/TestSOAuthorization.mm:
(-[TestSOAuthorizationScriptMessageHandler initWithExpectation:]):
(-[TestSOAuthorizationScriptMessageHandler userContentController:didReceiveScriptMessage:]):
(TestWebKitAPI::TEST):

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

Modified Paths

Diff

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/TestSOAuthorization.mm (294974 => 294975)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/TestSOAuthorization.mm	2022-05-28 01:23:18 UTC (rev 294974)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/TestSOAuthorization.mm	2022-05-28 01:46:33 UTC (rev 294975)
@@ -250,28 +250,24 @@
 
 @implementation TestSOAuthorizationScriptMessageHandler {
     RetainPtr<NSMutableArray> _messages;
+    RetainPtr<NSArray> _expectedMessages;
 }
 
+- (instancetype)initWithExpectation:(NSArray *)expectedMessages
+{
+    _messages = adoptNS([[NSMutableArray alloc] init]);
+    _expectedMessages = expectedMessages;
+    return self;
+}
+
 - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
 {
-    if (!_messages)
-        _messages = adoptNS([[NSMutableArray alloc] init]);
+    auto curIndex = [_messages count];
     [_messages addObject:message.body];
-
-    if ([message.body isEqual:@""]) {
+    if ([_messages count] == [_expectedMessages count])
         allMessagesReceived = true;
-        EXPECT_EQ([_messages count], 5u);
-        EXPECT_WK_STREQ("SOAuthorizationDidStart", [_messages objectAtIndex:1]);
-        EXPECT_WK_STREQ("SOAuthorizationDidCancel", [_messages objectAtIndex:3]);
-        EXPECT_WK_STREQ("", [_messages objectAtIndex:4]);
-    }
-
-    if ([message.body isEqual:@"Hello."]) {
-        allMessagesReceived = true;
-        EXPECT_EQ([_messages count], 4u);
-        EXPECT_WK_STREQ("SOAuthorizationDidStart", [_messages objectAtIndex:1]);
-        EXPECT_WK_STREQ("Hello.", [_messages objectAtIndex:3]);
-    }
+    if (curIndex < [_expectedMessages count] && [_expectedMessages objectAtIndex:curIndex] != [NSNull null])
+        EXPECT_WK_STREQ([_expectedMessages objectAtIndex:curIndex], [_messages objectAtIndex:curIndex]);
 }
 
 @end
@@ -2635,7 +2631,7 @@
 }
 
 // FIXME: https://bugs.webkit.org/show_bug.cgi?id=239311
-TEST(SOAuthorizationSubFrame, DISABLED_InterceptionErrorWithReferrer)
+TEST(SOAuthorizationSubFrame, InterceptionErrorWithReferrer)
 {
     resetState();
     ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast<IMP>(overrideCanPerformAuthorizationWithURL));
@@ -2668,21 +2664,22 @@
             });
         });
     });
+    auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    auto origin = makeString("http://127.0.0.1:", server.port());
 
-    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]);
+    auto messageHandler = adoptNS([[TestSOAuthorizationScriptMessageHandler alloc] initWithExpectation:@[origin, @"SOAuthorizationDidStart", origin, @"SOAuthorizationDidCancel", origin, @"Hello.", origin, makeString("Referrer: ", origin, "/")]]);
+    [[configuration userContentController] addScriptMessageHandler:messageHandler.get() name:@"testHandler"];
+    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500) configuration:configuration.get()]);
     auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]);
     configureSOAuthorizationWebView(webView.get(), delegate.get());
 
-    auto origin = makeString("http://127.0.0.1:", server.port());
     [webView _loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:(id)origin]] shouldOpenExternalURLs:NO];
-    [webView waitForMessage:(id)origin];
-    [webView waitForMessage:@"SOAuthorizationDidStart"];
+
+    Util::run(&authorizationPerformed);
     EXPECT_TRUE(policyForAppSSOPerformed);
 
     [gDelegate authorization:gAuthorization didCompleteWithError:adoptNS([[NSError alloc] initWithDomain:NSCocoaErrorDomain code:0 userInfo:nil]).get()];
-    [webView waitForMessage:(id)origin];
-    [webView waitForMessage:@"SOAuthorizationDidCancel"];
-    [webView waitForMessage:(id)makeString("Referrer: ", origin, "/")]; // Referrer policy requires '/' after origin.
+    Util::run(&allMessagesReceived);
 }
 
 TEST(SOAuthorizationSubFrame, InterceptionErrorMessageOrder)
@@ -2699,7 +2696,7 @@
     auto testHtml = generateHtml(parentTemplate, testURL.get().absoluteString);
 
     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
-    auto messageHandler = adoptNS([[TestSOAuthorizationScriptMessageHandler alloc] init]);
+    auto messageHandler = adoptNS([[TestSOAuthorizationScriptMessageHandler alloc] initWithExpectation:@[[NSNull null], @"SOAuthorizationDidStart", [NSNull null], @"SOAuthorizationDidCancel", @""]]);
     [[configuration userContentController] addScriptMessageHandler:messageHandler.get() name:@"testHandler"];
 
     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500) configuration:configuration.get()]);
@@ -2726,7 +2723,7 @@
     auto testHtml = generateHtml(parentTemplate, testURL.string());
 
     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
-    auto messageHandler = adoptNS([[TestSOAuthorizationScriptMessageHandler alloc] init]);
+    auto messageHandler = adoptNS([[TestSOAuthorizationScriptMessageHandler alloc] initWithExpectation:@[[NSNull null], @"SOAuthorizationDidStart", [NSNull null], @"Hello."]]);
     [[configuration userContentController] addScriptMessageHandler:messageHandler.get() name:@"testHandler"];
 
     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500) configuration:configuration.get()]);
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to