| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "core/paint/FirstMeaningfulPaintDetector.h" |
| 6 |
| 7 #include "core/paint/PaintTiming.h" |
| 8 #include "core/testing/DummyPageHolder.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace blink { |
| 12 |
| 13 class FirstMeaningfulPaintDetectorTest : public testing::Test { |
| 14 protected: |
| 15 void SetUp() override |
| 16 { |
| 17 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600)); |
| 18 s_timeElapsed = 0.0; |
| 19 m_originalTimeFunction = setTimeFunctionsForTesting(returnMockTime); |
| 20 } |
| 21 |
| 22 void TearDown() override |
| 23 { |
| 24 setTimeFunctionsForTesting(m_originalTimeFunction); |
| 25 } |
| 26 |
| 27 Document& document() { return m_dummyPageHolder->document(); } |
| 28 PaintTiming& paintTiming() { return PaintTiming::from(document()); } |
| 29 FirstMeaningfulPaintDetector& detector() { return paintTiming().firstMeaning
fulPaintDetector(); } |
| 30 |
| 31 void simulateLayoutAndPaint(int newElements) |
| 32 { |
| 33 StringBuilder builder; |
| 34 for (int i = 0; i < newElements; i++) |
| 35 builder.append("<span>a</span>"); |
| 36 document().write(builder.toString()); |
| 37 document().updateStyleAndLayout(); |
| 38 detector().notifyPaint(); |
| 39 } |
| 40 |
| 41 void simulateNetworkStable() |
| 42 { |
| 43 detector().networkStableTimerFired(nullptr); |
| 44 } |
| 45 |
| 46 private: |
| 47 static double returnMockTime() |
| 48 { |
| 49 s_timeElapsed += 1.0; |
| 50 return s_timeElapsed; |
| 51 } |
| 52 |
| 53 std::unique_ptr<DummyPageHolder> m_dummyPageHolder; |
| 54 TimeFunction m_originalTimeFunction; |
| 55 static double s_timeElapsed; |
| 56 }; |
| 57 |
| 58 double FirstMeaningfulPaintDetectorTest::s_timeElapsed; |
| 59 |
| 60 TEST_F(FirstMeaningfulPaintDetectorTest, NoFirstPaint) |
| 61 { |
| 62 simulateLayoutAndPaint(1); |
| 63 simulateNetworkStable(); |
| 64 EXPECT_EQ(paintTiming().firstMeaningfulPaint(), 0.0); |
| 65 } |
| 66 |
| 67 TEST_F(FirstMeaningfulPaintDetectorTest, OneLayout) |
| 68 { |
| 69 paintTiming().markFirstPaint(); |
| 70 simulateLayoutAndPaint(1); |
| 71 double afterPaint = monotonicallyIncreasingTime(); |
| 72 EXPECT_EQ(paintTiming().firstMeaningfulPaint(), 0.0); |
| 73 simulateNetworkStable(); |
| 74 EXPECT_GT(paintTiming().firstMeaningfulPaint(), paintTiming().firstPaint()); |
| 75 EXPECT_LT(paintTiming().firstMeaningfulPaint(), afterPaint); |
| 76 } |
| 77 |
| 78 TEST_F(FirstMeaningfulPaintDetectorTest, TwoLayoutsSignificantSecond) |
| 79 { |
| 80 paintTiming().markFirstPaint(); |
| 81 simulateLayoutAndPaint(1); |
| 82 double afterLayout1 = monotonicallyIncreasingTime(); |
| 83 simulateLayoutAndPaint(10); |
| 84 double afterLayout2 = monotonicallyIncreasingTime(); |
| 85 simulateNetworkStable(); |
| 86 EXPECT_GT(paintTiming().firstMeaningfulPaint(), afterLayout1); |
| 87 EXPECT_LT(paintTiming().firstMeaningfulPaint(), afterLayout2); |
| 88 } |
| 89 |
| 90 TEST_F(FirstMeaningfulPaintDetectorTest, TwoLayoutsSignificantFirst) |
| 91 { |
| 92 paintTiming().markFirstPaint(); |
| 93 simulateLayoutAndPaint(10); |
| 94 double afterLayout1 = monotonicallyIncreasingTime(); |
| 95 simulateLayoutAndPaint(1); |
| 96 simulateNetworkStable(); |
| 97 EXPECT_GT(paintTiming().firstMeaningfulPaint(), paintTiming().firstPaint()); |
| 98 EXPECT_LT(paintTiming().firstMeaningfulPaint(), afterLayout1); |
| 99 } |
| 100 |
| 101 } // namespace blink |
| OLD | NEW |