OLD | NEW |
| (Empty) |
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 "chrome/browser/network_time/network_time_tracker.h" | |
6 | |
7 #include <math.h> | |
8 | |
9 #include "base/compiler_specific.h" | |
10 #include "base/prefs/testing_pref_service.h" | |
11 #include "base/time/tick_clock.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace { | |
15 | |
16 // These are all in milliseconds. | |
17 const int64 kLatency1 = 50; | |
18 const int64 kLatency2 = 500; | |
19 | |
20 // Can not be smaller than 15, it's the NowFromSystemTime() resolution. | |
21 const int64 kResolution1 = 17; | |
22 const int64 kResolution2 = 177; | |
23 | |
24 const int64 kPseudoSleepTime1 = 500000001; | |
25 const int64 kPseudoSleepTime2 = 1888; | |
26 | |
27 // A custom tick clock that will return an arbitrary time. | |
28 class TestTickClock : public base::TickClock { | |
29 public: | |
30 explicit TestTickClock(base::TimeTicks* ticks_now) : ticks_now_(ticks_now) {} | |
31 virtual ~TestTickClock() {} | |
32 | |
33 virtual base::TimeTicks NowTicks() OVERRIDE { | |
34 return *ticks_now_; | |
35 } | |
36 | |
37 private: | |
38 base::TimeTicks* ticks_now_; | |
39 }; | |
40 | |
41 } // namespace | |
42 | |
43 class NetworkTimeTrackerTest : public testing::Test { | |
44 public: | |
45 virtual ~NetworkTimeTrackerTest() {} | |
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 | |
56 base::Time Now() const { | |
57 return now_ + (ticks_now_ - base::TimeTicks()); | |
58 } | |
59 | |
60 base::TimeTicks TicksNow() const { | |
61 return ticks_now_; | |
62 } | |
63 | |
64 void AddToTicksNow(int64 ms) { | |
65 ticks_now_ += base::TimeDelta::FromMilliseconds(ms); | |
66 } | |
67 | |
68 // Updates the notifier's time with the specified parameters. | |
69 void UpdateNetworkTime(const base::Time& network_time, | |
70 const base::TimeDelta& resolution, | |
71 const base::TimeDelta& latency, | |
72 const base::TimeTicks& post_time) { | |
73 network_time_tracker_->UpdateNetworkTime( | |
74 network_time, resolution, latency, post_time); | |
75 } | |
76 | |
77 // Ensures the network time tracker has a network time and that the | |
78 // disparity between the network time version of |ticks_now_| and the actual | |
79 // |ticks_now_| value is within the uncertainty (should always be true | |
80 // because the network time notifier uses |ticks_now_| for the tick clock). | |
81 testing::AssertionResult ValidateExpectedTime() const { | |
82 base::Time network_time; | |
83 base::TimeDelta uncertainty; | |
84 if (!network_time_tracker_->GetNetworkTime(TicksNow(), | |
85 &network_time, | |
86 &uncertainty)) | |
87 return testing::AssertionFailure() << "Failed to get network time."; | |
88 if (fabs(static_cast<double>(Now().ToInternalValue() - | |
89 network_time.ToInternalValue())) > | |
90 static_cast<double>(uncertainty.ToInternalValue())) { | |
91 return testing::AssertionFailure() | |
92 << "Expected network time not within uncertainty."; | |
93 } | |
94 return testing::AssertionSuccess(); | |
95 } | |
96 | |
97 NetworkTimeTracker* network_time_tracker() { | |
98 return network_time_tracker_.get(); | |
99 } | |
100 | |
101 private: | |
102 // Used in building the current time that TestTickClock reports. See Now() | |
103 // for details. | |
104 base::Time now_; | |
105 base::TimeTicks ticks_now_; | |
106 | |
107 TestingPrefServiceSimple pref_service_; | |
108 | |
109 // The network time tracker being tested. | |
110 scoped_ptr<NetworkTimeTracker> network_time_tracker_; | |
111 }; | |
112 | |
113 // Should not return a value before UpdateNetworkTime gets called. | |
114 TEST_F(NetworkTimeTrackerTest, Uninitialized) { | |
115 base::Time network_time; | |
116 base::TimeDelta uncertainty; | |
117 EXPECT_FALSE(network_time_tracker()->GetNetworkTime(base::TimeTicks(), | |
118 &network_time, | |
119 &uncertainty)); | |
120 } | |
121 | |
122 // Verify that the the tracker receives and properly handles updates to the | |
123 // network time. | |
124 TEST_F(NetworkTimeTrackerTest, NetworkTimeUpdates) { | |
125 UpdateNetworkTime( | |
126 Now(), | |
127 base::TimeDelta::FromMilliseconds(kResolution1), | |
128 base::TimeDelta::FromMilliseconds(kLatency1), | |
129 TicksNow()); | |
130 EXPECT_TRUE(ValidateExpectedTime()); | |
131 | |
132 // Fake a wait for kPseudoSleepTime1 to make sure we keep tracking. | |
133 AddToTicksNow(kPseudoSleepTime1); | |
134 EXPECT_TRUE(ValidateExpectedTime()); | |
135 | |
136 // Update the time with a new now value and kLatency2. | |
137 UpdateNetworkTime( | |
138 Now(), | |
139 base::TimeDelta::FromMilliseconds(kResolution2), | |
140 base::TimeDelta::FromMilliseconds(kLatency2), | |
141 TicksNow()); | |
142 | |
143 // Fake a wait for kPseudoSleepTime2 to make sure we keep tracking still. | |
144 AddToTicksNow(kPseudoSleepTime2); | |
145 EXPECT_TRUE(ValidateExpectedTime()); | |
146 | |
147 // Fake a long delay between update task post time and the network notifier | |
148 // updating its network time. The uncertainty should account for the | |
149 // disparity. | |
150 base::Time old_now = Now(); | |
151 base::TimeTicks old_ticks = TicksNow(); | |
152 AddToTicksNow(kPseudoSleepTime2); | |
153 UpdateNetworkTime( | |
154 old_now, | |
155 base::TimeDelta::FromMilliseconds(kResolution2), | |
156 base::TimeDelta::FromMilliseconds(kLatency2), | |
157 old_ticks); | |
158 EXPECT_TRUE(ValidateExpectedTime()); | |
159 } | |
OLD | NEW |