OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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/loader/NetworkQuietDetector.h" |
| 6 |
| 7 #include "core/testing/DummyPageHolder.h" |
| 8 #include "platform/testing/TestingPlatformSupport.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace blink { |
| 12 |
| 13 class NetworkQuietDetectorTest : public testing::Test { |
| 14 protected: |
| 15 void SetUp() override { |
| 16 platform_->AdvanceClockSeconds(1); |
| 17 dummy_page_holder_ = DummyPageHolder::Create(IntSize(800, 600)); |
| 18 } |
| 19 |
| 20 double AdvanceClockAndGetTime() { |
| 21 platform_->AdvanceClockSeconds(1); |
| 22 return MonotonicallyIncreasingTime(); |
| 23 } |
| 24 |
| 25 Document& GetDocument() { return dummy_page_holder_->GetDocument(); } |
| 26 NetworkQuietDetector& Detector() { |
| 27 return NetworkQuietDetector::From(GetDocument()); |
| 28 } |
| 29 |
| 30 void SimulateNetworkStable() { |
| 31 GetDocument().SetParsingState(Document::kFinishedParsing); |
| 32 Detector().NetworkQuietTimerFired(nullptr); |
| 33 } |
| 34 |
| 35 void SimulateNetworkQuiet() { |
| 36 GetDocument().SetParsingState(Document::kFinishedParsing); |
| 37 Detector().NetworkQuietTimerFired(nullptr); |
| 38 } |
| 39 |
| 40 void SetActiveConnections(int connections) { |
| 41 Detector().SetNetworkQuietTimers(connections); |
| 42 } |
| 43 |
| 44 bool IsNetworkQuietTimerActive() { |
| 45 return Detector().network_quiet_timer_.IsActive(); |
| 46 } |
| 47 |
| 48 bool HadNetworkQuiet() { return Detector().network_quiet_reached_; } |
| 49 |
| 50 protected: |
| 51 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler> |
| 52 platform_; |
| 53 |
| 54 static constexpr double kNetworkQuietWindowSeconds = |
| 55 NetworkQuietDetector::kNetworkQuietWindowSeconds; |
| 56 |
| 57 private: |
| 58 std::unique_ptr<DummyPageHolder> dummy_page_holder_; |
| 59 }; |
| 60 |
| 61 TEST_F(NetworkQuietDetectorTest, NetworkQuietTimer) { |
| 62 SetActiveConnections(3); |
| 63 EXPECT_FALSE(IsNetworkQuietTimerActive()); |
| 64 |
| 65 SetActiveConnections(2); |
| 66 platform_->RunForPeriodSeconds(kNetworkQuietWindowSeconds - 0.1); |
| 67 EXPECT_TRUE(IsNetworkQuietTimerActive()); |
| 68 EXPECT_FALSE(HadNetworkQuiet()); |
| 69 |
| 70 SetActiveConnections(2); // This should reset the quiet timer. |
| 71 platform_->RunForPeriodSeconds(kNetworkQuietWindowSeconds - 0.1); |
| 72 EXPECT_TRUE(IsNetworkQuietTimerActive()); |
| 73 EXPECT_FALSE(HadNetworkQuiet()); |
| 74 |
| 75 SetActiveConnections(1); // This should not reset the quiet timer. |
| 76 platform_->RunForPeriodSeconds(0.1001); |
| 77 EXPECT_TRUE(HadNetworkQuiet()); |
| 78 } |
| 79 |
| 80 } // namespace blink |
OLD | NEW |