Title: [286106] trunk/Tools
Revision
286106
Author
wenson_hs...@apple.com
Date
2021-11-21 15:41:28 -0800 (Sun, 21 Nov 2021)

Log Message

Add an API test for batch image analysis (-[WKWebView _startImageAnalysis:])
https://bugs.webkit.org/show_bug.cgi?id=233331

Reviewed by Sam Weinig.

Add a new API teset to exercise the `-_startImageAnalysis:` SPI introduced in r285949, along with helper
functions to simulate `VKImageAnalysis` results from VisionKitCore. See below for more details.

* TestWebKitAPI/SourcesCocoa.txt:
* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WebKitCocoa/ImageAnalysisTests.mm:
(-[TestWKWebView waitForImageAnalysisRequests:]):
(TestWebKitAPI::createWebViewWithTextRecognitionEnhancements):
(TestWebKitAPI::swizzledProcessRequestWithResults):
(TestWebKitAPI::swizzledProcessRequestWithError):
(TestWebKitAPI::TEST):
(TestWebKitAPI::swizzledProcessRequest): Deleted.

Add an API test that uses `-_startImageAnalysis:` to perform batch image analysis over 5 image elements on the
page, and verify that the text results are injected back into the page.

* TestWebKitAPI/Tests/WebKitCocoa/multiple-images.html: Added.
* TestWebKitAPI/cocoa/ImageAnalysisTestingUtilities.h: Added.
* TestWebKitAPI/cocoa/ImageAnalysisTestingUtilities.mm: Added.
(-[TestVKQuad initWithTopLeft:topRight:bottomLeft:bottomRight:]):
(-[TestVKWKTextInfo initWithString:quad:]):
(-[TestVKWKTextInfo string]):
(-[TestVKWKTextInfo quad]):
(-[TestVKWKLineInfo initWithString:quad:children:]):
(-[TestVKWKLineInfo children]):
(-[TestVKImageAnalysis initWithLines:]):
(-[TestVKImageAnalysis allLines]):
(TestWebKitAPI::createQuad):
(TestWebKitAPI::createTextInfo):
(TestWebKitAPI::createLineInfo):
(TestWebKitAPI::createImageAnalysis):
(TestWebKitAPI::createImageAnalysisWithSimpleFixedResults):

Add helper functions to create and return mock objects that represent VKImageAnalysis, VKWKLineInfo,
VKWKTextInfo and VKQuad.

Modified Paths

Added Paths

Diff

Modified: trunk/Tools/ChangeLog (286105 => 286106)


--- trunk/Tools/ChangeLog	2021-11-21 21:28:06 UTC (rev 286105)
+++ trunk/Tools/ChangeLog	2021-11-21 23:41:28 UTC (rev 286106)
@@ -1,3 +1,46 @@
+2021-11-21  Wenson Hsieh  <wenson_hs...@apple.com>
+
+        Add an API test for batch image analysis (-[WKWebView _startImageAnalysis:])
+        https://bugs.webkit.org/show_bug.cgi?id=233331
+
+        Reviewed by Sam Weinig.
+
+        Add a new API teset to exercise the `-_startImageAnalysis:` SPI introduced in r285949, along with helper
+        functions to simulate `VKImageAnalysis` results from VisionKitCore. See below for more details.
+
+        * TestWebKitAPI/SourcesCocoa.txt:
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/WebKitCocoa/ImageAnalysisTests.mm:
+        (-[TestWKWebView waitForImageAnalysisRequests:]):
+        (TestWebKitAPI::createWebViewWithTextRecognitionEnhancements):
+        (TestWebKitAPI::swizzledProcessRequestWithResults):
+        (TestWebKitAPI::swizzledProcessRequestWithError):
+        (TestWebKitAPI::TEST):
+        (TestWebKitAPI::swizzledProcessRequest): Deleted.
+
+        Add an API test that uses `-_startImageAnalysis:` to perform batch image analysis over 5 image elements on the
+        page, and verify that the text results are injected back into the page.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/multiple-images.html: Added.
+        * TestWebKitAPI/cocoa/ImageAnalysisTestingUtilities.h: Added.
+        * TestWebKitAPI/cocoa/ImageAnalysisTestingUtilities.mm: Added.
+        (-[TestVKQuad initWithTopLeft:topRight:bottomLeft:bottomRight:]):
+        (-[TestVKWKTextInfo initWithString:quad:]):
+        (-[TestVKWKTextInfo string]):
+        (-[TestVKWKTextInfo quad]):
+        (-[TestVKWKLineInfo initWithString:quad:children:]):
+        (-[TestVKWKLineInfo children]):
+        (-[TestVKImageAnalysis initWithLines:]):
+        (-[TestVKImageAnalysis allLines]):
+        (TestWebKitAPI::createQuad):
+        (TestWebKitAPI::createTextInfo):
+        (TestWebKitAPI::createLineInfo):
+        (TestWebKitAPI::createImageAnalysis):
+        (TestWebKitAPI::createImageAnalysisWithSimpleFixedResults):
+
+        Add helper functions to create and return mock objects that represent VKImageAnalysis, VKWKLineInfo,
+        VKWKTextInfo and VKQuad.
+
 2021-11-21  Andres Gonzalez  <andresg...@apple.com>
 
         Dispatch all AccessibilityUIElement parameterized attribute requests to the AX thread for isolated tree mode.

