Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: third_party/WebKit/Source/web/tests/TextFinderTest.cpp

Issue 1466603003: Revert of More regular Platform implementations in unit tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
18 #include "platform/testing/UnitTestHelpers.h" 17 #include "platform/testing/UnitTestHelpers.h"
19 #include "public/platform/Platform.h" 18 #include "public/platform/Platform.h"
20 #include "public/web/WebDocument.h" 19 #include "public/web/WebDocument.h"
21 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
22 #include "web/FindInPageCoordinates.h" 21 #include "web/FindInPageCoordinates.h"
23 #include "web/WebLocalFrameImpl.h" 22 #include "web/WebLocalFrameImpl.h"
24 #include "web/tests/FrameTestHelpers.h" 23 #include "web/tests/FrameTestHelpers.h"
25 #include "wtf/OwnPtr.h" 24 #include "wtf/OwnPtr.h"
26 25
27 using blink::testing::runPendingTasks; 26 using blink::testing::runPendingTasks;
28 27
29 namespace blink { 28 namespace blink {
30 29
31 class TextFinderTest : public ::testing::Test { 30 class TextFinderTest : public ::testing::Test {
32 protected: 31 protected:
33 TextFinderTest() 32 void SetUp() override;
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 }
42 33
43 Document& document() const; 34 Document& document() const;
44 TextFinder& textFinder() const; 35 TextFinder& textFinder() const;
45 36
46 static WebFloatRect findInPageRect(Node* startContainer, int startOffset, No de* endContainer, int endOffset); 37 static WebFloatRect findInPageRect(Node* startContainer, int startOffset, No de* endContainer, int endOffset);
47 38
48 private: 39 private:
49 FrameTestHelpers::WebViewHelper m_webViewHelper; 40 FrameTestHelpers::WebViewHelper m_webViewHelper;
50 RefPtrWillBePersistent<Document> m_document; 41 RefPtrWillBePersistent<Document> m_document;
51 RawPtrWillBePersistent<TextFinder> m_textFinder; 42 RawPtrWillBePersistent<TextFinder> m_textFinder;
52 }; 43 };
53 44
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
54 Document& TextFinderTest::document() const 55 Document& TextFinderTest::document() const
55 { 56 {
56 return *m_document; 57 return *m_document;
57 } 58 }
58 59
59 TextFinder& TextFinderTest::textFinder() const 60 TextFinder& TextFinderTest::textFinder() const
60 { 61 {
61 return *m_textFinder; 62 return *m_textFinder;
62 } 63 }
63 64
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 WebVector<WebFloatRect> matchRects; 391 WebVector<WebFloatRect> matchRects;
391 textFinder().findMatchRects(matchRects); 392 textFinder().findMatchRects(matchRects);
392 ASSERT_EQ(3u, matchRects.size()); 393 ASSERT_EQ(3u, matchRects.size());
393 EXPECT_EQ(findInPageRect(textNode, 0, textNode, 2), matchRects[0]); 394 EXPECT_EQ(findInPageRect(textNode, 0, textNode, 2), matchRects[0]);
394 EXPECT_EQ(findInPageRect(textNode, 2, textNode, 4), matchRects[1]); 395 EXPECT_EQ(findInPageRect(textNode, 2, textNode, 4), matchRects[1]);
395 EXPECT_EQ(findInPageRect(textNode, 4, textNode, 6), matchRects[2]); 396 EXPECT_EQ(findInPageRect(textNode, 4, textNode, 6), matchRects[2]);
396 } 397 }
397 398
398 class TextFinderFakeTimerTest : public TextFinderTest { 399 class TextFinderFakeTimerTest : public TextFinderTest {
399 protected: 400 protected:
401 void SetUp() override;
402 void TearDown() override;
403
400 // A simple platform that mocks out the clock. 404 // A simple platform that mocks out the clock.
401 class TimeProxyPlatform : public TestingPlatformSupport { 405 class TimeProxyPlatform : public Platform {
402 public: 406 public:
403 TimeProxyPlatform() 407 TimeProxyPlatform()
404 : m_timeCounter(m_oldPlatform->currentTimeSeconds()) 408 : m_timeCounter(0.)
409 , m_fallbackPlatform(0)
410 { }
411
412 void install()
405 { 413 {
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;
406 } 429 }
407 430
408 private: 431 private:
409 Platform& ensureFallback() 432 Platform& ensureFallback()
410 { 433 {
411 ASSERT(m_oldPlatform); 434 ASSERT(m_fallbackPlatform);
412 return *m_oldPlatform; 435 return *m_fallbackPlatform;
413 } 436 }
414 437
415 // From blink::Platform: 438 // From blink::Platform:
416 double currentTimeSeconds() override 439 double currentTimeSeconds() override
417 { 440 {
418 return ++m_timeCounter; 441 return ++m_timeCounter;
419 } 442 }
420 443
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
421 // These two methods allow timers to work correctly. 455 // These two methods allow timers to work correctly.
422 double monotonicallyIncreasingTimeSeconds() override 456 double monotonicallyIncreasingTimeSeconds() override
423 { 457 {
424 return ensureFallback().monotonicallyIncreasingTimeSeconds(); 458 return ensureFallback().monotonicallyIncreasingTimeSeconds();
425 } 459 }
426 460
461 WebThread* currentThread() override { return ensureFallback().currentThr ead(); }
427 WebUnitTestSupport* unitTestSupport() override { return ensureFallback() .unitTestSupport(); } 462 WebUnitTestSupport* unitTestSupport() override { return ensureFallback() .unitTestSupport(); }
428 WebString defaultLocale() override { return ensureFallback().defaultLoca le(); } 463 WebString defaultLocale() override { return ensureFallback().defaultLoca le(); }
429 WebCompositorSupport* compositorSupport() override { return ensureFallba ck().compositorSupport(); } 464 WebCompositorSupport* compositorSupport() override { return ensureFallba ck().compositorSupport(); }
430 465
431 double m_timeCounter; 466 double m_timeCounter;
467 Platform* m_fallbackPlatform;
432 }; 468 };
433 469
434 TimeProxyPlatform m_proxyTimePlatform; 470 TimeProxyPlatform m_proxyTimePlatform;
435 }; 471 };
436 472
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
437 TEST_F(TextFinderFakeTimerTest, ScopeWithTimeouts) 485 TEST_F(TextFinderFakeTimerTest, ScopeWithTimeouts)
438 { 486 {
439 // Make a long string. 487 // Make a long string.
440 String text(Vector<UChar>(100)); 488 String text(Vector<UChar>(100));
441 text.fill('a'); 489 text.fill('a');
442 String searchPattern("abc"); 490 String searchPattern("abc");
443 // Make 4 substrings "abc" in text. 491 // Make 4 substrings "abc" in text.
444 text.insert(searchPattern, 1); 492 text.insert(searchPattern, 1);
445 text.insert(searchPattern, 10); 493 text.insert(searchPattern, 10);
446 text.insert(searchPattern, 50); 494 text.insert(searchPattern, 50);
447 text.insert(searchPattern, 90); 495 text.insert(searchPattern, 90);
448 496
449 document().body()->setInnerHTML(text, ASSERT_NO_EXCEPTION); 497 document().body()->setInnerHTML(text, ASSERT_NO_EXCEPTION);
450 498
451 int identifier = 0; 499 int identifier = 0;
452 WebFindOptions findOptions; // Default. 500 WebFindOptions findOptions; // Default.
453 501
454 textFinder().resetMatchCount(); 502 textFinder().resetMatchCount();
455 503
456 // There will be only one iteration before timeout, because increment 504 // There will be only one iteration before timeout, because increment
457 // of the TimeProxyPlatform timer is greater than timeout threshold. 505 // of the TimeProxyPlatform timer is greater than timeout threshold.
458 textFinder().scopeStringMatches(identifier, searchPattern, findOptions, true ); 506 textFinder().scopeStringMatches(identifier, searchPattern, findOptions, true );
459 while (textFinder().scopingInProgress()) 507 while (textFinder().scopingInProgress())
460 runPendingTasks(); 508 runPendingTasks();
461 509
462 EXPECT_EQ(4, textFinder().totalMatchCount()); 510 EXPECT_EQ(4, textFinder().totalMatchCount());
463 } 511 }
464 512
465 } // namespace blink 513 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/web/ImageDecodeBench.cpp ('k') | third_party/WebKit/Source/web/web_tests.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698