| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2013 Apple Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions | |
| 6 * are met: | |
| 7 * 1. Redistributions of source code must retain the above copyright | |
| 8 * notice, this list of conditions and the following disclaimer. | |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer in the | |
| 11 * documentation and/or other materials provided with the distribution. | |
| 12 * | |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' | |
| 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | |
| 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | |
| 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
| 23 * THE POSSIBILITY OF SUCH DAMAGE. | |
| 24 */ | |
| 25 | |
| 26 #include "config.h" | |
| 27 #include "PlatformUtilities.h" | |
| 28 #include "PlatformWebView.h" | |
| 29 #import <WebKit/WebDocumentPrivate.h> | |
| 30 #import <WebKit/DOMPrivate.h> | |
| 31 #include <WebKit2/WKImage.h> | |
| 32 #import <wtf/RetainPtr.h> | |
| 33 | |
| 34 @interface FindMatchesWK1FrameLoadDelegate : NSObject { | |
| 35 } | |
| 36 @end | |
| 37 | |
| 38 static bool didFinishLoadWK1; | |
| 39 | |
| 40 @implementation FindMatchesWK1FrameLoadDelegate | |
| 41 | |
| 42 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame | |
| 43 { | |
| 44 didFinishLoadWK1 = true; | |
| 45 } | |
| 46 | |
| 47 @end | |
| 48 | |
| 49 namespace TestWebKitAPI { | |
| 50 | |
| 51 static bool didFinishLoad = false; | |
| 52 static bool didCallFindStringMatches = false; | |
| 53 static bool didCallGetImage = false; | |
| 54 | |
| 55 RetainPtr<WebView> webkit1View; | |
| 56 | |
| 57 static void didFinishLoadForFrame(WKPageRef page, WKFrameRef frame, WKTypeRef us
erData, const void* clientInfo) | |
| 58 { | |
| 59 didFinishLoad = true; | |
| 60 } | |
| 61 | |
| 62 static void didFindStringMatches(WKPageRef page, WKStringRef string, WKArrayRef
matches, int firstIndex, const void* clientInfo) | |
| 63 { | |
| 64 EXPECT_WK_STREQ("Hello", string); | |
| 65 size_t numMatches = WKArrayGetSize(matches); | |
| 66 EXPECT_EQ(3u, numMatches); | |
| 67 | |
| 68 for (size_t i = 0; i < numMatches; ++i) { | |
| 69 WKTypeRef items = WKArrayGetItemAtIndex(matches, i); | |
| 70 WKTypeID type = WKGetTypeID(items); | |
| 71 EXPECT_EQ(type, WKArrayGetTypeID()); | |
| 72 WKArrayRef rects = reinterpret_cast<WKArrayRef>(items); | |
| 73 size_t numRects = WKArrayGetSize(rects); | |
| 74 EXPECT_EQ(1u, numRects); | |
| 75 items = WKArrayGetItemAtIndex(rects, 0); | |
| 76 type = WKGetTypeID(items); | |
| 77 EXPECT_EQ(type, WKRectGetTypeID()); | |
| 78 WKRect rect = WKRectGetValue(reinterpret_cast<WKRectRef>(items)); | |
| 79 rect = rect; | |
| 80 } | |
| 81 didCallFindStringMatches = true; | |
| 82 } | |
| 83 | |
| 84 static void didGetImageForMatchResult(WKPageRef page, WKImageRef image, uint32_t
index, const void* clientInfo) | |
| 85 { | |
| 86 WKSize size = WKImageGetSize(image); | |
| 87 | |
| 88 DOMDocument *document = webkit1View.get().mainFrameDocument; | |
| 89 DOMRange *range = [document createRange]; | |
| 90 DOMNode *target = [document getElementById:@"target"]; | |
| 91 [range selectNode:target]; | |
| 92 NSImage *expectedImage = [range renderedImageForcingBlackText:YES]; | |
| 93 NSSize expectedSize = [expectedImage size]; | |
| 94 EXPECT_EQ(size.width, expectedSize.width); | |
| 95 EXPECT_EQ(size.height, expectedSize.height); | |
| 96 didCallGetImage = true; | |
| 97 } | |
| 98 | |
| 99 TEST(WebKit2, FindMatches) | |
| 100 { | |
| 101 WKRetainPtr<WKContextRef> context(AdoptWK, WKContextCreate()); | |
| 102 PlatformWebView webView(context.get()); | |
| 103 | |
| 104 WKPageLoaderClient loaderClient; | |
| 105 memset(&loaderClient, 0, sizeof(loaderClient)); | |
| 106 | |
| 107 loaderClient.version = 0; | |
| 108 loaderClient.didFinishLoadForFrame = didFinishLoadForFrame; | |
| 109 WKPageSetPageLoaderClient(webView.page(), &loaderClient); | |
| 110 | |
| 111 WKPageFindMatchesClient findMatchesClient; | |
| 112 memset(&findMatchesClient, 0, sizeof(findMatchesClient)); | |
| 113 | |
| 114 findMatchesClient.version = 0; | |
| 115 findMatchesClient.didFindStringMatches = didFindStringMatches; | |
| 116 findMatchesClient.didGetImageForMatchResult = didGetImageForMatchResult; | |
| 117 | |
| 118 WKPageSetPageFindMatchesClient(webView.page(), &findMatchesClient); | |
| 119 | |
| 120 WKRetainPtr<WKURLRef> url(AdoptWK, Util::createURLForResource("find", "html"
)); | |
| 121 WKPageLoadURL(webView.page(), url.get()); | |
| 122 | |
| 123 Util::run(&didFinishLoad); | |
| 124 | |
| 125 WKRetainPtr<WKStringRef> findString(AdoptWK, WKStringCreateWithUTF8CString("
Hello")); | |
| 126 | |
| 127 WKPageFindStringMatches(webView.page(), findString.get(), true, 100); | |
| 128 Util::run(&didCallFindStringMatches); | |
| 129 | |
| 130 webkit1View.adoptNS([[WebView alloc] initWithFrame:NSMakeRect(0, 0, 120, 200
) frameName:nil groupName:nil]); | |
| 131 RetainPtr<FindMatchesWK1FrameLoadDelegate> frameLoadDelegate(AdoptNS, [FindM
atchesWK1FrameLoadDelegate new]); | |
| 132 | |
| 133 webkit1View.get().frameLoadDelegate = frameLoadDelegate.get(); | |
| 134 [webkit1View.get().mainFrame loadHTMLString:@"Test search. Hello <span id=\"
target\">Hello</span> Hello!" baseURL:[NSURL URLWithString:@"about:blank"]]; | |
| 135 | |
| 136 Util::run(&didFinishLoadWK1); | |
| 137 | |
| 138 WKPageGetImageForFindMatch(webView.page(), 0); | |
| 139 Util::run(&didCallGetImage); | |
| 140 | |
| 141 WKPageHideFindUI(webView.page()); | |
| 142 } | |
| 143 | |
| 144 } // namespace TestWebKitAPI | |
| OLD | NEW |