1 // Copyright 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 using base::TimeDelta; 10 11 namespace content { 12 13 TEST(RateEstimatorTest, RateEstimator) { 14 base::TimeTicks now; 15 RateEstimator estimator(TimeDelta::FromSeconds(1), 10u, now); 16 EXPECT_EQ(0u, estimator.GetCountPerSecond(now)); 17 18 estimator.Increment(50u, now); 19 EXPECT_EQ(50u, estimator.GetCountPerSecond(now)); 20 21 now += TimeDelta::FromMilliseconds(800); 22 estimator.Increment(50, now); 23 EXPECT_EQ(100u, estimator.GetCountPerSecond(now)); 24 25 // Advance time. 26 now += TimeDelta::FromSeconds(3); 27 EXPECT_EQ(25u, estimator.GetCountPerSecond(now)); 28 estimator.Increment(60, now); 29 EXPECT_EQ(40u, estimator.GetCountPerSecond(now)); 30 31 // Advance time again. 32 now += TimeDelta::FromSeconds(4); 33 EXPECT_EQ(20u, estimator.GetCountPerSecond(now)); 34 35 // Advance time to the end. 36 now += TimeDelta::FromSeconds(2); 37 EXPECT_EQ(16u, estimator.GetCountPerSecond(now)); 38 estimator.Increment(100, now); 39 EXPECT_EQ(26u, estimator.GetCountPerSecond(now)); 40 41 // Now wrap around to the start. 42 now += TimeDelta::FromSeconds(1); 43 EXPECT_EQ(16u, estimator.GetCountPerSecond(now)); 44 estimator.Increment(100, now); 45 EXPECT_EQ(26u, estimator.GetCountPerSecond(now)); 46 47 // Advance far into the future. 48 now += TimeDelta::FromSeconds(40); 49 EXPECT_EQ(0u, estimator.GetCountPerSecond(now)); 50 estimator.Increment(100, now); 51 EXPECT_EQ(100u, estimator.GetCountPerSecond(now)); 52 53 // Pretend that there is timeticks wrap around. 54 now = base::TimeTicks(); 55 EXPECT_EQ(0u, estimator.GetCountPerSecond(now)); 56 } 57 58 } // namespace content 59