Title: [293938] trunk
Revision
293938
Author
[email protected]
Date
2022-05-06 18:15:17 -0700 (Fri, 06 May 2022)

Log Message

Don't show "Markup Image" item for images that have already been marked up
https://bugs.webkit.org/show_bug.cgi?id=240133
rdar://92064534

Reviewed by Aditya Keerthi.

Source/WebKit:

Add some logic to avoid showing the "Markup Image" menu item on both macOS (in the context menu) and iOS (in the
callout bar or editing context menu) for image elements that were created and inserted in the process of
invoking "Markup Image". See below for more details.

Tests: ImageAnalysisTests.AllowImageAnalysisMarkupOnce

* UIProcess/Cocoa/WebPageProxyCocoa.mm:
* UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::shouldAllowImageMarkup):

Add an async reply IPC message that the UI process uses to determine whether or not we should enable image
markup for an element corresponding to the given `ElementContext`.

* UIProcess/WebPageProxy.h:

Also move the declaration of `replaceImageWithMarkupResults` into the `ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)`
section.

* UIProcess/ios/WKContentViewInteraction.mm:
(-[WKContentView doAfterComputingImageAnalysisResultsForMarkup:]):
* UIProcess/mac/WebContextMenuProxyMac.mm:
(WebKit::WebContextMenuProxyMac::appendMarkupItemToControlledImageMenuIfNeeded):

Consult `shouldAllowImageMarkup()` before triggering image analysis when determining whether or not we should
include the "Markup Image" item.

* WebProcess/WebPage/Cocoa/WebPageCocoa.mm:
(WebKit::WebPage::replaceImageWithMarkupResults):

Keep track of image elements that were created to replace existing images when performing image markup. To
achieve this, we scan the selection range before the caret after replacing the image to find the newly inserted
image and add it to `m_elementsToExcludeFromMarkup`.

* WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::didCommitLoad):

Clear out the `m_imageElementsToExcludeFromMarkup` vector.

(WebKit::WebPage::shouldAllowImageMarkup const):
* WebProcess/WebPage/WebPage.h:
* WebProcess/WebPage/WebPage.messages.in:

Tools:

Add an API test to verify that the option to Markup Image goes away after invoking Markup Image on an image
element.

* TestWebKitAPI/Tests/WebKitCocoa/ImageAnalysisTests.mm:
(TestWebKitAPI::runMarkupTest):
(TestWebKitAPI::TEST):

Canonical link: https://commits.webkit.org/250384@main

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (293937 => 293938)


--- trunk/Source/WebKit/ChangeLog	2022-05-07 00:48:53 UTC (rev 293937)
+++ trunk/Source/WebKit/ChangeLog	2022-05-07 01:15:17 UTC (rev 293938)
@@ -1,3 +1,53 @@
+2022-05-06  Wenson Hsieh  <[email protected]>
+
+        Don't show "Markup Image" item for images that have already been marked up
+        https://bugs.webkit.org/show_bug.cgi?id=240133
+        rdar://92064534
+
+        Reviewed by Aditya Keerthi.
+
+        Add some logic to avoid showing the "Markup Image" menu item on both macOS (in the context menu) and iOS (in the
+        callout bar or editing context menu) for image elements that were created and inserted in the process of
+        invoking "Markup Image". See below for more details.
+
+        Tests: ImageAnalysisTests.AllowImageAnalysisMarkupOnce
+
+        * UIProcess/Cocoa/WebPageProxyCocoa.mm:
+        * UIProcess/WebPageProxy.cpp:
+        (WebKit::WebPageProxy::shouldAllowImageMarkup):
+
+        Add an async reply IPC message that the UI process uses to determine whether or not we should enable image
+        markup for an element corresponding to the given `ElementContext`.
+
+        * UIProcess/WebPageProxy.h:
+
+        Also move the declaration of `replaceImageWithMarkupResults` into the `ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)`
+        section.
+
+        * UIProcess/ios/WKContentViewInteraction.mm:
+        (-[WKContentView doAfterComputingImageAnalysisResultsForMarkup:]):
+        * UIProcess/mac/WebContextMenuProxyMac.mm:
+        (WebKit::WebContextMenuProxyMac::appendMarkupItemToControlledImageMenuIfNeeded):
+
+        Consult `shouldAllowImageMarkup()` before triggering image analysis when determining whether or not we should
+        include the "Markup Image" item.
+
+        * WebProcess/WebPage/Cocoa/WebPageCocoa.mm:
+        (WebKit::WebPage::replaceImageWithMarkupResults):
+
+        Keep track of image elements that were created to replace existing images when performing image markup. To
+        achieve this, we scan the selection range before the caret after replacing the image to find the newly inserted
+        image and add it to `m_elementsToExcludeFromMarkup`.
+
+        * WebProcess/WebPage/WebPage.cpp:
+        (WebKit::WebPage::didCommitLoad):
+
+        Clear out the `m_imageElementsToExcludeFromMarkup` vector.
+
+        (WebKit::WebPage::shouldAllowImageMarkup const):
+        * WebProcess/WebPage/WebPage.h:
+        * WebProcess/WebPage/WebPage.messages.in:
+
 2022-05-06  Alex Christensen  <[email protected]>
 
         Fix adattributiond build in internal build on older macOS

