COMMIT_MESSAGE

 1REGRESSION (Ventura): [ macOS ] 2X TestWebKitAPI.SOAuthorizationRedirect.DismissUIDuringMiniaturization (API-Tests) are constant failures
 2https://bugs.webkit.org/show_bug.cgi?id=247924
 3rdar://problem/102343029
 4
 5Reviewed by NOBODY (OOPS!).
 6
 7The test assumed that actions to NSWindows or NSApp reflect synchronously
 8back to the notifications. This is not the case.
 9
 10Ensure that the test expects asynchronous operation.
 11
 12Fix incorrect adoptNS() for notification center observers. The add function is
 13not a NS creation function, and as such it doesn't return an adoptable ref.
 14
 15* Tools/TestWebKitAPI/Tests/WebKitCocoa/SOAuthorizationTests.mm:
 16(TestWebKitAPI::TEST):

Tools/TestWebKitAPI/Tests/WebKitCocoa/SOAuthorizationTests.mm

@@static const char* samlResponse =
129129"</script>"
130130"</html>";
131131
 132static constexpr Seconds actionAbsenceTimeout = 300_ms;
 133static constexpr Seconds actionDoneTimeout = 1000_ms;
 134
 135namespace {
 136
 137class ScopedNotificationCenterObserveOnce {
 138public:
 139 ScopedNotificationCenterObserveOnce() = default;
 140 ScopedNotificationCenterObserveOnce(NSNotificationName name, NSObject* object)
 141 {
 142 m_observer = [m_center addObserverForName:name object:object queue:nil usingBlock:[this] (NSNotification *) {
 143 [m_center removeObserver:m_observer.get()];
 144 m_observer = nullptr;
 145 m_didObserve = true;
 146 m_center = nullptr;
 147 }];
 148 }
 149 ~ScopedNotificationCenterObserveOnce()
 150 {
 151 if (!m_observer)
 152 return;
 153 [m_center removeObserver: m_observer.get()];
 154 }
 155 ScopedNotificationCenterObserveOnce(ScopedNotificationCenterObserveOnce&&) = default;
 156 ScopedNotificationCenterObserveOnce& operator=(ScopedNotificationCenterObserveOnce&&) = default;
 157
 158 bool waitFor(Seconds timeout)
 159 {
 160 return TestWebKitAPI::Util::runFor(&m_didObserve, timeout);
 161 }
 162
 163 operator bool() const { return m_didObserve; }
 164private:
 165 NSNotificationCenter * __weak m_center = [NSNotificationCenter defaultCenter];
 166 RetainPtr<NSObject> m_observer;
 167 bool m_didObserve { false };
 168};
 169
 170}
 171
132172@interface TestSOAuthorizationBasicDelegate : NSObject <WKNavigationDelegate>
133173@end
134174

@@TEST(SOAuthorizationRedirect, InterceptionSucceedSuppressActiveSession)
956996{
957997 resetState();
958998 SWIZZLE_SOAUTH(PAL::getSOAuthorizationClass());
959 
 999
9601000 RetainPtr<NSURL> testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"];
9611001
9621002 auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]);

@@TEST(SOAuthorizationRedirect, DismissUIDuringMiniaturization)
14471487 }];
14481488 Util::run(&uiShowed);
14491489
1450  bool didEndSheet = false;
14511490 auto *hostWindow = [webView hostWindow];
1452  auto observer = adoptNS([[NSNotificationCenter defaultCenter] addObserverForName:NSWindowDidEndSheetNotification object:hostWindow queue:nil usingBlock:[&didEndSheet] (NSNotification *) {
1453  didEndSheet = true;
1454  }]);
 1491 ScopedNotificationCenterObserveOnce didEndSheet { NSWindowDidEndSheetNotification, hostWindow };
 1492
 1493 ScopedNotificationCenterObserveOnce didMiniaturize { NSWindowDidMiniaturizeNotification, hostWindow };
14551494 [hostWindow miniaturize:hostWindow];
 1495 EXPECT_TRUE(didMiniaturize.waitFor(actionDoneTimeout));
14561496
1457  // Dimiss the UI
 1497 // Dimiss the UI while it is miniaturized.
