Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / scheduler / renderer / deadline_task_runner_unittest.cc
blob1cffa69d88b0b86059812eb4f5c5cec7455a04a6
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
3 // found in the LICENSE file.
5 #include "base/test/simple_test_tick_clock.h"
6 #include "components/scheduler/renderer/deadline_task_runner.h"
8 #include "cc/test/ordered_simple_task_runner.h"
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace scheduler {
14 class DeadlineTaskRunnerTest : public testing::Test {
15 public:
16 DeadlineTaskRunnerTest() {}
17 ~DeadlineTaskRunnerTest() override {}
19 void SetUp() override {
20 clock_.reset(new base::SimpleTestTickClock());
21 clock_->Advance(base::TimeDelta::FromMicroseconds(5000));
22 mock_task_runner_ = new cc::OrderedSimpleTaskRunner(clock_.get(), true);
23 deadline_task_runner_.reset(new DeadlineTaskRunner(
24 base::Bind(&DeadlineTaskRunnerTest::TestTask, base::Unretained(this)),
25 mock_task_runner_));
26 run_times_.clear();
29 bool RunUntilIdle() { return mock_task_runner_->RunUntilIdle(); }
31 void TestTask() { run_times_.push_back(clock_->NowTicks()); }
33 scoped_ptr<base::SimpleTestTickClock> clock_;
34 scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_;
35 scoped_ptr<DeadlineTaskRunner> deadline_task_runner_;
36 std::vector<base::TimeTicks> run_times_;
39 TEST_F(DeadlineTaskRunnerTest, RunOnce) {
40 base::TimeTicks start_time = clock_->NowTicks();
41 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(10);
42 deadline_task_runner_->SetDeadline(FROM_HERE, delay, clock_->NowTicks());
43 RunUntilIdle();
45 EXPECT_THAT(run_times_, testing::ElementsAre(start_time + delay));
48 TEST_F(DeadlineTaskRunnerTest, RunTwice) {
49 base::TimeDelta delay1 = base::TimeDelta::FromMilliseconds(10);
50 base::TimeTicks deadline1 = clock_->NowTicks() + delay1;
51 deadline_task_runner_->SetDeadline(FROM_HERE, delay1, clock_->NowTicks());
52 RunUntilIdle();
54 base::TimeDelta delay2 = base::TimeDelta::FromMilliseconds(100);
55 base::TimeTicks deadline2 = clock_->NowTicks() + delay2;
56 deadline_task_runner_->SetDeadline(FROM_HERE, delay2, clock_->NowTicks());
57 RunUntilIdle();
59 EXPECT_THAT(run_times_, testing::ElementsAre(deadline1, deadline2));
62 TEST_F(DeadlineTaskRunnerTest, EarlierDeadlinesTakePrecidence) {
63 base::TimeTicks start_time = clock_->NowTicks();
64 base::TimeDelta delay1 = base::TimeDelta::FromMilliseconds(1);
65 base::TimeDelta delay10 = base::TimeDelta::FromMilliseconds(10);
66 base::TimeDelta delay100 = base::TimeDelta::FromMilliseconds(100);
67 deadline_task_runner_->SetDeadline(FROM_HERE, delay100, clock_->NowTicks());
68 deadline_task_runner_->SetDeadline(FROM_HERE, delay10, clock_->NowTicks());
69 deadline_task_runner_->SetDeadline(FROM_HERE, delay1, clock_->NowTicks());
71 RunUntilIdle();
73 EXPECT_THAT(run_times_, testing::ElementsAre(start_time + delay1));
76 TEST_F(DeadlineTaskRunnerTest, LaterDeadlinesIgnored) {
77 base::TimeTicks start_time = clock_->NowTicks();
78 base::TimeDelta delay100 = base::TimeDelta::FromMilliseconds(100);
79 base::TimeDelta delay10000 = base::TimeDelta::FromMilliseconds(10000);
80 deadline_task_runner_->SetDeadline(FROM_HERE, delay100, clock_->NowTicks());
81 deadline_task_runner_->SetDeadline(FROM_HERE, delay10000, clock_->NowTicks());
83 RunUntilIdle();
85 EXPECT_THAT(run_times_, testing::ElementsAre(start_time + delay100));
88 TEST_F(DeadlineTaskRunnerTest, DeleteDeadlineTaskRunnerAfterPosting) {
89 deadline_task_runner_->SetDeadline(
90 FROM_HERE, base::TimeDelta::FromMilliseconds(10), clock_->NowTicks());
92 // Deleting the pending task should cancel it.
93 deadline_task_runner_.reset(nullptr);
94 RunUntilIdle();
96 EXPECT_TRUE(run_times_.empty());
99 } // namespace scheduler