Modified: trunk/Source/WebKit/UIProcess/Cocoa/WebPageProxyCocoa.mm (293937 => 293938)


--- trunk/Source/WebKit/UIProcess/Cocoa/WebPageProxyCocoa.mm	2022-05-07 00:48:53 UTC (rev 293937)
+++ trunk/Source/WebKit/UIProcess/Cocoa/WebPageProxyCocoa.mm	2022-05-07 01:15:17 UTC (rev 293938)
@@ -916,11 +916,15 @@
     send(Messages::WebPage::ReplaceSelectionWithPasteboardData(types, data));
 }
 
+#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+
 void WebPageProxy::replaceImageWithMarkupResults(const ElementContext& elementContext, const Vector<String>& types, const IPC::DataReference& data)
 {
     send(Messages::WebPage::ReplaceImageWithMarkupResults(elementContext, types, data));
 }
 
+#endif
+
 } // namespace WebKit
 
 #undef MESSAGE_CHECK_COMPLETION

Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.cpp (293937 => 293938)


--- trunk/Source/WebKit/UIProcess/WebPageProxy.cpp	2022-05-07 00:48:53 UTC (rev 293937)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.cpp	2022-05-07 01:15:17 UTC (rev 293938)
@@ -11383,6 +11383,15 @@
     pageClient().cancelElementFullscreenVideoExtraction();
 }
 
+#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+
+void WebPageProxy::shouldAllowImageMarkup(const ElementContext& context, CompletionHandler<void(bool)>&& completion)
+{
+    sendWithAsyncReply(Messages::WebPage::ShouldAllowImageMarkup(context), WTFMove(completion));
+}
+
+#endif
+
 } // namespace WebKit
 
 #undef WEBPAGEPROXY_RELEASE_LOG

Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.h (293937 => 293938)


--- trunk/Source/WebKit/UIProcess/WebPageProxy.h	2022-05-07 00:48:53 UTC (rev 293937)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.h	2022-05-07 01:15:17 UTC (rev 293938)
@@ -1200,7 +1200,6 @@
     void registerWebProcessAccessibilityToken(const IPC::DataReference&);
     // Called by the UI process when it is ready to send its tokens to the web process.
     void registerUIProcessAccessibilityTokens(const IPC::DataReference& elemenToken, const IPC::DataReference& windowToken);
-    void replaceImageWithMarkupResults(const WebCore::ElementContext&, const Vector<String>& types, const IPC::DataReference&);
     void replaceSelectionWithPasteboardData(const Vector<String>& types, const IPC::DataReference&);
     bool readSelectionFromPasteboard(const String& pasteboardName);
     String stringSelectionForPasteboard();
@@ -2101,6 +2100,11 @@
     void extractVideoInElementFullScreen(WebCore::MediaPlayerIdentifier, WebCore::FloatRect videoBounds);
     void cancelVideoExtractionInElementFullScreen();
 
+#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+    void replaceImageWithMarkupResults(const WebCore::ElementContext&, const Vector<String>& types, const IPC::DataReference&);
+    void shouldAllowImageMarkup(const WebCore::ElementContext&, CompletionHandler<void(bool)>&&);
+#endif
+
 private:
     WebPageProxy(PageClient&, WebProcessProxy&, Ref<API::PageConfiguration>&&);
     void platformInitialize();

Modified: trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm (293937 => 293938)


--- trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm	2022-05-07 00:48:53 UTC (rev 293937)
+++ trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm	2022-05-07 01:15:17 UTC (rev 293938)
@@ -4787,26 +4787,14 @@
         return;
     }
 
-    _page->requestImageBitmap(*elementToAnalyze, [context = *elementToAnalyze, completion = WTFMove(completion), weakSelf = WeakObjCPtr<WKContentView>(self)](auto& imageData, auto& sourceMIMEType) mutable {
+    _page->shouldAllowImageMarkup(*elementToAnalyze, [context = *elementToAnalyze, completion = WTFMove(completion), weakSelf = WeakObjCPtr<WKContentView>(self)](bool shouldAllow) mutable {
         auto strongSelf = weakSelf.get();
-        if (!strongSelf) {
+        if (!shouldAllow || !strongSelf) {
             completion();
             return;
         }
 
-        auto imageBitmap = WebKit::ShareableBitmap::create(imageData);
-        if (!imageBitmap) {
-            completion();
-            return;
-        }
-
-        auto cgImage = imageBitmap->makeCGImage();
-        if (!cgImage) {
-            completion();
-            return;
-        }
-
-        WebKit::requestImageAnalysisMarkup(cgImage.get(), [sourceMIMEType, context, completion = WTFMove(completion), weakSelf](CGImageRef result, CGRect) mutable {
+        strongSelf->_page->requestImageBitmap(context, [context, completion = WTFMove(completion), weakSelf = WTFMove(weakSelf)](auto& imageData, auto& sourceMIMEType) mutable {
             auto strongSelf = weakSelf.get();
             if (!strongSelf) {
                 completion();
@@ -4813,11 +4801,31 @@
                 return;
             }
 
-            if (result)
-                strongSelf->_imageAnalysisMarkupData = { { context, { result }, sourceMIMEType } };
-            else
-                strongSelf->_imageAnalysisMarkupData = std::nullopt;
-            completion();
+            auto imageBitmap = WebKit::ShareableBitmap::create(imageData);
+            if (!imageBitmap) {
+                completion();
+                return;
+            }
+
+            auto cgImage = imageBitmap->makeCGImage();
+            if (!cgImage) {
+                completion();
+                return;
+            }
+
+            WebKit::requestImageAnalysisMarkup(cgImage.get(), [sourceMIMEType, context, completion = WTFMove(completion), weakSelf](CGImageRef result, CGRect) mutable {
+                auto strongSelf = weakSelf.get();
+                if (!strongSelf) {
+                    completion();
+                    return;
+                }
+
+                if (result)
+                    strongSelf->_imageAnalysisMarkupData = { { context, { result }, sourceMIMEType } };
+                else
+                    strongSelf->_imageAnalysisMarkupData = std::nullopt;
+                completion();
+            });
         });
     });
 }

Modified: trunk/Source/WebKit/UIProcess/mac/WebContextMenuProxyMac.mm (293937 => 293938)


--- trunk/Source/WebKit/UIProcess/mac/WebContextMenuProxyMac.mm	2022-05-07 00:48:53 UTC (rev 293937)
+++ trunk/Source/WebKit/UIProcess/mac/WebContextMenuProxyMac.mm	2022-05-07 01:15:17 UTC (rev 293938)
@@ -300,31 +300,39 @@
     if (!page || !page->preferences().imageAnalysisMarkupEnabled())
         return;
 
-    auto* imageBitmap = m_context.controlledImage();
-    if (!imageBitmap)
+    auto context = m_context.controlledImageElementContext();
+    if (!context)
         return;
 
-    auto image = imageBitmap->makeCGImage();
-    if (!image)
-        return;
+    page->shouldAllowImageMarkup(*context, [protectedThis = Ref { *this }, weakMenu = WeakObjCPtr<NSMenu> { m_menu.get() }](bool shouldAllow) mutable {
+        if (!shouldAllow)
+            return;
 
-    requestImageAnalysisMarkup(image.get(), [protectedThis = Ref { *this }, weakMenu = WeakObjCPtr<NSMenu> { m_menu.get() }](CGImageRef result, CGRect) {
-        if (!result)
+        auto* imageBitmap = protectedThis->m_context.controlledImage();
+        if (!imageBitmap)
             return;
 
-        auto strongMenu = weakMenu.get();
-        if (!strongMenu)
+        auto image = imageBitmap->makeCGImage();
+        if (!image)
             return;
 
-        auto markupImageItem = adoptNS([[NSMenuItem alloc] initWithTitle:contextMenuItemTitleMarkupImage() action:@selector(markupImage) keyEquivalent:@""]);
-        [markupImageItem setImage:[NSImage imageWithSystemSymbolName:@"person.fill.viewfinder" accessibilityDescription:contextMenuItemTitleMarkupImage()]];
-        [markupImageItem setTarget:WKSharingServicePickerDelegate.sharedSharingServicePickerDelegate];
-        [markupImageItem setAction:@selector(markupImage)];
-        if (auto numberOfItems = [strongMenu numberOfItems])
-            [markupImageItem setIndentationLevel:[strongMenu itemAtIndex:numberOfItems - 1].indentationLevel];
-        [strongMenu addItem:markupImageItem.get()];
+        requestImageAnalysisMarkup(image.get(), [protectedThis = WTFMove(protectedThis), weakMenu = WTFMove(weakMenu)](CGImageRef result, CGRect) {
+            if (!result)
+                return;
 
-        protectedThis->m_croppedImageResult = result;
+            auto strongMenu = weakMenu.get();
+            if (!strongMenu)
+                return;
+
+            auto markupImageItem = adoptNS([[NSMenuItem alloc] initWithTitle:contextMenuItemTitleMarkupImage() action:@selector(markupImage) keyEquivalent:@""]);
+            [markupImageItem setImage:[NSImage imageWithSystemSymbolName:@"person.fill.viewfinder" accessibilityDescription:contextMenuItemTitleMarkupImage()]];
+            [markupImageItem setTarget:WKSharingServicePickerDelegate.sharedSharingServicePickerDelegate];
+            [markupImageItem setAction:@selector(markupImage)];
+            [markupImageItem setIndentationLevel:[strongMenu itemArray].lastObject.indentationLevel];
+            [strongMenu addItem:markupImageItem.get()];
+
+            protectedThis->m_croppedImageResult = result;
+        });
     });
 #endif // ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
 }

Modified: trunk/Source/WebKit/WebProcess/WebPage/Cocoa/WebPageCocoa.mm (293937 => 293938)


--- trunk/Source/WebKit/WebProcess/WebPage/Cocoa/WebPageCocoa.mm	2022-05-07 00:48:53 UTC (rev 293937)
+++ trunk/Source/WebKit/WebProcess/WebPage/Cocoa/WebPageCocoa.mm	2022-05-07 01:15:17 UTC (rev 293938)
@@ -47,6 +47,7 @@
 #import <WebCore/GraphicsContextCG.h>
 #import <WebCore/HTMLBodyElement.h>
 #import <WebCore/HTMLConverter.h>
+#import <WebCore/HTMLImageElement.h>
 #import <WebCore/HTMLOListElement.h>
 #import <WebCore/HTMLUListElement.h>
 #import <WebCore/HitTestResult.h>
@@ -565,6 +566,8 @@
     Vector<String> m_types;
 };
 
+#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+
 void WebPage::replaceImageWithMarkupResults(const ElementContext& elementContext, const Vector<String>& types, const IPC::DataReference& data)
 {
     Ref frame = CheckedRef(m_page->focusController())->focusedOrMainFrame();
@@ -594,6 +597,16 @@
         OverridePasteboardForSelectionReplacement overridePasteboard { types, data };
         IgnoreSelectionChangeForScope ignoreSelectionChanges { frame.get() };
         frame->editor().replaceNodeFromPasteboard(*element, replaceSelectionPasteboardName(), EditAction::MarkupImage);
+
+        auto position = frame->selection().selection().visibleStart();
+        if (auto imageRange = makeSimpleRange(WebCore::VisiblePositionRange { position.previous(), position })) {
+            for (WebCore::TextIterator iterator { *imageRange, { } }; !iterator.atEnd(); iterator.advance()) {
+                if (RefPtr image = dynamicDowncast<HTMLImageElement>(iterator.node())) {
+                    m_elementsToExcludeFromMarkup.add(*image);
+                    break;
+                }
+            }
+        }
     }
 
     constexpr auto restoreSelectionOptions = FrameSelection::defaultSetSelectionOptions(UserTriggered);
@@ -620,6 +633,8 @@
     frame->selection().setSelection(newSelectionRange, restoreSelectionOptions);
 }
 
+#endif // ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+
 void WebPage::replaceSelectionWithPasteboardData(const Vector<String>& types, const IPC::DataReference& data)
 {
     OverridePasteboardForSelectionReplacement overridePasteboard { types, data };

Modified: trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp (293937 => 293938)


--- trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp	2022-05-07 00:48:53 UTC (rev 293937)
+++ trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp	2022-05-07 01:15:17 UTC (rev 293938)
@@ -6731,6 +6731,10 @@
     updateMainFrameScrollOffsetPinning();
 
     updateMockAccessibilityElementAfterCommittingLoad();
+
+#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+    m_elementsToExcludeFromMarkup.clear();
+#endif
 }
 
 void WebPage::didFinishDocumentLoad(WebFrame& frame)
@@ -8098,6 +8102,16 @@
 }
 #endif
 
+#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+
+void WebPage::shouldAllowImageMarkup(const ElementContext& context, CompletionHandler<void(bool)>&& completion) const
+{
+    auto element = elementForContext(context);
+    completion(element && !m_elementsToExcludeFromMarkup.contains(*element));
+}
+
+#endif
+
 } // namespace WebKit
 
 #undef WEBPAGE_RELEASE_LOG

Modified: trunk/Source/WebKit/WebProcess/WebPage/WebPage.h (293937 => 293938)


--- trunk/Source/WebKit/WebProcess/WebPage/WebPage.h	2022-05-07 00:48:53 UTC (rev 293937)
+++ trunk/Source/WebKit/WebProcess/WebPage/WebPage.h	2022-05-07 01:15:17 UTC (rev 293938)
@@ -106,6 +106,7 @@
 #include <wtf/RunLoop.h>
 #include <wtf/Seconds.h>
 #include <wtf/WallTime.h>
+#include <wtf/WeakHashSet.h>
 #include <wtf/text/WTFString.h>
 
 #if USE(ATSPI)
@@ -982,10 +983,13 @@
 #endif
 
 #if PLATFORM(COCOA)
-    void replaceImageWithMarkupResults(const WebCore::ElementContext&, const Vector<String>& types, const IPC::DataReference&);
     void replaceSelectionWithPasteboardData(const Vector<String>& types, const IPC::DataReference&);
 #endif
 
+#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+    void replaceImageWithMarkupResults(const WebCore::ElementContext&, const Vector<String>& types, const IPC::DataReference&);
+#endif
+
     void setCompositionForTesting(const String& compositionString, uint64_t from, uint64_t length, bool suppressUnderline, const Vector<WebCore::CompositionHighlight>&);
     bool hasCompositionForTesting();
     void confirmCompositionForTesting(const String& compositionString);
@@ -1533,6 +1537,10 @@
     void extractVideoInElementFullScreen(const WebCore::HTMLVideoElement&);
     void cancelVideoExtractionInElementFullScreen();
 
+#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+    void shouldAllowImageMarkup(const WebCore::ElementContext&, CompletionHandler<void(bool)>&&) const;
+#endif
+
 private:
     WebPage(WebCore::PageIdentifier, WebPageCreationParameters&&);
 
@@ -2466,6 +2474,10 @@
 #if ENABLE(APP_HIGHLIGHTS)
     WebCore::HighlightVisibility m_appHighlightsVisible { WebCore::HighlightVisibility::Hidden };
 #endif
+
+#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+    WeakHashSet<WebCore::HTMLImageElement> m_elementsToExcludeFromMarkup;
+#endif
 };
 
 #if !PLATFORM(IOS_FAMILY)

