REGRESSION (r237331): DismissingActionSheetShouldNotDismissPresentingViewController...
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / ios / ActionSheetTests.mm
index 2a6f226..bf7da79 100644 (file)
  * THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include "config.h"
+#import "config.h"
+#import "Test.h"
 
-#if PLATFORM(IOS)
+#if PLATFORM(IOS_FAMILY)
 
+#import "ClassMethodSwizzler.h"
 #import "InstanceMethodSwizzler.h"
 #import "PlatformUtilities.h"
+#import "TestNavigationDelegate.h"
 #import "TestWKWebView.h"
+#import "TestWKWebViewController.h"
 #import "UIKitSPI.h"
 #import <MobileCoreServices/MobileCoreServices.h>
 #import <WebKit/WKUIDelegatePrivate.h>
@@ -75,6 +79,70 @@ private:
     InstanceMethodSwizzler m_swizzler;
 };
 
+static RetainPtr<UIViewController> gOverrideViewControllerForFullscreenPresentation;
+static void setOverrideViewControllerForFullscreenPresentation(UIViewController *viewController)
+{
+    gOverrideViewControllerForFullscreenPresentation = viewController;
+}
+
+static UIViewController *overrideViewControllerForFullscreenPresentation()
+{
+    return gOverrideViewControllerForFullscreenPresentation.get();
+}
+
+TEST(ActionSheetTests, DISABLED_DismissingActionSheetShouldNotDismissPresentingViewController)
+{
+    IPadUserInterfaceSwizzler iPadUserInterface;
+    UIApplicationInitialize();
+
+    auto navigationDelegate = adoptNS([[TestNavigationDelegate alloc] init]);
+    auto window = adoptNS([[TestWKWebViewControllerWindow alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)]);
+    auto rootViewController = adoptNS([[UIViewController alloc] init]);
+    auto navigationController = adoptNS([[UINavigationController alloc] initWithRootViewController:rootViewController.get()]);
+    auto observer = adoptNS([[ActionSheetObserver alloc] init]);
+    [window setRootViewController:navigationController.get()];
+    [window makeKeyAndVisible];
+
+    auto webViewController = adoptNS([[TestWKWebViewController alloc] initWithFrame:CGRectMake(0, 0, 1024, 768) configuration:nil]);
+    TestWKWebView *webView = [webViewController webView];
+    webView.UIDelegate = observer.get();
+    [webView synchronouslyLoadTestPageNamed:@"link-and-input"];
+    [webView setNavigationDelegate:navigationDelegate.get()];
+    [rootViewController presentViewController:webViewController.get() animated:NO completion:nil];
+
+    // Since TestWebKitAPI is not a UI application, +[UIViewController _viewControllerForFullScreenPresentationFromView:]
+    // returns nil. To ensure that we actually present the action sheet from the web view controller, we mock this for the
+    // time being until https://webkit.org/b/175204 is fixed.
+    setOverrideViewControllerForFullscreenPresentation(webViewController.get());
+    ClassMethodSwizzler swizzler([UIViewController class], @selector(_viewControllerForFullScreenPresentationFromView:), reinterpret_cast<IMP>(overrideViewControllerForFullscreenPresentation));
+
+    [observer setPresentationHandler:^(_WKActivatedElementInfo *, NSArray *actions) {
+        // Killing the web content process should dismiss the action sheet.
+        // This should not tell the web view controller to dismiss in the process.
+        [webView _killWebContentProcess];
+        return actions;
+    }];
+
+    __block bool done = false;
+    [navigationDelegate setWebContentProcessDidTerminate:^(WKWebView *) {
+        dispatch_async(dispatch_get_main_queue(), ^{
+            done = true;
+        });
+    }];
+
+    __block bool didDismissWebViewController = false;
+    [webViewController setDismissalHandler:^{
+        didDismissWebViewController = true;
+    }];
+
+    [webView _simulateLongPressActionAtLocation:CGPointMake(100, 100)];
+    TestWebKitAPI::Util::run(&done);
+
+    EXPECT_FALSE(didDismissWebViewController);
+    EXPECT_NULL([webViewController presentedViewController]);
+    EXPECT_NOT_NULL([webViewController presentingViewController]);
+}
+
 TEST(ActionSheetTests, ImageMapDoesNotDestroySelection)
 {
     IPadUserInterfaceSwizzler iPadUserInterface;
@@ -140,6 +208,8 @@ TEST(ActionSheetTests, DataDetectorsLinkIsNotPresentedAsALink)
     EXPECT_TRUE(runTest(@"08080808080"));
 }
 