Modified: trunk/Tools/TestWebKitAPI/SourcesCocoa.txt (286105 => 286106)


--- trunk/Tools/TestWebKitAPI/SourcesCocoa.txt	2021-11-21 21:28:06 UTC (rev 286105)
+++ trunk/Tools/TestWebKitAPI/SourcesCocoa.txt	2021-11-21 23:41:28 UTC (rev 286106)
@@ -26,6 +26,7 @@
 WKWebViewConfigurationExtras.mm
 
 cocoa/DaemonTestUtilities.mm
+cocoa/ImageAnalysisTestingUtilities.mm
 cocoa/NSItemProviderAdditions.mm
 cocoa/PlatformUtilitiesCocoa.mm
 cocoa/TestCocoa.mm

Modified: trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj (286105 => 286106)


--- trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2021-11-21 21:28:06 UTC (rev 286105)
+++ trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2021-11-21 23:41:28 UTC (rev 286106)
@@ -1016,6 +1016,7 @@
 		F42BD7D9245CC508001E207A /* attributedStringNewlineAtEndOfDocument.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = F42BD7D8245CC4E6001E207A /* attributedStringNewlineAtEndOfDocument.html */; };
 		F42D634422A1729F00D2FB3A /* AutocorrectionTestsIOS.mm in Sources */ = {isa = PBXBuildFile; fileRef = F42D634322A1729F00D2FB3A /* AutocorrectionTestsIOS.mm */; };
 		F42DA5161D8CEFE400336F40 /* large-input-field-focus-onload.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = F42DA5151D8CEFDB00336F40 /* large-input-field-focus-onload.html */; };
+		F42F081227449892007E0D90 /* multiple-images.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = F42F0811274497C8007E0D90 /* multiple-images.html */; };
 		F434CA1A22E65BCA005DDB26 /* ScrollToRevealSelection.mm in Sources */ = {isa = PBXBuildFile; fileRef = F434CA1922E65BCA005DDB26 /* ScrollToRevealSelection.mm */; };
 		F4352F9F26D403DE00E605E4 /* editable-responsive-body.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = F4352F9E26D4037000E605E4 /* editable-responsive-body.html */; };
 		F43E3BBF20DADA1E00A4E7ED /* WKScrollViewTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = F43E3BBE20DADA1E00A4E7ED /* WKScrollViewTests.mm */; };
@@ -1464,6 +1465,7 @@
 				9B59F12A2034086F009E63D5 /* mso-list-compat-mode.html in Copy Resources */,
 				9BCD411A206DBCA3001D71BE /* mso-list-on-h4.html in Copy Resources */,
 				9BF356CD202D458500F71160 /* mso-list.html in Copy Resources */,
+				F42F081227449892007E0D90 /* multiple-images.html in Copy Resources */,
 				5797FE331EB15AB100B2F4A0 /* navigation-client-default-crypto.html in Copy Resources */,
 				2E4838472169DF30002F4531 /* nested-lists.html in Copy Resources */,
 				C99B675F1E39736F00FC6C80 /* no-autoplay-with-controls.html in Copy Resources */,
