| 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 #include "web/TextFinder.h" | 6 #include "web/TextFinder.h" |
| 7 | 7 |
| 8 #include "bindings/core/v8/ExceptionStatePlaceholder.h" | 8 #include "bindings/core/v8/ExceptionStatePlaceholder.h" |
| 9 #include "core/dom/Document.h" | 9 #include "core/dom/Document.h" |
| 10 #include "core/dom/NodeList.h" | 10 #include "core/dom/NodeList.h" |
| 11 #include "core/dom/Range.h" | 11 #include "core/dom/Range.h" |
| 12 #include "core/dom/shadow/ShadowRoot.h" | 12 #include "core/dom/shadow/ShadowRoot.h" |
| 13 #include "core/frame/FrameHost.h" | 13 #include "core/frame/FrameHost.h" |
| 14 #include "core/html/HTMLElement.h" | 14 #include "core/html/HTMLElement.h" |
| 15 #include "core/layout/TextAutosizer.h" | 15 #include "core/layout/TextAutosizer.h" |
| 16 #include "core/page/Page.h" | 16 #include "core/page/Page.h" |
| 17 #include "platform/testing/TestingPlatformSupport.h" |
| 17 #include "platform/testing/UnitTestHelpers.h" | 18 #include "platform/testing/UnitTestHelpers.h" |
| 18 #include "public/platform/Platform.h" | 19 #include "public/platform/Platform.h" |
| 19 #include "public/web/WebDocument.h" | 20 #include "public/web/WebDocument.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "web/FindInPageCoordinates.h" | 22 #include "web/FindInPageCoordinates.h" |
| 22 #include "web/WebLocalFrameImpl.h" | 23 #include "web/WebLocalFrameImpl.h" |
| 23 #include "web/tests/FrameTestHelpers.h" | 24 #include "web/tests/FrameTestHelpers.h" |
| 24 #include "wtf/OwnPtr.h" | 25 #include "wtf/OwnPtr.h" |
| 25 | 26 |
| 26 using blink::testing::runPendingTasks; | 27 using blink::testing::runPendingTasks; |
| 27 | 28 |
| 28 namespace blink { | 29 namespace blink { |
| 29 | 30 |
| 30 class TextFinderTest : public ::testing::Test { | 31 class TextFinderTest : public ::testing::Test { |
| 31 protected: | 32 protected: |
| 32 void SetUp() override; | 33 TextFinderTest() |
| 34 { |
| 35 m_webViewHelper.initialize(); |
| 36 WebLocalFrameImpl& frameImpl = *m_webViewHelper.webViewImpl()->mainFrame
Impl(); |
| 37 frameImpl.viewImpl()->resize(WebSize(640, 480)); |
| 38 frameImpl.viewImpl()->updateAllLifecyclePhases(); |
| 39 m_document = PassRefPtrWillBeRawPtr<Document>(frameImpl.document()); |
| 40 m_textFinder = &frameImpl.ensureTextFinder(); |
| 41 } |
| 33 | 42 |
| 34 Document& document() const; | 43 Document& document() const; |
| 35 TextFinder& textFinder() const; | 44 TextFinder& textFinder() const; |
| 36 | 45 |
| 37 static WebFloatRect findInPageRect(Node* startContainer, int startOffset, No
de* endContainer, int endOffset); | 46 static WebFloatRect findInPageRect(Node* startContainer, int startOffset, No
de* endContainer, int endOffset); |
| 38 | 47 |
| 39 private: | 48 private: |
| 40 FrameTestHelpers::WebViewHelper m_webViewHelper; | 49 FrameTestHelpers::WebViewHelper m_webViewHelper; |
| 41 RefPtrWillBePersistent<Document> m_document; | 50 RefPtrWillBePersistent<Document> m_document; |
| 42 RawPtrWillBePersistent<TextFinder> m_textFinder; | 51 RawPtrWillBePersistent<TextFinder> m_textFinder; |
| 43 }; | 52 }; |
| 44 | 53 |
| 45 void TextFinderTest::SetUp() | |
| 46 { | |
| 47 m_webViewHelper.initialize(); | |
| 48 WebLocalFrameImpl& frameImpl = *m_webViewHelper.webViewImpl()->mainFrameImpl
(); | |
| 49 frameImpl.viewImpl()->resize(WebSize(640, 480)); | |
| 50 frameImpl.viewImpl()->updateAllLifecyclePhases(); | |
| 51 m_document = PassRefPtrWillBeRawPtr<Document>(frameImpl.document()); | |
| 52 m_textFinder = &frameImpl.ensureTextFinder(); | |
| 53 } | |
| 54 | |
| 55 Document& TextFinderTest::document() const | 54 Document& TextFinderTest::document() const |
| 56 { | 55 { |
| 57 return *m_document; | 56 return *m_document; |
| 58 } | 57 } |
| 59 | 58 |
| 60 TextFinder& TextFinderTest::textFinder() const | 59 TextFinder& TextFinderTest::textFinder() const |
| 61 { | 60 { |
| 62 return *m_textFinder; | 61 return *m_textFinder; |
| 63 } | 62 } |
| 64 | 63 |
| (...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 WebVector<WebFloatRect> matchRects; | 390 WebVector<WebFloatRect> matchRects; |
| 392 textFinder().findMatchRects(matchRects); | 391 textFinder().findMatchRects(matchRects); |
| 393 ASSERT_EQ(3u, matchRects.size()); | 392 ASSERT_EQ(3u, matchRects.size()); |
| 394 EXPECT_EQ(findInPageRect(textNode, 0, textNode, 2), matchRects[0]); | 393 EXPECT_EQ(findInPageRect(textNode, 0, textNode, 2), matchRects[0]); |
| 395 EXPECT_EQ(findInPageRect(textNode, 2, textNode, 4), matchRects[1]); | 394 EXPECT_EQ(findInPageRect(textNode, 2, textNode, 4), matchRects[1]); |
| 396 EXPECT_EQ(findInPageRect(textNode, 4, textNode, 6), matchRects[2]); | 395 EXPECT_EQ(findInPageRect(textNode, 4, textNode, 6), matchRects[2]); |
| 397 } | 396 } |
| 398 | 397 |
| 399 class TextFinderFakeTimerTest : public TextFinderTest { | 398 class TextFinderFakeTimerTest : public TextFinderTest { |
| 400 protected: | 399 protected: |
| 401 void SetUp() override; | |
| 402 void TearDown() override; | |
| 403 | |
| 404 // A simple platform that mocks out the clock. | 400 // A simple platform that mocks out the clock. |
| 405 class TimeProxyPlatform : public Platform { | 401 class TimeProxyPlatform : public TestingPlatformSupport { |
| 406 public: | 402 public: |
| 407 TimeProxyPlatform() | 403 TimeProxyPlatform() |
| 408 : m_timeCounter(0.) | 404 : m_timeCounter(m_oldPlatform->currentTimeSeconds()) |
| 409 , m_fallbackPlatform(0) | |
| 410 { } | |
| 411 | |
| 412 void install() | |
| 413 { | 405 { |
| 414 // Check that the proxy wasn't installed yet. | |
| 415 ASSERT_NE(Platform::current(), this); | |
| 416 m_fallbackPlatform = Platform::current(); | |
| 417 m_timeCounter = m_fallbackPlatform->currentTimeSeconds(); | |
| 418 Platform::initialize(this); | |
| 419 ASSERT_EQ(Platform::current(), this); | |
| 420 } | |
| 421 | |
| 422 void remove() | |
| 423 { | |
| 424 // Check that the proxy was installed. | |
| 425 ASSERT_EQ(Platform::current(), this); | |
| 426 Platform::initialize(m_fallbackPlatform); | |
| 427 ASSERT_EQ(Platform::current(), m_fallbackPlatform); | |
| 428 m_fallbackPlatform = 0; | |
| 429 } | 406 } |
| 430 | 407 |
| 431 private: | 408 private: |
| 432 Platform& ensureFallback() | 409 Platform& ensureFallback() |
| 433 { | 410 { |
| 434 ASSERT(m_fallbackPlatform); | 411 ASSERT(m_oldPlatform); |
| 435 return *m_fallbackPlatform; | 412 return *m_oldPlatform; |
| 436 } | 413 } |
| 437 | 414 |
| 438 // From blink::Platform: | 415 // From blink::Platform: |
| 439 double currentTimeSeconds() override | 416 double currentTimeSeconds() override |
| 440 { | 417 { |
| 441 return ++m_timeCounter; | 418 return ++m_timeCounter; |
| 442 } | 419 } |
| 443 | 420 |
| 444 // These blink::Platform methods must be overriden to make a usable obje
ct. | |
| 445 void cryptographicallyRandomValues(unsigned char* buffer, size_t length)
override | |
| 446 { | |
| 447 ensureFallback().cryptographicallyRandomValues(buffer, length); | |
| 448 } | |
| 449 | |
| 450 const unsigned char* getTraceCategoryEnabledFlag(const char* categoryNam
e) override | |
| 451 { | |
| 452 return ensureFallback().getTraceCategoryEnabledFlag(categoryName); | |
| 453 } | |
| 454 | |
| 455 // These two methods allow timers to work correctly. | 421 // These two methods allow timers to work correctly. |
| 456 double monotonicallyIncreasingTimeSeconds() override | 422 double monotonicallyIncreasingTimeSeconds() override |
| 457 { | 423 { |
| 458 return ensureFallback().monotonicallyIncreasingTimeSeconds(); | 424 return ensureFallback().monotonicallyIncreasingTimeSeconds(); |
| 459 } | 425 } |
| 460 | 426 |
| 461 WebThread* currentThread() override { return ensureFallback().currentThr
ead(); } | |
| 462 WebUnitTestSupport* unitTestSupport() override { return ensureFallback()
.unitTestSupport(); } | 427 WebUnitTestSupport* unitTestSupport() override { return ensureFallback()
.unitTestSupport(); } |
| 463 WebString defaultLocale() override { return ensureFallback().defaultLoca
le(); } | 428 WebString defaultLocale() override { return ensureFallback().defaultLoca
le(); } |
| 464 WebCompositorSupport* compositorSupport() override { return ensureFallba
ck().compositorSupport(); } | 429 WebCompositorSupport* compositorSupport() override { return ensureFallba
ck().compositorSupport(); } |
| 465 | 430 |
| 466 double m_timeCounter; | 431 double m_timeCounter; |
| 467 Platform* m_fallbackPlatform; | |
| 468 }; | 432 }; |
| 469 | 433 |
| 470 TimeProxyPlatform m_proxyTimePlatform; | 434 TimeProxyPlatform m_proxyTimePlatform; |
| 471 }; | 435 }; |
| 472 | 436 |
| 473 void TextFinderFakeTimerTest::SetUp() | |
| 474 { | |
| 475 TextFinderTest::SetUp(); | |
| 476 m_proxyTimePlatform.install(); | |
| 477 } | |
| 478 | |
| 479 void TextFinderFakeTimerTest::TearDown() | |
| 480 { | |
| 481 m_proxyTimePlatform.remove(); | |
| 482 TextFinderTest::TearDown(); | |
| 483 } | |
| 484 | |
| 485 TEST_F(TextFinderFakeTimerTest, ScopeWithTimeouts) | 437 TEST_F(TextFinderFakeTimerTest, ScopeWithTimeouts) |
| 486 { | 438 { |
| 487 // Make a long string. | 439 // Make a long string. |
| 488 String text(Vector<UChar>(100)); | 440 String text(Vector<UChar>(100)); |
| 489 text.fill('a'); | 441 text.fill('a'); |
| 490 String searchPattern("abc"); | 442 String searchPattern("abc"); |
| 491 // Make 4 substrings "abc" in text. | 443 // Make 4 substrings "abc" in text. |
| 492 text.insert(searchPattern, 1); | 444 text.insert(searchPattern, 1); |
| 493 text.insert(searchPattern, 10); | 445 text.insert(searchPattern, 10); |
| 494 text.insert(searchPattern, 50); | 446 text.insert(searchPattern, 50); |
| 495 text.insert(searchPattern, 90); | 447 text.insert(searchPattern, 90); |
| 496 | 448 |
| 497 document().body()->setInnerHTML(text, ASSERT_NO_EXCEPTION); | 449 document().body()->setInnerHTML(text, ASSERT_NO_EXCEPTION); |
| 498 | 450 |
| 499 int identifier = 0; | 451 int identifier = 0; |
| 500 WebFindOptions findOptions; // Default. | 452 WebFindOptions findOptions; // Default. |
| 501 | 453 |
| 502 textFinder().resetMatchCount(); | 454 textFinder().resetMatchCount(); |
| 503 | 455 |
| 504 // There will be only one iteration before timeout, because increment | 456 // There will be only one iteration before timeout, because increment |
| 505 // of the TimeProxyPlatform timer is greater than timeout threshold. | 457 // of the TimeProxyPlatform timer is greater than timeout threshold. |
| 506 textFinder().scopeStringMatches(identifier, searchPattern, findOptions, true
); | 458 textFinder().scopeStringMatches(identifier, searchPattern, findOptions, true
); |
| 507 while (textFinder().scopingInProgress()) | 459 while (textFinder().scopingInProgress()) |
| 508 runPendingTasks(); | 460 runPendingTasks(); |
| 509 | 461 |
| 510 EXPECT_EQ(4, textFinder().totalMatchCount()); | 462 EXPECT_EQ(4, textFinder().totalMatchCount()); |
| 511 } | 463 } |
| 512 | 464 |
| 513 } // namespace blink | 465 } // namespace blink |
| OLD | NEW |