Several iOS API tests that call -makeKeyWindow assert after r255907
authorwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 8 Feb 2020 00:39:28 +0000 (00:39 +0000)
committerwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 8 Feb 2020 00:39:28 +0000 (00:39 +0000)
https://bugs.webkit.org/show_bug.cgi?id=207411

Reviewed by Tim Horton.

r255907 asserted in -makeKeyWindow that the shared UIApplication exists. While this was guaranteed for all call
sites of -makeKeyWindow from API tests, -makeKeyWindow may also be invoked from within UIKit code; in this case,
nothing guarantees that the application has been initialized, and we end up hitting this assertion. To fix this,
replace the assertion with logic to bootstrap the shared UIApplication (if it didn't already exist).

* TestWebKitAPI/Tests/ios/KeyboardInputTestsIOS.mm:
(TestWebKitAPI::TEST):
(TestWebKitAPI::overrideBundleIdentifier): Deleted.
* TestWebKitAPI/cocoa/TestWKWebView.mm:
(overrideBundleIdentifier):
(setOverriddenApplicationKeyWindow):

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

Tools/ChangeLog
Tools/TestWebKitAPI/Tests/ios/KeyboardInputTestsIOS.mm
Tools/TestWebKitAPI/cocoa/TestWKWebView.mm

index 633d9cd..05c736e 100644 (file)
@@ -1,3 +1,22 @@
+2020-02-07  Wenson Hsieh  <wenson_hsieh@apple.com>
+
+        Several iOS API tests that call -makeKeyWindow assert after r255907
+        https://bugs.webkit.org/show_bug.cgi?id=207411
+
+        Reviewed by Tim Horton.
+
+        r255907 asserted in -makeKeyWindow that the shared UIApplication exists. While this was guaranteed for all call
+        sites of -makeKeyWindow from API tests, -makeKeyWindow may also be invoked from within UIKit code; in this case,
+        nothing guarantees that the application has been initialized, and we end up hitting this assertion. To fix this,
+        replace the assertion with logic to bootstrap the shared UIApplication (if it didn't already exist).
+
+        * TestWebKitAPI/Tests/ios/KeyboardInputTestsIOS.mm:
+        (TestWebKitAPI::TEST):
+        (TestWebKitAPI::overrideBundleIdentifier): Deleted.
+        * TestWebKitAPI/cocoa/TestWKWebView.mm:
+        (overrideBundleIdentifier):
+        (setOverriddenApplicationKeyWindow):
+
 2020-02-07  Jonathan Bedard  <jbedard@apple.com>
 
         TestWebKitAPI: Disable legacy ActionSheet tests on Catalyst
index 3f0661d..e245a16 100644 (file)
@@ -630,17 +630,8 @@ TEST(KeyboardInputTests, SupportsImagePaste)
     EXPECT_TRUE(contentView.supportsImagePaste);
 }
 
-static NSString *overrideBundleIdentifier()
-{
-    return @"com.apple.TestWebKitAPI";
-}
-
 TEST(KeyboardInputTests, SuppressSoftwareKeyboard)
 {
-    InstanceMethodSwizzler bundleIdentifierSwizzler(NSBundle.class, @selector(bundleIdentifier), reinterpret_cast<IMP>(overrideBundleIdentifier));
-    UIApplicationInitialize();
-    UIApplicationInstantiateSingleton(UIApplication.class);
-
     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]);
     [webView _setSuppressSoftwareKeyboard:YES];
     [[webView window] makeKeyWindow];
index 77f7243..27a28aa 100644 (file)
@@ -312,12 +312,22 @@ static NeverDestroyed<RetainPtr<UIWindow>> gOverriddenApplicationKeyWindow;
 static NeverDestroyed<std::unique_ptr<InstanceMethodSwizzler>> gApplicationKeyWindowSwizzler;
 static NeverDestroyed<std::unique_ptr<InstanceMethodSwizzler>> gSharedApplicationSwizzler;
 
+static NSString *overrideBundleIdentifier()
+{
+    return @"com.apple.TestWebKitAPI";
+}
+
 static void setOverriddenApplicationKeyWindow(UIWindow *window)
 {
     if (gOverriddenApplicationKeyWindow.get() == window)
         return;
 
-    ASSERT(UIApplication.sharedApplication);
+    if (!UIApplication.sharedApplication) {
+        InstanceMethodSwizzler bundleIdentifierSwizzler(NSBundle.class, @selector(bundleIdentifier), reinterpret_cast<IMP>(overrideBundleIdentifier));
+        UIApplicationInitialize();
+        UIApplicationInstantiateSingleton(UIApplication.class);
+    }
+
     static dispatch_once_t onceToken;
     dispatch_once(&onceToken, ^{
         gApplicationKeyWindowSwizzler.get() = makeUnique<InstanceMethodSwizzler>(UIApplication.class, @selector(keyWindow), reinterpret_cast<IMP>(applicationKeyWindowOverride));