@@ -2937,6 +2939,9 @@
 		F42BD7D8245CC4E6001E207A /* attributedStringNewlineAtEndOfDocument.html */ = {isa = PBXFileReference; lastKnownFileType = text.html; path = attributedStringNewlineAtEndOfDocument.html; sourceTree = "<group>"; };
 		F42D634322A1729F00D2FB3A /* AutocorrectionTestsIOS.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = AutocorrectionTestsIOS.mm; sourceTree = "<group>"; };
 		F42DA5151D8CEFDB00336F40 /* large-input-field-focus-onload.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; name = "large-input-field-focus-onload.html"; path = "Tests/WebKitCocoa/large-input-field-focus-onload.html"; sourceTree = SOURCE_ROOT; };
+		F42F0811274497C8007E0D90 /* multiple-images.html */ = {isa = PBXFileReference; lastKnownFileType = text.html; path = "multiple-images.html"; sourceTree = "<group>"; };
+		F42F081627449FFD007E0D90 /* ImageAnalysisTestingUtilities.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = ImageAnalysisTestingUtilities.mm; path = cocoa/ImageAnalysisTestingUtilities.mm; sourceTree = "<group>"; };
+		F42F081727449FFD007E0D90 /* ImageAnalysisTestingUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ImageAnalysisTestingUtilities.h; path = cocoa/ImageAnalysisTestingUtilities.h; sourceTree = "<group>"; };
 		F434CA1922E65BCA005DDB26 /* ScrollToRevealSelection.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = ScrollToRevealSelection.mm; sourceTree = "<group>"; };
 		F4352F9E26D4037000E605E4 /* editable-responsive-body.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "editable-responsive-body.html"; sourceTree = "<group>"; };
 		F43E3BBE20DADA1E00A4E7ED /* WKScrollViewTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = WKScrollViewTests.mm; sourceTree = "<group>"; };
@@ -3210,6 +3215,8 @@
 				F44D06491F3962E3001A0E29 /* EditingTestHarness.mm */,
 				5C7C24FB237C972300599C91 /* HTTPServer.h */,
 				5C7C24FA237C972300599C91 /* HTTPServer.mm */,
+				F42F081727449FFD007E0D90 /* ImageAnalysisTestingUtilities.h */,
+				F42F081627449FFD007E0D90 /* ImageAnalysisTestingUtilities.mm */,
 				F44A530D21B8976900DBB99C /* InstanceMethodSwizzler.h */,
 				F44A531021B8976900DBB99C /* InstanceMethodSwizzler.mm */,
 				0F139E721A423A2B00F590F5 /* PlatformUtilitiesCocoa.mm */,
@@ -4102,6 +4109,7 @@
 				9B59F12920340854009E63D5 /* mso-list-compat-mode.html */,
 				9BCD4119206D5ED7001D71BE /* mso-list-on-h4.html */,
 				9BF356CC202D44F200F71160 /* mso-list.html */,
+				F42F0811274497C8007E0D90 /* multiple-images.html */,
 				2E4838462169DD42002F4531 /* nested-lists.html */,
 				466C3842210637CE006A88DE /* notify-resourceLoadObserver.html */,
 				CDB5DFFE21360ED800D3E189 /* now-playing.html */,

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ImageAnalysisTests.mm (286105 => 286106)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ImageAnalysisTests.mm	2021-11-21 21:28:06 UTC (rev 286105)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ImageAnalysisTests.mm	2021-11-21 23:41:28 UTC (rev 286106)
@@ -27,6 +27,7 @@
 
 #if ENABLE(IMAGE_ANALYSIS)
 
+#import "ImageAnalysisTestingUtilities.h"
 #import "InstanceMethodSwizzler.h"
 #import "PlatformUtilities.h"
 #import "TestWKWebView.h"
@@ -36,8 +37,47 @@
 #import <pal/cocoa/VisionKitCoreSoftLink.h>
 #import <pal/spi/cocoa/VisionKitCoreSPI.h>
 
