1 // Copyright 2014 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.
5 #include "components/network_time/network_time_tracker.h"
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"
14 namespace network_time
{
18 // These are all in milliseconds.
19 const int64 kLatency1
= 50;
20 const int64 kLatency2
= 500;
22 // Can not be smaller than 15, it's the NowFromSystemTime() resolution.
23 const int64 kResolution1
= 17;
24 const int64 kResolution2
= 177;
26 const int64 kPseudoSleepTime1
= 500000001;
27 const int64 kPseudoSleepTime2
= 1888;
29 // A custom tick clock that will return an arbitrary time.
30 class TestTickClock
: public base::TickClock
{
32 explicit TestTickClock(base::TimeTicks
* ticks_now
) : ticks_now_(ticks_now
) {}
33 virtual ~TestTickClock() {}
35 virtual base::TimeTicks
NowTicks() OVERRIDE
{
40 base::TimeTicks
* ticks_now_
;
45 class NetworkTimeTrackerTest
: public testing::Test
{
47 virtual ~NetworkTimeTrackerTest() {}
49 virtual void SetUp() OVERRIDE
{
50 NetworkTimeTracker::RegisterPrefs(pref_service_
.registry());
52 now_
= base::Time::NowFromSystemTime();
53 network_time_tracker_
.reset(new NetworkTimeTracker(
54 scoped_ptr
<base::TickClock
>(new TestTickClock(&ticks_now_
)),
58 base::Time
Now() const {
59 return now_
+ (ticks_now_
- base::TimeTicks());
62 base::TimeTicks
TicksNow() const {
66 void AddToTicksNow(int64 ms
) {
67 ticks_now_
+= base::TimeDelta::FromMilliseconds(ms
);
70 // Updates the notifier's time with the specified parameters.
71 void UpdateNetworkTime(const base::Time
& network_time
,
72 const base::TimeDelta
& resolution
,
73 const base::TimeDelta
& latency
,
74 const base::TimeTicks
& post_time
) {
75 network_time_tracker_
->UpdateNetworkTime(
76 network_time
, resolution
, latency
, post_time
);
79 // Ensures the network time tracker has a network time and that the
80 // disparity between the network time version of |ticks_now_| and the actual
81 // |ticks_now_| value is within the uncertainty (should always be true
82 // because the network time notifier uses |ticks_now_| for the tick clock).
83 testing::AssertionResult
ValidateExpectedTime() const {
84 base::Time network_time
;
85 base::TimeDelta uncertainty
;
86 if (!network_time_tracker_
->GetNetworkTime(TicksNow(),
89 return testing::AssertionFailure() << "Failed to get network time.";
90 if (fabs(static_cast<double>(Now().ToInternalValue() -
91 network_time
.ToInternalValue())) >
92 static_cast<double>(uncertainty
.ToInternalValue())) {
93 return testing::AssertionFailure()
94 << "Expected network time not within uncertainty.";
96 return testing::AssertionSuccess();
99 NetworkTimeTracker
* network_time_tracker() {
100 return network_time_tracker_
.get();
104 // Used in building the current time that TestTickClock reports. See Now()
107 base::TimeTicks ticks_now_
;
109 TestingPrefServiceSimple pref_service_
;
111 // The network time tracker being tested.
112 scoped_ptr
<NetworkTimeTracker
> network_time_tracker_
;
115 // Should not return a value before UpdateNetworkTime gets called.
116 TEST_F(NetworkTimeTrackerTest
, Uninitialized
) {
117 base::Time network_time
;
118 base::TimeDelta uncertainty
;
119 EXPECT_FALSE(network_time_tracker()->GetNetworkTime(base::TimeTicks(),
124 // Verify that the the tracker receives and properly handles updates to the
126 TEST_F(NetworkTimeTrackerTest
, NetworkTimeUpdates
) {
129 base::TimeDelta::FromMilliseconds(kResolution1
),
130 base::TimeDelta::FromMilliseconds(kLatency1
),
132 EXPECT_TRUE(ValidateExpectedTime());
134 // Fake a wait for kPseudoSleepTime1 to make sure we keep tracking.
135 AddToTicksNow(kPseudoSleepTime1
);
136 EXPECT_TRUE(ValidateExpectedTime());
138 // Update the time with a new now value and kLatency2.
141 base::TimeDelta::FromMilliseconds(kResolution2
),
142 base::TimeDelta::FromMilliseconds(kLatency2
),
145 // Fake a wait for kPseudoSleepTime2 to make sure we keep tracking still.
146 AddToTicksNow(kPseudoSleepTime2
);
147 EXPECT_TRUE(ValidateExpectedTime());
149 // Fake a long delay between update task post time and the network notifier
150 // updating its network time. The uncertainty should account for the
152 base::Time old_now
= Now();
153 base::TimeTicks old_ticks
= TicksNow();
154 AddToTicksNow(kPseudoSleepTime2
);
157 base::TimeDelta::FromMilliseconds(kResolution2
),
158 base::TimeDelta::FromMilliseconds(kLatency2
),
160 EXPECT_TRUE(ValidateExpectedTime());
163 } // namespace network_time