summaryrefslogtreecommitdiff
path: root/chromium/content/common/inter_process_time_ticks_converter_unittest.cc
blob: a651bfb9625d02e5d5e69f2cabdd57fe946f2cf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/common/inter_process_time_ticks_converter.h"

#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"

using base::TimeTicks;

namespace content {

namespace {

struct TestParams {
  int64 local_lower_bound;
  int64 remote_lower_bound;
  int64 remote_upper_bound;
  int64 local_upper_bound;
  int64 test_time;
  int64 test_delta;
};

struct TestResults {
  int64 result_time;
  int32 result_delta;
};

TestResults RunTest(const TestParams& params) {
  TimeTicks local_lower_bound = TimeTicks::FromInternalValue(
      params.local_lower_bound);
  TimeTicks local_upper_bound = TimeTicks::FromInternalValue(
      params.local_upper_bound);
  TimeTicks remote_lower_bound = TimeTicks::FromInternalValue(
      params.remote_lower_bound);
  TimeTicks remote_upper_bound = TimeTicks::FromInternalValue(
      params.remote_upper_bound);
  TimeTicks test_time = TimeTicks::FromInternalValue(params.test_time);

  InterProcessTimeTicksConverter converter(
      LocalTimeTicks::FromTimeTicks(local_lower_bound),
      LocalTimeTicks::FromTimeTicks(local_upper_bound),
      RemoteTimeTicks::FromTimeTicks(remote_lower_bound),
      RemoteTimeTicks::FromTimeTicks(remote_upper_bound));

  TestResults results;
  results.result_time = converter.ToLocalTimeTicks(
      RemoteTimeTicks::FromTimeTicks(
          test_time)).ToTimeTicks().ToInternalValue();
  results.result_delta = converter.ToLocalTimeDelta(
      RemoteTimeDelta::FromRawDelta(params.test_delta)).ToInt32();
  return results;
}

TEST(InterProcessTimeTicksConverterTest, NullTime) {
  // Null / zero times should remain null.
  TestParams p;
  p.local_lower_bound = 1;
  p.remote_lower_bound = 2;
  p.remote_upper_bound = 5;
  p.local_upper_bound = 6;
  p.test_time = 0;
  p.test_delta = 0;
  TestResults results = RunTest(p);
  EXPECT_EQ(0, results.result_time);
  EXPECT_EQ(0, results.result_delta);
}

TEST(InterProcessTimeTicksConverterTest, NoSkew) {
  // All times are monotonic and centered, so no adjustment should occur.
  TestParams p;
  p.local_lower_bound = 1;
  p.remote_lower_bound = 2;
  p.remote_upper_bound = 5;
  p.local_upper_bound = 6;
  p.test_time = 3;
  p.test_delta = 1;
  TestResults results = RunTest(p);
  EXPECT_EQ(3, results.result_time);
  EXPECT_EQ(1, results.result_delta);
}

TEST(InterProcessTimeTicksConverterTest, OffsetMidpoints) {
  // All times are monotonic, but not centered. Adjust the |remote_*| times so
  // they are centered within the |local_*| times.
  TestParams p;
  p.local_lower_bound = 1;
  p.remote_lower_bound = 3;
  p.remote_upper_bound = 6;
  p.local_upper_bound = 6;
  p.test_time = 4;
  p.test_delta = 1;
  TestResults results = RunTest(p);
  EXPECT_EQ(3, results.result_time);
  EXPECT_EQ(1, results.result_delta);
}

TEST(InterProcessTimeTicksConverterTest, DoubleEndedSkew) {
  // |remote_lower_bound| occurs before |local_lower_bound| and
  // |remote_upper_bound| occurs after |local_upper_bound|. We must adjust both
  // bounds and scale down the delta. |test_time| is on the midpoint, so it
  // doesn't change. The ratio of local time to network time is 1:2, so we scale
  // |test_delta| to half.
  TestParams p;
  p.local_lower_bound = 3;
  p.remote_lower_bound = 1;
  p.remote_upper_bound = 9;
  p.local_upper_bound = 7;
  p.test_time = 5;
  p.test_delta = 2;
  TestResults results = RunTest(p);
  EXPECT_EQ(5, results.result_time);
  EXPECT_EQ(1, results.result_delta);
}

TEST(InterProcessTimeTicksConverterTest, FrontEndSkew) {
  // |remote_upper_bound| is coherent, but |remote_lower_bound| is not. So we
  // adjust the lower bound and move |test_time| out. The scale factor is 2:3,
  // but since we use integers, the numbers truncate from 3.33 to 3 and 1.33
  // to 1.
  TestParams p;
  p.local_lower_bound = 3;
  p.remote_lower_bound = 1;
  p.remote_upper_bound = 7;
  p.local_upper_bound = 7;
  p.test_time = 3;
  p.test_delta = 2;
  TestResults results = RunTest(p);
  EXPECT_EQ(4, results.result_time);
  EXPECT_EQ(1, results.result_delta);
}

TEST(InterProcessTimeTicksConverterTest, BackEndSkew) {
  // Like the previous test, but |remote_lower_bound| is coherent and
  // |remote_upper_bound| is skewed.
  TestParams p;
  p.local_lower_bound = 1;
  p.remote_lower_bound = 1;
  p.remote_upper_bound = 7;
  p.local_upper_bound = 5;
  p.test_time = 3;
  p.test_delta = 2;
  TestResults results = RunTest(p);
  EXPECT_EQ(2, results.result_time);
  EXPECT_EQ(1, results.result_delta);
}

TEST(InterProcessTimeTicksConverterTest, Instantaneous) {
  // The bounds are all okay, but the |remote_lower_bound| and
  // |remote_upper_bound| have the same value. No adjustments should be made and
  // no divide-by-zero errors should occur.
  TestParams p;
  p.local_lower_bound = 1;
  p.remote_lower_bound = 2;
  p.remote_upper_bound = 2;
  p.local_upper_bound = 3;
  p.test_time = 2;
  p.test_delta = 0;
  TestResults results = RunTest(p);
  EXPECT_EQ(2, results.result_time);
  EXPECT_EQ(0, results.result_delta);
}

TEST(InterProcessTimeTicksConverterTest, OffsetInstantaneous) {
  // The bounds are all okay, but the |remote_lower_bound| and
  // |remote_upper_bound| have the same value and are offset from the midpoint
  // of |local_lower_bound| and |local_upper_bound|. An offset should be applied
  // to make the midpoints line up.
  TestParams p;
  p.local_lower_bound = 1;
  p.remote_lower_bound = 3;
  p.remote_upper_bound = 3;
  p.local_upper_bound = 3;
  p.test_time = 3;
  p.test_delta = 0;
  TestResults results = RunTest(p);
  EXPECT_EQ(2, results.result_time);
  EXPECT_EQ(0, results.result_delta);
}

TEST(InterProcessTimeTicksConverterTest, DisjointInstantaneous) {
  // |local_lower_bound| and |local_upper_bound| are the same. No matter what
  // the other values are, they must fit within [local_lower_bound,
  // local_upper_bound].  So, all of the values should be adjusted so they are
  // exactly that value.
  TestParams p;
  p.local_lower_bound = 1;
  p.remote_lower_bound = 2;
  p.remote_upper_bound = 2;
  p.local_upper_bound = 1;
  p.test_time = 2;
  p.test_delta = 0;
  TestResults results = RunTest(p);
  EXPECT_EQ(1, results.result_time);
  EXPECT_EQ(0, results.result_delta);
}

TEST(InterProcessTimeTicksConverterTest, RoundingNearEdges) {
  // Verify that rounding never causes a value to appear outside the given
  // |local_*| range.
  const int kMaxRange = 101;
  for (int i = 1; i < kMaxRange; ++i) {
    for (int j = 1; j < kMaxRange; ++j) {
      TestParams p;
      p.local_lower_bound = 1;
      p.remote_lower_bound = 1;
      p.remote_upper_bound = j;
      p.local_upper_bound = i;

      p.test_time = 1;
      p.test_delta = 0;
      TestResults results = RunTest(p);
      EXPECT_LE(1, results.result_time);
      EXPECT_EQ(0, results.result_delta);

      p.test_time = j;
      p.test_delta = j - 1;
      results = RunTest(p);
      EXPECT_GE(i, results.result_time);
      EXPECT_GE(i - 1, results.result_delta);
    }
  }
}

TEST(InterProcessTimeTicksConverterTest, DisjointRanges) {
  TestParams p;
  p.local_lower_bound = 10;
  p.remote_lower_bound = 30;
  p.remote_upper_bound = 41;
  p.local_upper_bound = 20;
  p.test_time = 41;
  p.test_delta = 0;
  TestResults results = RunTest(p);
  EXPECT_EQ(20, results.result_time);
  EXPECT_EQ(0, results.result_delta);
}

}  // anonymous namespace

}  // namespace content