OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 "content/browser/download/rate_estimator.h" |
| 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 |
| 9 namespace content { |
| 10 |
| 11 TEST(RateEstimatorTest, RateEstimator) { |
| 12 base::Time now; |
| 13 RateEstimator estimator(base::TimeDelta::FromSeconds(1), 10u, now); |
| 14 EXPECT_EQ(0u, estimator.GetBytesPerSecond(now)); |
| 15 |
| 16 estimator.AddBytes(50u, now); |
| 17 EXPECT_EQ(50u, estimator.GetBytesPerSecond(now)); |
| 18 |
| 19 now += base::TimeDelta::FromMilliseconds(800); |
| 20 estimator.AddBytes(50, now); |
| 21 EXPECT_EQ(100u, estimator.GetBytesPerSecond(now)); |
| 22 |
| 23 // Advance time. |
| 24 now += base::TimeDelta::FromSeconds(3); |
| 25 EXPECT_EQ(25u, estimator.GetBytesPerSecond(now)); |
| 26 estimator.AddBytes(60, now); |
| 27 EXPECT_EQ(40u, estimator.GetBytesPerSecond(now)); |
| 28 |
| 29 // Advance time again. |
| 30 now += base::TimeDelta::FromSeconds(4); |
| 31 EXPECT_EQ(20u, estimator.GetBytesPerSecond(now)); |
| 32 |
| 33 // Advance time to the end. |
| 34 now += base::TimeDelta::FromSeconds(2); |
| 35 EXPECT_EQ(16u, estimator.GetBytesPerSecond(now)); |
| 36 estimator.AddBytes(100, now); |
| 37 EXPECT_EQ(26u, estimator.GetBytesPerSecond(now)); |
| 38 |
| 39 // Now wrap around to the start. |
| 40 now += base::TimeDelta::FromSeconds(1); |
| 41 EXPECT_EQ(16u, estimator.GetBytesPerSecond(now)); |
| 42 estimator.AddBytes(100, now); |
| 43 EXPECT_EQ(26u, estimator.GetBytesPerSecond(now)); |
| 44 |
| 45 // Advance far into the future. |
| 46 now += base::TimeDelta::FromSeconds(40); |
| 47 EXPECT_EQ(0u, estimator.GetBytesPerSecond(now)); |
| 48 estimator.AddBytes(100, now); |
| 49 EXPECT_EQ(100u, estimator.GetBytesPerSecond(now)); |
| 50 |
| 51 // Go backwards - this can happen if the user changes the clock. |
| 52 now -= base::TimeDelta::FromSeconds(100); |
| 53 EXPECT_EQ(0u, estimator.GetBytesPerSecond(now)); |
| 54 } |
| 55 |
| 56 } // namespace content |
OLD | NEW |