14581498 [gDelegate authorizationDidCancel:gAuthorization];
1459  EXPECT_FALSE(didEndSheet);
14601499
1461  // UI is only dimissed after the hostWindow is deminimized.
 1500 // This is the condition that the test tests: it is expected that the sheet is not ended because the view is miniaturized.
 1501 EXPECT_FALSE(didEndSheet.waitFor(actionAbsenceTimeout));
 1502
 1503 ScopedNotificationCenterObserveOnce didDeminiaturize { NSWindowDidDeminiaturizeNotification, hostWindow };
14621504 [hostWindow deminiaturize:hostWindow];
1463  EXPECT_TRUE(didEndSheet);
 1505 EXPECT_TRUE(didDeminiaturize.waitFor(actionDoneTimeout));
 1506
 1507 EXPECT_TRUE(didEndSheet.waitFor(actionDoneTimeout));
14641508}
14651509
14661510TEST(SOAuthorizationRedirect, DismissUIDuringHiding)

@@TEST(SOAuthorizationRedirect, DismissUIDuringHiding)
14861530 EXPECT_TRUE(success);
14871531 }];
14881532 Util::run(&uiShowed);
 1533 auto *hostWindow = [webView hostWindow];
14891534
1490  bool didEndSheet = false;
1491  auto observer = adoptNS([[NSNotificationCenter defaultCenter] addObserverForName:NSWindowDidEndSheetNotification object:[webView hostWindow] queue:nil usingBlock:[&didEndSheet] (NSNotification *) {
1492  didEndSheet = true;
1493  }]);
 1535 ScopedNotificationCenterObserveOnce didEndSheet { NSWindowDidEndSheetNotification, hostWindow };
14941536
14951537 [NSApp hide:NSApp];
14961538
 1539 // Wait for hiding to finish.
 1540 while (!NSApp.hidden)
 1541 Util::spinRunLoop();
 1542
14971543 // Dimiss the UI
14981544 [gDelegate authorizationDidCancel:gAuthorization];
1499  EXPECT_FALSE(didEndSheet);
 1545
 1546 // This is the condition that the test tests: it is expected that the sheet is not ended because the view is hidden.
 1547 EXPECT_FALSE(didEndSheet.waitFor(actionAbsenceTimeout));
15001548
15011549 // UI is only dimissed after the hostApp is unhidden.
15021550 [NSApp unhide:NSApp];
1503  EXPECT_TRUE(didEndSheet);
 1551 while (NSApp.hidden)
 1552 Util::spinRunLoop();
 1553
 1554 EXPECT_TRUE(didEndSheet.waitFor(actionDoneTimeout));
15041555}
15051556
15061557TEST(SOAuthorizationRedirect, DismissUIDuringMiniaturizationThenAnother)

@@TEST(SOAuthorizationRedirect, DismissUIDuringMiniaturizationThenAnother)
15261577 EXPECT_TRUE(success);
15271578 }];
15281579 Util::run(&uiShowed);
1529 
1530  bool didEndSheet = false;
15311580 auto *hostWindow = [webView hostWindow];
1532  auto observer = adoptNS([[NSNotificationCenter defaultCenter] addObserverForName:NSWindowDidEndSheetNotification object:hostWindow queue:nil usingBlock:[&didEndSheet] (NSNotification *) {
1533  didEndSheet = true;
1534  }]);
 1581
 1582 ScopedNotificationCenterObserveOnce didEndSheet { NSWindowDidEndSheetNotification, hostWindow };
 1583 ScopedNotificationCenterObserveOnce didMiniaturize { NSWindowDidMiniaturizeNotification, hostWindow };
15351584 [hostWindow miniaturize:hostWindow];
 1585 didMiniaturize.waitFor(actionDoneTimeout);
15361586
15371587 // Dimiss the UI
15381588 [gDelegate authorizationDidCancel:gAuthorization];
1539  EXPECT_FALSE(didEndSheet);
 1589
 1590 // This is the condition that the test tests: it is expected that the sheet is not ended because the view is miniaturized.
 1591 EXPECT_FALSE(didEndSheet.waitFor(actionAbsenceTimeout));
15401592
15411593 // Load another AppSSO request.
15421594 authorizationPerformed = false;