+static unsigned gDidProcessRequestCount = 0;
+
+@interface TestWKWebView (ImageAnalysisTests)
+- (void)waitForImageAnalysisRequests:(unsigned)numberOfRequests;
+@end
+
+@implementation TestWKWebView (ImageAnalysisTests)
+
+- (void)waitForImageAnalysisRequests:(unsigned)numberOfRequests
+{
+    TestWebKitAPI::Util::waitForConditionWithLogging([&] {
+        return gDidProcessRequestCount == numberOfRequests;
+    }, 3, @"Timed out waiting for %u image analysis to complete.", numberOfRequests);
+
+    [self waitForNextPresentationUpdate];
+    EXPECT_EQ(gDidProcessRequestCount, numberOfRequests);
+}
+
+@end
+
 namespace TestWebKitAPI {
 
+static RetainPtr<TestWKWebView> createWebViewWithTextRecognitionEnhancements()
+{
+    RetainPtr configuration = [WKWebViewConfiguration _test_configurationWithTestPlugInClassName:@"WebProcessPlugInWithInternals" configureJSCForTesting:YES];
+    for (_WKInternalDebugFeature *feature in WKPreferences._internalDebugFeatures) {
+        if ([feature.key isEqualToString:@"TextRecognitionEnhancementsEnabled"]) {
+            [[configuration preferences] _setEnabled:YES forInternalDebugFeature:feature];
+            break;
+        }
+    }
+    return adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 300, 300) configuration:configuration.get()]);
+}
+
+static void swizzledProcessRequestWithResults(id, SEL, VKImageAnalyzerRequest *, void (^)(double progress), void (^completion)(VKImageAnalysis *, NSError *))
+{
+    gDidProcessRequestCount++;
+    auto analysis = createImageAnalysisWithSimpleFixedResults();
+    completion(analysis.get(), nil);
+}
+
 #if PLATFORM(IOS_FAMILY)
 
 static CGPoint swizzledLocationInView(id, SEL, UIView *)
@@ -45,10 +85,9 @@
     return CGPointMake(100, 100);
 }
 
-static bool gDidProcessRequest = false;
-static void swizzledProcessRequest(id, SEL, VKImageAnalyzerRequest *, void (^)(double progress), void (^completion)(VKImageAnalysis *analysis, NSError *error))
+static void swizzledProcessRequestWithError(id, SEL, VKImageAnalyzerRequest *, void (^)(double progress), void (^completion)(VKImageAnalysis *analysis, NSError *error))
 {
-    gDidProcessRequest = true;
+    gDidProcessRequestCount++;
     completion(nil, [NSError errorWithDomain:NSCocoaErrorDomain code:1 userInfo:nil]);
 }
 
@@ -55,7 +94,7 @@
 TEST(ImageAnalysisTests, DoNotAnalyzeImagesInEditableContent)
 {
     InstanceMethodSwizzler gestureLocationSwizzler { UIGestureRecognizer.class, @selector(locationInView:), reinterpret_cast<IMP>(swizzledLocationInView) };
-    InstanceMethodSwizzler imageAnalysisRequestSwizzler { PAL::getVKImageAnalyzerClass(), @selector(processRequest:progressHandler:completionHandler:), reinterpret_cast<IMP>(swizzledProcessRequest) };
+    InstanceMethodSwizzler imageAnalysisRequestSwizzler { PAL::getVKImageAnalyzerClass(), @selector(processRequest:progressHandler:completionHandler:), reinterpret_cast<IMP>(swizzledProcessRequestWithError) };
 
     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)]);
     [webView _setEditable:YES];
@@ -63,13 +102,13 @@
 
     [webView _imageAnalysisGestureRecognizer].state = UIGestureRecognizerStateBegan;
     [webView waitForNextPresentationUpdate];
-    EXPECT_FALSE(gDidProcessRequest);
+    EXPECT_EQ(gDidProcessRequestCount, 0U);
 }
 
 TEST(ImageAnalysisTests, HandleImageAnalyzerError)
 {
     InstanceMethodSwizzler gestureLocationSwizzler { UIGestureRecognizer.class, @selector(locationInView:), reinterpret_cast<IMP>(swizzledLocationInView) };
-    InstanceMethodSwizzler imageAnalysisRequestSwizzler { PAL::getVKImageAnalyzerClass(), @selector(processRequest:progressHandler:completionHandler:), reinterpret_cast<IMP>(swizzledProcessRequest) };
+    InstanceMethodSwizzler imageAnalysisRequestSwizzler { PAL::getVKImageAnalyzerClass(), @selector(processRequest:progressHandler:completionHandler:), reinterpret_cast<IMP>(swizzledProcessRequestWithError) };
 
     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)]);
     [webView synchronouslyLoadTestPageNamed:@"image"];
@@ -76,11 +115,26 @@
 
     [webView _imageAnalysisGestureRecognizer].state = UIGestureRecognizerStateBegan;
     [webView waitForNextPresentationUpdate];
-    EXPECT_TRUE(gDidProcessRequest);
+    EXPECT_EQ(gDidProcessRequestCount, 1U);
 }
 
 #endif // PLATFORM(IOS_FAMILY)
 
