| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/network_time/network_time_tracker.h" | 5 #include "chrome/browser/network_time/network_time_tracker.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/prefs/testing_pref_service.h" |
| 10 #include "base/time/tick_clock.h" | 11 #include "base/time/tick_clock.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 15 // These are all in milliseconds. | 16 // These are all in milliseconds. |
| 16 const int64 kLatency1 = 50; | 17 const int64 kLatency1 = 50; |
| 17 const int64 kLatency2 = 500; | 18 const int64 kLatency2 = 500; |
| 18 | 19 |
| 19 // Can not be smaller than 15, it's the NowFromSystemTime() resolution. | 20 // Can not be smaller than 15, it's the NowFromSystemTime() resolution. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 34 } | 35 } |
| 35 | 36 |
| 36 private: | 37 private: |
| 37 base::TimeTicks* ticks_now_; | 38 base::TimeTicks* ticks_now_; |
| 38 }; | 39 }; |
| 39 | 40 |
| 40 } // namespace | 41 } // namespace |
| 41 | 42 |
| 42 class NetworkTimeTrackerTest : public testing::Test { | 43 class NetworkTimeTrackerTest : public testing::Test { |
| 43 public: | 44 public: |
| 44 NetworkTimeTrackerTest() | |
| 45 : now_(base::Time::NowFromSystemTime()), | |
| 46 network_time_tracker_(new NetworkTimeTracker( | |
| 47 scoped_ptr<base::TickClock>(new TestTickClock(&ticks_now_)))) {} | |
| 48 virtual ~NetworkTimeTrackerTest() {} | 45 virtual ~NetworkTimeTrackerTest() {} |
| 49 | 46 |
| 47 virtual void SetUp() OVERRIDE { |
| 48 NetworkTimeTracker::RegisterPrefs(pref_service_.registry()); |
| 49 |
| 50 now_ = base::Time::NowFromSystemTime(); |
| 51 network_time_tracker_.reset(new NetworkTimeTracker( |
| 52 scoped_ptr<base::TickClock>(new TestTickClock(&ticks_now_)), |
| 53 &pref_service_)); |
| 54 } |
| 55 |
| 50 base::Time Now() const { | 56 base::Time Now() const { |
| 51 return now_ + (ticks_now_ - base::TimeTicks()); | 57 return now_ + (ticks_now_ - base::TimeTicks()); |
| 52 } | 58 } |
| 53 | 59 |
| 54 base::TimeTicks TicksNow() const { | 60 base::TimeTicks TicksNow() const { |
| 55 return ticks_now_; | 61 return ticks_now_; |
| 56 } | 62 } |
| 57 | 63 |
| 58 void AddToTicksNow(int64 ms) { | 64 void AddToTicksNow(int64 ms) { |
| 59 ticks_now_ += base::TimeDelta::FromMilliseconds(ms); | 65 ticks_now_ += base::TimeDelta::FromMilliseconds(ms); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 NetworkTimeTracker* network_time_tracker() { | 97 NetworkTimeTracker* network_time_tracker() { |
| 92 return network_time_tracker_.get(); | 98 return network_time_tracker_.get(); |
| 93 } | 99 } |
| 94 | 100 |
| 95 private: | 101 private: |
| 96 // Used in building the current time that TestTickClock reports. See Now() | 102 // Used in building the current time that TestTickClock reports. See Now() |
| 97 // for details. | 103 // for details. |
| 98 base::Time now_; | 104 base::Time now_; |
| 99 base::TimeTicks ticks_now_; | 105 base::TimeTicks ticks_now_; |
| 100 | 106 |
| 107 TestingPrefServiceSimple pref_service_; |
| 108 |
| 101 // The network time tracker being tested. | 109 // The network time tracker being tested. |
| 102 scoped_ptr<NetworkTimeTracker> network_time_tracker_; | 110 scoped_ptr<NetworkTimeTracker> network_time_tracker_; |
| 103 }; | 111 }; |
| 104 | 112 |
| 105 // Should not return a value before UpdateNetworkTime gets called. | 113 // Should not return a value before UpdateNetworkTime gets called. |
| 106 TEST_F(NetworkTimeTrackerTest, Uninitialized) { | 114 TEST_F(NetworkTimeTrackerTest, Uninitialized) { |
| 107 base::Time network_time; | 115 base::Time network_time; |
| 108 base::TimeDelta uncertainty; | 116 base::TimeDelta uncertainty; |
| 109 EXPECT_FALSE(network_time_tracker()->GetNetworkTime(base::TimeTicks(), | 117 EXPECT_FALSE(network_time_tracker()->GetNetworkTime(base::TimeTicks(), |
| 110 &network_time, | 118 &network_time, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 base::Time old_now = Now(); | 150 base::Time old_now = Now(); |
| 143 base::TimeTicks old_ticks = TicksNow(); | 151 base::TimeTicks old_ticks = TicksNow(); |
| 144 AddToTicksNow(kPseudoSleepTime2); | 152 AddToTicksNow(kPseudoSleepTime2); |
| 145 UpdateNetworkTime( | 153 UpdateNetworkTime( |
| 146 old_now, | 154 old_now, |
| 147 base::TimeDelta::FromMilliseconds(kResolution2), | 155 base::TimeDelta::FromMilliseconds(kResolution2), |
| 148 base::TimeDelta::FromMilliseconds(kLatency2), | 156 base::TimeDelta::FromMilliseconds(kLatency2), |
| 149 old_ticks); | 157 old_ticks); |
| 150 EXPECT_TRUE(ValidateExpectedTime()); | 158 EXPECT_TRUE(ValidateExpectedTime()); |
| 151 } | 159 } |
| OLD | NEW |