OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "base/time.h" |
| 6 #include "content/common/inter_process_time_ticks_converter.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 |
| 9 using base::TimeTicks; |
| 10 |
| 11 namespace content { |
| 12 |
| 13 namespace { |
| 14 |
| 15 struct TestParams { |
| 16 int64 local_lower_bound; |
| 17 int64 remote_lower_bound; |
| 18 int64 remote_upper_bound; |
| 19 int64 local_upper_bound; |
| 20 int64 test_time; |
| 21 int64 test_delta; |
| 22 }; |
| 23 |
| 24 struct TestResults { |
| 25 int64 result_time; |
| 26 int32 result_delta; |
| 27 }; |
| 28 |
| 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); |
| 39 |
| 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)); |
| 45 |
| 46 TestResults results; |
| 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(); |
| 52 return results; |
| 53 } |
| 54 |
| 55 TEST(InterProcessTimeTicksConverterTest, NoSkew) { |
| 56 // All times are monotonic and centered, so no adjustment should occur. |
| 57 TestParams p; |
| 58 p.local_lower_bound = 0; |
| 59 p.remote_lower_bound = 1; |
| 60 p.remote_upper_bound = 4; |
| 61 p.local_upper_bound = 5; |
| 62 p.test_time = 2; |
| 63 p.test_delta = 1; |
| 64 TestResults results = RunTest(p); |
| 65 EXPECT_EQ(2, results.result_time); |
| 66 EXPECT_EQ(1, results.result_delta); |
| 67 } |
| 68 |
| 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. |
| 72 TestParams p; |
| 73 p.local_lower_bound = 0; |
| 74 p.remote_lower_bound = 2; |
| 75 p.remote_upper_bound = 5; |
| 76 p.local_upper_bound = 5; |
| 77 p.test_time = 3; |
| 78 p.test_delta = 1; |
| 79 TestResults results = RunTest(p); |
| 80 EXPECT_EQ(2, results.result_time); |
| 81 EXPECT_EQ(1, results.result_delta); |
| 82 } |
| 83 |
| 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. |
| 90 TestParams p; |
| 91 p.local_lower_bound = 2; |
| 92 p.remote_lower_bound = 0; |
| 93 p.remote_upper_bound = 8; |
| 94 p.local_upper_bound = 6; |
| 95 p.test_time = 4; |
| 96 p.test_delta = 2; |
| 97 TestResults results = RunTest(p); |
| 98 EXPECT_EQ(4, results.result_time); |
| 99 EXPECT_EQ(1, results.result_delta); |
| 100 } |
| 101 |
| 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 |
| 106 // to 1. |
| 107 TestParams p; |
| 108 p.local_lower_bound = 2; |
| 109 p.remote_lower_bound = 0; |
| 110 p.remote_upper_bound = 6; |
| 111 p.local_upper_bound = 6; |
| 112 p.test_time = 2; |
| 113 p.test_delta = 2; |
| 114 TestResults results = RunTest(p); |
| 115 EXPECT_EQ(3, results.result_time); |
| 116 EXPECT_EQ(1, results.result_delta); |
| 117 } |
| 118 |
| 119 TEST(InterProcessTimeTicksConverterTest, BackEndSkew) { |
| 120 // Like the previous test, but |remote_lower_bound| is coherent and |
| 121 // |remote_upper_bound| is skewed. |
| 122 TestParams p; |
| 123 p.local_lower_bound = 0; |
| 124 p.remote_lower_bound = 0; |
| 125 p.remote_upper_bound = 6; |
| 126 p.local_upper_bound = 4; |
| 127 p.test_time = 2; |
| 128 p.test_delta = 2; |
| 129 TestResults results = RunTest(p); |
| 130 EXPECT_EQ(1, results.result_time); |
| 131 EXPECT_EQ(1, results.result_delta); |
| 132 } |
| 133 |
| 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. |
| 138 TestParams p; |
| 139 p.local_lower_bound = 0; |
| 140 p.remote_lower_bound = 1; |
| 141 p.remote_upper_bound = 1; |
| 142 p.local_upper_bound = 2; |
| 143 p.test_time = 1; |
| 144 p.test_delta = 0; |
| 145 TestResults results = RunTest(p); |
| 146 EXPECT_EQ(1, results.result_time); |
| 147 EXPECT_EQ(0, results.result_delta); |
| 148 } |
| 149 |
| 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. |
| 155 TestParams p; |
| 156 p.local_lower_bound = 0; |
| 157 p.remote_lower_bound = 2; |
| 158 p.remote_upper_bound = 2; |
| 159 p.local_upper_bound = 2; |
| 160 p.test_time = 2; |
| 161 p.test_delta = 0; |
| 162 TestResults results = RunTest(p); |
| 163 EXPECT_EQ(1, results.result_time); |
| 164 EXPECT_EQ(0, results.result_delta); |
| 165 } |
| 166 |
| 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. |
| 172 TestParams p; |
| 173 p.local_lower_bound = 1; |
| 174 p.remote_lower_bound = 2; |
| 175 p.remote_upper_bound = 2; |
| 176 p.local_upper_bound = 1; |
| 177 p.test_time = 2; |
| 178 p.test_delta = 0; |
| 179 TestResults results = RunTest(p); |
| 180 EXPECT_EQ(1, results.result_time); |
| 181 EXPECT_EQ(0, results.result_delta); |
| 182 } |
| 183 |
| 184 TEST(InterProcessTimeTicksConverterTest, RoundingNearEdges) { |
| 185 // Verify that rounding never causes a value to appear outside the given |
| 186 // |local_*| range. |
| 187 const int kMaxRange = 100; |
| 188 for (int i = 0; i < kMaxRange; ++i) { |
| 189 for (int j = 0; j < kMaxRange; ++j) { |
| 190 TestParams p; |
| 191 p.local_lower_bound = 0; |
| 192 p.remote_lower_bound = 0; |
| 193 p.remote_upper_bound = j; |
| 194 p.local_upper_bound = i; |
| 195 p.test_time = 0; |
| 196 p.test_delta = j; |
| 197 TestResults results = RunTest(p); |
| 198 EXPECT_LE(0, results.result_time); |
| 199 EXPECT_GE(i, results.result_delta); |
| 200 } |
| 201 } |
| 202 } |
| 203 |
| 204 } // anonymous namespace |
| 205 |
| 206 } // namespace content |
OLD | NEW |