+TEST(ImageAnalysisTests, StartImageAnalysisWithoutIdentifier)
+{
+    InstanceMethodSwizzler imageAnalysisRequestSwizzler { PAL::getVKImageAnalyzerClass(), @selector(processRequest:progressHandler:completionHandler:), reinterpret_cast<IMP>(swizzledProcessRequestWithResults) };
+
+    auto webView = createWebViewWithTextRecognitionEnhancements();
+    [webView synchronouslyLoadTestPageNamed:@"multiple-images"];
+    [webView _startImageAnalysis:nil];
+    [webView waitForImageAnalysisRequests:5];
+
+    NSArray<NSString *> *overlaysAsText = [webView objectByEvaluatingJavaScript:@"imageOverlaysAsText()"];
+    EXPECT_EQ(overlaysAsText.count, 5U);
+    for (NSString *overlayText in overlaysAsText)
+        EXPECT_WK_STREQ(overlayText, @"Foo bar");
+}
+
 } // namespace TestWebKitAPI
 
 #endif // ENABLE(IMAGE_ANALYSIS)

Added: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/multiple-images.html (0 => 286106)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/multiple-images.html	                        (rev 0)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/multiple-images.html	2021-11-21 23:41:28 UTC (rev 286106)
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<html>
+    <head>
+        <meta name='viewport' content='initial-scale=1'>
+        <script>
+            function imageOverlaysAsText()
+            {
+                const result = [];
+                for (const image of Array.from(document.images)) {
+                    const root = internals.shadowRoot(image);
+                    if (!root)
+                        continue;
+
+                    const overlay = root.getElementById("image-overlay");
+                    if (!overlay)
+                        continue;
+
+                    result.push(overlay.textContent);
+                }
+                return result;
+            }
+        </script>
+    </head>
+    <body style='margin: 0px;'>
+        <img src=""
+        <img src=""
+        <img src=""
+        <img src=""
+        <img src=""
+    </body>
+</html>

Added: trunk/Tools/TestWebKitAPI/cocoa/ImageAnalysisTestingUtilities.h (0 => 286106)


--- trunk/Tools/TestWebKitAPI/cocoa/ImageAnalysisTestingUtilities.h	                        (rev 0)
+++ trunk/Tools/TestWebKitAPI/cocoa/ImageAnalysisTestingUtilities.h	2021-11-21 23:41:28 UTC (rev 286106)
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2021 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
+
+#import <wtf/RetainPtr.h>
+
+#if HAVE(VK_IMAGE_ANALYSIS)
+
+@class VKImageAnalysis;
+@class VKQuad;
+@class VKWKTextInfo;
+@class VKWKLineInfo;
+
+namespace TestWebKitAPI {
+
+RetainPtr<VKImageAnalysis> createImageAnalysisWithSimpleFixedResults();
+
+RetainPtr<VKQuad> createQuad(CGPoint topLeft, CGPoint topRight, CGPoint bottomLeft, CGPoint bottomRight);
+RetainPtr<VKWKTextInfo> createTextInfo(NSString *text, VKQuad *);
+RetainPtr<VKWKLineInfo> createLineInfo(NSString *text, VKQuad *, NSArray<VKWKTextInfo *> *);
+RetainPtr<VKImageAnalysis> createImageAnalysis(NSArray<VKWKLineInfo *> *);
+
+} // namespace TestWebKitAPI
+
+#endif // HAVE(VK_IMAGE_ANALYSIS)

Added: trunk/Tools/TestWebKitAPI/cocoa/ImageAnalysisTestingUtilities.mm (0 => 286106)