@@TEST(SOAuthorizationRedirect, DismissUIDuringMiniaturizationThenAnother)
15451597 Util::run(&authorizationPerformed);
15461598 EXPECT_TRUE(policyForAppSSOPerformed);
15471599
1548  // UI is only dimissed after the hostApp is unhidden.
 1600 // This is the condition that the test tests: it is expected that the sheet is not ended because the view is miniaturized.
 1601 EXPECT_FALSE(didEndSheet.waitFor(actionAbsenceTimeout));
 1602
 1603 // UI is only dimissed after the window is deminiaturized.
 1604 ScopedNotificationCenterObserveOnce didDeminiaturize { NSWindowDidDeminiaturizeNotification, hostWindow };
15491605 [hostWindow deminiaturize:hostWindow];
1550  EXPECT_TRUE(didEndSheet);
 1606 EXPECT_TRUE(didDeminiaturize.waitFor(actionDoneTimeout));
 1607
 1608 EXPECT_TRUE(didEndSheet.waitFor(actionDoneTimeout));
15511609}
15521610
15531611TEST(SOAuthorizationRedirect, DismissUIDuringHidingThenAnother)

@@TEST(SOAuthorizationRedirect, DismissUIDuringHidingThenAnother)
15741632 }];
15751633 Util::run(&uiShowed);
15761634
1577  bool didEndSheet = false;
1578  auto observer = adoptNS([[NSNotificationCenter defaultCenter] addObserverForName:NSWindowDidEndSheetNotification object:[webView hostWindow] queue:nil usingBlock:[&didEndSheet] (NSNotification *) {
1579  didEndSheet = true;
1580  }]);
 1635 ScopedNotificationCenterObserveOnce didEndSheet { NSWindowDidEndSheetNotification, [webView hostWindow] };
15811636
15821637 [NSApp hide:NSApp];
 1638 while (!NSApp.hidden)
 1639 Util::spinRunLoop();
15831640
15841641 // Dimiss the UI
15851642 [gDelegate authorizationDidCancel:gAuthorization];
1586  EXPECT_FALSE(didEndSheet);
 1643
 1644 // This is the condition that the test tests: it is expected that the sheet is not ended because the view is hidden.
 1645 EXPECT_FALSE(didEndSheet.waitFor(actionAbsenceTimeout));
15871646
15881647 // Load another AppSSO request.
15891648 authorizationPerformed = false;

@@TEST(SOAuthorizationRedirect, DismissUIDuringHidingThenAnother)
15921651 Util::run(&authorizationPerformed);
15931652 EXPECT_TRUE(policyForAppSSOPerformed);
15941653
1595  // UI is only dimissed after the hostApp is unhidden.
15961654 [NSApp unhide:NSApp];
1597  EXPECT_TRUE(didEndSheet);
 1655 while (NSApp.hidden)
 1656 Util::spinRunLoop();
 1657
 1658 EXPECT_TRUE(didEndSheet.waitFor(actionDoneTimeout));
15981659}
15991660#endif
16001661

@@TEST(SOAuthorizationPopUp, InterceptionSucceedSuppressActiveSession)
20072068 EXPECT_TRUE(policyForAppSSOPerformed);
20082069
20092070 // Suppress the last active session.
2010 
 2071
20112072 auto configuration = adoptNS([webView.get().configuration copy]);
20122073 auto messageHandler = adoptNS([[TestSOAuthorizationScriptMessageHandler alloc] initWithExpectation:@[@"Hello.", @"WindowClosed."]]);
20132074 [[configuration userContentController] addScriptMessageHandler:messageHandler.get() name:@"testHandler"];

@@TEST(SOAuthorizationSubFrame, InterceptionError)
22672328 EXPECT_TRUE(policyForAppSSOPerformed);
22682329
22692330 [messageHandler extendExpectations:@[@"null", @"SOAuthorizationDidCancel", @""]];
2270 
 2331
22712332 [gDelegate authorization:gAuthorization didCompleteWithError:adoptNS([[NSError alloc] initWithDomain:NSCocoaErrorDomain code:0 userInfo:nil]).get()];
22722333 Util::run(&allMessagesReceived);
22732334 // Make sure we don't load the request of the iframe to the main frame.

@@TEST(SOAuthorizationSubFrame, InterceptionSuccessTwice)
24272488 policyForAppSSOPerformed = false;
24282489
24292490 [messageHandler resetExpectations:@[@"http://www.example.com", @"SOAuthorizationDidStart"]];
2430 
 2491
24312492 [webView loadHTMLString:testHtml baseURL:nil];
24322493 Util::run(&allMessagesReceived);
24332494