REGRESSION(r211643): Dismissing WKActionSheet should not also dismiss its presenting...
authorwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 12 Mar 2018 19:25:19 +0000 (19:25 +0000)
committerwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 12 Mar 2018 19:25:19 +0000 (19:25 +0000)
https://bugs.webkit.org/show_bug.cgi?id=183549
<rdar://problem/34960698>

Reviewed by Andy Estes.

Source/WebKit:

Fixes the bug by dismissing the presented view controller (i.e. the action sheet or the view controller being
presented during rotation) rather than the presenting view controller.

Test: ActionSheetTests.DismissingActionSheetShouldNotDismissPresentingViewController

* UIProcess/ios/WKActionSheet.mm:
(-[WKActionSheet doneWithSheet:]):

Tools:

Add TestWebKitAPI support for testing WKWebViews embedded within presented view controllers, and use this to
check that dismissing an action sheet does not additionally cause the view controller being used to present the
web view to also dismiss.

* TestWebKitAPI/ClassMethodSwizzler.h: Added.
* TestWebKitAPI/ClassMethodSwizzler.mm: Added.
(TestWebKitAPI::ClassMethodSwizzler::ClassMethodSwizzler):
(TestWebKitAPI::ClassMethodSwizzler::~ClassMethodSwizzler):

Add ClassMethodSwizzler, an RAII which swizzles an Objective-C class method over its lifetime.

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/ios/ActionSheetTests.mm:

Add a new API test that loads a view controller which embeds a WKWebView, and verifies that presenting and then
dismissing an action sheet from that web view does not cause the view controller to also dismiss.

(TestWebKitAPI::setOverrideViewControllerForFullscreenPresentation):
(TestWebKitAPI::overrideViewControllerForFullscreenPresentation):

Mock +[UIViewController _viewControllerForFullScreenPresentationFromView:] to return the web view. This works
around the fact that TestWebKitAPI is not a UI application, so certain pieces of UIKit API and SPI need to be
stubbed or mocked to simulate being a UI application. We can remove these workarounds once
https://webkit.org/b/175204 is addressed, and TestWebKitAPI becomes a UI application that can actually maintain
a root view controller and key window.

(TestWebKitAPI::TEST):
* TestWebKitAPI/cocoa/TestWKWebView.h:
* TestWebKitAPI/cocoa/TestWKWebView.mm:
(-[TestWKWebView initWithFrame:configuration:addToWindow:]):

Add a new initializer for TestWKWebView that doesn't force the view to be hosted within a UIWindow. This is used
by TestWKWebViewController to create a TestWKWebView in -loadView.

* TestWebKitAPI/ios/TestWKWebViewController.h: Added.
* TestWebKitAPI/ios/TestWKWebViewController.mm: Added.
(-[TestWKWebViewControllerWindow _beginKeyWindowDeferral]):
(-[TestWKWebViewControllerWindow _endKeyWindowDeferral]):

Stub out these methods to prevent UIKit from hitting assertions when making this UIWindow the key window. This
can also be removed once TestWebKitAPI is a UI application.

(-[TestWKWebViewController initWithFrame:configuration:]):
(-[TestWKWebViewController loadView]):
(-[TestWKWebViewController webView]):
(-[TestWKWebViewController dismissViewControllerAnimated:completion:]):
(-[TestWKWebViewController dismissalHandler]):
(-[TestWKWebViewController setDismissalHandler:]):

Add a UIViewController helper subclass whose -view is a WKWebView. The new API test presents this view
controller. Tests may also provide a dismissalHandler, which is invoked when the view controller is being
dismissed. The new API test uses this hook to verify that the view controller containing the web view isn't also
dismissed after the action sheet goes away.

* TestWebKitAPI/ios/UIKitSPI.h:

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

12 files changed:
Source/WebKit/ChangeLog
Source/WebKit/UIProcess/ios/WKActionSheet.mm
Tools/ChangeLog
Tools/TestWebKitAPI/ClassMethodSwizzler.h [new file with mode: 0644]
Tools/TestWebKitAPI/ClassMethodSwizzler.mm [new file with mode: 0644]
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/ios/ActionSheetTests.mm
Tools/TestWebKitAPI/cocoa/TestWKWebView.h
Tools/TestWebKitAPI/cocoa/TestWKWebView.mm
Tools/TestWebKitAPI/ios/TestWKWebViewController.h [new file with mode: 0644]
Tools/TestWebKitAPI/ios/TestWKWebViewController.mm [new file with mode: 0644]
Tools/TestWebKitAPI/ios/UIKitSPI.h

