Skip to content
Permalink
Browse files
[ iOS ] editing/selection/ios/show-callout-bar-after-selecting-word.h…
…tml 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: https://svn.webkit.org/repository/webkit/trunk@295732 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
whsieh committed Jun 22, 2022
1 parent 12bc4cb commit 6ccebd62d5e006e524bb2011e6f21f963ca556dd
Showing 10 changed files with 305 additions and 14 deletions.
@@ -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();
@@ -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
@@ -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 @@ class TestController {

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 @@ class TestController {
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,
@@ -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;
@@ -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
@@ -223,6 +223,11 @@ - (void)resetInteractionCallbacks

#if PLATFORM(IOS_FAMILY)

- (UITextEffectsWindow *)textEffectsWindow
{
return [UITextEffectsWindow sharedTextEffectsWindowForWindowScene:self.window.windowScene];
}

- (void)_willHideMenu
{
self.dismissingMenu = YES;
@@ -629,4 +634,41 @@ - (void)_didLoadNonAppInitiatedRequest:(void (^)(BOOL result))completionHandler
[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
@@ -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)

0 comments on commit 6ccebd6

Please sign in to comment.