Modified: trunk/Source/WebKit/WebProcess/WebPage/WebPage.messages.in (293937 => 293938)


--- trunk/Source/WebKit/WebProcess/WebPage/WebPage.messages.in	2022-05-07 00:48:53 UTC (rev 293937)
+++ trunk/Source/WebKit/WebProcess/WebPage/WebPage.messages.in	2022-05-07 01:15:17 UTC (rev 293938)
@@ -473,7 +473,6 @@
     GetStringSelectionForPasteboard() -> (String stringValue) Synchronous
     GetDataSelectionForPasteboard(String pasteboardType) -> (WebKit::SharedMemory::IPCHandle ipcHandle) Synchronous
     ReadSelectionFromPasteboard(String pasteboardName) -> (bool result) Synchronous
-    ReplaceImageWithMarkupResults(struct WebCore::ElementContext context, Vector<String> types, IPC::DataReference data)
     ReplaceSelectionWithPasteboardData(Vector<String> types, IPC::DataReference data)
 
     ShouldDelayWindowOrderingEvent(WebKit::WebMouseEvent event) -> (bool result) Synchronous
@@ -499,6 +498,11 @@
     RequestAcceptsFirstMouse(int eventNumber, WebKit::WebMouseEvent event)
 #endif
 
+#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+    ReplaceImageWithMarkupResults(struct WebCore::ElementContext context, Vector<String> types, IPC::DataReference data)
+    ShouldAllowImageMarkup(struct WebCore::ElementContext context) -> (bool result)
+#endif
+
     SetAlwaysShowsHorizontalScroller(bool alwaysShowsHorizontalScroller)
     SetAlwaysShowsVerticalScroller(bool alwaysShowsVerticalScroller)
 