index b5c6690..dc9a6c0 100644 (file)
@@ -1,3 +1,19 @@
+2018-03-12  Wenson Hsieh  <wenson_hsieh@apple.com>
+
+        REGRESSION(r211643): Dismissing WKActionSheet should not also dismiss its presenting view controller
+        https://bugs.webkit.org/show_bug.cgi?id=183549
+        <rdar://problem/34960698>
+
+        Reviewed by Andy Estes.
+
+        Fixes the bug by dismissing the presented view controller (i.e. the action sheet or the view controller being
+        presented during rotation) rather than the presenting view controller.
+
+        Test: ActionSheetTests.DismissingActionSheetShouldNotDismissPresentingViewController
+
+        * UIProcess/ios/WKActionSheet.mm:
+        (-[WKActionSheet doneWithSheet:]):
+
 2018-03-12  Javier Fernandez  <jfernandez@igalia.com>
 
         Remove GridLayout runtime flag
index 6b5ed8d..1b1c6b6 100644 (file)
 
 - (void)doneWithSheet:(BOOL)dismiss
 {
-    if (dismiss && _currentPresentingViewController)
-        [_currentPresentingViewController dismissViewControllerAnimated:YES completion:nil];
+    if (dismiss) {
+        UIViewController *currentPresentedViewController = [_currentPresentingViewController presentedViewController];
+        if (currentPresentedViewController == self || currentPresentedViewController == _presentedViewControllerWhileRotating)
+            [currentPresentedViewController dismissViewControllerAnimated:YES completion:nil];
+    }
 
     _currentPresentingViewController = nil;
     _presentedViewControllerWhileRotating = nil;
index 6517a10..4f6f224 100644 (file)
@@ -1,3 +1,67 @@
+2018-03-12  Wenson Hsieh  <wenson_hsieh@apple.com>
+
+        REGRESSION(r211643): Dismissing WKActionSheet should not also dismiss its presenting view controller
+        https://bugs.webkit.org/show_bug.cgi?id=183549
+        <rdar://problem/34960698>
+
+        Reviewed by Andy Estes.
+
+        Add TestWebKitAPI support for testing WKWebViews embedded within presented view controllers, and use this to
+        check that dismissing an action sheet does not additionally cause the view controller being used to present the
+        web view to also dismiss.
+
+        * TestWebKitAPI/ClassMethodSwizzler.h: Added.
+        * TestWebKitAPI/ClassMethodSwizzler.mm: Added.
+        (TestWebKitAPI::ClassMethodSwizzler::ClassMethodSwizzler):
+        (TestWebKitAPI::ClassMethodSwizzler::~ClassMethodSwizzler):
+
+        Add ClassMethodSwizzler, an RAII which swizzles an Objective-C class method over its lifetime.
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/ios/ActionSheetTests.mm:
+
+        Add a new API test that loads a view controller which embeds a WKWebView, and verifies that presenting and then
+        dismissing an action sheet from that web view does not cause the view controller to also dismiss.
+
+        (TestWebKitAPI::setOverrideViewControllerForFullscreenPresentation):
+        (TestWebKitAPI::overrideViewControllerForFullscreenPresentation):
+
+        Mock +[UIViewController _viewControllerForFullScreenPresentationFromView:] to return the web view. This works
+        around the fact that TestWebKitAPI is not a UI application, so certain pieces of UIKit API and SPI need to be
+        stubbed or mocked to simulate being a UI application. We can remove these workarounds once
+        https://webkit.org/b/175204 is addressed, and TestWebKitAPI becomes a UI application that can actually maintain
+        a root view controller and key window.
+
+        (TestWebKitAPI::TEST):
+        * TestWebKitAPI/cocoa/TestWKWebView.h:
+        * TestWebKitAPI/cocoa/TestWKWebView.mm:
+        (-[TestWKWebView initWithFrame:configuration:addToWindow:]):
+
+        Add a new initializer for TestWKWebView that doesn't force the view to be hosted within a UIWindow. This is used
+        by TestWKWebViewController to create a TestWKWebView in -loadView.
+
+        * TestWebKitAPI/ios/TestWKWebViewController.h: Added.
+        * TestWebKitAPI/ios/TestWKWebViewController.mm: Added.
+        (-[TestWKWebViewControllerWindow _beginKeyWindowDeferral]):
+        (-[TestWKWebViewControllerWindow _endKeyWindowDeferral]):
+
+        Stub out these methods to prevent UIKit from hitting assertions when making this UIWindow the key window. This
+        can also be removed once TestWebKitAPI is a UI application.
+
+        (-[TestWKWebViewController initWithFrame:configuration:]):
+        (-[TestWKWebViewController loadView]):
+        (-[TestWKWebViewController webView]):
+        (-[TestWKWebViewController dismissViewControllerAnimated:completion:]):
+        (-[TestWKWebViewController dismissalHandler]):
+        (-[TestWKWebViewController setDismissalHandler:]):
+
+        Add a UIViewController helper subclass whose -view is a WKWebView. The new API test presents this view
+        controller. Tests may also provide a dismissalHandler, which is invoked when the view controller is being
+        dismissed. The new API test uses this hook to verify that the view controller containing the web view isn't also
+        dismissed after the action sheet goes away.
+
+        * TestWebKitAPI/ios/UIKitSPI.h:
+
 2018-03-12  Basuke Suzuki  <Basuke.Suzuki@sony.com>
 
         [webkitpy] Remove openssl command dependency.
diff --git a/Tools/TestWebKitAPI/ClassMethodSwizzler.h b/Tools/TestWebKitAPI/ClassMethodSwizzler.h
new file mode 100644 (file)
index 0000000..6c52777
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2018 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <objc/runtime.h>
+#include <wtf/Noncopyable.h>
+
+namespace TestWebKitAPI {
+
+class ClassMethodSwizzler {
+    WTF_MAKE_NONCOPYABLE(ClassMethodSwizzler);
+public:
+    ClassMethodSwizzler(Class, SEL, IMP);
+    ~ClassMethodSwizzler();
+
+    Method m_method;
+    IMP m_originalImplementation;
+};
+
+} // namespace TestWebKitAPI
diff --git a/Tools/TestWebKitAPI/ClassMethodSwizzler.mm b/Tools/TestWebKitAPI/ClassMethodSwizzler.mm
new file mode 100644 (file)
index 0000000..f4df73f
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2018 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "config.h"
+#import "ClassMethodSwizzler.h"
+
+namespace TestWebKitAPI {
+
+ClassMethodSwizzler::ClassMethodSwizzler(Class cls, SEL originalSelector, IMP implementation)
+    : m_method(class_getClassMethod(objc_getMetaClass(NSStringFromClass(cls).UTF8String), originalSelector))
+    , m_originalImplementation(method_setImplementation(m_method, implementation))
+{
+}
+
+ClassMethodSwizzler::~ClassMethodSwizzler()
+{
+    method_setImplementation(m_method, m_originalImplementation);
+}
+
+} // namespace TestWebKitAPI
index f4e7f5f..cd5d5dc 100644 (file)
                F44D06471F39627A001A0E29 /* EditorStateTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = F44D06461F395C4D001A0E29 /* EditorStateTests.mm */; };
                F44D064A1F3962F2001A0E29 /* EditingTestHarness.mm in Sources */ = {isa = PBXBuildFile; fileRef = F44D06491F3962E3001A0E29 /* EditingTestHarness.mm */; };
                F4512E131F60C44600BB369E /* DataTransferItem-getAsEntry.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = F4512E121F60C43400BB369E /* DataTransferItem-getAsEntry.html */; };
