Title: [290286] trunk
Revision
290286
Author
wenson_hs...@apple.com
Date
2022-02-21 20:03:59 -0800 (Mon, 21 Feb 2022)

Log Message

[iOS] Adjust some behaviors around the "Markup Image" action in the callout bar
https://bugs.webkit.org/show_bug.cgi?id=236980

Reviewed by Aditya Keerthi.

Source/WebKit:

Adjust the following behaviors around the "Markup Image" callout bar item on iOS:
1.  The item should be present as long as the selection range contains a single image item (not only if the
    selection range exactly encompasses a single image element.
2.  The item should appear in the callout bar before other WebKit-client-provided menu controller items.

This patch also adds a new API test to exercise these behaviors and, in doing so, also refactors logic around
determining whether or not to show this item so that it's dependent on a WebKit internal feature instead of just
the system feature flag (with the default value of the internal feature being equal to whether or not the system
feature is enabled). This means we can run tests for these features without requiring the system feature flag to
be enabled.

See below for more details.

Test: ImageAnalysisTests.MenuControllerItems

* Platform/cocoa/TextRecognitionUtilities.mm:
(WebKit::makeTextRecognitionResult):

Drive-by fix for some API tests: only attempt to send the platform image analysis result over IPC if it is a
`VKCImageAnalysis`. In some API tests, we use mock objects here instead, which currently causes the IPC message
send to fail due to encoding failure.

* Shared/Cocoa/WebPreferencesDefaultValuesCocoa.mm:
(WebKit::defaultImageAnalysisMarkupEnabled):
* Shared/WebPreferencesDefaultValues.h:

Make the new internal feature flag default to the system feature flag.

* UIProcess/ios/WKActionSheetAssistant.h:
* UIProcess/ios/WKActionSheetAssistant.mm:
(-[WKActionSheetAssistant defaultActionsForLinkSheet:]):
(-[WKActionSheetAssistant defaultActionsForImageSheet:]):

Consult a new delegate method (`-actionSheetAssistantShouldIncludeCopyCroppedImageAction:`) instead of checking
the system feature flag directly.

* UIProcess/ios/WKContentViewInteraction.mm:
(-[WKContentView updateImageAnalysisMarkupMenuItems:]):

Append the "Markup Image" item at the start of the list of additional menu items.

(-[WKContentView canPerformImageAnalysisMarkup]):

Check the internal feature instead of the system feature flag.

(-[WKContentView performImageAnalysisMarkup:]):

Now that this action can be triggered even when the current selection doens't encompass only a single image, we
need to use `replaceWithPasteboardData()` instead, passing in the element context of the image element we want
to replace.

(-[WKContentView actionSheetAssistantShouldIncludeCopyCroppedImageAction:]):

Only attempt to show the new items if the internal feature is enabled. Consulted by `WKActionSheetAssistant`
above.

* UIProcess/mac/WebContextMenuProxyMac.mm:
(WebKit::WebContextMenuProxyMac::setupServicesMenu):

Check the internal feature instead of the system feature flag.

* WebProcess/WebPage/ios/WebPageIOS.mm:
(WebKit::WebPage::getPlatformEditorState const):

Make a minor adjustment here to bail early only if we find multiple image elements in the selected range,
rather than bailing if we find anything that's not an image element.

Source/WTF:

Add a WebKit internal feature flag to control whether or not the "Markup Image" and related "Copy Cropped Image"
menu items should be enabled on iOS and macOS. This enables us to test these features in API and layout tests
without requiring the corresponding system feature flag to be enabled.

* Scripts/Preferences/WebPreferencesInternal.yaml:

Tools:

Add an API test to verify that "Markup Image" appears as the first non-default callout bar item when a single
image element is in the selection range. The test is comprised of three parts:

1. Select just a single image, and expect the "Markup Image" item.
2. Select all images in the document, and expect no "Markup Image" item.
3. Select a single image and some surrounding text, and expect the "Markup Image" item.

See WebKit/ChangeLog for more details.

* TestWebKitAPI/Tests/WebKitCocoa/ImageAnalysisTests.mm:
(TestWebKitAPI::createWebViewWithTextRecognitionEnhancements):
(TestWebKitAPI::swizzledSetMenuItems):
(TestWebKitAPI::TEST):
* TestWebKitAPI/Tests/WebKitCocoa/multiple-images.html:

Add some text before and after each image so that we can select a single image alongside some text, and exercise
the changes in `WebPage::getPlatformEditorState` (see WebKit changes for more information).

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (290285 => 290286)


--- trunk/Source/WTF/ChangeLog	2022-02-22 03:57:42 UTC (rev 290285)
+++ trunk/Source/WTF/ChangeLog	2022-02-22 04:03:59 UTC (rev 290286)
@@ -1,3 +1,16 @@
+2022-02-21  Wenson Hsieh  <wenson_hs...@apple.com>
+
+        [iOS] Adjust some behaviors around the "Markup Image" action in the callout bar
+        https://bugs.webkit.org/show_bug.cgi?id=236980
+
+        Reviewed by Aditya Keerthi.
+
+        Add a WebKit internal feature flag to control whether or not the "Markup Image" and related "Copy Cropped Image"
+        menu items should be enabled on iOS and macOS. This enables us to test these features in API and layout tests
+        without requiring the corresponding system feature flag to be enabled.
+
+        * Scripts/Preferences/WebPreferencesInternal.yaml:
+
 2022-02-21  Yusuke Suzuki  <ysuz...@apple.com>
 
         [JSC] Temporal.PlainDate should validate input range

Modified: trunk/Source/WTF/Scripts/Preferences/WebPreferencesInternal.yaml (290285 => 290286)


--- trunk/Source/WTF/Scripts/Preferences/WebPreferencesInternal.yaml	2022-02-22 03:57:42 UTC (rev 290285)
+++ trunk/Source/WTF/Scripts/Preferences/WebPreferencesInternal.yaml	2022-02-22 04:03:59 UTC (rev 290286)
@@ -369,6 +369,20 @@
     WebKit:
       default: false
 
+ImageAnalysisMarkupEnabled:
+  type: bool
+  humanReadableName: "Image analysis markup"
+  humanReadableDescription: "Enable image analysis markup"
+  condition: ENABLE(IMAGE_ANALYSIS)
+  exposed: [ WebKit ]
+  defaultValue:
+    WebCore:
+      default: false
+    WebKitLegacy:
+      default: false
+    WebKit:
+      default: defaultImageAnalysisMarkupEnabled()
+
 ImageAnalysisQueueEnabled:
   type: bool
   humanReadableName: "Image analysis queue"

Modified: trunk/Source/WebKit/ChangeLog (290285 => 290286)


--- trunk/Source/WebKit/ChangeLog	2022-02-22 03:57:42 UTC (rev 290285)
+++ trunk/Source/WebKit/ChangeLog	2022-02-22 04:03:59 UTC (rev 290286)
@@ -1,3 +1,77 @@
+2022-02-21  Wenson Hsieh  <wenson_hs...@apple.com>
+
+        [iOS] Adjust some behaviors around the "Markup Image" action in the callout bar
+        https://bugs.webkit.org/show_bug.cgi?id=236980
+
+        Reviewed by Aditya Keerthi.
+
+        Adjust the following behaviors around the "Markup Image" callout bar item on iOS:
+        1.  The item should be present as long as the selection range contains a single image item (not only if the
+            selection range exactly encompasses a single image element.
+        2.  The item should appear in the callout bar before other WebKit-client-provided menu controller items.
+
+        This patch also adds a new API test to exercise these behaviors and, in doing so, also refactors logic around
+        determining whether or not to show this item so that it's dependent on a WebKit internal feature instead of just
+        the system feature flag (with the default value of the internal feature being equal to whether or not the system
+        feature is enabled). This means we can run tests for these features without requiring the system feature flag to
+        be enabled.
+
+        See below for more details.
+
+        Test: ImageAnalysisTests.MenuControllerItems
+
+        * Platform/cocoa/TextRecognitionUtilities.mm:
+        (WebKit::makeTextRecognitionResult):
+
+        Drive-by fix for some API tests: only attempt to send the platform image analysis result over IPC if it is a
+        `VKCImageAnalysis`. In some API tests, we use mock objects here instead, which currently causes the IPC message
+        send to fail due to encoding failure.
+
+        * Shared/Cocoa/WebPreferencesDefaultValuesCocoa.mm:
+        (WebKit::defaultImageAnalysisMarkupEnabled):
+        * Shared/WebPreferencesDefaultValues.h:
+
+        Make the new internal feature flag default to the system feature flag.
+
+        * UIProcess/ios/WKActionSheetAssistant.h:
+        * UIProcess/ios/WKActionSheetAssistant.mm:
+        (-[WKActionSheetAssistant defaultActionsForLinkSheet:]):
+        (-[WKActionSheetAssistant defaultActionsForImageSheet:]):
+
+        Consult a new delegate method (`-actionSheetAssistantShouldIncludeCopyCroppedImageAction:`) instead of checking
+        the system feature flag directly.
+
+        * UIProcess/ios/WKContentViewInteraction.mm:
+        (-[WKContentView updateImageAnalysisMarkupMenuItems:]):
+
+        Append the "Markup Image" item at the start of the list of additional menu items.
+
+        (-[WKContentView canPerformImageAnalysisMarkup]):
+
+        Check the internal feature instead of the system feature flag.
+
+        (-[WKContentView performImageAnalysisMarkup:]):
+
+        Now that this action can be triggered even when the current selection doens't encompass only a single image, we
+        need to use `replaceWithPasteboardData()` instead, passing in the element context of the image element we want
+        to replace.
+
+        (-[WKContentView actionSheetAssistantShouldIncludeCopyCroppedImageAction:]):
+
+        Only attempt to show the new items if the internal feature is enabled. Consulted by `WKActionSheetAssistant`
+        above.
+
+        * UIProcess/mac/WebContextMenuProxyMac.mm:
+        (WebKit::WebContextMenuProxyMac::setupServicesMenu):
+
+        Check the internal feature instead of the system feature flag.
+
+        * WebProcess/WebPage/ios/WebPageIOS.mm:
+        (WebKit::WebPage::getPlatformEditorState const):
+
+        Make a minor adjustment here to bail early only if we find multiple image elements in the selected range,
+        rather than bailing if we find anything that's not an image element.
+
 2022-02-21  Alexander Kanavin  <a...@linutronix.de>
 
         Fix racy parallel build of WebKit2-4.0.gir

Modified: trunk/Source/WebKit/Platform/cocoa/TextRecognitionUtilities.mm (290285 => 290286)


--- trunk/Source/WebKit/Platform/cocoa/TextRecognitionUtilities.mm	2022-02-22 03:57:42 UTC (rev 290285)
+++ trunk/Source/WebKit/Platform/cocoa/TextRecognitionUtilities.mm	2022-02-22 04:03:59 UTC (rev 290286)
@@ -128,7 +128,8 @@
 #endif // ENABLE(DATA_DETECTION)
 
 #if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
-    result.platformData = { RetainPtr { analysis } };
+    if ([analysis isKindOfClass:PAL::getVKCImageAnalysisClass()])
+        result.platformData = analysis;
 #endif
 
     return result;

Modified: trunk/Source/WebKit/Shared/Cocoa/WebPreferencesDefaultValuesCocoa.mm (290285 => 290286)


--- trunk/Source/WebKit/Shared/Cocoa/WebPreferencesDefaultValuesCocoa.mm	2022-02-22 03:57:42 UTC (rev 290285)
+++ trunk/Source/WebKit/Shared/Cocoa/WebPreferencesDefaultValuesCocoa.mm	2022-02-22 04:03:59 UTC (rev 290286)
@@ -84,6 +84,14 @@
     return imageAnalysisQueueSystemFeatureEnabled();
 }
 
+bool defaultImageAnalysisMarkupEnabled()
+{
+    // FIXME: The is- prefix on this helper method is inconsistent with the adjacent system
+    // feature flag checks. This will be fixed upon upstreaming the code from WebKitAdditions,
+    // when these helper methods will be removed entirely.
+    return isImageAnalysisMarkupSystemFeatureEnabled();
+}
+
 #endif // ENABLE(IMAGE_ANALYSIS)
 
 } // namespace WebKit

