1 // Copyright (c) 2011 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.
6 #include "content/common/inter_process_time_ticks_converter.h"
7 #include "testing/gtest/include/gtest/gtest.h"
16 int64 local_lower_bound
;
17 int64 remote_lower_bound
;
18 int64 remote_upper_bound
;
19 int64 local_upper_bound
;
29 TestResults
RunTest(const TestParams
& params
) {
30 TimeTicks local_lower_bound
= TimeTicks::FromInternalValue(
31 params
.local_lower_bound
);
32 TimeTicks local_upper_bound
= TimeTicks::FromInternalValue(
33 params
.local_upper_bound
);
34 TimeTicks remote_lower_bound
= TimeTicks::FromInternalValue(
35 params
.remote_lower_bound
);
36 TimeTicks remote_upper_bound
= TimeTicks::FromInternalValue(
37 params
.remote_upper_bound
);
38 TimeTicks test_time
= TimeTicks::FromInternalValue(params
.test_time
);
40 InterProcessTimeTicksConverter
converter(
41 LocalTimeTicks::FromTimeTicks(local_lower_bound
),
42 LocalTimeTicks::FromTimeTicks(local_upper_bound
),
43 RemoteTimeTicks::FromTimeTicks(remote_lower_bound
),
44 RemoteTimeTicks::FromTimeTicks(remote_upper_bound
));
47 results
.result_time
= converter
.ToLocalTimeTicks(
48 RemoteTimeTicks::FromTimeTicks(
49 test_time
)).ToTimeTicks().ToInternalValue();
50 results
.result_delta
= converter
.ToLocalTimeDelta(
51 RemoteTimeDelta::FromRawDelta(params
.test_delta
)).ToInt32();
55 TEST(InterProcessTimeTicksConverterTest
, NoSkew
) {
56 // All times are monotonic and centered, so no adjustment should occur.
58 p
.local_lower_bound
= 0;
59 p
.remote_lower_bound
= 1;
60 p
.remote_upper_bound
= 4;
61 p
.local_upper_bound
= 5;
64 TestResults results
= RunTest(p
);
65 EXPECT_EQ(2, results
.result_time
);
66 EXPECT_EQ(1, results
.result_delta
);
69 TEST(InterProcessTimeTicksConverterTest
, OffsetMidpoints
) {
70 // All times are monotonic, but not centered. Adjust the |remote_*| times so
71 // they are centered within the |local_*| times.
73 p
.local_lower_bound
= 0;
74 p
.remote_lower_bound
= 2;
75 p
.remote_upper_bound
= 5;
76 p
.local_upper_bound
= 5;
79 TestResults results
= RunTest(p
);
80 EXPECT_EQ(2, results
.result_time
);
81 EXPECT_EQ(1, results
.result_delta
);
84 TEST(InterProcessTimeTicksConverterTest
, DoubleEndedSkew
) {
85 // |remote_lower_bound| occurs before |local_lower_bound| and
86 // |remote_upper_bound| occurs after |local_upper_bound|. We must adjust both
87 // bounds and scale down the delta. |test_time| is on the midpoint, so it
88 // doesn't change. The ratio of local time to network time is 1:2, so we scale
89 // |test_delta| to half.
91 p
.local_lower_bound
= 2;
92 p
.remote_lower_bound
= 0;
93 p
.remote_upper_bound
= 8;
94 p
.local_upper_bound
= 6;
97 TestResults results
= RunTest(p
);
98 EXPECT_EQ(4, results
.result_time
);
99 EXPECT_EQ(1, results
.result_delta
);
102 TEST(InterProcessTimeTicksConverterTest
, FrontEndSkew
) {
103 // |remote_upper_bound| is coherent, but |remote_lower_bound| is not. So we
104 // adjust the lower bound and move |test_time| out. The scale factor is 2:3,
105 // but since we use integers, the numbers truncate from 3.33 to 3 and 1.33
108 p
.local_lower_bound
= 2;
109 p
.remote_lower_bound
= 0;
110 p
.remote_upper_bound
= 6;
111 p
.local_upper_bound
= 6;
114 TestResults results
= RunTest(p
);
115 EXPECT_EQ(3, results
.result_time
);
116 EXPECT_EQ(1, results
.result_delta
);
119 TEST(InterProcessTimeTicksConverterTest
, BackEndSkew
) {
120 // Like the previous test, but |remote_lower_bound| is coherent and
121 // |remote_upper_bound| is skewed.
123 p
.local_lower_bound
= 0;
124 p
.remote_lower_bound
= 0;
125 p
.remote_upper_bound
= 6;
126 p
.local_upper_bound
= 4;
129 TestResults results
= RunTest(p
);
130 EXPECT_EQ(1, results
.result_time
);
131 EXPECT_EQ(1, results
.result_delta
);
134 TEST(InterProcessTimeTicksConverterTest
, Instantaneous
) {
135 // The bounds are all okay, but the |remote_lower_bound| and
136 // |remote_upper_bound| have the same value. No adjustments should be made and
137 // no divide-by-zero errors should occur.
139 p
.local_lower_bound
= 0;
140 p
.remote_lower_bound
= 1;
141 p
.remote_upper_bound
= 1;
142 p
.local_upper_bound
= 2;
145 TestResults results
= RunTest(p
);
146 EXPECT_EQ(1, results
.result_time
);
147 EXPECT_EQ(0, results
.result_delta
);
150 TEST(InterProcessTimeTicksConverterTest
, OffsetInstantaneous
) {
151 // The bounds are all okay, but the |remote_lower_bound| and
152 // |remote_upper_bound| have the same value and are offset from the midpoint
153 // of |local_lower_bound| and |local_upper_bound|. An offset should be applied
154 // to make the midpoints line up.
156 p
.local_lower_bound
= 0;
157 p
.remote_lower_bound
= 2;
158 p
.remote_upper_bound
= 2;
159 p
.local_upper_bound
= 2;
162 TestResults results
= RunTest(p
);
163 EXPECT_EQ(1, results
.result_time
);
164 EXPECT_EQ(0, results
.result_delta
);
167 TEST(InterProcessTimeTicksConverterTest
, DisjointInstantaneous
) {
168 // |local_lower_bound| and |local_upper_bound| are the same. No matter what
169 // the other values are, they must fit within [local_lower_bound,
170 // local_upper_bound]. So, all of the values should be adjusted so they are
171 // exactly that value.
173 p
.local_lower_bound
= 1;
174 p
.remote_lower_bound
= 2;
175 p
.remote_upper_bound
= 2;
176 p
.local_upper_bound
= 1;
179 TestResults results
= RunTest(p
);
180 EXPECT_EQ(1, results
.result_time
);
181 EXPECT_EQ(0, results
.result_delta
);
184 TEST(InterProcessTimeTicksConverterTest
, RoundingNearEdges
) {
185 // Verify that rounding never causes a value to appear outside the given
187 const int kMaxRange
= 100;
188 for (int i
= 0; i
< kMaxRange
; ++i
) {
189 for (int j
= 0; j
< kMaxRange
; ++j
) {
191 p
.local_lower_bound
= 0;
192 p
.remote_lower_bound
= 0;
193 p
.remote_upper_bound
= j
;
194 p
.local_upper_bound
= i
;
197 TestResults results
= RunTest(p
);
198 EXPECT_LE(0, results
.result_time
);
199 EXPECT_GE(i
, results
.result_delta
);
204 } // anonymous namespace
206 } // namespace content