[ iOS ] editing/selection/ios/show-callout-bar-after-selecting-word.html is a flaky timeout
https://bugs.webkit.org/show_bug.cgi?id=240544
rdar://93458507
Reviewed by Aditya Keerthi.
Currently, any LayoutTest that attempts to wait for (or interact with) the callout bar after
selecting text fails, due to the fact that on iOS 16, the callout bar is implemented using a
`UIEditMenuInteraction` on the content view, which doesn't use the extant classes for managing and
observing callout bar UI -- i.e. `UIMenuController`, `UICalloutBar` and the associated global
notifications.
Unfortunately, when using iOS 16, there are no longer any global notifications that the test harness
can listen for to detect when the callout bar has been presented or dismissed. To keep these tests
passing on iOS 16, we introduce a mechanism to the test harness to intercept edit menu interaction
delegate method calls, and notify `TestController` when the edit menu has completed its presention
or dismissal animations.
Also, tweak a flaky layout test that exercises this logic to be more robust — see below for more
detail.
* LayoutTests/editing/selection/ios/show-callout-bar-after-selecting-word.html:
Additionally, adjust this layout test to explicitly wait for the 'Select' menu action before trying
to select that item -- this might mitigate potential flakiness on iOS 15, due to calling
`UIHelper.chooseMenuAction()` too early.
* Source/WTF/wtf/PlatformHave.h:
Add a new build-time flag to guard the availability of `UIEditMenuInteraction`.
* Tools/WebKitTestRunner/TestController.h:
* Tools/WebKitTestRunner/WebKitTestRunner.xcodeproj/project.pbxproj:
* Tools/WebKitTestRunner/cocoa/TestRunnerWKWebView.h:
* Tools/WebKitTestRunner/cocoa/TestRunnerWKWebView.mm:
(-[TestRunnerWKWebView textEffectsWindow]):
Add a helper method to return the web view's associated text effects window. This is used below in
`TestController` when recursively searching the view hierarchy for callout menu buttons with a given
label string (e.g. "Select", "Select All", "Copy", etc.), because these buttons and labels now exist
within the view hierarchy of the `UITextEffectsWindow` on iOS 16, when using the edit menu
interaction.
(-[TestRunnerWKWebView currentEditMenuInteraction]):
Add a helper method to grab the edit menu interaction corresponding to text selection on the content
view.
(-[TestRunnerWKWebView didPresentEditMenuInteraction:]):
(-[TestRunnerWKWebView didDismissEditMenuInteraction:]):
Plumb into the existing `-_didHideMenu` and `-_didShowMenu` methods, which we currently use to
observe callout bar presentation and dismissal. This allows the test harness to simultaneously
support iOS 15 and iOS 16, for layout tests that exercise the callout bar.
(-[TestRunnerWKWebView immediatelyDismissEditMenuInteractionIfNeeded]):
Add a hook to immediately dismiss the callout bar between layout tests, that is compatible with
UIEditMenuInteraction-backed callout bars.
* Tools/WebKitTestRunner/ios/EditMenuInteractionSwizzler.h: Added.
* Tools/WebKitTestRunner/ios/EditMenuInteractionSwizzler.mm: Added.
Add a helper class to swizzle `-[UIEditMenuInteraction initWithDelegate:]`, and call the original
method implementation with an object that wraps the given `UIEditMenuInteractionDelegate` in another
object (`EditMenuInteractionDelegateWrapper`) that forwards all selector invocations to the original
delegate (in practice: `_UIContextMenuInteractionBasedTextContextInteraction`), but additionally
implements the `-willPresentMenuForConfiguration:` and `-willDismissMenuForConfiguration:` delegate
hooks and adds animation completion blocks that call out to the global `TestController`.
This allows us to passively observe calls to
`-editMenuInteraction:(will|did)PresentMenuForConfiguration:animator:`, without affecting the
internal UIKit implementation of the text interaction assistant's edit menu interaction.
(-[EditMenuInteractionDelegateWrapper initWithDelegate:]):
(-[EditMenuInteractionDelegateWrapper forwardInvocation:]):
(-[EditMenuInteractionDelegateWrapper respondsToSelector:]):
(-[EditMenuInteractionDelegateWrapper methodSignatureForSelector:]):
(-[EditMenuInteractionDelegateWrapper editMenuInteraction:willPresentMenuForConfiguration:animator:]):
(-[EditMenuInteractionDelegateWrapper editMenuInteraction:willDismissMenuForConfiguration:animator:]):
(-[UIEditMenuInteraction swizzled_initWithDelegate:]):
(WTR::EditMenuInteractionSwizzler::EditMenuInteractionSwizzler):
(WTR::EditMenuInteractionSwizzler::~EditMenuInteractionSwizzler):
* Tools/WebKitTestRunner/ios/TestControllerIOS.mm:
(WTR::TestController::platformInitialize):
Initialize the edit menu interaction swizzler.
(WTR::TestController::platformResetStateToConsistentValues):
(WTR::TestController::didPresentEditMenuInteraction):
(WTR::TestController::didDismissEditMenuInteraction):
* Tools/WebKitTestRunner/ios/UIScriptControllerIOS.mm:
(WTR::UIScriptControllerIOS::rectForMenuAction const):
Canonical link: https://commits.webkit.org/251737@main
git-svn-id: http://svn.webkit.org/repository/webkit/trunk@295732 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/LayoutTests/editing/selection/ios/show-callout-bar-after-selecting-word.html b/LayoutTests/editing/selection/ios/show-callout-bar-after-selecting-word.html
index d89d5d5..8f2b0b7 100644
--- a/LayoutTests/editing/selection/ios/show-callout-bar-after-selecting-word.html
+++ b/LayoutTests/editing/selection/ios/show-callout-bar-after-selecting-word.html
@@ -27,9 +27,9 @@
<script>
jsTestIsAsync = true;
-async function waitUntilMenuContainsCopyAction() {
+async function waitUntilMenuContains(action) {
while (true) {
- if (await UIHelper.rectForMenuAction("Copy"))
+ if (await UIHelper.rectForMenuAction(action))
break;
await UIHelper.delayFor(100);
}
@@ -48,11 +48,12 @@
await UIHelper.waitForMenuToShow();
testPassed("Showed menu by tapping");
+ await waitUntilMenuContains("Select");
await UIHelper.chooseMenuAction("Select");
await UIHelper.waitForSelectionToAppear();
testPassed("Selected word using menu action");
- await waitUntilMenuContainsCopyAction();
+ await waitUntilMenuContains("Copy");
testPassed("Callout bar contains 'Copy' action");
editor.remove();
diff --git a/Source/WTF/wtf/PlatformHave.h b/Source/WTF/wtf/PlatformHave.h
index d904b40..8b81ebd 100644
--- a/Source/WTF/wtf/PlatformHave.h
+++ b/Source/WTF/wtf/PlatformHave.h
@@ -1107,10 +1107,14 @@
#define HAVE_CORE_LOCATION_WEBSITE_IDENTIFIERS 1
#endif
-#if !defined(HAVE_TEXT_INTERACTION_WITH_CONTEXT_MENU_INTERACTION) \
- && PLATFORM(IOS) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 160000
+#if PLATFORM(IOS) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 160000
+#if !defined(HAVE_TEXT_INTERACTION_WITH_CONTEXT_MENU_INTERACTION)
#define HAVE_TEXT_INTERACTION_WITH_CONTEXT_MENU_INTERACTION 1
#endif
+#if !defined(HAVE_UI_EDIT_MENU_INTERACTION)
+#define HAVE_UI_EDIT_MENU_INTERACTION 1
+#endif
+#endif
#if PLATFORM(COCOA)
#define HAVE_CORE_LOCATION 1
diff --git a/Tools/WebKitTestRunner/TestController.h b/Tools/WebKitTestRunner/TestController.h
index 21361fb..82c3772 100644
--- a/Tools/WebKitTestRunner/TestController.h
+++ b/Tools/WebKitTestRunner/TestController.h
@@ -45,8 +45,13 @@
#include "InstanceMethodSwizzler.h"
#endif
+#if HAVE(UI_EDIT_MENU_INTERACTION)
+#include "EditMenuInteractionSwizzler.h"
+#endif
+
OBJC_CLASS NSString;
OBJC_CLASS UIKeyboardInputMode;
+OBJC_CLASS UIEditMenuInteraction;
OBJC_CLASS UIPasteboardConsistencyEnforcer;
OBJC_CLASS WKWebViewConfiguration;
@@ -381,6 +386,11 @@
PlatformWebView* createOtherPlatformWebView(PlatformWebView* parentView, WKPageConfigurationRef, WKNavigationActionRef, WKWindowFeaturesRef);
+#if HAVE(UI_EDIT_MENU_INTERACTION)
+ void didPresentEditMenuInteraction(UIEditMenuInteraction *);
+ void didDismissEditMenuInteraction(UIEditMenuInteraction *);
+#endif
+
private:
WKRetainPtr<WKPageConfigurationRef> generatePageConfiguration(const TestOptions&);
WKRetainPtr<WKContextConfigurationRef> generateContextConfiguration(const TestOptions&) const;
@@ -594,6 +604,10 @@
Vector<std::unique_ptr<InstanceMethodSwizzler>> m_presentPopoverSwizzlers;
#endif
+#if HAVE(UI_EDIT_MENU_INTERACTION)
+ std::unique_ptr<EditMenuInteractionSwizzler> m_editMenuInteractionSwizzler;
+#endif
+
enum State {
Initial,
Resetting,
diff --git a/Tools/WebKitTestRunner/WebKitTestRunner.xcodeproj/project.pbxproj b/Tools/WebKitTestRunner/WebKitTestRunner.xcodeproj/project.pbxproj
index eef5785..7a30064 100644
--- a/Tools/WebKitTestRunner/WebKitTestRunner.xcodeproj/project.pbxproj
+++ b/Tools/WebKitTestRunner/WebKitTestRunner.xcodeproj/project.pbxproj
@@ -166,6 +166,8 @@
F44A531721B899E200DBB99C /* ClassMethodSwizzler.mm in Sources */ = {isa = PBXBuildFile; fileRef = F44A531421B899DA00DBB99C /* ClassMethodSwizzler.mm */; };
F44A531821B899E500DBB99C /* InstanceMethodSwizzler.mm in Sources */ = {isa = PBXBuildFile; fileRef = F44A531621B899DA00DBB99C /* InstanceMethodSwizzler.mm */; };
F46240B1217013E500917B16 /* UIScriptControllerCocoa.mm in Sources */ = {isa = PBXBuildFile; fileRef = F46240AF2170128300917B16 /* UIScriptControllerCocoa.mm */; };
+ F47A0ED2285FB1C1001D0ECE /* EditMenuInteractionSwizzler.mm in Sources */ = {isa = PBXBuildFile; fileRef = F47A0ECF285FB1B3001D0ECE /* EditMenuInteractionSwizzler.mm */; };
+ F47A0ED3285FB2AE001D0ECE /* EditMenuInteractionSwizzler.h in Headers */ = {isa = PBXBuildFile; fileRef = F47A0ECE285FB1B3001D0ECE /* EditMenuInteractionSwizzler.h */; };
F4C3578C20E8444600FA0748 /* LayoutTestSpellChecker.mm in Sources */ = {isa = PBXBuildFile; fileRef = F4C3578A20E8444000FA0748 /* LayoutTestSpellChecker.mm */; };
F4FED324235823A3003C139C /* NSPasteboardAdditions.mm in Sources */ = {isa = PBXBuildFile; fileRef = F4FED3222358215E003C139C /* NSPasteboardAdditions.mm */; };
/* End PBXBuildFile section */
@@ -447,6 +449,8 @@
F44A531521B899DA00DBB99C /* ClassMethodSwizzler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ClassMethodSwizzler.h; path = ../TestRunnerShared/cocoa/ClassMethodSwizzler.h; sourceTree = "<group>"; };
F44A531621B899DA00DBB99C /* InstanceMethodSwizzler.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = InstanceMethodSwizzler.mm; path = ../TestRunnerShared/cocoa/InstanceMethodSwizzler.mm; sourceTree = "<group>"; };
F46240AF2170128300917B16 /* UIScriptControllerCocoa.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = UIScriptControllerCocoa.mm; sourceTree = "<group>"; };
+ F47A0ECE285FB1B3001D0ECE /* EditMenuInteractionSwizzler.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EditMenuInteractionSwizzler.h; sourceTree = "<group>"; };
+ F47A0ECF285FB1B3001D0ECE /* EditMenuInteractionSwizzler.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = EditMenuInteractionSwizzler.mm; sourceTree = "<group>"; };
F4C3578A20E8444000FA0748 /* LayoutTestSpellChecker.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = LayoutTestSpellChecker.mm; path = ../TestRunnerShared/cocoa/LayoutTestSpellChecker.mm; sourceTree = "<group>"; };
F4C3578B20E8444000FA0748 /* LayoutTestSpellChecker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = LayoutTestSpellChecker.h; path = ../TestRunnerShared/cocoa/LayoutTestSpellChecker.h; sourceTree = "<group>"; };
F4FED3212358215E003C139C /* NSPasteboardAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NSPasteboardAdditions.h; path = ../TestRunnerShared/mac/NSPasteboardAdditions.h; sourceTree = "<group>"; };
@@ -745,6 +749,8 @@
2EE52D121890A9FB0010ED21 /* ios */ = {
isa = PBXGroup;
children = (
+ F47A0ECE285FB1B3001D0ECE /* EditMenuInteractionSwizzler.h */,
+ F47A0ECF285FB1B3001D0ECE /* EditMenuInteractionSwizzler.mm */,
4430AE181F82C4EF0099915A /* GeneratedTouchesDebugWindow.h */,
4430AE171F82C4EE0099915A /* GeneratedTouchesDebugWindow.mm */,
0FEBF8581BB61DF20028722D /* HIDEventGenerator.h */,
@@ -996,6 +1002,7 @@
buildActionMask = 2147483647;
files = (
2DB6187E1D7D58D400978D19 /* CoreGraphicsTestSPI.h in Headers */,
+ F47A0ED3285FB2AE001D0ECE /* EditMenuInteractionSwizzler.h in Headers */,
2DD4C49A1D6E7D3B0007379C /* EventSerializerMac.h in Headers */,
0F73B5521BA78968004B3EF4 /* JSUIScriptController.h in Headers */,
2DFA98481D7F70CF00AFF2C9 /* SharedEventStreamsMac.h in Headers */,
@@ -1225,6 +1232,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
+ F47A0ED2285FB1C1001D0ECE /* EditMenuInteractionSwizzler.mm in Sources */,
2E749BF21891EBFA007FC175 /* EventSenderProxyIOS.mm in Sources */,
4430AE191F82C4FD0099915A /* GeneratedTouchesDebugWindow.mm in Sources */,
0FEBF85A1BB61DF20028722D /* HIDEventGenerator.mm in Sources */,
@@ -1284,11 +1292,11 @@
41C5378E21F13414008B1FAD /* TestWebsiteDataStoreDelegate.mm in Sources */,
0F18E6E51D6B9B9E0027E547 /* UIScriptContext.cpp in Sources */,
F46240B1217013E500917B16 /* UIScriptControllerCocoa.mm in Sources */,
- 51998A082810FBD1009D68EB /* WebNotificationProviderCocoa.mm in Sources */,
277CCEDD250F300A0050C572 /* UIScriptControllerCommon.cpp in Sources */,
0F73B55C1BA89042004B3EF4 /* UIScriptControllerIOS.mm in Sources */,
0F18E6E61D6B9BA20027E547 /* UIScriptControllerShared.cpp in Sources */,
A18510431B9AE14500744AEB /* WebNotificationProvider.cpp in Sources */,
+ 51998A082810FBD1009D68EB /* WebNotificationProviderCocoa.mm in Sources */,
A18510441B9AE14A00744AEB /* WorkQueueManager.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
diff --git a/Tools/WebKitTestRunner/cocoa/TestRunnerWKWebView.h b/Tools/WebKitTestRunner/cocoa/TestRunnerWKWebView.h
index 5616c2b..7151e94 100644
--- a/Tools/WebKitTestRunner/cocoa/TestRunnerWKWebView.h
+++ b/Tools/WebKitTestRunner/cocoa/TestRunnerWKWebView.h
@@ -25,6 +25,9 @@
#import <WebKit/WebKit.h>
+@class UIEditMenuInteraction;
+@class UITextEffectsWindow;
+
@interface WKWebView(SpellChecking)
- (IBAction)toggleContinuousSpellChecking:(id)sender;
@end
@@ -46,6 +49,7 @@
@property (nonatomic, copy) void (^rotationDidEndCallback)(void);
@property (nonatomic, copy) void (^windowTapRecognizedCallback)(void);
@property (nonatomic, copy) NSString *accessibilitySpeakSelectionContent;
+@property (nonatomic, readonly) UITextEffectsWindow *textEffectsWindow;
- (void)setAllowedMenuActions:(NSArray<NSString *> *)actions;
@@ -86,4 +90,10 @@
- (void)_didLoadAppInitiatedRequest:(void (^)(BOOL result))completionHandler;
- (void)_didLoadNonAppInitiatedRequest:(void (^)(BOOL result))completionHandler;
+#if HAVE(UI_EDIT_MENU_INTERACTION)
+- (void)immediatelyDismissEditMenuInteractionIfNeeded;
+- (void)didPresentEditMenuInteraction:(UIEditMenuInteraction *)interaction;
+- (void)didDismissEditMenuInteraction:(UIEditMenuInteraction *)interaction;
+#endif
+
@end
diff --git a/Tools/WebKitTestRunner/cocoa/TestRunnerWKWebView.mm b/Tools/WebKitTestRunner/cocoa/TestRunnerWKWebView.mm
index 806f4b0..d71a7bc 100644
--- a/Tools/WebKitTestRunner/cocoa/TestRunnerWKWebView.mm
+++ b/Tools/WebKitTestRunner/cocoa/TestRunnerWKWebView.mm
@@ -223,6 +223,11 @@
#if PLATFORM(IOS_FAMILY)
+- (UITextEffectsWindow *)textEffectsWindow
+{
+ return [UITextEffectsWindow sharedTextEffectsWindowForWindowScene:self.window.windowScene];
+}
+
- (void)_willHideMenu
{
self.dismissingMenu = YES;
@@ -629,4 +634,41 @@
[super _didLoadNonAppInitiatedRequest:completionHandler];
}
+#if HAVE(UI_EDIT_MENU_INTERACTION)
+
+- (UIEditMenuInteraction *)currentEditMenuInteraction
+{
+ for (id<UIInteraction> interaction in self.contentView.interactions) {
+ if (auto *editMenuInteraction = dynamic_objc_cast<UIEditMenuInteraction>(interaction))
+ return editMenuInteraction;
+ }
+ return nil;
+}
+
+- (void)didPresentEditMenuInteraction:(UIEditMenuInteraction *)interaction
+{
+ if (interaction == self.currentEditMenuInteraction)
+ [self _didShowMenu];
+}
+
+- (void)didDismissEditMenuInteraction:(UIEditMenuInteraction *)interaction
+{
+ if (interaction == self.currentEditMenuInteraction)
+ [self _didHideMenu];
+}
+
+- (void)immediatelyDismissEditMenuInteractionIfNeeded
+{
+ if (!self.isShowingMenu)
+ return;
+
+ self.showingMenu = NO;
+
+ [UIView performWithoutAnimation:^{
+ [self.currentEditMenuInteraction dismissMenu];
+ }];
+}
+
+#endif // HAVE(UI_EDIT_MENU_INTERACTION)
+
@end
diff --git a/Tools/WebKitTestRunner/ios/EditMenuInteractionSwizzler.h b/Tools/WebKitTestRunner/ios/EditMenuInteractionSwizzler.h
new file mode 100644
index 0000000..6ad9b4a
--- /dev/null
+++ b/Tools/WebKitTestRunner/ios/EditMenuInteractionSwizzler.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2022 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
+
+#if HAVE(UI_EDIT_MENU_INTERACTION)
+
+#import <objc/runtime.h>
+#import <wtf/FastMalloc.h>
+
+namespace WTR {
+
+class EditMenuInteractionSwizzler {
+ WTF_MAKE_FAST_ALLOCATED;
+public:
+ EditMenuInteractionSwizzler();
+ ~EditMenuInteractionSwizzler();
+
+private:
+ Method m_originalMethod;
+ Method m_swizzledMethod;
+ IMP m_originalImplementation;
+ IMP m_swizzledImplementation;
+};
+
+} // namespace WTR
+
+#endif // HAVE(UI_EDIT_MENU_INTERACTION)
diff --git a/Tools/WebKitTestRunner/ios/EditMenuInteractionSwizzler.mm b/Tools/WebKitTestRunner/ios/EditMenuInteractionSwizzler.mm
new file mode 100644
index 0000000..eee9e5a
--- /dev/null
+++ b/Tools/WebKitTestRunner/ios/EditMenuInteractionSwizzler.mm
@@ -0,0 +1,137 @@
+/*
+ * Copyright (C) 2022 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 "EditMenuInteractionSwizzler.h"
+
+#if HAVE(UI_EDIT_MENU_INTERACTION)
+
+#import "TestController.h"
+#import <UIKit/UIKit.h>
+#import <wtf/WeakObjCPtr.h>
+
+@interface EditMenuInteractionDelegateWrapper : NSObject<UIEditMenuInteractionDelegate>
+- (instancetype)initWithDelegate:(id<UIEditMenuInteractionDelegate>)delegate;
+@end
+
+@implementation EditMenuInteractionDelegateWrapper {
+ __weak id<UIEditMenuInteractionDelegate> _delegate;
+}
+
+- (instancetype)initWithDelegate:(id<UIEditMenuInteractionDelegate>)delegate
+{
+ if (!(self = [super init]))
+ return nil;
+
+ _delegate = delegate;
+ return self;
+}
+
+- (void)forwardInvocation:(NSInvocation *)invocation
+{
+ if (![_delegate respondsToSelector:invocation.selector])
+ return;
+
+ @try {
+ [invocation invokeWithTarget:_delegate];
+ } @catch (id exception) {
+ NSLog(@"Caught exception: %@ while forwarding invocation: %@", exception, invocation);
+ }
+}
+
+- (BOOL)respondsToSelector:(SEL)selector
+{
+ return [super respondsToSelector:selector] || [_delegate respondsToSelector:selector];
+}
+
+- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
+{
+ return [(NSObject *)_delegate methodSignatureForSelector:selector];
+}
+
+#pragma mark - UIEditMenuInteractionDelegate
+
+- (void)editMenuInteraction:(UIEditMenuInteraction *)interaction willPresentMenuForConfiguration:(UIEditMenuConfiguration *)configuration animator:(id<UIEditMenuInteractionAnimating>)animator
+{
+ if ([_delegate respondsToSelector:@selector(editMenuInteraction:willPresentMenuForConfiguration:animator:)])
+ [_delegate editMenuInteraction:interaction willPresentMenuForConfiguration:configuration animator:animator];
+
+ [animator addCompletion:[weakInteraction = WeakObjCPtr<UIEditMenuInteraction>(interaction)] {
+ if (auto strongInteraction = weakInteraction.get())
+ WTR::TestController::singleton().didPresentEditMenuInteraction(strongInteraction.get());
+ }];
+}
+
+- (void)editMenuInteraction:(UIEditMenuInteraction *)interaction willDismissMenuForConfiguration:(UIEditMenuConfiguration *)configuration animator:(id<UIEditMenuInteractionAnimating>)animator
+{
+ if ([_delegate respondsToSelector:@selector(editMenuInteraction:willDismissMenuForConfiguration:animator:)])
+ [_delegate editMenuInteraction:interaction willDismissMenuForConfiguration:configuration animator:animator];
+
+ [animator addCompletion:[weakInteraction = WeakObjCPtr<UIEditMenuInteraction>(interaction)] {
+ if (auto strongInteraction = weakInteraction.get())
+ WTR::TestController::singleton().didDismissEditMenuInteraction(strongInteraction.get());
+ }];
+}
+
+@end
+
+@interface UIEditMenuInteraction (WebKitTestRunner)
+- (instancetype)swizzled_initWithDelegate:(id<UIEditMenuInteractionDelegate>)delegate;
+@end
+
+@implementation UIEditMenuInteraction (WebKitTestRunner)
+
+- (instancetype)swizzled_initWithDelegate:(id<UIEditMenuInteractionDelegate>)delegate
+{
+ static void* delegateWrapperKey;
+
+ auto delegateWrapper = adoptNS([[EditMenuInteractionDelegateWrapper alloc] initWithDelegate:delegate]);
+ objc_setAssociatedObject(delegate, &delegateWrapperKey, delegateWrapper.get(), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ return [self swizzled_initWithDelegate:delegateWrapper.get()];
+}
+
+@end
+
+namespace WTR {
+
+EditMenuInteractionSwizzler::EditMenuInteractionSwizzler()
+ : m_originalMethod(class_getInstanceMethod(UIEditMenuInteraction.class, @selector(initWithDelegate:)))
+ , m_swizzledMethod(class_getInstanceMethod(UIEditMenuInteraction.class, @selector(swizzled_initWithDelegate:)))
+{
+ m_originalImplementation = method_getImplementation(m_originalMethod);
+ m_swizzledImplementation = method_getImplementation(m_swizzledMethod);
+ class_replaceMethod(UIEditMenuInteraction.class, @selector(swizzled_initWithDelegate:), m_originalImplementation, method_getTypeEncoding(m_originalMethod));
+ class_replaceMethod(UIEditMenuInteraction.class, @selector(initWithDelegate:), m_swizzledImplementation, method_getTypeEncoding(m_swizzledMethod));
+}
+
+EditMenuInteractionSwizzler::~EditMenuInteractionSwizzler()
+{
+ class_replaceMethod(UIEditMenuInteraction.class, @selector(swizzled_initWithDelegate:), m_swizzledImplementation, method_getTypeEncoding(m_originalMethod));
+ class_replaceMethod(UIEditMenuInteraction.class, @selector(initWithDelegate:), m_originalImplementation, method_getTypeEncoding(m_swizzledMethod));
+}
+
+} // namespace WTR
+
+#endif // HAVE(UI_EDIT_MENU_INTERACTION)
diff --git a/Tools/WebKitTestRunner/ios/TestControllerIOS.mm b/Tools/WebKitTestRunner/ios/TestControllerIOS.mm
index 4a11e87..86e4c76 100644
--- a/Tools/WebKitTestRunner/ios/TestControllerIOS.mm
+++ b/Tools/WebKitTestRunner/ios/TestControllerIOS.mm
@@ -26,6 +26,7 @@
#import "config.h"
#import "TestController.h"
+#import "EditMenuInteractionSwizzler.h"
#import "GeneratedTouchesDebugWindow.h"
#import "HIDEventGenerator.h"
#import "IOSLayoutTestCommunication.h"
@@ -114,6 +115,10 @@
CFNotificationCenterAddObserver(center, this, handleMenuWillHideNotification, (CFStringRef)UIMenuControllerWillHideMenuNotification, nullptr, CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(center, this, handleMenuDidHideNotification, (CFStringRef)UIMenuControllerDidHideMenuNotification, nullptr, CFNotificationSuspensionBehaviorDeliverImmediately);
ALLOW_DEPRECATED_DECLARATIONS_END
+
+#if HAVE(UI_EDIT_MENU_INTERACTION)
+ m_editMenuInteractionSwizzler = makeUnique<EditMenuInteractionSwizzler>();
+#endif
}
void TestController::platformDestroy()
@@ -247,6 +252,10 @@
shouldRestoreFirstResponder = [webView resignFirstResponder];
[webView immediatelyDismissContextMenuIfNeeded];
+
+#if HAVE(UI_EDIT_MENU_INTERACTION)
+ [webView immediatelyDismissEditMenuInteractionIfNeeded];
+#endif
}
UIMenuController.sharedMenuController.menuVisible = NO;
@@ -397,4 +406,20 @@
return m_pasteboardConsistencyEnforcer.get();
}
+#if HAVE(UI_EDIT_MENU_INTERACTION)
+
+void TestController::didPresentEditMenuInteraction(UIEditMenuInteraction *interaction)
+{
+ if (auto* webView = mainWebView())
+ [webView->platformView() didPresentEditMenuInteraction:interaction];
+}
+
+void TestController::didDismissEditMenuInteraction(UIEditMenuInteraction *interaction)
+{
+ if (auto* webView = mainWebView())
+ [webView->platformView() didDismissEditMenuInteraction:interaction];
+}
+
+#endif // HAVE(UI_EDIT_MENU_INTERACTION)
+
} // namespace WTR
diff --git a/Tools/WebKitTestRunner/ios/UIScriptControllerIOS.mm b/Tools/WebKitTestRunner/ios/UIScriptControllerIOS.mm
index 05adf78..08d84f0 100644
--- a/Tools/WebKitTestRunner/ios/UIScriptControllerIOS.mm
+++ b/Tools/WebKitTestRunner/ios/UIScriptControllerIOS.mm
@@ -1066,7 +1066,6 @@
WebCore::FloatRect UIScriptControllerIOS::rectForMenuAction(CFStringRef action) const
{
UIView *viewForAction = nil;
- UIWindow *window = webView().window;
if (UIView *calloutBar = UICalloutBar.activeCalloutBar; calloutBar.window) {
for (UIButton *button in findAllViewsInHierarchyOfType(calloutBar, UIButton.class)) {
@@ -1082,15 +1081,16 @@
}
}
- if (!viewForAction) {
+ auto searchForLabel = [&](UIWindow *window) -> UILabel * {
for (UILabel *label in findAllViewsInHierarchyOfType(window, UILabel.class)) {
- if (![label.text isEqualToString:(__bridge NSString *)action])
- continue;
-
- viewForAction = label;
- break;
+ if ([label.text isEqualToString:(__bridge NSString *)action])
+ return label;
}
- }
+ return nil;
+ };
+
+ if (!viewForAction)
+ viewForAction = searchForLabel(webView().window) ?: searchForLabel(webView().textEffectsWindow);
if (!viewForAction)
return { };