Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(277)

Side by Side Diff: chrome/browser/metrics/variations/network_time_tracker_unittest.cc

Issue 12096096: Give access to a network time kept in the variation service. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Final nits. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 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 <math.h>
6
7 #include "base/compiler_specific.h"
8 #include "chrome/browser/metrics/variations/network_time_tracker.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace {
12
13 // These are all in milliseconds.
14 const int64 kLatency1 = 50;
15 const int64 kLatency2 = 500;
16
17 // Can not be smaller than 15, it's the NowFromSystemTime() resolution.
18 const int64 kResolution1 = 17;
19 const int64 kResolution2 = 177;
20
21 const int64 kPseudoSleepTime1 = 500000001;
22 const int64 kPseudoSleepTime2 = 1888;
23
24 class TestNetworkTimeTracker : public NetworkTimeTracker {
25 public:
26 TestNetworkTimeTracker() : now_(base::Time::NowFromSystemTime()) {
27 }
28
29 void AddToTicksNow(int64 ms) {
30 ticks_now_ += base::TimeDelta::FromMilliseconds(ms);
31 }
32
33 base::Time now() const {
34 return now_ + (ticks_now_ - base::TimeTicks());
35 }
36
37 void ValidateExpectedTime() {
38 base::Time network_time;
39 base::TimeDelta uncertainty;
40 EXPECT_TRUE(GetNetworkTime(&network_time, &uncertainty));
41 EXPECT_LE(fabs(static_cast<double>(now().ToInternalValue() -
42 network_time.ToInternalValue())),
43 static_cast<double>(uncertainty.ToInternalValue()));
44 }
45
46 protected:
47 virtual base::TimeTicks GetTicksNow() const OVERRIDE {
48 return ticks_now_;
49 }
50
51 base::TimeTicks ticks_now_;
52 base::Time now_;
53 };
54
55 } // namespace.
56
57 TEST(NetworkTimeTrackerTest, ProperTimeTracking) {
58 TestNetworkTimeTracker network_time_tracker;
59
60 // Should not return a value before UpdateNetworkTime gets called.
61 base::Time network_time;
62 base::TimeDelta uncertainty;
63 EXPECT_FALSE(network_time_tracker.GetNetworkTime(&network_time,
64 &uncertainty));
65
66 network_time_tracker.UpdateNetworkTime(
67 network_time_tracker.now(),
68 base::TimeDelta::FromMilliseconds(kResolution1),
69 base::TimeDelta::FromMilliseconds(kLatency1));
70 network_time_tracker.ValidateExpectedTime();
71
72 // Fake a wait for kPseudoSleepTime1 to make sure we keep tracking.
73 network_time_tracker.AddToTicksNow(kPseudoSleepTime1);
74 network_time_tracker.ValidateExpectedTime();
75
76 // Update the time with a new now value and kLatency2.
77 network_time_tracker.UpdateNetworkTime(
78 network_time_tracker.now(),
79 base::TimeDelta::FromMilliseconds(kResolution2),
80 base::TimeDelta::FromMilliseconds(kLatency2));
81
82 // Fake a wait for kPseudoSleepTime2 to make sure we keep tracking still.
83 network_time_tracker.AddToTicksNow(kPseudoSleepTime2);
84 network_time_tracker.ValidateExpectedTime();
85 }
OLDNEW
« no previous file with comments | « chrome/browser/metrics/variations/network_time_tracker.cc ('k') | chrome/browser/metrics/variations/variations_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698