Modified: trunk/Source/WebKit/Shared/WebPreferencesDefaultValues.h (290285 => 290286)


--- trunk/Source/WebKit/Shared/WebPreferencesDefaultValues.h	2022-02-22 03:57:42 UTC (rev 290285)
+++ trunk/Source/WebKit/Shared/WebPreferencesDefaultValues.h	2022-02-22 04:03:59 UTC (rev 290286)
@@ -118,6 +118,7 @@
 #if ENABLE(IMAGE_ANALYSIS)
 bool defaultTextRecognitionEnhancementsEnabled();
 bool defaultImageAnalysisQueueEnabled();
+bool defaultImageAnalysisMarkupEnabled();
 #endif
 
 } // namespace WebKit

Modified: trunk/Source/WebKit/UIProcess/ios/WKActionSheetAssistant.h (290285 => 290286)


--- trunk/Source/WebKit/UIProcess/ios/WKActionSheetAssistant.h	2022-02-22 03:57:42 UTC (rev 290285)
+++ trunk/Source/WebKit/UIProcess/ios/WKActionSheetAssistant.h	2022-02-22 04:03:59 UTC (rev 290286)
@@ -86,6 +86,7 @@
 - (void)actionSheetAssistant:(WKActionSheetAssistant *)assistant lookUpImage:(UIImage *)image imageURL:(NSURL *)imageURL title:(NSString *)title imageBounds:(CGRect)imageBounds;
 #endif
 #if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+- (BOOL)actionSheetAssistantShouldIncludeCopyCroppedImageAction:(WKActionSheetAssistant *)assistant;
 - (void)actionSheetAssistant:(WKActionSheetAssistant *)assistant copyCroppedImage:(UIImage *)image sourceMIMEType:(NSString *)sourceMIMEType;
 #endif
 @end