+#if !PLATFORM(WATCHOS) && !PLATFORM(APPLETV)
+
 static void presentActionSheetAndChooseAction(WKWebView *webView, ActionSheetObserver *observer, CGPoint location, _WKElementActionType actionType)
 {
     __block RetainPtr<_WKElementAction> copyAction;
@@ -180,14 +250,14 @@ TEST(ActionSheetTests, CopyImageElementWithHREFAndTitle)
     [webView _doAfterNextPresentationUpdate:^() {
         NSArray <NSString *> *pasteboardTypes = [[UIPasteboard generalPasteboard] pasteboardTypes];
         EXPECT_EQ(2UL, pasteboardTypes.count);
-        EXPECT_WK_STREQ((NSString *)kUTTypePNG, pasteboardTypes.firstObject);
-        EXPECT_WK_STREQ((NSString *)kUTTypeURL, pasteboardTypes.lastObject);
+        EXPECT_WK_STREQ((__bridge NSString *)kUTTypePNG, pasteboardTypes.firstObject);
+        EXPECT_WK_STREQ((__bridge NSString *)kUTTypeURL, pasteboardTypes.lastObject);
         NSArray <NSItemProvider *> *itemProviders = [[UIPasteboard generalPasteboard] itemProviders];
         EXPECT_EQ(1UL, itemProviders.count);
         itemProvider = itemProviders.firstObject;
         EXPECT_EQ(2UL, [itemProvider registeredTypeIdentifiers].count);
-        EXPECT_WK_STREQ((NSString *)kUTTypePNG, [itemProvider registeredTypeIdentifiers].firstObject);
-        EXPECT_WK_STREQ((NSString *)kUTTypeURL, [itemProvider registeredTypeIdentifiers].lastObject);
+        EXPECT_WK_STREQ((__bridge NSString *)kUTTypePNG, [itemProvider registeredTypeIdentifiers].firstObject);
+        EXPECT_WK_STREQ((__bridge NSString *)kUTTypeURL, [itemProvider registeredTypeIdentifiers].lastObject);
         done = true;
     }];
     TestWebKitAPI::Util::run(&done);
@@ -220,14 +290,14 @@ TEST(ActionSheetTests, CopyImageElementWithHREF)
     [webView _doAfterNextPresentationUpdate:^() {
         NSArray <NSString *> *pasteboardTypes = [[UIPasteboard generalPasteboard] pasteboardTypes];
         EXPECT_EQ(2UL, pasteboardTypes.count);
-        EXPECT_WK_STREQ((NSString *)kUTTypePNG, pasteboardTypes.firstObject);
-        EXPECT_WK_STREQ((NSString *)kUTTypeURL, pasteboardTypes.lastObject);
+        EXPECT_WK_STREQ((__bridge NSString *)kUTTypePNG, pasteboardTypes.firstObject);
+        EXPECT_WK_STREQ((__bridge NSString *)kUTTypeURL, pasteboardTypes.lastObject);
         NSArray <NSItemProvider *> *itemProviders = [[UIPasteboard generalPasteboard] itemProviders];
         EXPECT_EQ(1UL, itemProviders.count);
         itemProvider = itemProviders.firstObject;
         EXPECT_EQ(2UL, [itemProvider registeredTypeIdentifiers].count);
-        EXPECT_WK_STREQ((NSString *)kUTTypePNG, [itemProvider registeredTypeIdentifiers].firstObject);
-        EXPECT_WK_STREQ((NSString *)kUTTypeURL, [itemProvider registeredTypeIdentifiers].lastObject);
+        EXPECT_WK_STREQ((__bridge NSString *)kUTTypePNG, [itemProvider registeredTypeIdentifiers].firstObject);
+        EXPECT_WK_STREQ((__bridge NSString *)kUTTypeURL, [itemProvider registeredTypeIdentifiers].lastObject);
         done = true;
     }];
     TestWebKitAPI::Util::run(&done);
@@ -259,12 +329,12 @@ TEST(ActionSheetTests, CopyImageElementWithoutHREF)
     [webView _doAfterNextPresentationUpdate:^() {
         NSArray <NSString *> *pasteboardTypes = [[UIPasteboard generalPasteboard] pasteboardTypes];
         EXPECT_EQ(1UL, pasteboardTypes.count);
-        EXPECT_WK_STREQ((NSString *)kUTTypePNG, pasteboardTypes.firstObject);
+        EXPECT_WK_STREQ((__bridge NSString *)kUTTypePNG, pasteboardTypes.firstObject);
         NSArray <NSItemProvider *> *itemProviders = [[UIPasteboard generalPasteboard] itemProviders];
         EXPECT_EQ(1UL, itemProviders.count);
         NSItemProvider *itemProvider = itemProviders.firstObject;
         EXPECT_EQ(1UL, itemProvider.registeredTypeIdentifiers.count);
-        EXPECT_WK_STREQ((NSString *)kUTTypePNG, itemProvider.registeredTypeIdentifiers.firstObject);
+        EXPECT_WK_STREQ((__bridge NSString *)kUTTypePNG, itemProvider.registeredTypeIdentifiers.firstObject);
         done = true;
     }];
     TestWebKitAPI::Util::run(&done);
@@ -294,6 +364,8 @@ TEST(ActionSheetTests, CopyLinkWritesURLAndPlainText)
     EXPECT_WK_STREQ("", [webView stringByEvaluatingJavaScript:@"rawHTMLData.textContent"]);
 }
 
+#endif // !PLATFORM(WATCHOS) && !PLATFORM(APPLETV)
+
 } // namespace TestWebKitAPI
 
-#endif // PLATFORM(IOS)
+#endif // PLATFORM(IOS_FAMILY)