| OLD | NEW |
| 1 // Copyright 2011 The Chromium Authors. All rights reserved. | 1 // Copyright 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/scheduler/delay_based_time_source.h" | 5 #include "cc/scheduler/delay_based_time_source.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/test/test_simple_task_runner.h" | 8 #include "base/test/test_simple_task_runner.h" |
| 9 #include "cc/test/scheduler_test_common.h" | 9 #include "cc/test/scheduler_test_common.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 // At 60Hz, when the tick returns at exactly the requested next time, make sure | 81 // At 60Hz, when the tick returns at exactly the requested next time, make sure |
| 82 // a 16ms next delay is posted. | 82 // a 16ms next delay is posted. |
| 83 TEST(DelayBasedTimeSource, NextDelaySaneWhenExactlyOnRequestedTime) { | 83 TEST(DelayBasedTimeSource, NextDelaySaneWhenExactlyOnRequestedTime) { |
| 84 scoped_refptr<base::TestSimpleTaskRunner> task_runner = | 84 scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 85 new base::TestSimpleTaskRunner; | 85 new base::TestSimpleTaskRunner; |
| 86 FakeTimeSourceClient client; | 86 FakeTimeSourceClient client; |
| 87 scoped_refptr<FakeDelayBasedTimeSource> timer = | 87 scoped_refptr<FakeDelayBasedTimeSource> timer = |
| 88 FakeDelayBasedTimeSource::Create(Interval(), task_runner.get()); | 88 FakeDelayBasedTimeSource::Create(Interval(), task_runner.get()); |
| 89 timer->SetClient(&client); | 89 timer->SetClient(&client); |
| 90 timer->SetActive(true); | 90 timer->SetActive(true); |
| 91 // Run the first tick. | 91 // Run the first task, as that activates the timer and picks up a timebase. |
| 92 task_runner->RunPendingTasks(); | 92 task_runner->RunPendingTasks(); |
| 93 | 93 |
| 94 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 94 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 95 | 95 |
| 96 timer->SetNow(timer->Now() + Interval()); | 96 timer->SetNow(timer->Now() + Interval()); |
| 97 task_runner->RunPendingTasks(); | 97 task_runner->RunPendingTasks(); |
| 98 | 98 |
| 99 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 99 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 100 } | 100 } |
| 101 | 101 |
| 102 // At 60Hz, when the tick returns at slightly after the requested next time, | 102 // At 60Hz, when the tick returns at slightly after the requested next time, |
| 103 // make sure a 16ms next delay is posted. | 103 // make sure a 16ms next delay is posted. |
| 104 TEST(DelayBasedTimeSource, NextDelaySaneWhenSlightlyAfterRequestedTime) { | 104 TEST(DelayBasedTimeSource, NextDelaySaneWhenSlightlyAfterRequestedTime) { |
| 105 scoped_refptr<base::TestSimpleTaskRunner> task_runner = | 105 scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 106 new base::TestSimpleTaskRunner; | 106 new base::TestSimpleTaskRunner; |
| 107 FakeTimeSourceClient client; | 107 FakeTimeSourceClient client; |
| 108 scoped_refptr<FakeDelayBasedTimeSource> timer = | 108 scoped_refptr<FakeDelayBasedTimeSource> timer = |
| 109 FakeDelayBasedTimeSource::Create(Interval(), task_runner.get()); | 109 FakeDelayBasedTimeSource::Create(Interval(), task_runner.get()); |
| 110 timer->SetClient(&client); | 110 timer->SetClient(&client); |
| 111 timer->SetActive(true); | 111 timer->SetActive(true); |
| 112 // Run the first tick. | 112 // Run the first task, as that activates the timer and picks up a timebase. |
| 113 task_runner->RunPendingTasks(); | 113 task_runner->RunPendingTasks(); |
| 114 | 114 |
| 115 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 115 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 116 | 116 |
| 117 timer->SetNow(timer->Now() + Interval() + | 117 timer->SetNow(timer->Now() + Interval() + |
| 118 base::TimeDelta::FromMicroseconds(1)); | 118 base::TimeDelta::FromMicroseconds(1)); |
| 119 task_runner->RunPendingTasks(); | 119 task_runner->RunPendingTasks(); |
| 120 | 120 |
| 121 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 121 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 122 } | 122 } |
| 123 | 123 |
| 124 // At 60Hz, when the tick returns at exactly 2*interval after the requested next | 124 // At 60Hz, when the tick returns at exactly 2*interval after the requested next |
| 125 // time, make sure a 0ms next delay is posted. | 125 // time, make sure a 16ms next delay is posted. |
| 126 TEST(DelayBasedTimeSource, NextDelaySaneWhenExactlyTwiceAfterRequestedTime) { | 126 TEST(DelayBasedTimeSource, NextDelaySaneWhenExactlyTwiceAfterRequestedTime) { |
| 127 scoped_refptr<base::TestSimpleTaskRunner> task_runner = | 127 scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 128 new base::TestSimpleTaskRunner; | 128 new base::TestSimpleTaskRunner; |
| 129 FakeTimeSourceClient client; | 129 FakeTimeSourceClient client; |
| 130 scoped_refptr<FakeDelayBasedTimeSource> timer = | 130 scoped_refptr<FakeDelayBasedTimeSource> timer = |
| 131 FakeDelayBasedTimeSource::Create(Interval(), task_runner.get()); | 131 FakeDelayBasedTimeSource::Create(Interval(), task_runner.get()); |
| 132 timer->SetClient(&client); | 132 timer->SetClient(&client); |
| 133 timer->SetActive(true); | 133 timer->SetActive(true); |
| 134 // Run the first tick. | 134 // Run the first task, as that activates the timer and picks up a timebase. |
| 135 task_runner->RunPendingTasks(); | 135 task_runner->RunPendingTasks(); |
| 136 | 136 |
| 137 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 137 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 138 | 138 |
| 139 timer->SetNow(timer->Now() + 2 * Interval()); | 139 timer->SetNow(timer->Now() + 2 * Interval()); |
| 140 task_runner->RunPendingTasks(); | 140 task_runner->RunPendingTasks(); |
| 141 | 141 |
| 142 EXPECT_EQ(0, task_runner->NextPendingTaskDelay().InMilliseconds()); | 142 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 143 } | 143 } |
| 144 | 144 |
| 145 // At 60Hz, when the tick returns at 2*interval and a bit after the requested | 145 // At 60Hz, when the tick returns at 2*interval and a bit after the requested |
| 146 // next time, make sure a 16ms next delay is posted. | 146 // next time, make sure a 16ms next delay is posted. |
| 147 TEST(DelayBasedTimeSource, NextDelaySaneWhenSlightlyAfterTwiceRequestedTime) { | 147 TEST(DelayBasedTimeSource, NextDelaySaneWhenSlightlyAfterTwiceRequestedTime) { |
| 148 scoped_refptr<base::TestSimpleTaskRunner> task_runner = | 148 scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 149 new base::TestSimpleTaskRunner; | 149 new base::TestSimpleTaskRunner; |
| 150 FakeTimeSourceClient client; | 150 FakeTimeSourceClient client; |
| 151 scoped_refptr<FakeDelayBasedTimeSource> timer = | 151 scoped_refptr<FakeDelayBasedTimeSource> timer = |
| 152 FakeDelayBasedTimeSource::Create(Interval(), task_runner.get()); | 152 FakeDelayBasedTimeSource::Create(Interval(), task_runner.get()); |
| 153 timer->SetClient(&client); | 153 timer->SetClient(&client); |
| 154 timer->SetActive(true); | 154 timer->SetActive(true); |
| 155 // Run the first tick. | 155 // Run the first task, as that activates the timer and picks up a timebase. |
| 156 task_runner->RunPendingTasks(); | 156 task_runner->RunPendingTasks(); |
| 157 | 157 |
| 158 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 158 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 159 | 159 |
| 160 timer->SetNow(timer->Now() + 2 * Interval() + | 160 timer->SetNow(timer->Now() + 2 * Interval() + |
| 161 base::TimeDelta::FromMicroseconds(1)); | 161 base::TimeDelta::FromMicroseconds(1)); |
| 162 task_runner->RunPendingTasks(); | 162 task_runner->RunPendingTasks(); |
| 163 | 163 |
| 164 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 164 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 165 } | 165 } |
| 166 | 166 |
| 167 // At 60Hz, when the tick returns halfway to the next frame time, make sure | 167 // At 60Hz, when the tick returns halfway to the next frame time, make sure |
| 168 // a correct next delay value is posted. | 168 // a correct next delay value is posted. |
| 169 TEST(DelayBasedTimeSource, NextDelaySaneWhenHalfAfterRequestedTime) { | 169 TEST(DelayBasedTimeSource, NextDelaySaneWhenHalfAfterRequestedTime) { |
| 170 scoped_refptr<base::TestSimpleTaskRunner> task_runner = | 170 scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 171 new base::TestSimpleTaskRunner; | 171 new base::TestSimpleTaskRunner; |
| 172 FakeTimeSourceClient client; | 172 FakeTimeSourceClient client; |
| 173 scoped_refptr<FakeDelayBasedTimeSource> timer = | 173 scoped_refptr<FakeDelayBasedTimeSource> timer = |
| 174 FakeDelayBasedTimeSource::Create(Interval(), task_runner.get()); | 174 FakeDelayBasedTimeSource::Create(Interval(), task_runner.get()); |
| 175 timer->SetClient(&client); | 175 timer->SetClient(&client); |
| 176 timer->SetActive(true); | 176 timer->SetActive(true); |
| 177 // Run the first tick. | 177 // Run the first task, as that activates the timer and picks up a timebase. |
| 178 task_runner->RunPendingTasks(); | 178 task_runner->RunPendingTasks(); |
| 179 | 179 |
| 180 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 180 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 181 | 181 |
| 182 timer->SetNow(timer->Now() + Interval() + | 182 timer->SetNow(timer->Now() + Interval() + |
| 183 base::TimeDelta::FromMilliseconds(8)); | 183 base::TimeDelta::FromMilliseconds(8)); |
| 184 task_runner->RunPendingTasks(); | 184 task_runner->RunPendingTasks(); |
| 185 | 185 |
| 186 EXPECT_EQ(8, task_runner->NextPendingTaskDelay().InMilliseconds()); | 186 EXPECT_EQ(8, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 187 } | 187 } |
| 188 | 188 |
| 189 // If the timebase and interval are updated with a jittery source, we want to | 189 // If the timebase and interval are updated with a jittery source, we want to |
| 190 // make sure we do not double tick. | 190 // make sure we do not double tick. |
| 191 TEST(DelayBasedTimeSource, SaneHandlingOfJitteryTimebase) { | 191 TEST(DelayBasedTimeSource, SaneHandlingOfJitteryTimebase) { |
| 192 scoped_refptr<base::TestSimpleTaskRunner> task_runner = | 192 scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 193 new base::TestSimpleTaskRunner; | 193 new base::TestSimpleTaskRunner; |
| 194 FakeTimeSourceClient client; | 194 FakeTimeSourceClient client; |
| 195 scoped_refptr<FakeDelayBasedTimeSource> timer = | 195 scoped_refptr<FakeDelayBasedTimeSource> timer = |
| 196 FakeDelayBasedTimeSource::Create(Interval(), task_runner.get()); | 196 FakeDelayBasedTimeSource::Create(Interval(), task_runner.get()); |
| 197 timer->SetClient(&client); | 197 timer->SetClient(&client); |
| 198 timer->SetActive(true); | 198 timer->SetActive(true); |
| 199 // Run the first tick. | 199 // Run the first task, as that activates the timer and picks up a timebase. |
| 200 task_runner->RunPendingTasks(); | 200 task_runner->RunPendingTasks(); |
| 201 | 201 |
| 202 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 202 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 203 | 203 |
| 204 // Jitter timebase ~1ms late | 204 // Jitter timebase ~1ms late |
| 205 timer->SetNow(timer->Now() + Interval()); | 205 timer->SetNow(timer->Now() + Interval()); |
| 206 timer->SetTimebaseAndInterval( | 206 timer->SetTimebaseAndInterval( |
| 207 timer->Now() + base::TimeDelta::FromMilliseconds(1), Interval()); | 207 timer->Now() + base::TimeDelta::FromMilliseconds(1), Interval()); |
| 208 task_runner->RunPendingTasks(); | 208 task_runner->RunPendingTasks(); |
| 209 | 209 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 220 } | 220 } |
| 221 | 221 |
| 222 TEST(DelayBasedTimeSource, HandlesSignificantTimebaseChangesImmediately) { | 222 TEST(DelayBasedTimeSource, HandlesSignificantTimebaseChangesImmediately) { |
| 223 scoped_refptr<base::TestSimpleTaskRunner> task_runner = | 223 scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 224 new base::TestSimpleTaskRunner; | 224 new base::TestSimpleTaskRunner; |
| 225 FakeTimeSourceClient client; | 225 FakeTimeSourceClient client; |
| 226 scoped_refptr<FakeDelayBasedTimeSource> timer = | 226 scoped_refptr<FakeDelayBasedTimeSource> timer = |
| 227 FakeDelayBasedTimeSource::Create(Interval(), task_runner.get()); | 227 FakeDelayBasedTimeSource::Create(Interval(), task_runner.get()); |
| 228 timer->SetClient(&client); | 228 timer->SetClient(&client); |
| 229 timer->SetActive(true); | 229 timer->SetActive(true); |
| 230 // Run the first tick. | 230 // Run the first task, as that activates the timer and picks up a timebase. |
| 231 task_runner->RunPendingTasks(); | 231 task_runner->RunPendingTasks(); |
| 232 | 232 |
| 233 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 233 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 234 | 234 |
| 235 // Tick, then shift timebase by +7ms. | 235 // Tick, then shift timebase by +7ms. |
| 236 timer->SetNow(timer->Now() + Interval()); | 236 timer->SetNow(timer->Now() + Interval()); |
| 237 task_runner->RunPendingTasks(); | 237 task_runner->RunPendingTasks(); |
| 238 | 238 |
| 239 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 239 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 240 | 240 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 264 } | 264 } |
| 265 | 265 |
| 266 TEST(DelayBasedTimeSource, HanldlesSignificantIntervalChangesImmediately) { | 266 TEST(DelayBasedTimeSource, HanldlesSignificantIntervalChangesImmediately) { |
| 267 scoped_refptr<base::TestSimpleTaskRunner> task_runner = | 267 scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 268 new base::TestSimpleTaskRunner; | 268 new base::TestSimpleTaskRunner; |
| 269 FakeTimeSourceClient client; | 269 FakeTimeSourceClient client; |
| 270 scoped_refptr<FakeDelayBasedTimeSource> timer = | 270 scoped_refptr<FakeDelayBasedTimeSource> timer = |
| 271 FakeDelayBasedTimeSource::Create(Interval(), task_runner.get()); | 271 FakeDelayBasedTimeSource::Create(Interval(), task_runner.get()); |
| 272 timer->SetClient(&client); | 272 timer->SetClient(&client); |
| 273 timer->SetActive(true); | 273 timer->SetActive(true); |
| 274 // Run the first tick. | 274 // Run the first task, as that activates the timer and picks up a timebase. |
| 275 task_runner->RunPendingTasks(); | 275 task_runner->RunPendingTasks(); |
| 276 | 276 |
| 277 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 277 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 278 | 278 |
| 279 // Tick, then double the interval. | 279 // Tick, then double the interval. |
| 280 timer->SetNow(timer->Now() + Interval()); | 280 timer->SetNow(timer->Now() + Interval()); |
| 281 task_runner->RunPendingTasks(); | 281 task_runner->RunPendingTasks(); |
| 282 | 282 |
| 283 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 283 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 284 | 284 |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 | 392 |
| 393 // Start the timer again, but before the next tick time the timer previously | 393 // Start the timer again, but before the next tick time the timer previously |
| 394 // planned on using. That same tick time should still be targeted. | 394 // planned on using. That same tick time should still be targeted. |
| 395 timer->SetNow(timer->Now() + base::TimeDelta::FromMilliseconds(20)); | 395 timer->SetNow(timer->Now() + base::TimeDelta::FromMilliseconds(20)); |
| 396 timer->SetActive(true); | 396 timer->SetActive(true); |
| 397 EXPECT_EQ(13, task_runner->NextPendingTaskDelay().InMilliseconds()); | 397 EXPECT_EQ(13, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 398 } | 398 } |
| 399 | 399 |
| 400 } // namespace | 400 } // namespace |
| 401 } // namespace cc | 401 } // namespace cc |
| OLD | NEW |