Modified: trunk/Source/WebKit/UIProcess/ios/WKActionSheetAssistant.mm (290285 => 290286)


--- trunk/Source/WebKit/UIProcess/ios/WKActionSheetAssistant.mm	2022-02-22 03:57:42 UTC (rev 290285)
+++ trunk/Source/WebKit/UIProcess/ios/WKActionSheetAssistant.mm	2022-02-22 04:03:59 UTC (rev 290286)
@@ -555,7 +555,7 @@
 
     if (elementInfo.type == _WKActivatedElementTypeImage || [elementInfo image]) {
 #if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
-        if (WebKit::isImageAnalysisMarkupSystemFeatureEnabled()) {
+        if ([_delegate respondsToSelector:@selector(actionSheetAssistantShouldIncludeCopyCroppedImageAction:)] && [_delegate actionSheetAssistantShouldIncludeCopyCroppedImageAction:self]) {
             // FIXME (rdar://88834304): This should be additionally gated on the relevant VisionKit SPI.
             [defaultActions addObject:[_WKElementAction _elementActionWithType:_WKElementActionTypeCopyCroppedImage assistant:self]];
         }
@@ -591,7 +591,7 @@
 
     [defaultActions addObject:[_WKElementAction _elementActionWithType:_WKElementActionTypeCopy assistant:self]];
 #if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
-    if (WebKit::isImageAnalysisMarkupSystemFeatureEnabled()) {
+    if ([_delegate respondsToSelector:@selector(actionSheetAssistantShouldIncludeCopyCroppedImageAction:)] && [_delegate actionSheetAssistantShouldIncludeCopyCroppedImageAction:self]) {
         // FIXME (rdar://88834304): This should be additionally gated on the relevant VisionKit SPI.
         [defaultActions addObject:[_WKElementAction _elementActionWithType:_WKElementActionTypeCopyCroppedImage assistant:self]];
     }

Modified: trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm (290285 => 290286)


--- trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm	2022-02-22 03:57:42 UTC (rev 290285)
+++ trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm	2022-02-22 04:03:59 UTC (rev 290286)
@@ -4697,16 +4697,18 @@
 {
     auto currentItem = findMenuItemWithAction(updatedItems, @selector(performImageAnalysisMarkup:));
     auto& editorState = _page->editorState();
-    if (!WebKit::isImageAnalysisMarkupSystemFeatureEnabled() || !self.window || editorState.isMissingPostLayoutData || !editorState.postLayoutData().selectedEditableImage) {
+    if (!_page->preferences().imageAnalysisMarkupEnabled() || !self.window || editorState.isMissingPostLayoutData || !editorState.postLayoutData().selectedEditableImage) {
         if (currentItem)
             [updatedItems removeObject:currentItem];
-    } else if (!currentItem)
-        [updatedItems addObject:adoptNS([[UIMenuItem alloc] initWithTitle:WebCore::contextMenuItemTitleMarkupImage() action:@selector(performImageAnalysisMarkup:)]).get()];
+    } else if (!currentItem) {
+        auto item = adoptNS([[UIMenuItem alloc] initWithTitle:WebCore::contextMenuItemTitleMarkupImage() action:@selector(performImageAnalysisMarkup:)]);
+        [updatedItems insertObject:item.get() atIndex:0];
+    }
 }
 
 - (BOOL)canPerformImageAnalysisMarkup
 {
-    if (!WebKit::isImageAnalysisMarkupSystemFeatureEnabled())
+    if (!_page || !_page->preferences().imageAnalysisMarkupEnabled())
         return NO;
 
     if (!_imageAnalysisMarkupData)
@@ -4723,7 +4725,7 @@
 
     auto [elementContext, image, preferredMIMEType] = *_imageAnalysisMarkupData;
     if (auto [data, type] = WebKit::transcodeWithPreferredMIMEType(image.get(), preferredMIMEType.createCFString().get(), (__bridge CFStringRef)UTTypeTIFF.identifier); data)
-        _page->replaceSelectionWithPasteboardData({ String { type.get() } }, { static_cast<const uint8_t*>([data bytes]), [data length] });
+        _page->replaceWithPasteboardData(elementContext, { String { type.get() } }, { static_cast<const uint8_t*>([data bytes]), [data length] });
 }
 
 - (void)doAfterComputingImageAnalysisResultsForMarkup:(CompletionHandler<void()>&&)completion
@@ -10902,6 +10904,11 @@
 
 #if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
 
+- (BOOL)actionSheetAssistantShouldIncludeCopyCroppedImageAction:(WKActionSheetAssistant *)assistant
+{
+    return _page->preferences().imageAnalysisMarkupEnabled();
+}
+
 - (void)actionSheetAssistant:(WKActionSheetAssistant *)assistant copyCroppedImage:(UIImage *)image sourceMIMEType:(NSString *)sourceMIMEType
 {
     auto changeCount = UIPasteboard.generalPasteboard.changeCount;

Modified: trunk/Source/WebKit/UIProcess/mac/WebContextMenuProxyMac.mm (290285 => 290286)


--- trunk/Source/WebKit/UIProcess/mac/WebContextMenuProxyMac.mm	2022-02-22 03:57:42 UTC (rev 290285)
+++ trunk/Source/WebKit/UIProcess/mac/WebContextMenuProxyMac.mm	2022-02-22 04:03:59 UTC (rev 290286)
@@ -271,7 +271,7 @@
     if (!hasControlledImage)
         [m_menu setShowsStateColumn:YES];
 #if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
-    else if (isImageAnalysisMarkupSystemFeatureEnabled()) {
+    else if (page()->preferences().imageAnalysisMarkupEnabled()) {
         auto markupImageItem = adoptNS([[NSMenuItem alloc] initWithTitle:contextMenuItemTitleMarkupImage() action:@selector(markupImage) keyEquivalent:@""]);
         [markupImageItem setImage:[NSImage imageWithSystemSymbolName:@"person.fill.viewfinder" accessibilityDescription:contextMenuItemTitleMarkupImage()]];
         [markupImageItem setTarget:WKSharingServicePickerDelegate.sharedSharingServicePickerDelegate];

Modified: trunk/Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm (290285 => 290286)


--- trunk/Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm	2022-02-22 03:57:42 UTC (rev 290285)
+++ trunk/Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm	2022-02-22 04:03:59 UTC (rev 290286)
@@ -328,13 +328,16 @@
                     return foundImage;
 
                 for (TextIterator iterator { *selectedRange, { } }; !iterator.atEnd(); iterator.advance()) {
+                    auto imageElement = dynamicDowncast<HTMLImageElement>(iterator.node());
+                    if (!imageElement)
+                        continue;
+
                     if (foundImage) {
                         foundImage = nullptr;
                         break;
                     }
-                    foundImage = dynamicDowncast<HTMLImageElement>(iterator.node());
-                    if (!foundImage)
-                        break;
+
+                    foundImage = imageElement;
                 }
                 return foundImage;
             };

Modified: trunk/Tools/ChangeLog (290285 => 290286)


--- trunk/Tools/ChangeLog	2022-02-22 03:57:42 UTC (rev 290285)
+++ trunk/Tools/ChangeLog	2022-02-22 04:03:59 UTC (rev 290286)
@@ -1,3 +1,28 @@
+2022-02-21  Wenson Hsieh  <wenson_hs...@apple.com>
+
+        [iOS] Adjust some behaviors around the "Markup Image" action in the callout bar
+        https://bugs.webkit.org/show_bug.cgi?id=236980
+
+        Reviewed by Aditya Keerthi.
+
+        Add an API test to verify that "Markup Image" appears as the first non-default callout bar item when a single
+        image element is in the selection range. The test is comprised of three parts:
+
+        1. Select just a single image, and expect the "Markup Image" item.
+        2. Select all images in the document, and expect no "Markup Image" item.
+        3. Select a single image and some surrounding text, and expect the "Markup Image" item.
+
+        See WebKit/ChangeLog for more details.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/ImageAnalysisTests.mm:
+        (TestWebKitAPI::createWebViewWithTextRecognitionEnhancements):
+        (TestWebKitAPI::swizzledSetMenuItems):
+        (TestWebKitAPI::TEST):
+        * TestWebKitAPI/Tests/WebKitCocoa/multiple-images.html:
+
+        Add some text before and after each image so that we can select a single image alongside some text, and exercise
+        the changes in `WebPage::getPlatformEditorState` (see WebKit changes for more information).
+
 2022-02-21  Jon Lee  <jon...@apple.com>
 
         Add test name to the image diff template

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ImageAnalysisTests.mm (290285 => 290286)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ImageAnalysisTests.mm	2022-02-22 03:57:42 UTC (rev 290285)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ImageAnalysisTests.mm	2022-02-22 04:03:59 UTC (rev 290286)
@@ -30,8 +30,10 @@
 #import "ImageAnalysisTestingUtilities.h"
 #import "InstanceMethodSwizzler.h"
 #import "PlatformUtilities.h"
+#import "TestInputDelegate.h"
 #import "TestWKWebView.h"
 #import "WKWebViewConfigurationExtras.h"
+#import <WebCore/LocalizedStrings.h>
 #import <WebKit/WKWebViewPrivate.h>
 #import <WebKit/WKWebViewPrivateForTesting.h>
 #import <pal/cocoa/VisionKitCoreSoftLink.h>
@@ -106,10 +108,9 @@
 {
     RetainPtr configuration = [WKWebViewConfiguration _test_configurationWithTestPlugInClassName:@"WebProcessPlugInWithInternals" configureJSCForTesting:YES];
     for (_WKInternalDebugFeature *feature in WKPreferences._internalDebugFeatures) {
-        if ([feature.key isEqualToString:@"TextRecognitionEnhancementsEnabled"]) {
+        NSString *key = feature.key;
+        if ([key isEqualToString:@"TextRecognitionEnhancementsEnabled"] || [key isEqualToString:@"ImageAnalysisQueueEnabled"] || [key isEqualToString:@"ImageAnalysisMarkupEnabled"])
             [[configuration preferences] _setEnabled:YES forInternalDebugFeature:feature];
-            break;
-        }
     }
     return adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 300, 300) configuration:configuration.get()]);
 }
@@ -260,6 +261,66 @@
     EXPECT_EQ(450U, CGImageGetHeight(lastRequestedImage));
 }
 
+#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS) && PLATFORM(IOS_FAMILY)
+
+static NeverDestroyed<RetainPtr<NSArray<UIMenuItem *>>> gSwizzledMenuItems;
+static void swizzledSetMenuItems(id, SEL, NSArray<UIMenuItem *> *items)
+{
+    gSwizzledMenuItems.get() = items;
+}
+
+static NSArray<UIMenuItem *> *swizzledMenuItems(id, SEL)
+{
+    return gSwizzledMenuItems.get().get();
+}
+
+TEST(ImageAnalysisTests, MenuControllerItems)
+{
+    auto sharedMenuController = UIMenuController.sharedMenuController;
+    InstanceMethodSwizzler menuItemsSwizzler { sharedMenuController.class, @selector(menuItems), reinterpret_cast<IMP>(swizzledMenuItems) };
+    InstanceMethodSwizzler setMenuItemsSwizzler { sharedMenuController.class, @selector(setMenuItems:), reinterpret_cast<IMP>(swizzledSetMenuItems) };
+
+    NSString *testActionName = @"Test action";
+    auto customAction = adoptNS([[UIMenuItem alloc] initWithTitle:testActionName action:@selector(becomeFirstResponder)]);
+    [sharedMenuController setMenuItems:@[customAction.get()]];
+
+    auto webView = createWebViewWithTextRecognitionEnhancements();
+    auto inputDelegate = adoptNS([TestInputDelegate new]);
+    [inputDelegate setFocusStartsInputSessionPolicyHandler:[](WKWebView *, id <_WKFocusedElementInfo>) {
+        return _WKFocusStartsInputSessionPolicyAllow;
+    }];
+
+    [webView _setEditable:YES];
+    [webView _setInputDelegate:inputDelegate.get()];
+    [webView synchronouslyLoadTestPageNamed:@"multiple-images"];
+    [webView objectByEvaluatingJavaScript:@"let image = document.images[0]; getSelection().setBaseAndExtent(image, 0, image, 1);"];
+    [webView waitForNextPresentationUpdate];
+
+    auto hasMenuItemWithTitle = [&] (NSString *title) {
+        for (UIMenuItem *item in sharedMenuController.menuItems) {
+            if ([item.title isEqualToString:title])
+                return YES;
+        }
+        return NO;
+    };
+
+    EXPECT_TRUE(hasMenuItemWithTitle(testActionName));
+    EXPECT_WK_STREQ(sharedMenuController.menuItems.firstObject.title, WebCore::contextMenuItemTitleMarkupImage());
+
+    [webView selectAll:nil];
+    [webView waitForNextPresentationUpdate];
+
+    EXPECT_TRUE(hasMenuItemWithTitle(testActionName));
+    EXPECT_FALSE(hasMenuItemWithTitle(WebCore::contextMenuItemTitleMarkupImage()));
+
+    [webView objectByEvaluatingJavaScript:@"getSelection().setBaseAndExtent(document.body, 0, document.images[0], 1);"];
+    [webView waitForNextPresentationUpdate];
+    EXPECT_TRUE(hasMenuItemWithTitle(testActionName));
+    EXPECT_WK_STREQ(sharedMenuController.menuItems.firstObject.title, WebCore::contextMenuItemTitleMarkupImage());
+}
+
+#endif // ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS) && PLATFORM(IOS_FAMILY)
+
 } // namespace TestWebKitAPI
 
 #endif // ENABLE(IMAGE_ANALYSIS)

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/multiple-images.html (290285 => 290286)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/multiple-images.html	2022-02-22 03:57:42 UTC (rev 290285)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/multiple-images.html	2022-02-22 04:03:59 UTC (rev 290286)
@@ -39,10 +39,15 @@
         </script>
     </head>
     <body style='margin: 0px;'>
+        <p>large-red-square.png</p>
         <img src=""
+        <p>sunset-in-cupertino-200px.png</p>
         <img src=""
+        <p>test.jpg</p>
         <img src=""
+        <p>400x400-green.png</p>
         <img src=""
+        <p>sunset-in-cupertino-100px.tiff</p>
         <img src=""
     </body>
 </html>
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to