--- trunk/Tools/TestWebKitAPI/cocoa/ImageAnalysisTestingUtilities.mm	                        (rev 0)
+++ trunk/Tools/TestWebKitAPI/cocoa/ImageAnalysisTestingUtilities.mm	2021-11-21 23:41:28 UTC (rev 286106)
@@ -0,0 +1,168 @@
+/*
+ * Copyright (C) 2021 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 "ImageAnalysisTestingUtilities.h"
+
+#if HAVE(VK_IMAGE_ANALYSIS)
+
+#import <pal/spi/cocoa/VisionKitCoreSPI.h>
+
+@interface TestVKQuad : NSObject
+- (instancetype)initWithTopLeft:(CGPoint)topLeft topRight:(CGPoint)topRight bottomLeft:(CGPoint)bottomLeft bottomRight:(CGPoint)bottomRight;
+@property (nonatomic, readonly) CGPoint topLeft;
+@property (nonatomic, readonly) CGPoint topRight;
+@property (nonatomic, readonly) CGPoint bottomLeft;
+@property (nonatomic, readonly) CGPoint bottomRight;
+@end
+
+@implementation TestVKQuad
+
+- (instancetype)initWithTopLeft:(CGPoint)topLeft topRight:(CGPoint)topRight bottomLeft:(CGPoint)bottomLeft bottomRight:(CGPoint)bottomRight
+{
+    if (!(self = [super init]))
+        return nil;
+
+    _topLeft = topLeft;
+    _topRight = topRight;
+    _bottomLeft = bottomLeft;
+    _bottomRight = bottomRight;
+    return self;
+}
+
+@end
+
+@interface TestVKWKTextInfo : NSObject
+- (instancetype)initWithString:(NSString *)string quad:(VKQuad *)quad;
+@end
+
+@implementation TestVKWKTextInfo {
+    RetainPtr<NSString> _string;
+    RetainPtr<VKQuad> _quad;
+}
+
+- (instancetype)initWithString:(NSString *)string quad:(VKQuad *)quad
+{
+    if (!(self = [super init]))
+        return nil;
+
+    _string = string;
+    _quad = quad;
+    return self;
+}
+
+- (NSString *)string
+{
+    return _string.get();
+}
+
+- (VKQuad *)quad
+{
+    return _quad.get();
+}
+
+@end
+
+@interface TestVKWKLineInfo : TestVKWKTextInfo
+- (instancetype)initWithString:(NSString *)string quad:(VKQuad *)quad children:(NSArray<VKWKTextInfo *> *)children;
+@end
+
+@implementation TestVKWKLineInfo {
+    RetainPtr<NSArray> _children;
+}
+
+- (instancetype)initWithString:(NSString *)string quad:(VKQuad *)quad children:(NSArray<VKWKTextInfo *> *)children
+{
+    if (!(self = [super initWithString:string quad:quad]))
+        return nil;
+
+    _children = children;
+    return self;
+}
+
+- (NSArray<VKWKTextInfo *> *)children
+{
+    return _children.get();
+}
+
+@end
+
+@interface TestVKImageAnalysis : NSObject
+- (instancetype)initWithLines:(NSArray<VKWKLineInfo *> *)lines;
+@end
+
+@implementation TestVKImageAnalysis {
+    RetainPtr<NSArray> _lines;
+}
+
+- (instancetype)initWithLines:(NSArray<VKWKLineInfo *> *)lines
+{
+    if (!(self = [super init]))
+        return nil;
+
+    _lines = lines;
+    return self;
+}
+
+- (NSArray<VKWKLineInfo *> *)allLines
+{
+    return _lines.get();
+}
+
+@end
+
+namespace TestWebKitAPI {
+
+RetainPtr<VKQuad> createQuad(CGPoint topLeft, CGPoint topRight, CGPoint bottomLeft, CGPoint bottomRight)
+{
+    return adoptNS(static_cast<VKQuad *>([[TestVKQuad alloc] initWithTopLeft:topLeft topRight:topRight bottomLeft:bottomLeft bottomRight:bottomRight]));
+}
+
+RetainPtr<VKWKTextInfo> createTextInfo(NSString *string, VKQuad *quad)
+{
+    return adoptNS(static_cast<VKWKTextInfo *>([[TestVKWKTextInfo alloc] initWithString:string quad:quad]));
+}
+
+RetainPtr<VKWKLineInfo> createLineInfo(NSString *string, VKQuad *quad, NSArray<VKWKTextInfo *> *children)
+{
+    return adoptNS(static_cast<VKWKLineInfo *>([[TestVKWKLineInfo alloc] initWithString:string quad:quad children:children]));
+}
+
+RetainPtr<VKImageAnalysis> createImageAnalysis(NSArray<VKWKLineInfo *> *lines)
+{
+    return adoptNS(static_cast<VKImageAnalysis *>([[TestVKImageAnalysis alloc] initWithLines:lines]));
+}
+
+RetainPtr<VKImageAnalysis> createImageAnalysisWithSimpleFixedResults()
+{
+    auto quad = createQuad(CGPointMake(0, 0), CGPointMake(1, 0), CGPointMake(1, 0.5), CGPointMake(0, 0.5));
+    auto text = createTextInfo(@"Foo bar", quad.get());
+    auto line = createLineInfo(@"Foo bar", quad.get(), @[ text.get() ]);
+    return createImageAnalysis(@[ line.get() ]);
+}
+
+} // namespace TestWebKitAPI
+
+#endif // HAVE(VK_IMAGE_ANALYSIS)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to