Modified: trunk/Tools/ChangeLog (293937 => 293938)


--- trunk/Tools/ChangeLog	2022-05-07 00:48:53 UTC (rev 293937)
+++ trunk/Tools/ChangeLog	2022-05-07 01:15:17 UTC (rev 293938)
@@ -1,3 +1,18 @@
+2022-05-06  Wenson Hsieh  <[email protected]>
+
+        Don't show "Markup Image" item for images that have already been marked up
+        https://bugs.webkit.org/show_bug.cgi?id=240133
+        rdar://92064534
+
+        Reviewed by Aditya Keerthi.
+
+        Add an API test to verify that the option to Markup Image goes away after invoking Markup Image on an image
+        element.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/ImageAnalysisTests.mm:
+        (TestWebKitAPI::runMarkupTest):
+        (TestWebKitAPI::TEST):
+
 2022-05-06  Jonathan Bedard  <[email protected]>
 
         [git-webkit] Forward environment to `git commit`

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ImageAnalysisTests.mm (293937 => 293938)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ImageAnalysisTests.mm	2022-05-07 00:48:53 UTC (rev 293937)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ImageAnalysisTests.mm	2022-05-07 01:15:17 UTC (rev 293938)
@@ -428,7 +428,7 @@
     EXPECT_NOT_NULL([menuBuilder actionWithTitle:WebCore::contextMenuItemTitleMarkupImage()]);
 }
 