+               F4517B672054C49500C26721 /* TestWKWebViewController.mm in Sources */ = {isa = PBXBuildFile; fileRef = F4517B662054C49500C26721 /* TestWKWebViewController.mm */; };
+               F4517B7F2055101B00C26721 /* ClassMethodSwizzler.mm in Sources */ = {isa = PBXBuildFile; fileRef = F4517B692054E0AC00C26721 /* ClassMethodSwizzler.mm */; };
                F4538EF71E8473E600B5C953 /* large-red-square.png in Copy Resources */ = {isa = PBXBuildFile; fileRef = F4538EF01E846B4100B5C953 /* large-red-square.png */; };
                F457A9B8202D5CDC00F7E9D5 /* PasteMixedContent.mm in Sources */ = {isa = PBXBuildFile; fileRef = F457A9B6202D5CDC00F7E9D5 /* PasteMixedContent.mm */; };
                F457A9D6202D68AF00F7E9D5 /* DataTransfer.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = F457A9B3202D535300F7E9D5 /* DataTransfer.html */; };
                F44D06481F3962E3001A0E29 /* EditingTestHarness.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EditingTestHarness.h; sourceTree = "<group>"; };
                F44D06491F3962E3001A0E29 /* EditingTestHarness.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = EditingTestHarness.mm; sourceTree = "<group>"; };
                F4512E121F60C43400BB369E /* DataTransferItem-getAsEntry.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "DataTransferItem-getAsEntry.html"; sourceTree = "<group>"; };
+               F4517B652054C49500C26721 /* TestWKWebViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TestWKWebViewController.h; sourceTree = "<group>"; };
+               F4517B662054C49500C26721 /* TestWKWebViewController.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = TestWKWebViewController.mm; sourceTree = "<group>"; };
+               F4517B682054E0AC00C26721 /* ClassMethodSwizzler.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ClassMethodSwizzler.h; sourceTree = "<group>"; };
+               F4517B692054E0AC00C26721 /* ClassMethodSwizzler.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = ClassMethodSwizzler.mm; sourceTree = "<group>"; };
                F4538EF01E846B4100B5C953 /* large-red-square.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "large-red-square.png"; sourceTree = "<group>"; };
                F457A9B3202D535300F7E9D5 /* DataTransfer.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = DataTransfer.html; sourceTree = "<group>"; };
                F457A9B6202D5CDC00F7E9D5 /* PasteMixedContent.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = PasteMixedContent.mm; sourceTree = "<group>"; };
                        isa = PBXGroup;
                        children = (
                                A13EBB441B87332B00097110 /* WebProcessPlugIn */,
