MD Downloads: change date font size from 12 -> 13px
[chromium-blink-merge.git] / components / network_time / network_time_tracker_unittest.cc
blobb2352c0a697140d8d390ee674949934902fed93a
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"
7 #include <math.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 {
16 namespace {
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 {
31 public:
32 explicit TestTickClock(base::TimeTicks* ticks_now) : ticks_now_(ticks_now) {}
33 ~TestTickClock() override {}
35 base::TimeTicks NowTicks() override { return *ticks_now_; }
37 private:
38 base::TimeTicks* ticks_now_;
41 } // namespace
43 class NetworkTimeTrackerTest : public testing::Test {
44 public:
45 ~NetworkTimeTrackerTest() override {}
47 void SetUp() override {
48 NetworkTimeTracker::RegisterPrefs(pref_service_.registry());
50 now_ = base::Time::NowFromSystemTime();
51 network_time_tracker_.reset(new NetworkTimeTracker(
52 scoped_ptr<base::TickClock>(new TestTickClock(&ticks_now_)),
53 &pref_service_));
56 base::Time Now() const {
57 return now_ + (ticks_now_ - base::TimeTicks());
60 base::TimeTicks TicksNow() const {
61 return ticks_now_;
64 void AddToTicksNow(int64 ms) {
65 ticks_now_ += base::TimeDelta::FromMilliseconds(ms);
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);
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.";
94 return testing::AssertionSuccess();
97 NetworkTimeTracker* network_time_tracker() {
98 return network_time_tracker_.get();
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_;
107 TestingPrefServiceSimple pref_service_;
109 // The network time tracker being tested.
110 scoped_ptr<NetworkTimeTracker> network_time_tracker_;
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));
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());
132 // Fake a wait for kPseudoSleepTime1 to make sure we keep tracking.
133 AddToTicksNow(kPseudoSleepTime1);
134 EXPECT_TRUE(ValidateExpectedTime());
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());
143 // Fake a wait for kPseudoSleepTime2 to make sure we keep tracking still.
144 AddToTicksNow(kPseudoSleepTime2);
145 EXPECT_TRUE(ValidateExpectedTime());
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());
161 } // namespace network_time