| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/test/test_simple_task_runner.h" | 9 #include "base/test/test_simple_task_runner.h" |
| 10 #include "cc/test/scheduler_test_common.h" | 10 #include "cc/test/scheduler_test_common.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 namespace cc { | 13 namespace cc { |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 base::TimeDelta Interval() { | 16 base::TimeDelta Interval() { |
| 17 return base::TimeDelta::FromMicroseconds(base::Time::kMicrosecondsPerSecond / | 17 return base::TimeDelta::FromMicroseconds(base::Time::kMicrosecondsPerSecond / |
| 18 60); | 18 60); |
| 19 } | 19 } |
| 20 | 20 |
| 21 TEST(DelayBasedTimeSourceTest, TaskPostedAndTickCalled) { | 21 TEST(DelayBasedTimeSourceTest, TaskPostedAndTickCalled) { |
| 22 scoped_refptr<base::TestSimpleTaskRunner> task_runner = | 22 scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 23 new base::TestSimpleTaskRunner; | 23 new base::TestSimpleTaskRunner; |
| 24 FakeDelayBasedTimeSourceClient client; | 24 FakeDelayBasedTimeSourceClient client; |
| 25 std::unique_ptr<FakeDelayBasedTimeSource> timer( | 25 base::SimpleTestTickClock now_src; |
| 26 new FakeDelayBasedTimeSource(task_runner.get())); | 26 std::unique_ptr<DelayBasedTimeSource> timer( |
| 27 new DelayBasedTimeSource(task_runner.get(), &now_src)); |
| 27 timer->SetClient(&client); | 28 timer->SetClient(&client); |
| 28 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); | 29 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); |
| 29 timer->SetActive(true); | 30 timer->SetActive(true); |
| 30 EXPECT_TRUE(timer->Active()); | 31 EXPECT_TRUE(timer->Active()); |
| 31 EXPECT_TRUE(task_runner->HasPendingTask()); | 32 EXPECT_TRUE(task_runner->HasPendingTask()); |
| 32 | 33 |
| 33 timer->SetNow(timer->Now() + base::TimeDelta::FromMilliseconds(16)); | 34 now_src.SetNowTicks(now_src.NowTicks() + |
| 35 base::TimeDelta::FromMilliseconds(16)); |
| 34 task_runner->RunPendingTasks(); | 36 task_runner->RunPendingTasks(); |
| 35 EXPECT_TRUE(timer->Active()); | 37 EXPECT_TRUE(timer->Active()); |
| 36 EXPECT_TRUE(client.TickCalled()); | 38 EXPECT_TRUE(client.TickCalled()); |
| 37 } | 39 } |
| 38 | 40 |
| 39 TEST(DelayBasedTimeSourceTest, TickNotCalledWithTaskPosted) { | 41 TEST(DelayBasedTimeSourceTest, TickNotCalledWithTaskPosted) { |
| 40 scoped_refptr<base::TestSimpleTaskRunner> task_runner = | 42 scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 41 new base::TestSimpleTaskRunner; | 43 new base::TestSimpleTaskRunner; |
| 42 FakeDelayBasedTimeSourceClient client; | 44 FakeDelayBasedTimeSourceClient client; |
| 43 std::unique_ptr<FakeDelayBasedTimeSource> timer( | 45 base::SimpleTestTickClock now_src; |
| 44 new FakeDelayBasedTimeSource(task_runner.get())); | 46 std::unique_ptr<DelayBasedTimeSource> timer( |
| 47 new DelayBasedTimeSource(task_runner.get(), &now_src)); |
| 45 timer->SetClient(&client); | 48 timer->SetClient(&client); |
| 46 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); | 49 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); |
| 47 timer->SetActive(true); | 50 timer->SetActive(true); |
| 48 EXPECT_TRUE(task_runner->HasPendingTask()); | 51 EXPECT_TRUE(task_runner->HasPendingTask()); |
| 49 timer->SetActive(false); | 52 timer->SetActive(false); |
| 50 task_runner->RunPendingTasks(); | 53 task_runner->RunPendingTasks(); |
| 51 EXPECT_FALSE(client.TickCalled()); | 54 EXPECT_FALSE(client.TickCalled()); |
| 52 } | 55 } |
| 53 | 56 |
| 54 TEST(DelayBasedTimeSourceTest, StartTwiceEnqueuesOneTask) { | 57 TEST(DelayBasedTimeSourceTest, StartTwiceEnqueuesOneTask) { |
| 55 scoped_refptr<base::TestSimpleTaskRunner> task_runner = | 58 scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 56 new base::TestSimpleTaskRunner; | 59 new base::TestSimpleTaskRunner; |
| 57 FakeDelayBasedTimeSourceClient client; | 60 FakeDelayBasedTimeSourceClient client; |
| 58 std::unique_ptr<FakeDelayBasedTimeSource> timer( | 61 base::SimpleTestTickClock now_src; |
| 59 new FakeDelayBasedTimeSource(task_runner.get())); | 62 std::unique_ptr<DelayBasedTimeSource> timer( |
| 63 new DelayBasedTimeSource(task_runner.get(), &now_src)); |
| 60 timer->SetClient(&client); | 64 timer->SetClient(&client); |
| 61 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); | 65 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); |
| 62 timer->SetActive(true); | 66 timer->SetActive(true); |
| 63 EXPECT_TRUE(task_runner->HasPendingTask()); | 67 EXPECT_TRUE(task_runner->HasPendingTask()); |
| 64 task_runner->ClearPendingTasks(); | 68 task_runner->ClearPendingTasks(); |
| 65 timer->SetActive(true); | 69 timer->SetActive(true); |
| 66 EXPECT_FALSE(task_runner->HasPendingTask()); | 70 EXPECT_FALSE(task_runner->HasPendingTask()); |
| 67 } | 71 } |
| 68 | 72 |
| 69 TEST(DelayBasedTimeSourceTest, StartWhenRunningDoesntTick) { | 73 TEST(DelayBasedTimeSourceTest, StartWhenRunningDoesntTick) { |
| 70 scoped_refptr<base::TestSimpleTaskRunner> task_runner = | 74 scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 71 new base::TestSimpleTaskRunner; | 75 new base::TestSimpleTaskRunner; |
| 72 FakeDelayBasedTimeSourceClient client; | 76 FakeDelayBasedTimeSourceClient client; |
| 73 std::unique_ptr<FakeDelayBasedTimeSource> timer( | 77 base::SimpleTestTickClock now_src; |
| 74 new FakeDelayBasedTimeSource(task_runner.get())); | 78 std::unique_ptr<DelayBasedTimeSource> timer( |
| 79 new DelayBasedTimeSource(task_runner.get(), &now_src)); |
| 75 timer->SetClient(&client); | 80 timer->SetClient(&client); |
| 76 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); | 81 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); |
| 77 timer->SetActive(true); | 82 timer->SetActive(true); |
| 78 EXPECT_TRUE(task_runner->HasPendingTask()); | 83 EXPECT_TRUE(task_runner->HasPendingTask()); |
| 79 task_runner->RunPendingTasks(); | 84 task_runner->RunPendingTasks(); |
| 80 task_runner->ClearPendingTasks(); | 85 task_runner->ClearPendingTasks(); |
| 81 timer->SetActive(true); | 86 timer->SetActive(true); |
| 82 EXPECT_FALSE(task_runner->HasPendingTask()); | 87 EXPECT_FALSE(task_runner->HasPendingTask()); |
| 83 } | 88 } |
| 84 | 89 |
| 85 // At 60Hz, when the tick returns at exactly the requested next time, make sure | 90 // At 60Hz, when the tick returns at exactly the requested next time, make sure |
| 86 // a 16ms next delay is posted. | 91 // a 16ms next delay is posted. |
| 87 TEST(DelayBasedTimeSourceTest, NextDelaySaneWhenExactlyOnRequestedTime) { | 92 TEST(DelayBasedTimeSourceTest, NextDelaySaneWhenExactlyOnRequestedTime) { |
| 88 scoped_refptr<base::TestSimpleTaskRunner> task_runner = | 93 scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 89 new base::TestSimpleTaskRunner; | 94 new base::TestSimpleTaskRunner; |
| 90 FakeDelayBasedTimeSourceClient client; | 95 FakeDelayBasedTimeSourceClient client; |
| 91 std::unique_ptr<FakeDelayBasedTimeSource> timer( | 96 base::SimpleTestTickClock now_src; |
| 92 new FakeDelayBasedTimeSource(task_runner.get())); | 97 std::unique_ptr<DelayBasedTimeSource> timer( |
| 98 new DelayBasedTimeSource(task_runner.get(), &now_src)); |
| 93 timer->SetClient(&client); | 99 timer->SetClient(&client); |
| 94 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); | 100 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); |
| 95 timer->SetActive(true); | 101 timer->SetActive(true); |
| 96 // Run the first tick. | 102 // Run the first tick. |
| 97 task_runner->RunPendingTasks(); | 103 task_runner->RunPendingTasks(); |
| 98 | 104 |
| 99 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 105 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 100 | 106 |
| 101 timer->SetNow(timer->Now() + Interval()); | 107 now_src.SetNowTicks(now_src.NowTicks() + Interval()); |
| 102 task_runner->RunPendingTasks(); | 108 task_runner->RunPendingTasks(); |
| 103 | 109 |
| 104 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 110 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 105 } | 111 } |
| 106 | 112 |
| 107 // At 60Hz, when the tick returns at slightly after the requested next time, | 113 // At 60Hz, when the tick returns at slightly after the requested next time, |
| 108 // make sure a 16ms next delay is posted. | 114 // make sure a 16ms next delay is posted. |
| 109 TEST(DelayBasedTimeSourceTest, NextDelaySaneWhenSlightlyAfterRequestedTime) { | 115 TEST(DelayBasedTimeSourceTest, NextDelaySaneWhenSlightlyAfterRequestedTime) { |
| 110 scoped_refptr<base::TestSimpleTaskRunner> task_runner = | 116 scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 111 new base::TestSimpleTaskRunner; | 117 new base::TestSimpleTaskRunner; |
| 112 FakeDelayBasedTimeSourceClient client; | 118 FakeDelayBasedTimeSourceClient client; |
| 113 std::unique_ptr<FakeDelayBasedTimeSource> timer( | 119 base::SimpleTestTickClock now_src; |
| 114 new FakeDelayBasedTimeSource(task_runner.get())); | 120 std::unique_ptr<DelayBasedTimeSource> timer( |
| 121 new DelayBasedTimeSource(task_runner.get(), &now_src)); |
| 115 timer->SetClient(&client); | 122 timer->SetClient(&client); |
| 116 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); | 123 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); |
| 117 timer->SetActive(true); | 124 timer->SetActive(true); |
| 118 // Run the first tick. | 125 // Run the first tick. |
| 119 task_runner->RunPendingTasks(); | 126 task_runner->RunPendingTasks(); |
| 120 | 127 |
| 121 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 128 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 122 | 129 |
| 123 timer->SetNow(timer->Now() + Interval() + | 130 now_src.SetNowTicks(now_src.NowTicks() + Interval() + |
| 124 base::TimeDelta::FromMicroseconds(1)); | 131 base::TimeDelta::FromMicroseconds(1)); |
| 125 task_runner->RunPendingTasks(); | 132 task_runner->RunPendingTasks(); |
| 126 | 133 |
| 127 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 134 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 128 } | 135 } |
| 129 | 136 |
| 130 // At 60Hz, when the tick returns at exactly 2*interval after the requested next | 137 // At 60Hz, when the tick returns at exactly 2*interval after the requested next |
| 131 // time, make sure we don't tick unnecessarily. | 138 // time, make sure we don't tick unnecessarily. |
| 132 TEST(DelayBasedTimeSourceTest, | 139 TEST(DelayBasedTimeSourceTest, |
| 133 NextDelaySaneWhenExactlyTwiceAfterRequestedTime) { | 140 NextDelaySaneWhenExactlyTwiceAfterRequestedTime) { |
| 134 scoped_refptr<base::TestSimpleTaskRunner> task_runner = | 141 scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 135 new base::TestSimpleTaskRunner; | 142 new base::TestSimpleTaskRunner; |
| 136 FakeDelayBasedTimeSourceClient client; | 143 FakeDelayBasedTimeSourceClient client; |
| 137 std::unique_ptr<FakeDelayBasedTimeSource> timer( | 144 base::SimpleTestTickClock now_src; |
| 138 new FakeDelayBasedTimeSource(task_runner.get())); | 145 std::unique_ptr<DelayBasedTimeSource> timer( |
| 146 new DelayBasedTimeSource(task_runner.get(), &now_src)); |
| 139 timer->SetClient(&client); | 147 timer->SetClient(&client); |
| 140 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); | 148 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); |
| 141 timer->SetActive(true); | 149 timer->SetActive(true); |
| 142 // Run the first tick. | 150 // Run the first tick. |
| 143 task_runner->RunPendingTasks(); | 151 task_runner->RunPendingTasks(); |
| 144 | 152 |
| 145 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 153 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 146 | 154 |
| 147 timer->SetNow(timer->Now() + 2 * Interval()); | 155 now_src.SetNowTicks(now_src.NowTicks() + 2 * Interval()); |
| 148 task_runner->RunPendingTasks(); | 156 task_runner->RunPendingTasks(); |
| 149 | 157 |
| 150 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 158 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 151 } | 159 } |
| 152 | 160 |
| 153 // At 60Hz, when the tick returns at 2*interval and a bit after the requested | 161 // At 60Hz, when the tick returns at 2*interval and a bit after the requested |
| 154 // next time, make sure a 16ms next delay is posted. | 162 // next time, make sure a 16ms next delay is posted. |
| 155 TEST(DelayBasedTimeSourceTest, | 163 TEST(DelayBasedTimeSourceTest, |
| 156 NextDelaySaneWhenSlightlyAfterTwiceRequestedTime) { | 164 NextDelaySaneWhenSlightlyAfterTwiceRequestedTime) { |
| 157 scoped_refptr<base::TestSimpleTaskRunner> task_runner = | 165 scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 158 new base::TestSimpleTaskRunner; | 166 new base::TestSimpleTaskRunner; |
| 159 FakeDelayBasedTimeSourceClient client; | 167 FakeDelayBasedTimeSourceClient client; |
| 160 std::unique_ptr<FakeDelayBasedTimeSource> timer( | 168 base::SimpleTestTickClock now_src; |
| 161 new FakeDelayBasedTimeSource(task_runner.get())); | 169 std::unique_ptr<DelayBasedTimeSource> timer( |
| 170 new DelayBasedTimeSource(task_runner.get(), &now_src)); |
| 162 timer->SetClient(&client); | 171 timer->SetClient(&client); |
| 163 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); | 172 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); |
| 164 timer->SetActive(true); | 173 timer->SetActive(true); |
| 165 // Run the first tick. | 174 // Run the first tick. |
| 166 task_runner->RunPendingTasks(); | 175 task_runner->RunPendingTasks(); |
| 167 | 176 |
| 168 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 177 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 169 | 178 |
| 170 timer->SetNow(timer->Now() + 2 * Interval() + | 179 now_src.SetNowTicks(now_src.NowTicks() + 2 * Interval() + |
| 171 base::TimeDelta::FromMicroseconds(1)); | 180 base::TimeDelta::FromMicroseconds(1)); |
| 172 task_runner->RunPendingTasks(); | 181 task_runner->RunPendingTasks(); |
| 173 | 182 |
| 174 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 183 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 175 } | 184 } |
| 176 | 185 |
| 177 // At 60Hz, when the tick returns halfway to the next frame time, make sure | 186 // At 60Hz, when the tick returns halfway to the next frame time, make sure |
| 178 // a correct next delay value is posted. | 187 // a correct next delay value is posted. |
| 179 TEST(DelayBasedTimeSourceTest, NextDelaySaneWhenHalfAfterRequestedTime) { | 188 TEST(DelayBasedTimeSourceTest, NextDelaySaneWhenHalfAfterRequestedTime) { |
| 180 scoped_refptr<base::TestSimpleTaskRunner> task_runner = | 189 scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 181 new base::TestSimpleTaskRunner; | 190 new base::TestSimpleTaskRunner; |
| 182 FakeDelayBasedTimeSourceClient client; | 191 FakeDelayBasedTimeSourceClient client; |
| 183 std::unique_ptr<FakeDelayBasedTimeSource> timer( | 192 base::SimpleTestTickClock now_src; |
| 184 new FakeDelayBasedTimeSource(task_runner.get())); | 193 std::unique_ptr<DelayBasedTimeSource> timer( |
| 194 new DelayBasedTimeSource(task_runner.get(), &now_src)); |
| 185 timer->SetClient(&client); | 195 timer->SetClient(&client); |
| 186 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); | 196 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); |
| 187 timer->SetActive(true); | 197 timer->SetActive(true); |
| 188 // Run the first tick. | 198 // Run the first tick. |
| 189 task_runner->RunPendingTasks(); | 199 task_runner->RunPendingTasks(); |
| 190 | 200 |
| 191 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 201 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 192 | 202 |
| 193 timer->SetNow(timer->Now() + Interval() + | 203 now_src.SetNowTicks(now_src.NowTicks() + Interval() + |
| 194 base::TimeDelta::FromMilliseconds(8)); | 204 base::TimeDelta::FromMilliseconds(8)); |
| 195 task_runner->RunPendingTasks(); | 205 task_runner->RunPendingTasks(); |
| 196 | 206 |
| 197 EXPECT_EQ(8, task_runner->NextPendingTaskDelay().InMilliseconds()); | 207 EXPECT_EQ(8, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 198 } | 208 } |
| 199 | 209 |
| 200 TEST(DelayBasedTimeSourceTest, JitteryRuntimeWithFutureTimebases) { | 210 TEST(DelayBasedTimeSourceTest, JitteryRuntimeWithFutureTimebases) { |
| 201 scoped_refptr<base::TestSimpleTaskRunner> task_runner = | 211 scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 202 new base::TestSimpleTaskRunner; | 212 new base::TestSimpleTaskRunner; |
| 203 FakeDelayBasedTimeSourceClient client; | 213 FakeDelayBasedTimeSourceClient client; |
| 204 std::unique_ptr<FakeDelayBasedTimeSource> timer( | 214 base::SimpleTestTickClock now_src; |
| 205 new FakeDelayBasedTimeSource(task_runner.get())); | 215 std::unique_ptr<DelayBasedTimeSource> timer( |
| 216 new DelayBasedTimeSource(task_runner.get(), &now_src)); |
| 206 timer->SetClient(&client); | 217 timer->SetClient(&client); |
| 207 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); | 218 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); |
| 208 timer->SetActive(true); | 219 timer->SetActive(true); |
| 209 | 220 |
| 210 // Run the first tick. | 221 // Run the first tick. |
| 211 task_runner->RunPendingTasks(); | 222 task_runner->RunPendingTasks(); |
| 212 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 223 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 213 | 224 |
| 214 base::TimeTicks future_timebase = timer->Now() + Interval() * 10; | 225 base::TimeTicks future_timebase = now_src.NowTicks() + Interval() * 10; |
| 215 | 226 |
| 216 // 1ms jitter | 227 // 1ms jitter |
| 217 base::TimeDelta jitter1 = base::TimeDelta::FromMilliseconds(1); | 228 base::TimeDelta jitter1 = base::TimeDelta::FromMilliseconds(1); |
| 218 | 229 |
| 219 // Tick with +1ms of jitter | 230 // Tick with +1ms of jitter |
| 220 future_timebase += Interval(); | 231 future_timebase += Interval(); |
| 221 timer->SetTimebaseAndInterval(future_timebase, Interval()); | 232 timer->SetTimebaseAndInterval(future_timebase, Interval()); |
| 222 timer->SetNow(timer->Now() + Interval() + jitter1); | 233 now_src.SetNowTicks(now_src.NowTicks() + Interval() + jitter1); |
| 223 task_runner->RunPendingTasks(); | 234 task_runner->RunPendingTasks(); |
| 224 EXPECT_EQ(15, task_runner->NextPendingTaskDelay().InMilliseconds()); | 235 EXPECT_EQ(15, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 225 | 236 |
| 226 // Tick with 0ms of jitter | 237 // Tick with 0ms of jitter |
| 227 future_timebase += Interval(); | 238 future_timebase += Interval(); |
| 228 timer->SetTimebaseAndInterval(future_timebase, Interval()); | 239 timer->SetTimebaseAndInterval(future_timebase, Interval()); |
| 229 timer->SetNow(timer->Now() + Interval() - jitter1); | 240 now_src.SetNowTicks(now_src.NowTicks() + Interval() - jitter1); |
| 230 task_runner->RunPendingTasks(); | 241 task_runner->RunPendingTasks(); |
| 231 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 242 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 232 | 243 |
| 233 // Tick with -1ms of jitter | 244 // Tick with -1ms of jitter |
| 234 future_timebase += Interval(); | 245 future_timebase += Interval(); |
| 235 timer->SetTimebaseAndInterval(future_timebase, Interval()); | 246 timer->SetTimebaseAndInterval(future_timebase, Interval()); |
| 236 timer->SetNow(timer->Now() + Interval() - jitter1); | 247 now_src.SetNowTicks(now_src.NowTicks() + Interval() - jitter1); |
| 237 task_runner->RunPendingTasks(); | 248 task_runner->RunPendingTasks(); |
| 238 EXPECT_EQ(1, task_runner->NextPendingTaskDelay().InMilliseconds()); | 249 EXPECT_EQ(1, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 239 | 250 |
| 240 // Tick with 0ms of jitter | 251 // Tick with 0ms of jitter |
| 241 future_timebase += Interval(); | 252 future_timebase += Interval(); |
| 242 timer->SetTimebaseAndInterval(future_timebase, Interval()); | 253 timer->SetTimebaseAndInterval(future_timebase, Interval()); |
| 243 timer->SetNow(timer->Now() + Interval() + jitter1); | 254 now_src.SetNowTicks(now_src.NowTicks() + Interval() + jitter1); |
| 244 task_runner->RunPendingTasks(); | 255 task_runner->RunPendingTasks(); |
| 245 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 256 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 246 | 257 |
| 247 // 8 ms jitter | 258 // 8 ms jitter |
| 248 base::TimeDelta jitter8 = base::TimeDelta::FromMilliseconds(8); | 259 base::TimeDelta jitter8 = base::TimeDelta::FromMilliseconds(8); |
| 249 | 260 |
| 250 // Tick with +8ms of jitter | 261 // Tick with +8ms of jitter |
| 251 future_timebase += Interval(); | 262 future_timebase += Interval(); |
| 252 timer->SetTimebaseAndInterval(future_timebase, Interval()); | 263 timer->SetTimebaseAndInterval(future_timebase, Interval()); |
| 253 timer->SetNow(timer->Now() + Interval() + jitter8); | 264 now_src.SetNowTicks(now_src.NowTicks() + Interval() + jitter8); |
| 254 task_runner->RunPendingTasks(); | 265 task_runner->RunPendingTasks(); |
| 255 EXPECT_EQ(8, task_runner->NextPendingTaskDelay().InMilliseconds()); | 266 EXPECT_EQ(8, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 256 | 267 |
| 257 // Tick with 0ms of jitter | 268 // Tick with 0ms of jitter |
| 258 future_timebase += Interval(); | 269 future_timebase += Interval(); |
| 259 timer->SetTimebaseAndInterval(future_timebase, Interval()); | 270 timer->SetTimebaseAndInterval(future_timebase, Interval()); |
| 260 timer->SetNow(timer->Now() + Interval() - jitter8); | 271 now_src.SetNowTicks(now_src.NowTicks() + Interval() - jitter8); |
| 261 task_runner->RunPendingTasks(); | 272 task_runner->RunPendingTasks(); |
| 262 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 273 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 263 | 274 |
| 264 // Tick with -8ms of jitter | 275 // Tick with -8ms of jitter |
| 265 future_timebase += Interval(); | 276 future_timebase += Interval(); |
| 266 timer->SetTimebaseAndInterval(future_timebase, Interval()); | 277 timer->SetTimebaseAndInterval(future_timebase, Interval()); |
| 267 timer->SetNow(timer->Now() + Interval() - jitter8); | 278 now_src.SetNowTicks(now_src.NowTicks() + Interval() - jitter8); |
| 268 task_runner->RunPendingTasks(); | 279 task_runner->RunPendingTasks(); |
| 269 EXPECT_EQ(8, task_runner->NextPendingTaskDelay().InMilliseconds()); | 280 EXPECT_EQ(8, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 270 | 281 |
| 271 // Tick with 0ms of jitter | 282 // Tick with 0ms of jitter |
| 272 future_timebase += Interval(); | 283 future_timebase += Interval(); |
| 273 timer->SetTimebaseAndInterval(future_timebase, Interval()); | 284 timer->SetTimebaseAndInterval(future_timebase, Interval()); |
| 274 timer->SetNow(timer->Now() + Interval() + jitter8); | 285 now_src.SetNowTicks(now_src.NowTicks() + Interval() + jitter8); |
| 275 task_runner->RunPendingTasks(); | 286 task_runner->RunPendingTasks(); |
| 276 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 287 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 277 | 288 |
| 278 // 15 ms jitter | 289 // 15 ms jitter |
| 279 base::TimeDelta jitter15 = base::TimeDelta::FromMilliseconds(15); | 290 base::TimeDelta jitter15 = base::TimeDelta::FromMilliseconds(15); |
| 280 | 291 |
| 281 // Tick with +15ms jitter | 292 // Tick with +15ms jitter |
| 282 future_timebase += Interval(); | 293 future_timebase += Interval(); |
| 283 timer->SetTimebaseAndInterval(future_timebase, Interval()); | 294 timer->SetTimebaseAndInterval(future_timebase, Interval()); |
| 284 timer->SetNow(timer->Now() + Interval() + jitter15); | 295 now_src.SetNowTicks(now_src.NowTicks() + Interval() + jitter15); |
| 285 task_runner->RunPendingTasks(); | 296 task_runner->RunPendingTasks(); |
| 286 EXPECT_EQ(1, task_runner->NextPendingTaskDelay().InMilliseconds()); | 297 EXPECT_EQ(1, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 287 | 298 |
| 288 // Tick with 0ms of jitter | 299 // Tick with 0ms of jitter |
| 289 future_timebase += Interval(); | 300 future_timebase += Interval(); |
| 290 timer->SetTimebaseAndInterval(future_timebase, Interval()); | 301 timer->SetTimebaseAndInterval(future_timebase, Interval()); |
| 291 timer->SetNow(timer->Now() + Interval() - jitter15); | 302 now_src.SetNowTicks(now_src.NowTicks() + Interval() - jitter15); |
| 292 task_runner->RunPendingTasks(); | 303 task_runner->RunPendingTasks(); |
| 293 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 304 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 294 | 305 |
| 295 // Tick with -15ms of jitter | 306 // Tick with -15ms of jitter |
| 296 future_timebase += Interval(); | 307 future_timebase += Interval(); |
| 297 timer->SetTimebaseAndInterval(future_timebase, Interval()); | 308 timer->SetTimebaseAndInterval(future_timebase, Interval()); |
| 298 timer->SetNow(timer->Now() + Interval() - jitter15); | 309 now_src.SetNowTicks(now_src.NowTicks() + Interval() - jitter15); |
| 299 task_runner->RunPendingTasks(); | 310 task_runner->RunPendingTasks(); |
| 300 EXPECT_EQ(15, task_runner->NextPendingTaskDelay().InMilliseconds()); | 311 EXPECT_EQ(15, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 301 | 312 |
| 302 // Tick with 0ms of jitter | 313 // Tick with 0ms of jitter |
| 303 future_timebase += Interval(); | 314 future_timebase += Interval(); |
| 304 timer->SetTimebaseAndInterval(future_timebase, Interval()); | 315 timer->SetTimebaseAndInterval(future_timebase, Interval()); |
| 305 timer->SetNow(timer->Now() + Interval() + jitter15); | 316 now_src.SetNowTicks(now_src.NowTicks() + Interval() + jitter15); |
| 306 task_runner->RunPendingTasks(); | 317 task_runner->RunPendingTasks(); |
| 307 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); | 318 EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 308 } | 319 } |
| 309 | 320 |
| 310 TEST(DelayBasedTimeSourceTest, AchievesTargetRateWithNoNoise) { | 321 TEST(DelayBasedTimeSourceTest, AchievesTargetRateWithNoNoise) { |
| 311 int num_iterations = 10; | 322 int num_iterations = 10; |
| 312 | 323 |
| 313 scoped_refptr<base::TestSimpleTaskRunner> task_runner = | 324 scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 314 new base::TestSimpleTaskRunner; | 325 new base::TestSimpleTaskRunner; |
| 315 FakeDelayBasedTimeSourceClient client; | 326 FakeDelayBasedTimeSourceClient client; |
| 316 std::unique_ptr<FakeDelayBasedTimeSource> timer( | 327 base::SimpleTestTickClock now_src; |
| 317 new FakeDelayBasedTimeSource(task_runner.get())); | 328 std::unique_ptr<DelayBasedTimeSource> timer( |
| 329 new DelayBasedTimeSource(task_runner.get(), &now_src)); |
| 318 timer->SetClient(&client); | 330 timer->SetClient(&client); |
| 319 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); | 331 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); |
| 320 timer->SetActive(true); | 332 timer->SetActive(true); |
| 321 | 333 |
| 322 double total_frame_time = 0.0; | 334 double total_frame_time = 0.0; |
| 323 for (int i = 0; i < num_iterations; ++i) { | 335 for (int i = 0; i < num_iterations; ++i) { |
| 324 int64_t delay_ms = task_runner->NextPendingTaskDelay().InMilliseconds(); | 336 int64_t delay_ms = task_runner->NextPendingTaskDelay().InMilliseconds(); |
| 325 | 337 |
| 326 // accumulate the "delay" | 338 // accumulate the "delay" |
| 327 total_frame_time += delay_ms / 1000.0; | 339 total_frame_time += delay_ms / 1000.0; |
| 328 | 340 |
| 329 // Run the callback exactly when asked | 341 // Run the callback exactly when asked |
| 330 timer->SetNow(timer->Now() + base::TimeDelta::FromMilliseconds(delay_ms)); | 342 now_src.SetNowTicks(now_src.NowTicks() + |
| 343 base::TimeDelta::FromMilliseconds(delay_ms)); |
| 331 task_runner->RunPendingTasks(); | 344 task_runner->RunPendingTasks(); |
| 332 } | 345 } |
| 333 double average_interval = | 346 double average_interval = |
| 334 total_frame_time / static_cast<double>(num_iterations); | 347 total_frame_time / static_cast<double>(num_iterations); |
| 335 EXPECT_NEAR(1.0 / 60.0, average_interval, 0.1); | 348 EXPECT_NEAR(1.0 / 60.0, average_interval, 0.1); |
| 336 } | 349 } |
| 337 | 350 |
| 338 TEST(DelayBasedTimeSourceTest, TestDeactivateWhilePending) { | 351 TEST(DelayBasedTimeSourceTest, TestDeactivateWhilePending) { |
| 339 scoped_refptr<base::TestSimpleTaskRunner> task_runner = | 352 scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 340 new base::TestSimpleTaskRunner; | 353 new base::TestSimpleTaskRunner; |
| 341 FakeDelayBasedTimeSourceClient client; | 354 FakeDelayBasedTimeSourceClient client; |
| 342 std::unique_ptr<FakeDelayBasedTimeSource> timer( | 355 base::SimpleTestTickClock now_src; |
| 343 new FakeDelayBasedTimeSource(task_runner.get())); | 356 std::unique_ptr<DelayBasedTimeSource> timer( |
| 357 new DelayBasedTimeSource(task_runner.get(), &now_src)); |
| 344 timer->SetClient(&client); | 358 timer->SetClient(&client); |
| 345 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); | 359 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); |
| 346 timer->SetActive(true); // Should post a task. | 360 timer->SetActive(true); // Should post a task. |
| 347 timer->SetActive(false); | 361 timer->SetActive(false); |
| 348 timer = NULL; | 362 timer = NULL; |
| 349 // Should run the posted task without crashing. | 363 // Should run the posted task without crashing. |
| 350 EXPECT_TRUE(task_runner->HasPendingTask()); | 364 EXPECT_TRUE(task_runner->HasPendingTask()); |
| 351 task_runner->RunPendingTasks(); | 365 task_runner->RunPendingTasks(); |
| 352 } | 366 } |
| 353 | 367 |
| 354 TEST(DelayBasedTimeSourceTest, TestDeactivateAndReactivateBeforeNextTickTime) { | 368 TEST(DelayBasedTimeSourceTest, TestDeactivateAndReactivateBeforeNextTickTime) { |
| 355 scoped_refptr<base::TestSimpleTaskRunner> task_runner = | 369 scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 356 new base::TestSimpleTaskRunner; | 370 new base::TestSimpleTaskRunner; |
| 357 FakeDelayBasedTimeSourceClient client; | 371 FakeDelayBasedTimeSourceClient client; |
| 358 std::unique_ptr<FakeDelayBasedTimeSource> timer( | 372 base::SimpleTestTickClock now_src; |
| 359 new FakeDelayBasedTimeSource(task_runner.get())); | 373 std::unique_ptr<DelayBasedTimeSource> timer( |
| 374 new DelayBasedTimeSource(task_runner.get(), &now_src)); |
| 360 timer->SetClient(&client); | 375 timer->SetClient(&client); |
| 361 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); | 376 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); |
| 362 | 377 |
| 363 // Should run the activate task, and pick up a new timebase. | 378 // Should run the activate task, and pick up a new timebase. |
| 364 timer->SetActive(true); | 379 timer->SetActive(true); |
| 365 task_runner->RunPendingTasks(); | 380 task_runner->RunPendingTasks(); |
| 366 | 381 |
| 367 // Stop the timer | 382 // Stop the timer |
| 368 timer->SetActive(false); | 383 timer->SetActive(false); |
| 369 | 384 |
| 370 // Task will be pending anyway, run it | 385 // Task will be pending anyway, run it |
| 371 task_runner->RunPendingTasks(); | 386 task_runner->RunPendingTasks(); |
| 372 | 387 |
| 373 // Start the timer again, but before the next tick time the timer previously | 388 // Start the timer again, but before the next tick time the timer previously |
| 374 // planned on using. That same tick time should still be targeted. | 389 // planned on using. That same tick time should still be targeted. |
| 375 timer->SetNow(timer->Now() + base::TimeDelta::FromMilliseconds(4)); | 390 now_src.SetNowTicks(now_src.NowTicks() + |
| 391 base::TimeDelta::FromMilliseconds(4)); |
| 376 timer->SetActive(true); | 392 timer->SetActive(true); |
| 377 EXPECT_EQ(12, task_runner->NextPendingTaskDelay().InMilliseconds()); | 393 EXPECT_EQ(12, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 378 } | 394 } |
| 379 | 395 |
| 380 TEST(DelayBasedTimeSourceTest, TestDeactivateAndReactivateAfterNextTickTime) { | 396 TEST(DelayBasedTimeSourceTest, TestDeactivateAndReactivateAfterNextTickTime) { |
| 381 scoped_refptr<base::TestSimpleTaskRunner> task_runner = | 397 scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 382 new base::TestSimpleTaskRunner; | 398 new base::TestSimpleTaskRunner; |
| 383 FakeDelayBasedTimeSourceClient client; | 399 FakeDelayBasedTimeSourceClient client; |
| 384 std::unique_ptr<FakeDelayBasedTimeSource> timer( | 400 base::SimpleTestTickClock now_src; |
| 385 new FakeDelayBasedTimeSource(task_runner.get())); | 401 std::unique_ptr<DelayBasedTimeSource> timer( |
| 402 new DelayBasedTimeSource(task_runner.get(), &now_src)); |
| 386 timer->SetClient(&client); | 403 timer->SetClient(&client); |
| 387 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); | 404 timer->SetTimebaseAndInterval(base::TimeTicks(), Interval()); |
| 388 | 405 |
| 389 // Should run the activate task, and pick up a new timebase. | 406 // Should run the activate task, and pick up a new timebase. |
| 390 timer->SetActive(true); | 407 timer->SetActive(true); |
| 391 task_runner->RunPendingTasks(); | 408 task_runner->RunPendingTasks(); |
| 392 | 409 |
| 393 // Stop the timer. | 410 // Stop the timer. |
| 394 timer->SetActive(false); | 411 timer->SetActive(false); |
| 395 | 412 |
| 396 // Task will be pending anyway, run it. | 413 // Task will be pending anyway, run it. |
| 397 task_runner->RunPendingTasks(); | 414 task_runner->RunPendingTasks(); |
| 398 | 415 |
| 399 // Start the timer again, but before the next tick time the timer previously | 416 // Start the timer again, but before the next tick time the timer previously |
| 400 // planned on using. That same tick time should still be targeted. | 417 // planned on using. That same tick time should still be targeted. |
| 401 timer->SetNow(timer->Now() + base::TimeDelta::FromMilliseconds(20)); | 418 now_src.SetNowTicks(now_src.NowTicks() + |
| 419 base::TimeDelta::FromMilliseconds(20)); |
| 402 timer->SetActive(true); | 420 timer->SetActive(true); |
| 403 EXPECT_EQ(13, task_runner->NextPendingTaskDelay().InMilliseconds()); | 421 EXPECT_EQ(13, task_runner->NextPendingTaskDelay().InMilliseconds()); |
| 404 } | 422 } |
| 405 | 423 |
| 406 } // namespace | 424 } // namespace |
| 407 } // namespace cc | 425 } // namespace cc |
| OLD | NEW |