+                               F4517B682054E0AC00C26721 /* ClassMethodSwizzler.h */,
+                               F4517B692054E0AC00C26721 /* ClassMethodSwizzler.mm */,
                                F44D06481F3962E3001A0E29 /* EditingTestHarness.h */,
                                F44D06491F3962E3001A0E29 /* EditingTestHarness.mm */,
                                5C726D6D1D3EE06800C5E1A1 /* InstanceMethodSwizzler.h */,
                                F4D4F3B51E4E2BCB00BB2767 /* DataInteractionSimulator.h */,
                                F4D4F3B41E4E2BCB00BB2767 /* DataInteractionSimulator.mm */,
                                2E7765CC16C4D80A00BA2BB1 /* mainIOS.mm */,
+                               F4517B652054C49500C26721 /* TestWKWebViewController.h */,
+                               F4517B662054C49500C26721 /* TestWKWebViewController.mm */,
                                F493247C1F44DF8D006F4336 /* UIKitSPI.h */,
                        );
                        path = ios;
                                57303BCB2008376500355965 /* CBORReaderTest.cpp in Sources */,
                                57303BC9200824D300355965 /* CBORValueTest.cpp in Sources */,
                                57303BCA20082C0100355965 /* CBORWriterTest.cpp in Sources */,
+                               F4517B7F2055101B00C26721 /* ClassMethodSwizzler.mm in Sources */,
                                7CCE7EE61A411AE600447C4C /* CloseFromWithinCreatePage.cpp in Sources */,
                                7CCE7EB71A411A7E00447C4C /* CloseNewWindowInNavigationPolicyDelegate.mm in Sources */,
                                7CCE7EE51A411AE600447C4C /* CloseThenTerminate.cpp in Sources */,
                                A14FC5901B8AE36F00D107EB /* TestProtocol.mm in Sources */,
                                7CCE7EAE1A411A3400447C4C /* TestsController.cpp in Sources */,
                                2EFF06D41D8AEDBB0004BB30 /* TestWKWebView.mm in Sources */,
+                               F4517B672054C49500C26721 /* TestWKWebViewController.mm in Sources */,
                                93E6193B1F931B3A00AF245E /* TextCodec.cpp in Sources */,
                                CE3524F91B1441C40028A7C5 /* TextFieldDidBeginAndEndEditing.cpp in Sources */,
                                7CCE7EDD1A411A9200447C4C /* TimeRanges.cpp in Sources */,
index 0ea49c0..491e335 100644 (file)
  * THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include "config.h"
+#import "config.h"
+#import "Test.h"
 
 #if PLATFORM(IOS)
 