-static void runMarkupTest(NSString *testPage, NSString *scriptToSelectText, Function<void(TestWKWebView *, NSString *)>&& checkWebView)
+static RetainPtr<TestWKWebView> runMarkupTest(NSString *testPage, NSString *scriptToSelectText, Function<void(TestWKWebView *, NSString *)>&& checkWebView = { })
 {
     ImageAnalysisMarkupSwizzler swizzler { iconImage().autorelease(), CGRectMake(10, 10, 215, 174) };
 
@@ -446,7 +446,11 @@
 
     NSString *previousSelectedText = [webView selectedText];
     invokeImageMarkupAction(webView.get());
-    checkWebView(webView.get(), previousSelectedText);
+
+    if (checkWebView)
+        checkWebView(webView.get(), previousSelectedText);
+
+    return webView;
 }
 
 TEST(ImageAnalysisTests, PerformImageAnalysisMarkup)
@@ -472,6 +476,22 @@
     });
 }
 
+TEST(ImageAnalysisTests, AllowImageAnalysisMarkupOnce)
+{
+    auto webView = runMarkupTest(@"image", @"getSelection().selectAllChildren(document.body)");
+
+    ImageAnalysisMarkupSwizzler swizzler { iconImage().autorelease(), CGRectMake(10, 10, 215, 174) };
+
+    [webView objectByEvaluatingJavaScript:@"let image = document.images[0]; getSelection().setBaseAndExtent(image, 0, image, 1)"];
+    [webView waitForNextPresentationUpdate];
+    simulateCalloutBarAppearance(webView.get());
+
+    auto menuBuilder = adoptNS([TestUIMenuBuilder new]);
+    [webView buildMenuWithBuilder:menuBuilder.get()];
+
+    EXPECT_NULL([menuBuilder actionWithTitle:WebCore::contextMenuItemTitleMarkupImage()]);
+}
+
 #endif // ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS) && PLATFORM(IOS_FAMILY)
 
 #if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS) && ENABLE(SERVICE_CONTROLS)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to