Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(117)

Side by Side Diff: components/scheduler/child/idle_helper_unittest.cc

Issue 1132753008: Replaced TestNowSource with SimpleTestTickClock. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased. Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "components/scheduler/child/idle_helper.h" 5 #include "components/scheduler/child/idle_helper.h"
6 6
7 #include "base/callback.h" 7 #include "base/callback.h"
8 #include "base/test/simple_test_tick_clock.h"
8 #include "cc/test/ordered_simple_task_runner.h" 9 #include "cc/test/ordered_simple_task_runner.h"
9 #include "cc/test/test_now_source.h"
10 #include "components/scheduler/child/nestable_task_runner_for_test.h" 10 #include "components/scheduler/child/nestable_task_runner_for_test.h"
11 #include "components/scheduler/child/scheduler_helper.h" 11 #include "components/scheduler/child/scheduler_helper.h"
12 #include "components/scheduler/child/scheduler_message_loop_delegate.h" 12 #include "components/scheduler/child/scheduler_message_loop_delegate.h"
13 #include "components/scheduler/child/task_queue_manager.h" 13 #include "components/scheduler/child/task_queue_manager.h"
14 #include "components/scheduler/child/test_time_source.h" 14 #include "components/scheduler/child/test_time_source.h"
15 #include "testing/gmock/include/gmock/gmock.h" 15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 17
18 using testing::_; 18 using testing::_;
19 using testing::AnyNumber; 19 using testing::AnyNumber;
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 base::Bind(&RepostingIdleTestTask, base::Unretained(idle_task_runner), 74 base::Bind(&RepostingIdleTestTask, base::Unretained(idle_task_runner),
75 run_count, deadline_out)); 75 run_count, deadline_out));
76 } 76 }
77 *deadline_out = deadline; 77 *deadline_out = deadline;
78 (*run_count)++; 78 (*run_count)++;
79 } 79 }
80 80
81 void RepostingUpdateClockIdleTestTask( 81 void RepostingUpdateClockIdleTestTask(
82 SingleThreadIdleTaskRunner* idle_task_runner, 82 SingleThreadIdleTaskRunner* idle_task_runner,
83 int* run_count, 83 int* run_count,
84 scoped_refptr<cc::TestNowSource> clock, 84 base::SimpleTestTickClock* clock,
85 base::TimeDelta advance_time, 85 base::TimeDelta advance_time,
86 std::vector<base::TimeTicks>* deadlines, 86 std::vector<base::TimeTicks>* deadlines,
87 base::TimeTicks deadline) { 87 base::TimeTicks deadline) {
88 if ((*run_count + 1) < max_idle_task_reposts) { 88 if ((*run_count + 1) < max_idle_task_reposts) {
89 idle_task_runner->PostIdleTask( 89 idle_task_runner->PostIdleTask(
90 FROM_HERE, base::Bind(&RepostingUpdateClockIdleTestTask, 90 FROM_HERE, base::Bind(&RepostingUpdateClockIdleTestTask,
91 base::Unretained(idle_task_runner), run_count, 91 base::Unretained(idle_task_runner), run_count,
92 clock, advance_time, deadlines)); 92 clock, advance_time, deadlines));
93 } 93 }
94 deadlines->push_back(deadline); 94 deadlines->push_back(deadline);
95 (*run_count)++; 95 (*run_count)++;
96 clock->AdvanceNow(advance_time); 96 clock->Advance(advance_time);
97 } 97 }
98 98
99 void RepeatingTask(base::SingleThreadTaskRunner* task_runner, 99 void RepeatingTask(base::SingleThreadTaskRunner* task_runner,
100 int num_repeats, 100 int num_repeats,
101 base::TimeDelta delay) { 101 base::TimeDelta delay) {
102 if (num_repeats > 1) { 102 if (num_repeats > 1) {
103 task_runner->PostDelayedTask( 103 task_runner->PostDelayedTask(
104 FROM_HERE, base::Bind(&RepeatingTask, base::Unretained(task_runner), 104 FROM_HERE, base::Bind(&RepeatingTask, base::Unretained(task_runner),
105 num_repeats - 1, delay), 105 num_repeats - 1, delay),
106 delay); 106 delay);
107 } 107 }
108 } 108 }
109 109
110 void UpdateClockIdleTestTask(scoped_refptr<cc::TestNowSource> clock, 110 void UpdateClockIdleTestTask(base::SimpleTestTickClock* clock,
111 int* run_count, 111 int* run_count,
112 base::TimeTicks set_time, 112 base::TimeTicks set_time,
113 base::TimeTicks deadline) { 113 base::TimeTicks deadline) {
114 clock->SetNow(set_time); 114 clock->Advance(set_time - clock->NowTicks());
115 (*run_count)++; 115 (*run_count)++;
116 } 116 }
117 117
118 void UpdateClockToDeadlineIdleTestTask(scoped_refptr<cc::TestNowSource> clock, 118 void UpdateClockToDeadlineIdleTestTask(base::SimpleTestTickClock* clock,
119 int* run_count, 119 int* run_count,
120 base::TimeTicks deadline) { 120 base::TimeTicks deadline) {
121 UpdateClockIdleTestTask(clock, run_count, deadline, deadline); 121 UpdateClockIdleTestTask(clock, run_count, deadline, deadline);
122 } 122 }
123 123
124 void EndIdlePeriodIdleTask(IdleHelper* idle_helper, base::TimeTicks deadline) { 124 void EndIdlePeriodIdleTask(IdleHelper* idle_helper, base::TimeTicks deadline) {
125 idle_helper->EndIdlePeriod(); 125 idle_helper->EndIdlePeriod();
126 } 126 }
127 127
128 scoped_refptr<NestableSingleThreadTaskRunner> 128 scoped_refptr<NestableSingleThreadTaskRunner>
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 MOCK_METHOD0(IsNotQuiescent, void()); 160 MOCK_METHOD0(IsNotQuiescent, void());
161 MOCK_METHOD0(OnIdlePeriodStarted, void()); 161 MOCK_METHOD0(OnIdlePeriodStarted, void());
162 MOCK_METHOD0(OnIdlePeriodEnded, void()); 162 MOCK_METHOD0(OnIdlePeriodEnded, void());
163 }; 163 };
164 164
165 class BaseIdleHelperTest : public testing::Test { 165 class BaseIdleHelperTest : public testing::Test {
166 public: 166 public:
167 BaseIdleHelperTest( 167 BaseIdleHelperTest(
168 base::MessageLoop* message_loop, 168 base::MessageLoop* message_loop,
169 base::TimeDelta required_quiescence_duration_before_long_idle_period) 169 base::TimeDelta required_quiescence_duration_before_long_idle_period)
170 : clock_(cc::TestNowSource::Create(5000)), 170 : clock_(new base::SimpleTestTickClock()),
171 mock_task_runner_(message_loop 171 mock_task_runner_(
172 ? nullptr 172 message_loop
173 : new cc::OrderedSimpleTaskRunner(clock_, false)), 173 ? nullptr
174 : new cc::OrderedSimpleTaskRunner(clock_.get(), false)),
174 message_loop_(message_loop), 175 message_loop_(message_loop),
175 nestable_task_runner_( 176 nestable_task_runner_(
176 CreateNestableSingleThreadTaskRunner(message_loop, 177 CreateNestableSingleThreadTaskRunner(message_loop,
177 mock_task_runner_)), 178 mock_task_runner_)),
178 scheduler_helper_( 179 scheduler_helper_(
179 new SchedulerHelper(nestable_task_runner_, 180 new SchedulerHelper(nestable_task_runner_,
180 "test.idle", 181 "test.idle",
181 TRACE_DISABLED_BY_DEFAULT("test.idle"), 182 TRACE_DISABLED_BY_DEFAULT("test.idle"),
182 SchedulerHelper::TASK_QUEUE_COUNT + 1)), 183 SchedulerHelper::TASK_QUEUE_COUNT + 1)),
183 idle_helper_(new IdleHelperForTest( 184 idle_helper_(new IdleHelperForTest(
184 scheduler_helper_.get(), 185 scheduler_helper_.get(),
185 required_quiescence_duration_before_long_idle_period)), 186 required_quiescence_duration_before_long_idle_period)),
186 default_task_runner_(scheduler_helper_->DefaultTaskRunner()), 187 default_task_runner_(scheduler_helper_->DefaultTaskRunner()),
187 idle_task_runner_(idle_helper_->IdleTaskRunner()) { 188 idle_task_runner_(idle_helper_->IdleTaskRunner()) {
189 clock_->Advance(base::TimeDelta::FromMicroseconds(5000));
188 scheduler_helper_->SetTimeSourceForTesting( 190 scheduler_helper_->SetTimeSourceForTesting(
189 make_scoped_ptr(new TestTimeSource(clock_))); 191 make_scoped_ptr(new TestTimeSource(clock_.get())));
190 scheduler_helper_->GetTaskQueueManagerForTesting()->SetTimeSourceForTesting( 192 scheduler_helper_->GetTaskQueueManagerForTesting()->SetTimeSourceForTesting(
191 make_scoped_ptr(new TestTimeSource(clock_))); 193 make_scoped_ptr(new TestTimeSource(clock_.get())));
192 } 194 }
193 195
194 ~BaseIdleHelperTest() override {} 196 ~BaseIdleHelperTest() override {}
195 197
196 void SetUp() override { 198 void SetUp() override {
197 EXPECT_CALL(*idle_helper_, OnIdlePeriodStarted()).Times(AnyNumber()); 199 EXPECT_CALL(*idle_helper_, OnIdlePeriodStarted()).Times(AnyNumber());
198 EXPECT_CALL(*idle_helper_, OnIdlePeriodEnded()).Times(AnyNumber()); 200 EXPECT_CALL(*idle_helper_, OnIdlePeriodEnded()).Times(AnyNumber());
199 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) 201 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _))
200 .Times(AnyNumber()) 202 .Times(AnyNumber())
201 .WillRepeatedly(Return(true)); 203 .WillRepeatedly(Return(true));
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 264
263 base::TimeTicks CurrentIdleTaskDeadline() { 265 base::TimeTicks CurrentIdleTaskDeadline() {
264 return idle_helper_->CurrentIdleTaskDeadline(); 266 return idle_helper_->CurrentIdleTaskDeadline();
265 } 267 }
266 268
267 void CheckIdlePeriodStateIs(const char* expected) { 269 void CheckIdlePeriodStateIs(const char* expected) {
268 EXPECT_STREQ(expected, IdleHelper::IdlePeriodStateToString( 270 EXPECT_STREQ(expected, IdleHelper::IdlePeriodStateToString(
269 idle_helper_->SchedulerIdlePeriodState())); 271 idle_helper_->SchedulerIdlePeriodState()));
270 } 272 }
271 273
272 scoped_refptr<cc::TestNowSource> clock_; 274 scoped_ptr<base::SimpleTestTickClock> clock_;
273 // Only one of mock_task_runner_ or message_loop_ will be set. 275 // Only one of mock_task_runner_ or message_loop_ will be set.
274 scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_; 276 scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_;
275 scoped_ptr<base::MessageLoop> message_loop_; 277 scoped_ptr<base::MessageLoop> message_loop_;
276 278
277 scoped_refptr<NestableSingleThreadTaskRunner> nestable_task_runner_; 279 scoped_refptr<NestableSingleThreadTaskRunner> nestable_task_runner_;
278 scoped_ptr<SchedulerHelper> scheduler_helper_; 280 scoped_ptr<SchedulerHelper> scheduler_helper_;
279 scoped_ptr<IdleHelperForTest> idle_helper_; 281 scoped_ptr<IdleHelperForTest> idle_helper_;
280 scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_; 282 scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_;
281 scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner_; 283 scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner_;
282 284
(...skipping 10 matching lines...) Expand all
293 return scheduler_helper_->GetTaskQueueManagerForTesting(); 295 return scheduler_helper_->GetTaskQueueManagerForTesting();
294 } 296 }
295 297
296 private: 298 private:
297 DISALLOW_COPY_AND_ASSIGN(IdleHelperTest); 299 DISALLOW_COPY_AND_ASSIGN(IdleHelperTest);
298 }; 300 };
299 301
300 TEST_F(IdleHelperTest, TestPostIdleTask) { 302 TEST_F(IdleHelperTest, TestPostIdleTask) {
301 int run_count = 0; 303 int run_count = 0;
302 base::TimeTicks expected_deadline = 304 base::TimeTicks expected_deadline =
303 clock_->Now() + base::TimeDelta::FromMilliseconds(2300); 305 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(2300);
304 base::TimeTicks deadline_in_task; 306 base::TimeTicks deadline_in_task;
305 307
306 clock_->AdvanceNow(base::TimeDelta::FromMilliseconds(100)); 308 clock_->Advance(base::TimeDelta::FromMilliseconds(100));
307 idle_task_runner_->PostIdleTask( 309 idle_task_runner_->PostIdleTask(
308 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); 310 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
309 311
310 RunUntilIdle(); 312 RunUntilIdle();
311 EXPECT_EQ(0, run_count); 313 EXPECT_EQ(0, run_count);
312 314
313 idle_helper_->StartIdlePeriod( 315 idle_helper_->StartIdlePeriod(
314 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), 316 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(),
315 expected_deadline); 317 expected_deadline);
316 RunUntilIdle(); 318 RunUntilIdle();
317 EXPECT_EQ(1, run_count); 319 EXPECT_EQ(1, run_count);
318 EXPECT_EQ(expected_deadline, deadline_in_task); 320 EXPECT_EQ(expected_deadline, deadline_in_task);
319 } 321 }
320 322
321 TEST_F(IdleHelperTest, TestPostIdleTask_EndIdlePeriod) { 323 TEST_F(IdleHelperTest, TestPostIdleTask_EndIdlePeriod) {
322 int run_count = 0; 324 int run_count = 0;
323 base::TimeTicks deadline_in_task; 325 base::TimeTicks deadline_in_task;
324 326
325 clock_->AdvanceNow(base::TimeDelta::FromMilliseconds(100)); 327 clock_->Advance(base::TimeDelta::FromMilliseconds(100));
326 idle_task_runner_->PostIdleTask( 328 idle_task_runner_->PostIdleTask(
327 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); 329 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
328 330
329 RunUntilIdle(); 331 RunUntilIdle();
330 EXPECT_EQ(0, run_count); 332 EXPECT_EQ(0, run_count);
331 333
332 idle_helper_->StartIdlePeriod( 334 idle_helper_->StartIdlePeriod(
333 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), 335 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(),
334 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); 336 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10));
335 idle_helper_->EndIdlePeriod(); 337 idle_helper_->EndIdlePeriod();
336 RunUntilIdle(); 338 RunUntilIdle();
337 EXPECT_EQ(0, run_count); 339 EXPECT_EQ(0, run_count);
338 } 340 }
339 341
340 TEST_F(IdleHelperTest, TestRepostingIdleTask) { 342 TEST_F(IdleHelperTest, TestRepostingIdleTask) {
341 base::TimeTicks actual_deadline; 343 base::TimeTicks actual_deadline;
342 int run_count = 0; 344 int run_count = 0;
343 345
344 max_idle_task_reposts = 2; 346 max_idle_task_reposts = 2;
345 idle_task_runner_->PostIdleTask( 347 idle_task_runner_->PostIdleTask(
346 FROM_HERE, base::Bind(&RepostingIdleTestTask, idle_task_runner_, 348 FROM_HERE, base::Bind(&RepostingIdleTestTask, idle_task_runner_,
347 &run_count, &actual_deadline)); 349 &run_count, &actual_deadline));
348 idle_helper_->StartIdlePeriod( 350 idle_helper_->StartIdlePeriod(
349 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), 351 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(),
350 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); 352 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10));
351 RunUntilIdle(); 353 RunUntilIdle();
352 EXPECT_EQ(1, run_count); 354 EXPECT_EQ(1, run_count);
353 355
354 // Reposted tasks shouldn't run until next idle period. 356 // Reposted tasks shouldn't run until next idle period.
355 RunUntilIdle(); 357 RunUntilIdle();
356 EXPECT_EQ(1, run_count); 358 EXPECT_EQ(1, run_count);
357 359
358 idle_helper_->StartIdlePeriod( 360 idle_helper_->StartIdlePeriod(
359 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), 361 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(),
360 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); 362 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10));
361 RunUntilIdle(); 363 RunUntilIdle();
362 EXPECT_EQ(2, run_count); 364 EXPECT_EQ(2, run_count);
363 } 365 }
364 366
365 TEST_F(IdleHelperTest, TestIdleTaskExceedsDeadline) { 367 TEST_F(IdleHelperTest, TestIdleTaskExceedsDeadline) {
366 int run_count = 0; 368 int run_count = 0;
367 369
368 // Post two UpdateClockToDeadlineIdleTestTask tasks. 370 // Post two UpdateClockToDeadlineIdleTestTask tasks.
369 idle_task_runner_->PostIdleTask( 371 idle_task_runner_->PostIdleTask(
370 FROM_HERE, 372 FROM_HERE,
371 base::Bind(&UpdateClockToDeadlineIdleTestTask, clock_, &run_count)); 373 base::Bind(&UpdateClockToDeadlineIdleTestTask, clock_.get(), &run_count));
372 idle_task_runner_->PostIdleTask( 374 idle_task_runner_->PostIdleTask(
373 FROM_HERE, 375 FROM_HERE,
374 base::Bind(&UpdateClockToDeadlineIdleTestTask, clock_, &run_count)); 376 base::Bind(&UpdateClockToDeadlineIdleTestTask, clock_.get(), &run_count));
375 377
376 idle_helper_->StartIdlePeriod( 378 idle_helper_->StartIdlePeriod(
377 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), 379 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(),
378 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); 380 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10));
379 RunUntilIdle(); 381 RunUntilIdle();
380 // Only the first idle task should execute since it's used up the deadline. 382 // Only the first idle task should execute since it's used up the deadline.
381 EXPECT_EQ(1, run_count); 383 EXPECT_EQ(1, run_count);
382 384
383 idle_helper_->EndIdlePeriod(); 385 idle_helper_->EndIdlePeriod();
384 idle_helper_->StartIdlePeriod( 386 idle_helper_->StartIdlePeriod(
385 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), 387 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(),
386 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); 388 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10));
387 RunUntilIdle(); 389 RunUntilIdle();
388 // Second task should be run on the next idle period. 390 // Second task should be run on the next idle period.
389 EXPECT_EQ(2, run_count); 391 EXPECT_EQ(2, run_count);
390 } 392 }
391 393
392 TEST_F(IdleHelperTest, TestPostIdleTaskAfterWakeup) { 394 TEST_F(IdleHelperTest, TestPostIdleTaskAfterWakeup) {
393 base::TimeTicks deadline_in_task; 395 base::TimeTicks deadline_in_task;
394 int run_count = 0; 396 int run_count = 0;
395 397
396 idle_task_runner_->PostIdleTaskAfterWakeup( 398 idle_task_runner_->PostIdleTaskAfterWakeup(
397 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); 399 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
398 400
399 idle_helper_->StartIdlePeriod( 401 idle_helper_->StartIdlePeriod(
400 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), 402 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(),
401 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); 403 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10));
402 RunUntilIdle(); 404 RunUntilIdle();
403 // Shouldn't run yet as no other task woke up the scheduler. 405 // Shouldn't run yet as no other task woke up the scheduler.
404 EXPECT_EQ(0, run_count); 406 EXPECT_EQ(0, run_count);
405 407
406 // Must start a new idle period before idle task runs. 408 // Must start a new idle period before idle task runs.
407 idle_task_runner_->PostIdleTaskAfterWakeup( 409 idle_task_runner_->PostIdleTaskAfterWakeup(
408 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); 410 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
409 411
410 idle_helper_->StartIdlePeriod( 412 idle_helper_->StartIdlePeriod(
411 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), 413 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(),
412 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); 414 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10));
413 RunUntilIdle(); 415 RunUntilIdle();
414 // Another after wakeup idle task shouldn't wake the scheduler. 416 // Another after wakeup idle task shouldn't wake the scheduler.
415 EXPECT_EQ(0, run_count); 417 EXPECT_EQ(0, run_count);
416 418
417 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); 419 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask));
418 420
419 RunUntilIdle(); 421 RunUntilIdle();
420 idle_helper_->StartIdlePeriod( 422 idle_helper_->StartIdlePeriod(
421 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), 423 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(),
422 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); 424 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10));
423 RunUntilIdle(); 425 RunUntilIdle();
424 // Execution of default task queue task should trigger execution of idle task. 426 // Execution of default task queue task should trigger execution of idle task.
425 EXPECT_EQ(2, run_count); 427 EXPECT_EQ(2, run_count);
426 } 428 }
427 429
428 TEST_F(IdleHelperTest, TestPostIdleTaskAfterWakeupWhileAwake) { 430 TEST_F(IdleHelperTest, TestPostIdleTaskAfterWakeupWhileAwake) {
429 base::TimeTicks deadline_in_task; 431 base::TimeTicks deadline_in_task;
430 int run_count = 0; 432 int run_count = 0;
431 433
432 idle_task_runner_->PostIdleTaskAfterWakeup( 434 idle_task_runner_->PostIdleTaskAfterWakeup(
433 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); 435 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
434 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); 436 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask));
435 437
436 RunUntilIdle(); 438 RunUntilIdle();
437 // Must start a new idle period before idle task runs. 439 // Must start a new idle period before idle task runs.
438 idle_helper_->StartIdlePeriod( 440 idle_helper_->StartIdlePeriod(
439 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), 441 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(),
440 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); 442 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10));
441 RunUntilIdle(); 443 RunUntilIdle();
442 // Should run as the scheduler was already awakened by the normal task. 444 // Should run as the scheduler was already awakened by the normal task.
443 EXPECT_EQ(1, run_count); 445 EXPECT_EQ(1, run_count);
444 } 446 }
445 447
446 TEST_F(IdleHelperTest, TestPostIdleTaskWakesAfterWakeupIdleTask) { 448 TEST_F(IdleHelperTest, TestPostIdleTaskWakesAfterWakeupIdleTask) {
447 base::TimeTicks deadline_in_task; 449 base::TimeTicks deadline_in_task;
448 int run_count = 0; 450 int run_count = 0;
449 451
450 idle_task_runner_->PostIdleTaskAfterWakeup( 452 idle_task_runner_->PostIdleTaskAfterWakeup(
451 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); 453 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
452 idle_task_runner_->PostIdleTask( 454 idle_task_runner_->PostIdleTask(
453 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); 455 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
454 456
455 idle_helper_->StartIdlePeriod( 457 idle_helper_->StartIdlePeriod(
456 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), 458 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(),
457 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); 459 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10));
458 RunUntilIdle(); 460 RunUntilIdle();
459 // Must start a new idle period before after-wakeup idle task runs. 461 // Must start a new idle period before after-wakeup idle task runs.
460 idle_helper_->StartIdlePeriod( 462 idle_helper_->StartIdlePeriod(
461 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), 463 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(),
462 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); 464 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10));
463 RunUntilIdle(); 465 RunUntilIdle();
464 // Normal idle task should wake up after-wakeup idle task. 466 // Normal idle task should wake up after-wakeup idle task.
465 EXPECT_EQ(2, run_count); 467 EXPECT_EQ(2, run_count);
466 } 468 }
467 469
468 class IdleHelperTestWithIdlePeriodObserver : public BaseIdleHelperTest { 470 class IdleHelperTestWithIdlePeriodObserver : public BaseIdleHelperTest {
469 public: 471 public:
470 IdleHelperTestWithIdlePeriodObserver() 472 IdleHelperTestWithIdlePeriodObserver()
471 : BaseIdleHelperTest(nullptr, base::TimeDelta()) {} 473 : BaseIdleHelperTest(nullptr, base::TimeDelta()) {}
472 474
(...skipping 18 matching lines...) Expand all
491 } 493 }
492 494
493 private: 495 private:
494 DISALLOW_COPY_AND_ASSIGN(IdleHelperTestWithIdlePeriodObserver); 496 DISALLOW_COPY_AND_ASSIGN(IdleHelperTestWithIdlePeriodObserver);
495 }; 497 };
496 498
497 TEST_F(IdleHelperTestWithIdlePeriodObserver, TestEnterButNotExitIdlePeriod) { 499 TEST_F(IdleHelperTestWithIdlePeriodObserver, TestEnterButNotExitIdlePeriod) {
498 ExpectIdlePeriodStartsButNeverEnds(); 500 ExpectIdlePeriodStartsButNeverEnds();
499 501
500 idle_helper_->StartIdlePeriod( 502 idle_helper_->StartIdlePeriod(
501 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), 503 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(),
502 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); 504 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10));
503 } 505 }
504 506
505 TEST_F(IdleHelperTestWithIdlePeriodObserver, TestEnterAndExitIdlePeriod) { 507 TEST_F(IdleHelperTestWithIdlePeriodObserver, TestEnterAndExitIdlePeriod) {
506 BaseIdleHelperTest* fixture = this; 508 BaseIdleHelperTest* fixture = this;
507 ON_CALL(*idle_helper_, OnIdlePeriodStarted()) 509 ON_CALL(*idle_helper_, OnIdlePeriodStarted())
508 .WillByDefault( 510 .WillByDefault(
509 Invoke([fixture]() { EXPECT_TRUE(fixture->IsInIdlePeriod()); })); 511 Invoke([fixture]() { EXPECT_TRUE(fixture->IsInIdlePeriod()); }));
510 ON_CALL(*idle_helper_, OnIdlePeriodEnded()) 512 ON_CALL(*idle_helper_, OnIdlePeriodEnded())
511 .WillByDefault( 513 .WillByDefault(
512 Invoke([fixture]() { EXPECT_FALSE(fixture->IsInIdlePeriod()); })); 514 Invoke([fixture]() { EXPECT_FALSE(fixture->IsInIdlePeriod()); }));
513 515
514 ExpectIdlePeriodStartsAndEnds(Exactly(1)); 516 ExpectIdlePeriodStartsAndEnds(Exactly(1));
515 517
516 idle_helper_->StartIdlePeriod( 518 idle_helper_->StartIdlePeriod(
517 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), 519 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(),
518 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); 520 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10));
519 idle_helper_->EndIdlePeriod(); 521 idle_helper_->EndIdlePeriod();
520 } 522 }
521 523
522 class IdleHelperWithMessageLoopTest : public BaseIdleHelperTest { 524 class IdleHelperWithMessageLoopTest : public BaseIdleHelperTest {
523 public: 525 public:
524 IdleHelperWithMessageLoopTest() 526 IdleHelperWithMessageLoopTest()
525 : BaseIdleHelperTest(new base::MessageLoop(), base::TimeDelta()) {} 527 : BaseIdleHelperTest(new base::MessageLoop(), base::TimeDelta()) {}
526 ~IdleHelperWithMessageLoopTest() override {} 528 ~IdleHelperWithMessageLoopTest() override {}
527 529
528 void PostFromNestedRunloop(std::vector< 530 void PostFromNestedRunloop(std::vector<
529 std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>>* tasks) { 531 std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>>* tasks) {
530 base::MessageLoop::ScopedNestableTaskAllower allow(message_loop_.get()); 532 base::MessageLoop::ScopedNestableTaskAllower allow(message_loop_.get());
531 for (std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>& pair : *tasks) { 533 for (std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>& pair : *tasks) {
532 if (pair.second) { 534 if (pair.second) {
533 idle_task_runner_->PostIdleTask(FROM_HERE, pair.first); 535 idle_task_runner_->PostIdleTask(FROM_HERE, pair.first);
534 } else { 536 } else {
535 idle_task_runner_->PostNonNestableIdleTask(FROM_HERE, pair.first); 537 idle_task_runner_->PostNonNestableIdleTask(FROM_HERE, pair.first);
536 } 538 }
537 } 539 }
538 idle_helper_->StartIdlePeriod( 540 idle_helper_->StartIdlePeriod(
539 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), 541 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(),
540 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); 542 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10));
541 message_loop_->RunUntilIdle(); 543 message_loop_->RunUntilIdle();
542 } 544 }
543 545
544 void SetUp() override { 546 void SetUp() override {
545 EXPECT_CALL(*idle_helper_, OnIdlePeriodStarted()).Times(AnyNumber()); 547 EXPECT_CALL(*idle_helper_, OnIdlePeriodStarted()).Times(AnyNumber());
546 EXPECT_CALL(*idle_helper_, OnIdlePeriodEnded()).Times(AnyNumber()); 548 EXPECT_CALL(*idle_helper_, OnIdlePeriodEnded()).Times(AnyNumber());
547 } 549 }
548 550
549 private: 551 private:
550 DISALLOW_COPY_AND_ASSIGN(IdleHelperWithMessageLoopTest); 552 DISALLOW_COPY_AND_ASSIGN(IdleHelperWithMessageLoopTest);
(...skipping 19 matching lines...) Expand all
570 tasks_to_post_from_nested_loop.push_back(std::make_pair( 572 tasks_to_post_from_nested_loop.push_back(std::make_pair(
571 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("5")), true)); 573 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("5")), true));
572 574
573 default_task_runner_->PostTask( 575 default_task_runner_->PostTask(
574 FROM_HERE, 576 FROM_HERE,
575 base::Bind(&IdleHelperWithMessageLoopTest::PostFromNestedRunloop, 577 base::Bind(&IdleHelperWithMessageLoopTest::PostFromNestedRunloop,
576 base::Unretained(this), 578 base::Unretained(this),
577 base::Unretained(&tasks_to_post_from_nested_loop))); 579 base::Unretained(&tasks_to_post_from_nested_loop)));
578 580
579 idle_helper_->StartIdlePeriod( 581 idle_helper_->StartIdlePeriod(
580 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), 582 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(),
581 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); 583 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10));
582 RunUntilIdle(); 584 RunUntilIdle();
583 // Note we expect task 3 to run last because it's non-nestable. 585 // Note we expect task 3 to run last because it's non-nestable.
584 EXPECT_THAT(order, testing::ElementsAre(std::string("1"), std::string("2"), 586 EXPECT_THAT(order, testing::ElementsAre(std::string("1"), std::string("2"),
585 std::string("4"), std::string("5"), 587 std::string("4"), std::string("5"),
586 std::string("3"))); 588 std::string("3")));
587 } 589 }
588 590
589 TEST_F(IdleHelperTestWithIdlePeriodObserver, TestLongIdlePeriod) { 591 TEST_F(IdleHelperTestWithIdlePeriodObserver, TestLongIdlePeriod) {
590 base::TimeTicks expected_deadline = 592 base::TimeTicks expected_deadline =
591 clock_->Now() + maximum_idle_period_duration(); 593 clock_->NowTicks() + maximum_idle_period_duration();
592 base::TimeTicks deadline_in_task; 594 base::TimeTicks deadline_in_task;
593 int run_count = 0; 595 int run_count = 0;
594 596
595 idle_task_runner_->PostIdleTask( 597 idle_task_runner_->PostIdleTask(
596 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); 598 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
597 599
598 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) 600 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _))
599 .Times(1) 601 .Times(1)
600 .WillRepeatedly(Return(true)); 602 .WillRepeatedly(Return(true));
601 ExpectIdlePeriodStartsButNeverEnds(); 603 ExpectIdlePeriodStartsButNeverEnds();
602 604
603 RunUntilIdle(); 605 RunUntilIdle();
604 EXPECT_EQ(0, run_count); // Shouldn't run yet as no idle period. 606 EXPECT_EQ(0, run_count); // Shouldn't run yet as no idle period.
605 607
606 idle_helper_->EnableLongIdlePeriod(); 608 idle_helper_->EnableLongIdlePeriod();
607 RunUntilIdle(); 609 RunUntilIdle();
608 EXPECT_EQ(1, run_count); // Should have run in a long idle time. 610 EXPECT_EQ(1, run_count); // Should have run in a long idle time.
609 EXPECT_EQ(expected_deadline, deadline_in_task); 611 EXPECT_EQ(expected_deadline, deadline_in_task);
610 } 612 }
611 613
612 TEST_F(IdleHelperTest, TestLongIdlePeriodWithPendingDelayedTask) { 614 TEST_F(IdleHelperTest, TestLongIdlePeriodWithPendingDelayedTask) {
613 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(30); 615 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(30);
614 base::TimeTicks expected_deadline = clock_->Now() + pending_task_delay; 616 base::TimeTicks expected_deadline = clock_->NowTicks() + pending_task_delay;
615 base::TimeTicks deadline_in_task; 617 base::TimeTicks deadline_in_task;
616 int run_count = 0; 618 int run_count = 0;
617 619
618 idle_task_runner_->PostIdleTask( 620 idle_task_runner_->PostIdleTask(
619 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); 621 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
620 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), 622 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask),
621 pending_task_delay); 623 pending_task_delay);
622 624
623 idle_helper_->EnableLongIdlePeriod(); 625 idle_helper_->EnableLongIdlePeriod();
624 RunUntilIdle(); 626 RunUntilIdle();
625 EXPECT_EQ(1, run_count); // Should have run in a long idle time. 627 EXPECT_EQ(1, run_count); // Should have run in a long idle time.
626 EXPECT_EQ(expected_deadline, deadline_in_task); 628 EXPECT_EQ(expected_deadline, deadline_in_task);
627 } 629 }
628 630
629 TEST_F(IdleHelperTest, TestLongIdlePeriodWithLatePendingDelayedTask) { 631 TEST_F(IdleHelperTest, TestLongIdlePeriodWithLatePendingDelayedTask) {
630 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(10); 632 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(10);
631 base::TimeTicks deadline_in_task; 633 base::TimeTicks deadline_in_task;
632 int run_count = 0; 634 int run_count = 0;
633 635
634 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), 636 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask),
635 pending_task_delay); 637 pending_task_delay);
636 638
637 // Advance clock until after delayed task was meant to be run. 639 // Advance clock until after delayed task was meant to be run.
638 clock_->AdvanceNow(base::TimeDelta::FromMilliseconds(20)); 640 clock_->Advance(base::TimeDelta::FromMilliseconds(20));
639 641
640 // Post an idle task and then EnableLongIdlePeriod. Since there is a late 642 // Post an idle task and then EnableLongIdlePeriod. Since there is a late
641 // pending delayed task this shouldn't actually start an idle period. 643 // pending delayed task this shouldn't actually start an idle period.
642 idle_task_runner_->PostIdleTask( 644 idle_task_runner_->PostIdleTask(
643 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); 645 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
644 idle_helper_->EnableLongIdlePeriod(); 646 idle_helper_->EnableLongIdlePeriod();
645 RunUntilIdle(); 647 RunUntilIdle();
646 EXPECT_EQ(0, run_count); 648 EXPECT_EQ(0, run_count);
647 649
648 // After the delayed task has been run we should trigger an idle period. 650 // After the delayed task has been run we should trigger an idle period.
649 clock_->AdvanceNow(maximum_idle_period_duration()); 651 clock_->Advance(maximum_idle_period_duration());
650 RunUntilIdle(); 652 RunUntilIdle();
651 EXPECT_EQ(1, run_count); 653 EXPECT_EQ(1, run_count);
652 } 654 }
653 655
654 TEST_F(IdleHelperTestWithIdlePeriodObserver, TestLongIdlePeriodRepeating) { 656 TEST_F(IdleHelperTestWithIdlePeriodObserver, TestLongIdlePeriodRepeating) {
655 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); 657 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
656 std::vector<base::TimeTicks> actual_deadlines; 658 std::vector<base::TimeTicks> actual_deadlines;
657 int run_count = 0; 659 int run_count = 0;
658 660
659 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) 661 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _))
660 .Times(4) 662 .Times(4)
661 .WillRepeatedly(Return(true)); 663 .WillRepeatedly(Return(true));
662 ExpectIdlePeriodStartsAndEnds(AtLeast(2)); 664 ExpectIdlePeriodStartsAndEnds(AtLeast(2));
663 665
664 max_idle_task_reposts = 3; 666 max_idle_task_reposts = 3;
665 base::TimeTicks clock_before(clock_->Now()); 667 base::TimeTicks clock_before(clock_->NowTicks());
666 base::TimeDelta idle_task_runtime(base::TimeDelta::FromMilliseconds(10)); 668 base::TimeDelta idle_task_runtime(base::TimeDelta::FromMilliseconds(10));
667 idle_task_runner_->PostIdleTask( 669 idle_task_runner_->PostIdleTask(
668 FROM_HERE, 670 FROM_HERE, base::Bind(&RepostingUpdateClockIdleTestTask,
669 base::Bind(&RepostingUpdateClockIdleTestTask, idle_task_runner_, 671 idle_task_runner_, &run_count, clock_.get(),
670 &run_count, clock_, idle_task_runtime, &actual_deadlines)); 672 idle_task_runtime, &actual_deadlines));
671 673
672 // Check each idle task runs in their own idle period. 674 // Check each idle task runs in their own idle period.
673 idle_helper_->EnableLongIdlePeriod(); 675 idle_helper_->EnableLongIdlePeriod();
674 RunUntilIdle(); 676 RunUntilIdle();
675 EXPECT_EQ(3, run_count); 677 EXPECT_EQ(3, run_count);
676 EXPECT_THAT( 678 EXPECT_THAT(
677 actual_deadlines, 679 actual_deadlines,
678 testing::ElementsAre( 680 testing::ElementsAre(
679 clock_before + maximum_idle_period_duration(), 681 clock_before + maximum_idle_period_duration(),
680 clock_before + idle_task_runtime + maximum_idle_period_duration(), 682 clock_before + idle_task_runtime + maximum_idle_period_duration(),
681 clock_before + (2 * idle_task_runtime) + 683 clock_before + (2 * idle_task_runtime) +
682 maximum_idle_period_duration())); 684 maximum_idle_period_duration()));
683 685
684 max_idle_task_reposts = 5; 686 max_idle_task_reposts = 5;
685 idle_task_runner_->PostIdleTask( 687 idle_task_runner_->PostIdleTask(
686 FROM_HERE, 688 FROM_HERE, base::Bind(&RepostingUpdateClockIdleTestTask,
687 base::Bind(&RepostingUpdateClockIdleTestTask, idle_task_runner_, 689 idle_task_runner_, &run_count, clock_.get(),
688 &run_count, clock_, idle_task_runtime, &actual_deadlines)); 690 idle_task_runtime, &actual_deadlines));
689 idle_task_runner_->PostIdleTask( 691 idle_task_runner_->PostIdleTask(
690 FROM_HERE, 692 FROM_HERE,
691 base::Bind(&EndIdlePeriodIdleTask, base::Unretained(idle_helper_.get()))); 693 base::Bind(&EndIdlePeriodIdleTask, base::Unretained(idle_helper_.get())));
692 694
693 // Ensure that reposting tasks stop after EndIdlePeriod is called. 695 // Ensure that reposting tasks stop after EndIdlePeriod is called.
694 RunUntilIdle(); 696 RunUntilIdle();
695 EXPECT_EQ(4, run_count); 697 EXPECT_EQ(4, run_count);
696 } 698 }
697 699
698 TEST_F(IdleHelperTest, TestLongIdlePeriodDoesNotWakeScheduler) { 700 TEST_F(IdleHelperTest, TestLongIdlePeriodDoesNotWakeScheduler) {
699 base::TimeTicks deadline_in_task; 701 base::TimeTicks deadline_in_task;
700 int run_count = 0; 702 int run_count = 0;
701 703
702 // Start a long idle period and get the time it should end. 704 // Start a long idle period and get the time it should end.
703 idle_helper_->EnableLongIdlePeriod(); 705 idle_helper_->EnableLongIdlePeriod();
704 // The scheduler should not run the enable_next_long_idle_period task if 706 // The scheduler should not run the enable_next_long_idle_period task if
705 // there are no idle tasks and no other task woke up the scheduler, thus 707 // there are no idle tasks and no other task woke up the scheduler, thus
706 // the idle period deadline shouldn't update at the end of the current long 708 // the idle period deadline shouldn't update at the end of the current long
707 // idle period. 709 // idle period.
708 base::TimeTicks idle_period_deadline = CurrentIdleTaskDeadline(); 710 base::TimeTicks idle_period_deadline = CurrentIdleTaskDeadline();
709 clock_->AdvanceNow(maximum_idle_period_duration()); 711 clock_->Advance(maximum_idle_period_duration());
710 RunUntilIdle(); 712 RunUntilIdle();
711 713
712 base::TimeTicks new_idle_period_deadline = CurrentIdleTaskDeadline(); 714 base::TimeTicks new_idle_period_deadline = CurrentIdleTaskDeadline();
713 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline); 715 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline);
714 716
715 // Posting a after-wakeup idle task also shouldn't wake the scheduler or 717 // Posting a after-wakeup idle task also shouldn't wake the scheduler or
716 // initiate the next long idle period. 718 // initiate the next long idle period.
717 idle_task_runner_->PostIdleTaskAfterWakeup( 719 idle_task_runner_->PostIdleTaskAfterWakeup(
718 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); 720 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
719 RunUntilIdle(); 721 RunUntilIdle();
720 new_idle_period_deadline = CurrentIdleTaskDeadline(); 722 new_idle_period_deadline = CurrentIdleTaskDeadline();
721 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline); 723 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline);
722 EXPECT_EQ(0, run_count); 724 EXPECT_EQ(0, run_count);
723 725
724 // Running a normal task should initiate a new long idle period though. 726 // Running a normal task should initiate a new long idle period though.
725 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); 727 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask));
726 RunUntilIdle(); 728 RunUntilIdle();
727 new_idle_period_deadline = CurrentIdleTaskDeadline(); 729 new_idle_period_deadline = CurrentIdleTaskDeadline();
728 EXPECT_EQ(idle_period_deadline + maximum_idle_period_duration(), 730 EXPECT_EQ(idle_period_deadline + maximum_idle_period_duration(),
729 new_idle_period_deadline); 731 new_idle_period_deadline);
730 732
731 EXPECT_EQ(1, run_count); 733 EXPECT_EQ(1, run_count);
732 } 734 }
733 735
734 TEST_F(IdleHelperTestWithIdlePeriodObserver, 736 TEST_F(IdleHelperTestWithIdlePeriodObserver,
735 TestLongIdlePeriodWhenNotCanEnterLongIdlePeriod) { 737 TestLongIdlePeriodWhenNotCanEnterLongIdlePeriod) {
736 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(1000); 738 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(1000);
737 base::TimeDelta halfDelay = base::TimeDelta::FromMilliseconds(500); 739 base::TimeDelta halfDelay = base::TimeDelta::FromMilliseconds(500);
738 base::TimeTicks delayOver = clock_->Now() + delay; 740 base::TimeTicks delayOver = clock_->NowTicks() + delay;
739 base::TimeTicks deadline_in_task; 741 base::TimeTicks deadline_in_task;
740 int run_count = 0; 742 int run_count = 0;
741 743
742 ON_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) 744 ON_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _))
743 .WillByDefault(Invoke( 745 .WillByDefault(Invoke(
744 [delay, delayOver](base::TimeTicks now, 746 [delay, delayOver](base::TimeTicks now,
745 base::TimeDelta* next_long_idle_period_delay_out) { 747 base::TimeDelta* next_long_idle_period_delay_out) {
746 if (now >= delayOver) 748 if (now >= delayOver)
747 return true; 749 return true;
748 *next_long_idle_period_delay_out = delay; 750 *next_long_idle_period_delay_out = delay;
749 return false; 751 return false;
750 })); 752 }));
751 753
752 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)).Times(2); 754 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)).Times(2);
753 EXPECT_CALL(*idle_helper_, OnIdlePeriodStarted()).Times(AnyNumber()); 755 EXPECT_CALL(*idle_helper_, OnIdlePeriodStarted()).Times(AnyNumber());
754 756
755 idle_task_runner_->PostIdleTask( 757 idle_task_runner_->PostIdleTask(
756 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); 758 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
757 759
758 // Make sure Idle tasks don't run until the delay has occurred. 760 // Make sure Idle tasks don't run until the delay has occurred.
759 idle_helper_->EnableLongIdlePeriod(); 761 idle_helper_->EnableLongIdlePeriod();
760 RunUntilIdle(); 762 RunUntilIdle();
761 EXPECT_EQ(0, run_count); 763 EXPECT_EQ(0, run_count);
762 764
763 clock_->AdvanceNow(halfDelay); 765 clock_->Advance(halfDelay);
764 RunUntilIdle(); 766 RunUntilIdle();
765 EXPECT_EQ(0, run_count); 767 EXPECT_EQ(0, run_count);
766 768
767 // Delay is finished, idle task should run. 769 // Delay is finished, idle task should run.
768 clock_->AdvanceNow(halfDelay); 770 clock_->Advance(halfDelay);
769 RunUntilIdle(); 771 RunUntilIdle();
770 EXPECT_EQ(1, run_count); 772 EXPECT_EQ(1, run_count);
771 } 773 }
772 774
773 TEST_F(IdleHelperTest, TestLongIdlePeriodImmediatelyRestartsIfMaxDeadline) { 775 TEST_F(IdleHelperTest, TestLongIdlePeriodImmediatelyRestartsIfMaxDeadline) {
774 std::vector<base::TimeTicks> actual_deadlines; 776 std::vector<base::TimeTicks> actual_deadlines;
775 int run_count = 0; 777 int run_count = 0;
776 778
777 base::TimeTicks clock_before(clock_->Now()); 779 base::TimeTicks clock_before(clock_->NowTicks());
778 base::TimeDelta idle_task_runtime(base::TimeDelta::FromMilliseconds(10)); 780 base::TimeDelta idle_task_runtime(base::TimeDelta::FromMilliseconds(10));
779 781
780 // The second idle period should happen immediately after the first the 782 // The second idle period should happen immediately after the first the
781 // they have max deadlines. 783 // they have max deadlines.
782 max_idle_task_reposts = 2; 784 max_idle_task_reposts = 2;
783 idle_task_runner_->PostIdleTask( 785 idle_task_runner_->PostIdleTask(
784 FROM_HERE, 786 FROM_HERE, base::Bind(&RepostingUpdateClockIdleTestTask,
785 base::Bind(&RepostingUpdateClockIdleTestTask, idle_task_runner_, 787 idle_task_runner_, &run_count, clock_.get(),
786 &run_count, clock_, idle_task_runtime, &actual_deadlines)); 788 idle_task_runtime, &actual_deadlines));
787 789
788 idle_helper_->EnableLongIdlePeriod(); 790 idle_helper_->EnableLongIdlePeriod();
789 RunUntilIdle(); 791 RunUntilIdle();
790 EXPECT_EQ(2, run_count); 792 EXPECT_EQ(2, run_count);
791 EXPECT_THAT( 793 EXPECT_THAT(
792 actual_deadlines, 794 actual_deadlines,
793 testing::ElementsAre( 795 testing::ElementsAre(
794 clock_before + maximum_idle_period_duration(), 796 clock_before + maximum_idle_period_duration(),
795 clock_before + idle_task_runtime + maximum_idle_period_duration())); 797 clock_before + idle_task_runtime + maximum_idle_period_duration()));
796 } 798 }
797 799
798 TEST_F(IdleHelperTest, TestLongIdlePeriodRestartWaitsIfNotMaxDeadline) { 800 TEST_F(IdleHelperTest, TestLongIdlePeriodRestartWaitsIfNotMaxDeadline) {
799 base::TimeTicks actual_deadline; 801 base::TimeTicks actual_deadline;
800 int run_count = 0; 802 int run_count = 0;
801 803
802 base::TimeDelta pending_task_delay(base::TimeDelta::FromMilliseconds(20)); 804 base::TimeDelta pending_task_delay(base::TimeDelta::FromMilliseconds(20));
803 base::TimeDelta idle_task_duration(base::TimeDelta::FromMilliseconds(10)); 805 base::TimeDelta idle_task_duration(base::TimeDelta::FromMilliseconds(10));
804 base::TimeTicks expected_deadline(clock_->Now() + pending_task_delay + 806 base::TimeTicks expected_deadline(clock_->NowTicks() + pending_task_delay +
805 maximum_idle_period_duration() + 807 maximum_idle_period_duration() +
806 retry_enable_long_idle_period_delay()); 808 retry_enable_long_idle_period_delay());
807 809
808 // Post delayed task to ensure idle period doesn't have a max deadline. 810 // Post delayed task to ensure idle period doesn't have a max deadline.
809 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), 811 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask),
810 pending_task_delay); 812 pending_task_delay);
811 813
812 max_idle_task_reposts = 2; 814 max_idle_task_reposts = 2;
813 idle_task_runner_->PostIdleTask( 815 idle_task_runner_->PostIdleTask(
814 FROM_HERE, base::Bind(&RepostingIdleTestTask, idle_task_runner_, 816 FROM_HERE, base::Bind(&RepostingIdleTestTask, idle_task_runner_,
815 &run_count, &actual_deadline)); 817 &run_count, &actual_deadline));
816 idle_helper_->EnableLongIdlePeriod(); 818 idle_helper_->EnableLongIdlePeriod();
817 RunUntilIdle(); 819 RunUntilIdle();
818 EXPECT_EQ(1, run_count); 820 EXPECT_EQ(1, run_count);
819 clock_->AdvanceNow(idle_task_duration); 821 clock_->Advance(idle_task_duration);
820 822
821 // Next idle period shouldn't happen until the pending task has been run. 823 // Next idle period shouldn't happen until the pending task has been run.
822 RunUntilIdle(); 824 RunUntilIdle();
823 EXPECT_EQ(1, run_count); 825 EXPECT_EQ(1, run_count);
824 826
825 // Once the pending task is run the new idle period should start. 827 // Once the pending task is run the new idle period should start.
826 clock_->AdvanceNow(pending_task_delay - idle_task_duration); 828 clock_->Advance(pending_task_delay - idle_task_duration);
827 RunUntilIdle(); 829 RunUntilIdle();
828 // Since the idle period tried to start before the pending task ran we have to 830 // Since the idle period tried to start before the pending task ran we have to
829 // wait for the idle helper to retry starting the long idle period. 831 // wait for the idle helper to retry starting the long idle period.
830 clock_->AdvanceNow(retry_enable_long_idle_period_delay()); 832 clock_->Advance(retry_enable_long_idle_period_delay());
831 RunUntilIdle(); 833 RunUntilIdle();
832 834
833 EXPECT_EQ(2, run_count); 835 EXPECT_EQ(2, run_count);
834 EXPECT_EQ(expected_deadline, actual_deadline); 836 EXPECT_EQ(expected_deadline, actual_deadline);
835 } 837 }
836 838
837 TEST_F(IdleHelperTest, TestLongIdlePeriodPaused) { 839 TEST_F(IdleHelperTest, TestLongIdlePeriodPaused) {
838 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); 840 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
839 std::vector<base::TimeTicks> actual_deadlines; 841 std::vector<base::TimeTicks> actual_deadlines;
840 int run_count = 0; 842 int run_count = 0;
841 843
842 // If there are no idle tasks posted we should start in the paused state. 844 // If there are no idle tasks posted we should start in the paused state.
843 idle_helper_->EnableLongIdlePeriod(); 845 idle_helper_->EnableLongIdlePeriod();
844 CheckIdlePeriodStateIs("in_long_idle_period_paused"); 846 CheckIdlePeriodStateIs("in_long_idle_period_paused");
845 // There shouldn't be any delayed tasks posted by the idle helper when paused. 847 // There shouldn't be any delayed tasks posted by the idle helper when paused.
846 EXPECT_EQ(base::TimeTicks(), 848 EXPECT_EQ(base::TimeTicks(),
847 scheduler_helper_->NextPendingDelayedTaskRunTime()); 849 scheduler_helper_->NextPendingDelayedTaskRunTime());
848 850
849 // Posting a task should transition us to the an active state. 851 // Posting a task should transition us to the an active state.
850 max_idle_task_reposts = 2; 852 max_idle_task_reposts = 2;
851 base::TimeTicks clock_before(clock_->Now()); 853 base::TimeTicks clock_before(clock_->NowTicks());
852 base::TimeDelta idle_task_runtime(base::TimeDelta::FromMilliseconds(10)); 854 base::TimeDelta idle_task_runtime(base::TimeDelta::FromMilliseconds(10));
853 idle_task_runner_->PostIdleTask( 855 idle_task_runner_->PostIdleTask(
854 FROM_HERE, 856 FROM_HERE, base::Bind(&RepostingUpdateClockIdleTestTask,
855 base::Bind(&RepostingUpdateClockIdleTestTask, idle_task_runner_, 857 idle_task_runner_, &run_count, clock_.get(),
856 &run_count, clock_, idle_task_runtime, &actual_deadlines)); 858 idle_task_runtime, &actual_deadlines));
857 RunUntilIdle(); 859 RunUntilIdle();
858 EXPECT_EQ(2, run_count); 860 EXPECT_EQ(2, run_count);
859 EXPECT_THAT( 861 EXPECT_THAT(
860 actual_deadlines, 862 actual_deadlines,
861 testing::ElementsAre( 863 testing::ElementsAre(
862 clock_before + maximum_idle_period_duration(), 864 clock_before + maximum_idle_period_duration(),
863 clock_before + idle_task_runtime + maximum_idle_period_duration())); 865 clock_before + idle_task_runtime + maximum_idle_period_duration()));
864 866
865 // Once all task have been run we should go back to the paused state. 867 // Once all task have been run we should go back to the paused state.
866 CheckIdlePeriodStateIs("in_long_idle_period_paused"); 868 CheckIdlePeriodStateIs("in_long_idle_period_paused");
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
901 903
902 // Should return false if not in an idle period. 904 // Should return false if not in an idle period.
903 EXPECT_FALSE(idle_helper_->CanExceedIdleDeadlineIfRequired()); 905 EXPECT_FALSE(idle_helper_->CanExceedIdleDeadlineIfRequired());
904 906
905 // Should return false for short idle periods. 907 // Should return false for short idle periods.
906 idle_task_runner_->PostIdleTask( 908 idle_task_runner_->PostIdleTask(
907 FROM_HERE, 909 FROM_HERE,
908 base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, idle_helper_.get(), 910 base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, idle_helper_.get(),
909 &can_exceed_idle_deadline, &run_count)); 911 &can_exceed_idle_deadline, &run_count));
910 idle_helper_->StartIdlePeriod( 912 idle_helper_->StartIdlePeriod(
911 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), 913 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(),
912 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); 914 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10));
913 RunUntilIdle(); 915 RunUntilIdle();
914 EXPECT_EQ(1, run_count); 916 EXPECT_EQ(1, run_count);
915 EXPECT_FALSE(can_exceed_idle_deadline); 917 EXPECT_FALSE(can_exceed_idle_deadline);
916 918
917 // Should return false for a long idle period which is shortened due to a 919 // Should return false for a long idle period which is shortened due to a
918 // pending delayed task. 920 // pending delayed task.
919 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), 921 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask),
920 base::TimeDelta::FromMilliseconds(10)); 922 base::TimeDelta::FromMilliseconds(10));
921 idle_task_runner_->PostIdleTask( 923 idle_task_runner_->PostIdleTask(
922 FROM_HERE, 924 FROM_HERE,
923 base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, idle_helper_.get(), 925 base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, idle_helper_.get(),
924 &can_exceed_idle_deadline, &run_count)); 926 &can_exceed_idle_deadline, &run_count));
925 idle_helper_->EnableLongIdlePeriod(); 927 idle_helper_->EnableLongIdlePeriod();
926 RunUntilIdle(); 928 RunUntilIdle();
927 EXPECT_EQ(2, run_count); 929 EXPECT_EQ(2, run_count);
928 EXPECT_FALSE(can_exceed_idle_deadline); 930 EXPECT_FALSE(can_exceed_idle_deadline);
929 931
930 // Next long idle period will be for the maximum time, so 932 // Next long idle period will be for the maximum time, so
931 // CanExceedIdleDeadlineIfRequired should return true. 933 // CanExceedIdleDeadlineIfRequired should return true.
932 clock_->AdvanceNow(maximum_idle_period_duration()); 934 clock_->Advance(maximum_idle_period_duration());
933 idle_task_runner_->PostIdleTask( 935 idle_task_runner_->PostIdleTask(
934 FROM_HERE, 936 FROM_HERE,
935 base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, idle_helper_.get(), 937 base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, idle_helper_.get(),
936 &can_exceed_idle_deadline, &run_count)); 938 &can_exceed_idle_deadline, &run_count));
937 RunUntilIdle(); 939 RunUntilIdle();
938 EXPECT_EQ(3, run_count); 940 EXPECT_EQ(3, run_count);
939 EXPECT_TRUE(can_exceed_idle_deadline); 941 EXPECT_TRUE(can_exceed_idle_deadline);
940 } 942 }
941 943
942 class IdleHelperWithQuiescencePeriodTest : public BaseIdleHelperTest { 944 class IdleHelperWithQuiescencePeriodTest : public BaseIdleHelperTest {
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
1037 // Run a repeating task so we're deemed to be busy for the next 400ms. 1039 // Run a repeating task so we're deemed to be busy for the next 400ms.
1038 default_task_runner_->PostTask( 1040 default_task_runner_->PostTask(
1039 FROM_HERE, 1041 FROM_HERE,
1040 base::Bind(&RepeatingTask, base::Unretained(default_task_runner_.get()), 1042 base::Bind(&RepeatingTask, base::Unretained(default_task_runner_.get()),
1041 10, base::TimeDelta::FromMilliseconds(40))); 1043 10, base::TimeDelta::FromMilliseconds(40)));
1042 1044
1043 int run_count = 0; 1045 int run_count = 0;
1044 // In this scenario EnableLongIdlePeriod deems us not to be quiescent 5x in 1046 // In this scenario EnableLongIdlePeriod deems us not to be quiescent 5x in
1045 // a row. 1047 // a row.
1046 base::TimeTicks expected_deadline = 1048 base::TimeTicks expected_deadline =
1047 clock_->Now() + base::TimeDelta::FromMilliseconds(5 * kQuiescenceDelayMs + 1049 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(
1048 kLongIdlePeriodMs); 1050 5 * kQuiescenceDelayMs + kLongIdlePeriodMs);
1049 base::TimeTicks deadline_in_task; 1051 base::TimeTicks deadline_in_task;
1050 idle_task_runner_->PostIdleTask( 1052 idle_task_runner_->PostIdleTask(
1051 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); 1053 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1052 1054
1053 idle_helper_->EnableLongIdlePeriod(); 1055 idle_helper_->EnableLongIdlePeriod();
1054 RunUntilIdle(); 1056 RunUntilIdle();
1055 EXPECT_EQ(1, run_count); 1057 EXPECT_EQ(1, run_count);
1056 EXPECT_EQ(expected_deadline, deadline_in_task); 1058 EXPECT_EQ(expected_deadline, deadline_in_task);
1057 } 1059 }
1058 1060
1059 TEST_F(IdleHelperWithQuiescencePeriodTest, 1061 TEST_F(IdleHelperWithQuiescencePeriodTest,
1060 QuescienceCheckedForAfterLongIdlePeriodEnds) { 1062 QuescienceCheckedForAfterLongIdlePeriodEnds) {
1061 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); 1063 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
1062 1064
1063 idle_task_runner_->PostIdleTask(FROM_HERE, base::Bind(&NullIdleTask)); 1065 idle_task_runner_->PostIdleTask(FROM_HERE, base::Bind(&NullIdleTask));
1064 idle_helper_->EnableLongIdlePeriod(); 1066 idle_helper_->EnableLongIdlePeriod();
1065 RunUntilIdle(); 1067 RunUntilIdle();
1066 1068
1067 // Post a normal task to make the scheduler non-quiescent. 1069 // Post a normal task to make the scheduler non-quiescent.
1068 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); 1070 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask));
1069 RunUntilIdle(); 1071 RunUntilIdle();
1070 1072
1071 // Post an idle task. The idle task won't run initially because the system is 1073 // Post an idle task. The idle task won't run initially because the system is
1072 // not judged to be quiescent, but should be run after the quiescence delay. 1074 // not judged to be quiescent, but should be run after the quiescence delay.
1073 int run_count = 0; 1075 int run_count = 0;
1074 base::TimeTicks deadline_in_task; 1076 base::TimeTicks deadline_in_task;
1075 base::TimeTicks expected_deadline = 1077 base::TimeTicks expected_deadline =
1076 clock_->Now() + 1078 clock_->NowTicks() +
1077 base::TimeDelta::FromMilliseconds(kQuiescenceDelayMs + kLongIdlePeriodMs); 1079 base::TimeDelta::FromMilliseconds(kQuiescenceDelayMs + kLongIdlePeriodMs);
1078 idle_task_runner_->PostIdleTask( 1080 idle_task_runner_->PostIdleTask(
1079 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); 1081 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1080 idle_helper_->EnableLongIdlePeriod(); 1082 idle_helper_->EnableLongIdlePeriod();
1081 RunUntilIdle(); 1083 RunUntilIdle();
1082 1084
1083 EXPECT_EQ(1, run_count); 1085 EXPECT_EQ(1, run_count);
1084 EXPECT_EQ(expected_deadline, deadline_in_task); 1086 EXPECT_EQ(expected_deadline, deadline_in_task);
1085 } 1087 }
1086 1088
1087 TEST_F(IdleHelperTest, NoShortIdlePeriodWhenDeadlineTooClose) { 1089 TEST_F(IdleHelperTest, NoShortIdlePeriodWhenDeadlineTooClose) {
1088 int run_count = 0; 1090 int run_count = 0;
1089 base::TimeTicks deadline_in_task; 1091 base::TimeTicks deadline_in_task;
1090 1092
1091 idle_task_runner_->PostIdleTask( 1093 idle_task_runner_->PostIdleTask(
1092 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); 1094 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1093 1095
1094 base::TimeDelta half_a_ms(base::TimeDelta::FromMicroseconds(50)); 1096 base::TimeDelta half_a_ms(base::TimeDelta::FromMicroseconds(50));
1095 base::TimeTicks less_than_min_deadline( 1097 base::TimeTicks less_than_min_deadline(
1096 clock_->Now() + minimum_idle_period_duration() - half_a_ms); 1098 clock_->NowTicks() + minimum_idle_period_duration() - half_a_ms);
1097 base::TimeTicks more_than_min_deadline( 1099 base::TimeTicks more_than_min_deadline(
1098 clock_->Now() + minimum_idle_period_duration() + half_a_ms); 1100 clock_->NowTicks() + minimum_idle_period_duration() + half_a_ms);
1099 1101
1100 idle_helper_->StartIdlePeriod( 1102 idle_helper_->StartIdlePeriod(
1101 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), 1103 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(),
1102 less_than_min_deadline); 1104 less_than_min_deadline);
1103 RunUntilIdle(); 1105 RunUntilIdle();
1104 EXPECT_EQ(0, run_count); 1106 EXPECT_EQ(0, run_count);
1105 1107
1106 idle_helper_->StartIdlePeriod( 1108 idle_helper_->StartIdlePeriod(
1107 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), 1109 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(),
1108 more_than_min_deadline); 1110 more_than_min_deadline);
1109 RunUntilIdle(); 1111 RunUntilIdle();
1110 EXPECT_EQ(1, run_count); 1112 EXPECT_EQ(1, run_count);
1111 } 1113 }
1112 1114
1113 TEST_F(IdleHelperTest, NoLongIdlePeriodWhenDeadlineTooClose) { 1115 TEST_F(IdleHelperTest, NoLongIdlePeriodWhenDeadlineTooClose) {
1114 int run_count = 0; 1116 int run_count = 0;
1115 base::TimeTicks deadline_in_task; 1117 base::TimeTicks deadline_in_task;
1116 1118
1117 base::TimeDelta half_a_ms(base::TimeDelta::FromMicroseconds(50)); 1119 base::TimeDelta half_a_ms(base::TimeDelta::FromMicroseconds(50));
1118 base::TimeDelta less_than_min_deadline_duration( 1120 base::TimeDelta less_than_min_deadline_duration(
1119 minimum_idle_period_duration() - half_a_ms); 1121 minimum_idle_period_duration() - half_a_ms);
1120 base::TimeDelta more_than_min_deadline_duration( 1122 base::TimeDelta more_than_min_deadline_duration(
1121 minimum_idle_period_duration() + half_a_ms); 1123 minimum_idle_period_duration() + half_a_ms);
1122 1124
1123 idle_task_runner_->PostIdleTask( 1125 idle_task_runner_->PostIdleTask(
1124 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); 1126 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1125 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), 1127 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask),
1126 less_than_min_deadline_duration); 1128 less_than_min_deadline_duration);
1127 1129
1128 idle_helper_->EnableLongIdlePeriod(); 1130 idle_helper_->EnableLongIdlePeriod();
1129 RunUntilIdle(); 1131 RunUntilIdle();
1130 EXPECT_EQ(0, run_count); 1132 EXPECT_EQ(0, run_count);
1131 1133
1132 idle_helper_->EndIdlePeriod(); 1134 idle_helper_->EndIdlePeriod();
1133 clock_->AdvanceNow(maximum_idle_period_duration()); 1135 clock_->Advance(maximum_idle_period_duration());
1134 RunUntilIdle(); 1136 RunUntilIdle();
1135 EXPECT_EQ(0, run_count); 1137 EXPECT_EQ(0, run_count);
1136 1138
1137 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), 1139 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask),
1138 more_than_min_deadline_duration); 1140 more_than_min_deadline_duration);
1139 idle_helper_->EnableLongIdlePeriod(); 1141 idle_helper_->EnableLongIdlePeriod();
1140 RunUntilIdle(); 1142 RunUntilIdle();
1141 EXPECT_EQ(1, run_count); 1143 EXPECT_EQ(1, run_count);
1142 } 1144 }
1143 1145
1144 } // namespace scheduler 1146 } // namespace scheduler
OLDNEW
« no previous file with comments | « components/scheduler/child/DEPS ('k') | components/scheduler/child/scheduler_helper_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698