+#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, DismissingActionSheetShouldNotDismissPresentingViewController)
+{
+    IPadUserInterfaceSwizzler iPadUserInterface;
+    UIApplicationInitialize();
+
+    auto navigationDelegate = adoptNS([[TestNavigationDelegate alloc] init]);
+    auto window = [[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;
index 908e052..45d9e8a 100644 (file)
@@ -46,6 +46,7 @@
 
 @interface TestWKWebView : WKWebView
 - (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration processPoolConfiguration:(_WKProcessPoolConfiguration *)processPoolConfiguration;
+- (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration addToWindow:(BOOL)addToWindow;
 - (void)clearMessageHandlers:(NSArray *)messageNames;
 - (void)performAfterReceivingMessage:(NSString *)message action:(dispatch_block_t)action;
 - (void)loadTestPageNamed:(NSString *)pageName;
index 25897d6..81e376e 100644 (file)
@@ -175,7 +175,16 @@ NSEventMask __simulated_forceClickAssociatedEventsMask(id self, SEL _cmd)
 
 - (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration
 {
-    if (self = [super initWithFrame:frame configuration:configuration])
+    return [self initWithFrame:frame configuration:configuration addToWindow:YES];
+}
+
+- (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration addToWindow:(BOOL)addToWindow
+{
+    self = [super initWithFrame:frame configuration:configuration];
+    if (!self)
+        return nil;
+
+    if (addToWindow)
         [self _setUpTestWindow:frame];
 
     return self;
diff --git a/Tools/TestWebKitAPI/ios/TestWKWebViewController.h b/Tools/TestWebKitAPI/ios/TestWKWebViewController.h
new file mode 100644 (file)
index 0000000..811f25d
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2018 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "TestWKWebView.h"
+
+#if PLATFORM(IOS)
+
+#import <UIKit/UIKit.h>
+
+@interface TestWKWebViewControllerWindow : UIWindow
+
+@end
+
+@interface TestWKWebViewController : UIViewController
+
+- (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration;
+
+@property (nonatomic, readonly) TestWKWebView *webView;
+@property (nonatomic, copy) dispatch_block_t dismissalHandler;
+
+@end
+
+#endif // PLATFORM(IOS)
diff --git a/Tools/TestWebKitAPI/ios/TestWKWebViewController.mm b/Tools/TestWebKitAPI/ios/TestWKWebViewController.mm
new file mode 100644 (file)
index 0000000..3ea463a
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2018 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "config.h"
+#import "TestWKWebViewController.h"
+
+#if PLATFORM(IOS)
+
+#import <wtf/BlockPtr.h>
+
+@implementation TestWKWebViewControllerWindow
+
+// These internal methods need to be stubbed out to prevent UIKit from throwing inconsistency
+// exceptions when making a UIWindow the key window, since TestWebKitAPI is not a UI application.
+// These stubs and this class can be removed once https://webkit.org/b/175204 is fixed.
+- (void)_beginKeyWindowDeferral
+{
+}
+
+- (void)_endKeyWindowDeferral
+{
+}
+
+@end
+
+@implementation TestWKWebViewController {
+    RetainPtr<WKWebViewConfiguration> _configuration;
+    CGRect _initialViewFrame;
+    RetainPtr<TestWKWebView> _webView;
+    BlockPtr<void()> _dismissalHandler;
+}
+
+- (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration
+{
+    self = [super init];
+    if (!self)
+        return nil;
+
+    _initialViewFrame = frame;
+    _configuration = configuration ?: adoptNS([[WKWebViewConfiguration alloc] init]);
+    return self;
+}
+
+- (void)loadView
+{
+    _webView = adoptNS([[TestWKWebView alloc] initWithFrame:_initialViewFrame configuration:_configuration.get() addToWindow:NO]);
+    self.view = _webView.get();
+}
+
+- (TestWKWebView *)webView
+{
+    [self loadViewIfNeeded];
+    return _webView.get();
+}
+
+- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)())completion
+{
+    if (_dismissalHandler)
+        _dismissalHandler();
+    [super dismissViewControllerAnimated:flag completion:completion];
+}
+
+- (dispatch_block_t)dismissalHandler
+{
+    return _dismissalHandler.get();
+}
+
+- (void)setDismissalHandler:(dispatch_block_t)dismissalHandler
+{
+    _dismissalHandler = makeBlockPtr(dismissalHandler);
+}
+
+@end
+
+#endif // PLATFORM(IOS)
index c687210..c4b7277 100644 (file)
 #import <UIKit/UIApplication_Private.h>
 #import <UIKit/UITextInputTraits_Private.h>
 #import <UIKit/UITextInput_Private.h>
+#import <UIKit/UIViewController_Private.h>
 
 #if ENABLE(DRAG_SUPPORT)
 @protocol UIDragSession;
 @class UIDragInteraction;
 @class UIDragItem;
 #import <UIKit/UIDragInteraction_Private.h>
-#endif
+#endif // ENABLE(DRAG_SUPPORT)
 
 #else
 
@@ -95,4 +96,8 @@ WTF_EXTERN_C_END
 @property (nonatomic, copy, setter=_setTitle:) NSString *_title;
 @end
 
+@interface UIViewController (UIKitSPI)
++ (UIViewController *)_viewControllerForFullScreenPresentationFromView:(UIView *)view;
+@end
+
 #endif // PLATFORM(IOS)