| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "config.h" | 5 #include "config.h" |
| 6 | |
| 7 #include "web/TextFinder.h" | 6 #include "web/TextFinder.h" |
| 8 | 7 |
| 9 #include "bindings/core/v8/ExceptionStatePlaceholder.h" | 8 #include "bindings/core/v8/ExceptionStatePlaceholder.h" |
| 10 #include "core/dom/Document.h" | 9 #include "core/dom/Document.h" |
| 11 #include "core/dom/NodeList.h" | 10 #include "core/dom/NodeList.h" |
| 12 #include "core/dom/Range.h" | 11 #include "core/dom/Range.h" |
| 13 #include "core/dom/shadow/ShadowRoot.h" | 12 #include "core/dom/shadow/ShadowRoot.h" |
| 14 #include "core/html/HTMLElement.h" | 13 #include "core/html/HTMLElement.h" |
| 15 #include "platform/testing/UnitTestHelpers.h" | 14 #include "platform/testing/UnitTestHelpers.h" |
| 16 #include "public/platform/Platform.h" | 15 #include "public/platform/Platform.h" |
| 17 #include "public/web/WebDocument.h" | 16 #include "public/web/WebDocument.h" |
| 18 #include "web/FindInPageCoordinates.h" | 17 #include "web/FindInPageCoordinates.h" |
| 19 #include "web/WebLocalFrameImpl.h" | 18 #include "web/WebLocalFrameImpl.h" |
| 20 #include "web/tests/FrameTestHelpers.h" | 19 #include "web/tests/FrameTestHelpers.h" |
| 21 #include "wtf/OwnPtr.h" | 20 #include "wtf/OwnPtr.h" |
| 22 #include <gtest/gtest.h> | 21 #include <gtest/gtest.h> |
| 23 | 22 |
| 24 using namespace blink; | |
| 25 using blink::testing::runPendingTasks; | 23 using blink::testing::runPendingTasks; |
| 26 | 24 |
| 27 namespace { | 25 namespace blink { |
| 28 | 26 |
| 29 class TextFinderTest : public ::testing::Test { | 27 class TextFinderTest : public ::testing::Test { |
| 30 protected: | 28 protected: |
| 31 virtual void SetUp() override; | 29 void SetUp() override; |
| 32 | 30 |
| 33 Document& document() const; | 31 Document& document() const; |
| 34 TextFinder& textFinder() const; | 32 TextFinder& textFinder() const; |
| 35 | 33 |
| 36 static WebFloatRect findInPageRect(Node* startContainer, int startOffset, No
de* endContainer, int endOffset); | 34 static WebFloatRect findInPageRect(Node* startContainer, int startOffset, No
de* endContainer, int endOffset); |
| 37 | 35 |
| 38 private: | 36 private: |
| 39 FrameTestHelpers::WebViewHelper m_webViewHelper; | 37 FrameTestHelpers::WebViewHelper m_webViewHelper; |
| 40 RefPtrWillBePersistent<Document> m_document; | 38 RefPtrWillBePersistent<Document> m_document; |
| 41 TextFinder* m_textFinder; | 39 TextFinder* m_textFinder; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 69 | 67 |
| 70 TEST_F(TextFinderTest, FindTextSimple) | 68 TEST_F(TextFinderTest, FindTextSimple) |
| 71 { | 69 { |
| 72 document().body()->setInnerHTML("XXXXFindMeYYYYfindmeZZZZ", ASSERT_NO_EXCEPT
ION); | 70 document().body()->setInnerHTML("XXXXFindMeYYYYfindmeZZZZ", ASSERT_NO_EXCEPT
ION); |
| 73 Node* textNode = document().body()->firstChild(); | 71 Node* textNode = document().body()->firstChild(); |
| 74 | 72 |
| 75 int identifier = 0; | 73 int identifier = 0; |
| 76 WebString searchText(String("FindMe")); | 74 WebString searchText(String("FindMe")); |
| 77 WebFindOptions findOptions; // Default. | 75 WebFindOptions findOptions; // Default. |
| 78 bool wrapWithinFrame = true; | 76 bool wrapWithinFrame = true; |
| 79 WebRect* selectionRect = 0; | 77 WebRect* selectionRect = nullptr; |
| 80 | 78 |
| 81 ASSERT_TRUE(textFinder().find(identifier, searchText, findOptions, wrapWithi
nFrame, selectionRect)); | 79 ASSERT_TRUE(textFinder().find(identifier, searchText, findOptions, wrapWithi
nFrame, selectionRect)); |
| 82 Range* activeMatch = textFinder().activeMatch(); | 80 Range* activeMatch = textFinder().activeMatch(); |
| 83 ASSERT_TRUE(activeMatch); | 81 ASSERT_TRUE(activeMatch); |
| 84 EXPECT_EQ(textNode, activeMatch->startContainer()); | 82 EXPECT_EQ(textNode, activeMatch->startContainer()); |
| 85 EXPECT_EQ(4, activeMatch->startOffset()); | 83 EXPECT_EQ(4, activeMatch->startOffset()); |
| 86 EXPECT_EQ(textNode, activeMatch->endContainer()); | 84 EXPECT_EQ(textNode, activeMatch->endContainer()); |
| 87 EXPECT_EQ(10, activeMatch->endOffset()); | 85 EXPECT_EQ(10, activeMatch->endOffset()); |
| 88 | 86 |
| 89 findOptions.findNext = true; | 87 findOptions.findNext = true; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 } | 135 } |
| 138 | 136 |
| 139 TEST_F(TextFinderTest, FindTextNotFound) | 137 TEST_F(TextFinderTest, FindTextNotFound) |
| 140 { | 138 { |
| 141 document().body()->setInnerHTML("XXXXFindMeYYYYfindmeZZZZ", ASSERT_NO_EXCEPT
ION); | 139 document().body()->setInnerHTML("XXXXFindMeYYYYfindmeZZZZ", ASSERT_NO_EXCEPT
ION); |
| 142 | 140 |
| 143 int identifier = 0; | 141 int identifier = 0; |
| 144 WebString searchText(String("Boo")); | 142 WebString searchText(String("Boo")); |
| 145 WebFindOptions findOptions; // Default. | 143 WebFindOptions findOptions; // Default. |
| 146 bool wrapWithinFrame = true; | 144 bool wrapWithinFrame = true; |
| 147 WebRect* selectionRect = 0; | 145 WebRect* selectionRect = nullptr; |
| 148 | 146 |
| 149 EXPECT_FALSE(textFinder().find(identifier, searchText, findOptions, wrapWith
inFrame, selectionRect)); | 147 EXPECT_FALSE(textFinder().find(identifier, searchText, findOptions, wrapWith
inFrame, selectionRect)); |
| 150 EXPECT_FALSE(textFinder().activeMatch()); | 148 EXPECT_FALSE(textFinder().activeMatch()); |
| 151 } | 149 } |
| 152 | 150 |
| 153 TEST_F(TextFinderTest, FindTextInShadowDOM) | 151 TEST_F(TextFinderTest, FindTextInShadowDOM) |
| 154 { | 152 { |
| 155 document().body()->setInnerHTML("<b>FOO</b><i>foo</i>", ASSERT_NO_EXCEPTION)
; | 153 document().body()->setInnerHTML("<b>FOO</b><i>foo</i>", ASSERT_NO_EXCEPTION)
; |
| 156 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = document().body()->createShadowR
oot(ASSERT_NO_EXCEPTION); | 154 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = document().body()->createShadowR
oot(ASSERT_NO_EXCEPTION); |
| 157 shadowRoot->setInnerHTML("<content select=\"i\"></content><u>Foo</u><content
></content>", ASSERT_NO_EXCEPTION); | 155 shadowRoot->setInnerHTML("<content select=\"i\"></content><u>Foo</u><content
></content>", ASSERT_NO_EXCEPTION); |
| 158 Node* textInBElement = document().body()->firstChild()->firstChild(); | 156 Node* textInBElement = document().body()->firstChild()->firstChild(); |
| 159 Node* textInIElement = document().body()->lastChild()->firstChild(); | 157 Node* textInIElement = document().body()->lastChild()->firstChild(); |
| 160 Node* textInUElement = shadowRoot->childNodes()->item(1)->firstChild(); | 158 Node* textInUElement = shadowRoot->childNodes()->item(1)->firstChild(); |
| 161 | 159 |
| 162 int identifier = 0; | 160 int identifier = 0; |
| 163 WebString searchText(String("foo")); | 161 WebString searchText(String("foo")); |
| 164 WebFindOptions findOptions; // Default. | 162 WebFindOptions findOptions; // Default. |
| 165 bool wrapWithinFrame = true; | 163 bool wrapWithinFrame = true; |
| 166 WebRect* selectionRect = 0; | 164 WebRect* selectionRect = nullptr; |
| 167 | 165 |
| 168 // TextIterator currently returns the matches in the document order, instead
of the visual order. It visits | 166 // TextIterator currently returns the matches in the document order, instead
of the visual order. It visits |
| 169 // the shadow roots first, so in this case the matches will be returned in t
he order of <u> -> <b> -> <i>. | 167 // the shadow roots first, so in this case the matches will be returned in t
he order of <u> -> <b> -> <i>. |
| 170 ASSERT_TRUE(textFinder().find(identifier, searchText, findOptions, wrapWithi
nFrame, selectionRect)); | 168 ASSERT_TRUE(textFinder().find(identifier, searchText, findOptions, wrapWithi
nFrame, selectionRect)); |
| 171 Range* activeMatch = textFinder().activeMatch(); | 169 Range* activeMatch = textFinder().activeMatch(); |
| 172 ASSERT_TRUE(activeMatch); | 170 ASSERT_TRUE(activeMatch); |
| 173 EXPECT_EQ(textInUElement, activeMatch->startContainer()); | 171 EXPECT_EQ(textInUElement, activeMatch->startContainer()); |
| 174 EXPECT_EQ(0, activeMatch->startOffset()); | 172 EXPECT_EQ(0, activeMatch->startOffset()); |
| 175 EXPECT_EQ(textInUElement, activeMatch->endContainer()); | 173 EXPECT_EQ(textInUElement, activeMatch->endContainer()); |
| 176 EXPECT_EQ(3, activeMatch->endOffset()); | 174 EXPECT_EQ(3, activeMatch->endOffset()); |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 WebVector<WebFloatRect> matchRects; | 352 WebVector<WebFloatRect> matchRects; |
| 355 textFinder().findMatchRects(matchRects); | 353 textFinder().findMatchRects(matchRects); |
| 356 ASSERT_EQ(3u, matchRects.size()); | 354 ASSERT_EQ(3u, matchRects.size()); |
| 357 EXPECT_EQ(findInPageRect(textNode, 0, textNode, 2), matchRects[0]); | 355 EXPECT_EQ(findInPageRect(textNode, 0, textNode, 2), matchRects[0]); |
| 358 EXPECT_EQ(findInPageRect(textNode, 2, textNode, 4), matchRects[1]); | 356 EXPECT_EQ(findInPageRect(textNode, 2, textNode, 4), matchRects[1]); |
| 359 EXPECT_EQ(findInPageRect(textNode, 4, textNode, 6), matchRects[2]); | 357 EXPECT_EQ(findInPageRect(textNode, 4, textNode, 6), matchRects[2]); |
| 360 } | 358 } |
| 361 | 359 |
| 362 class TextFinderFakeTimerTest : public TextFinderTest { | 360 class TextFinderFakeTimerTest : public TextFinderTest { |
| 363 protected: | 361 protected: |
| 364 virtual void SetUp() override; | 362 void SetUp() override; |
| 365 virtual void TearDown() override; | 363 void TearDown() override; |
| 366 | 364 |
| 367 // A simple platform that mocks out the clock. | 365 // A simple platform that mocks out the clock. |
| 368 class TimeProxyPlatform : public Platform { | 366 class TimeProxyPlatform : public Platform { |
| 369 public: | 367 public: |
| 370 TimeProxyPlatform() | 368 TimeProxyPlatform() |
| 371 : m_timeCounter(0.) | 369 : m_timeCounter(0.) |
| 372 , m_fallbackPlatform(0) | 370 , m_fallbackPlatform(0) |
| 373 { } | 371 { } |
| 374 | 372 |
| 375 void install() | 373 void install() |
| (...skipping 16 matching lines...) Expand all Loading... |
| 392 } | 390 } |
| 393 | 391 |
| 394 private: | 392 private: |
| 395 Platform& ensureFallback() | 393 Platform& ensureFallback() |
| 396 { | 394 { |
| 397 ASSERT(m_fallbackPlatform); | 395 ASSERT(m_fallbackPlatform); |
| 398 return *m_fallbackPlatform; | 396 return *m_fallbackPlatform; |
| 399 } | 397 } |
| 400 | 398 |
| 401 // From blink::Platform: | 399 // From blink::Platform: |
| 402 virtual double currentTime() override | 400 double currentTime() override |
| 403 { | 401 { |
| 404 return ++m_timeCounter; | 402 return ++m_timeCounter; |
| 405 } | 403 } |
| 406 | 404 |
| 407 // These blink::Platform methods must be overriden to make a usable obje
ct. | 405 // These blink::Platform methods must be overriden to make a usable obje
ct. |
| 408 virtual void cryptographicallyRandomValues(unsigned char* buffer, size_t
length) override | 406 void cryptographicallyRandomValues(unsigned char* buffer, size_t length)
override |
| 409 { | 407 { |
| 410 ensureFallback().cryptographicallyRandomValues(buffer, length); | 408 ensureFallback().cryptographicallyRandomValues(buffer, length); |
| 411 } | 409 } |
| 412 | 410 |
| 413 virtual const unsigned char* getTraceCategoryEnabledFlag(const char* cat
egoryName) override | 411 const unsigned char* getTraceCategoryEnabledFlag(const char* categoryNam
e) override |
| 414 { | 412 { |
| 415 return ensureFallback().getTraceCategoryEnabledFlag(categoryName); | 413 return ensureFallback().getTraceCategoryEnabledFlag(categoryName); |
| 416 } | 414 } |
| 417 | 415 |
| 418 // These two methods allow timers to work correctly. | 416 // These two methods allow timers to work correctly. |
| 419 virtual double monotonicallyIncreasingTime() override | 417 double monotonicallyIncreasingTime() override |
| 420 { | 418 { |
| 421 return ensureFallback().monotonicallyIncreasingTime(); | 419 return ensureFallback().monotonicallyIncreasingTime(); |
| 422 } | 420 } |
| 423 | 421 |
| 424 virtual WebThread* currentThread() override { return ensureFallback().cu
rrentThread(); } | 422 WebThread* currentThread() override { return ensureFallback().currentThr
ead(); } |
| 425 virtual WebUnitTestSupport* unitTestSupport() override { return ensureFa
llback().unitTestSupport(); } | 423 WebUnitTestSupport* unitTestSupport() override { return ensureFallback()
.unitTestSupport(); } |
| 426 virtual WebString defaultLocale() override { return ensureFallback().def
aultLocale(); } | 424 WebString defaultLocale() override { return ensureFallback().defaultLoca
le(); } |
| 427 virtual WebCompositorSupport* compositorSupport() override { return ensu
reFallback().compositorSupport(); } | 425 WebCompositorSupport* compositorSupport() override { return ensureFallba
ck().compositorSupport(); } |
| 428 | 426 |
| 429 double m_timeCounter; | 427 double m_timeCounter; |
| 430 Platform* m_fallbackPlatform; | 428 Platform* m_fallbackPlatform; |
| 431 }; | 429 }; |
| 432 | 430 |
| 433 TimeProxyPlatform m_proxyTimePlatform; | 431 TimeProxyPlatform m_proxyTimePlatform; |
| 434 }; | 432 }; |
| 435 | 433 |
| 436 void TextFinderFakeTimerTest::SetUp() | 434 void TextFinderFakeTimerTest::SetUp() |
| 437 { | 435 { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 466 | 464 |
| 467 // There will be only one iteration before timeout, because increment | 465 // There will be only one iteration before timeout, because increment |
| 468 // of the TimeProxyPlatform timer is greater than timeout threshold. | 466 // of the TimeProxyPlatform timer is greater than timeout threshold. |
| 469 textFinder().scopeStringMatches(identifier, searchPattern, findOptions, true
); | 467 textFinder().scopeStringMatches(identifier, searchPattern, findOptions, true
); |
| 470 while (textFinder().scopingInProgress()) | 468 while (textFinder().scopingInProgress()) |
| 471 runPendingTasks(); | 469 runPendingTasks(); |
| 472 | 470 |
| 473 EXPECT_EQ(4, textFinder().totalMatchCount()); | 471 EXPECT_EQ(4, textFinder().totalMatchCount()); |
| 474 } | 472 } |
| 475 | 473 |
| 476 } // namespace | 474 } // namespace blink |
| OLD | NEW |