FindMatches.mm 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #include "config.h"
  26. #include "PlatformUtilities.h"
  27. #include "PlatformWebView.h"
  28. #import <WebKit/WebDocumentPrivate.h>
  29. #import <WebKit/DOMPrivate.h>
  30. #include <WebKit2/WKImage.h>
  31. #import <wtf/RetainPtr.h>
  32. @interface FindMatchesWK1FrameLoadDelegate : NSObject {
  33. }
  34. @end
  35. static bool didFinishLoadWK1;
  36. @implementation FindMatchesWK1FrameLoadDelegate
  37. - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
  38. {
  39. didFinishLoadWK1 = true;
  40. }
  41. @end
  42. namespace TestWebKitAPI {
  43. static bool didFinishLoad = false;
  44. static bool didCallFindStringMatches = false;
  45. static bool didCallGetImage = false;
  46. static WKFindOptions findOptions = kWKFindOptionsAtWordStarts;
  47. RetainPtr<WebView> webkit1View;
  48. static void didFinishLoadForFrame(WKPageRef page, WKFrameRef frame, WKTypeRef userData, const void* clientInfo)
  49. {
  50. didFinishLoad = true;
  51. }
  52. static void didFindStringMatches(WKPageRef page, WKStringRef string, WKArrayRef matches, int firstIndex, const void* clientInfo)
  53. {
  54. if (WKStringIsEqualToUTF8CString(string, "Hello")) {
  55. size_t numMatches = WKArrayGetSize(matches);
  56. EXPECT_EQ(3u, numMatches);
  57. if (findOptions & kWKFindOptionsBackwards)
  58. EXPECT_EQ(1, firstIndex);
  59. else
  60. EXPECT_EQ(2, firstIndex);
  61. for (size_t i = 0; i < numMatches; ++i) {
  62. WKTypeRef items = WKArrayGetItemAtIndex(matches, i);
  63. WKTypeID type = WKGetTypeID(items);
  64. EXPECT_EQ(type, WKArrayGetTypeID());
  65. WKArrayRef rects = reinterpret_cast<WKArrayRef>(items);
  66. size_t numRects = WKArrayGetSize(rects);
  67. EXPECT_EQ(1u, numRects);
  68. items = WKArrayGetItemAtIndex(rects, 0);
  69. type = WKGetTypeID(items);
  70. EXPECT_EQ(type, WKRectGetTypeID());
  71. WKRect rect = WKRectGetValue(reinterpret_cast<WKRectRef>(items));
  72. rect = rect;
  73. }
  74. } else if (WKStringIsEqualToUTF8CString(string, "crazy")) {
  75. size_t numMatches = WKArrayGetSize(matches);
  76. EXPECT_EQ(1u, numMatches);
  77. EXPECT_EQ(kWKFindResultNoMatchAfterUserSelection, firstIndex);
  78. }
  79. didCallFindStringMatches = true;
  80. }
  81. static void didGetImageForMatchResult(WKPageRef page, WKImageRef image, uint32_t index, const void* clientInfo)
  82. {
  83. WKSize size = WKImageGetSize(image);
  84. DOMDocument *document = webkit1View.get().mainFrameDocument;
  85. DOMRange *range = [document createRange];
  86. DOMNode *target = [document getElementById:@"target"];
  87. [range selectNode:target];
  88. NSImage *expectedImage = [range renderedImageForcingBlackText:YES];
  89. NSSize expectedSize = [expectedImage size];
  90. EXPECT_EQ(size.width, expectedSize.width);
  91. EXPECT_EQ(size.height, expectedSize.height);
  92. didCallGetImage = true;
  93. }
  94. TEST(WebKit2, FindMatches)
  95. {
  96. WKRetainPtr<WKContextRef> context(AdoptWK, WKContextCreate());
  97. PlatformWebView webView(context.get());
  98. WKPageLoaderClient loaderClient;
  99. memset(&loaderClient, 0, sizeof(loaderClient));
  100. loaderClient.version = 0;
  101. loaderClient.didFinishLoadForFrame = didFinishLoadForFrame;
  102. WKPageSetPageLoaderClient(webView.page(), &loaderClient);
  103. WKPageFindMatchesClient findMatchesClient;
  104. memset(&findMatchesClient, 0, sizeof(findMatchesClient));
  105. findMatchesClient.version = 0;
  106. findMatchesClient.didFindStringMatches = didFindStringMatches;
  107. findMatchesClient.didGetImageForMatchResult = didGetImageForMatchResult;
  108. WKPageSetPageFindMatchesClient(webView.page(), &findMatchesClient);
  109. // This HTML file contains 3 occurrences of the word Hello and has the second occurence of the word 'world' selected.
  110. // It contains 1 occurrence of the word 'crazy' that is before the selected word.
  111. WKRetainPtr<WKURLRef> url(AdoptWK, Util::createURLForResource("findRanges", "html"));
  112. WKPageLoadURL(webView.page(), url.get());
  113. Util::run(&didFinishLoad);
  114. WKRetainPtr<WKStringRef> findString(AdoptWK, WKStringCreateWithUTF8CString("Hello"));
  115. WKPageFindStringMatches(webView.page(), findString.get(), findOptions, 100);
  116. Util::run(&didCallFindStringMatches);
  117. didCallFindStringMatches = false;
  118. findOptions |= kWKFindOptionsBackwards;
  119. WKPageFindStringMatches(webView.page(), findString.get(), findOptions, 100);
  120. Util::run(&didCallFindStringMatches);
  121. webkit1View = adoptNS([[WebView alloc] initWithFrame:NSMakeRect(0, 0, 120, 200) frameName:nil groupName:nil]);
  122. RetainPtr<FindMatchesWK1FrameLoadDelegate> frameLoadDelegate = adoptNS([FindMatchesWK1FrameLoadDelegate new]);
  123. webkit1View.get().frameLoadDelegate = frameLoadDelegate.get();
  124. [webkit1View.get().mainFrame loadHTMLString:@"Test search. Hello <span id=\"target\">Hello</span> Hello!" baseURL:[NSURL URLWithString:@"about:blank"]];
  125. Util::run(&didFinishLoadWK1);
  126. WKPageGetImageForFindMatch(webView.page(), 0);
  127. Util::run(&didCallGetImage);
  128. didCallFindStringMatches = false;
  129. findOptions &= ~kWKFindOptionsBackwards;
  130. WKRetainPtr<WKStringRef> findOtherString(AdoptWK, WKStringCreateWithUTF8CString("crazy"));
  131. WKPageFindStringMatches(webView.page(), findOtherString.get(), findOptions, 100);
  132. Util::run(&didCallFindStringMatches);
  133. WKPageHideFindUI(webView.page());
  134. }
  135. } // namespace TestWebKitAPI