Roll src/third_party/WebKit 605a979:06cb9e9 (svn 202556:202558)
[chromium-blink-merge.git] / components / scheduler / child / scheduler_helper_unittest.cc
blob2807186a853e3aa176fca7a4c5004b2c02cc19e4
1 // Copyright 2014 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 "components/scheduler/child/scheduler_helper.h"
7 #include "base/callback.h"
8 #include "base/test/simple_test_tick_clock.h"
9 #include "cc/test/ordered_simple_task_runner.h"
10 #include "components/scheduler/child/scheduler_task_runner_delegate_for_test.h"
11 #include "components/scheduler/child/task_queue.h"
12 #include "components/scheduler/child/test_time_source.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using testing::_;
17 using testing::AnyNumber;
18 using testing::Invoke;
19 using testing::Return;
21 namespace scheduler {
23 namespace {
24 void AppendToVectorTestTask(std::vector<std::string>* vector,
25 std::string value) {
26 vector->push_back(value);
29 void AppendToVectorReentrantTask(base::SingleThreadTaskRunner* task_runner,
30 std::vector<int>* vector,
31 int* reentrant_count,
32 int max_reentrant_count) {
33 vector->push_back((*reentrant_count)++);
34 if (*reentrant_count < max_reentrant_count) {
35 task_runner->PostTask(
36 FROM_HERE,
37 base::Bind(AppendToVectorReentrantTask, base::Unretained(task_runner),
38 vector, reentrant_count, max_reentrant_count));
42 }; // namespace
44 class SchedulerHelperTest : public testing::Test {
45 public:
46 SchedulerHelperTest()
47 : clock_(new base::SimpleTestTickClock()),
48 mock_task_runner_(new cc::OrderedSimpleTaskRunner(clock_.get(), false)),
49 main_task_runner_(
50 SchedulerTaskRunnerDelegateForTest::Create(mock_task_runner_)),
51 scheduler_helper_(new SchedulerHelper(
52 main_task_runner_,
53 "test.scheduler",
54 TRACE_DISABLED_BY_DEFAULT("test.scheduler"),
55 TRACE_DISABLED_BY_DEFAULT("test.scheduler.dbg"))),
56 default_task_runner_(scheduler_helper_->DefaultTaskRunner()) {
57 clock_->Advance(base::TimeDelta::FromMicroseconds(5000));
58 scheduler_helper_->SetTimeSourceForTesting(
59 make_scoped_ptr(new TestTimeSource(clock_.get())));
60 scheduler_helper_->GetTaskQueueManagerForTesting()->SetTimeSourceForTesting(
61 make_scoped_ptr(new TestTimeSource(clock_.get())));
64 ~SchedulerHelperTest() override {}
66 void TearDown() override {
67 // Check that all tests stop posting tasks.
68 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
69 while (mock_task_runner_->RunUntilIdle()) {
73 void RunUntilIdle() { mock_task_runner_->RunUntilIdle(); }
75 template <typename E>
76 static void CallForEachEnumValue(E first,
77 E last,
78 const char* (*function)(E)) {
79 for (E val = first; val < last;
80 val = static_cast<E>(static_cast<int>(val) + 1)) {
81 (*function)(val);
85 protected:
86 scoped_ptr<base::SimpleTestTickClock> clock_;
87 scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_;
89 scoped_refptr<SchedulerTaskRunnerDelegateForTest> main_task_runner_;
90 scoped_ptr<SchedulerHelper> scheduler_helper_;
91 scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_;
93 DISALLOW_COPY_AND_ASSIGN(SchedulerHelperTest);
96 TEST_F(SchedulerHelperTest, TestPostDefaultTask) {
97 std::vector<std::string> run_order;
98 default_task_runner_->PostTask(
99 FROM_HERE, base::Bind(&AppendToVectorTestTask, &run_order, "D1"));
100 default_task_runner_->PostTask(
101 FROM_HERE, base::Bind(&AppendToVectorTestTask, &run_order, "D2"));
102 default_task_runner_->PostTask(
103 FROM_HERE, base::Bind(&AppendToVectorTestTask, &run_order, "D3"));
104 default_task_runner_->PostTask(
105 FROM_HERE, base::Bind(&AppendToVectorTestTask, &run_order, "D4"));
107 RunUntilIdle();
108 EXPECT_THAT(run_order,
109 testing::ElementsAre(std::string("D1"), std::string("D2"),
110 std::string("D3"), std::string("D4")));
113 TEST_F(SchedulerHelperTest, TestRentrantTask) {
114 int count = 0;
115 std::vector<int> run_order;
116 default_task_runner_->PostTask(
117 FROM_HERE, base::Bind(AppendToVectorReentrantTask, default_task_runner_,
118 &run_order, &count, 5));
119 RunUntilIdle();
121 EXPECT_THAT(run_order, testing::ElementsAre(0, 1, 2, 3, 4));
124 TEST_F(SchedulerHelperTest, IsShutdown) {
125 EXPECT_FALSE(scheduler_helper_->IsShutdown());
127 scheduler_helper_->Shutdown();
128 EXPECT_TRUE(scheduler_helper_->IsShutdown());
131 TEST_F(SchedulerHelperTest, DefaultTaskRunnerRegistration) {
132 EXPECT_EQ(main_task_runner_->default_task_runner(),
133 scheduler_helper_->DefaultTaskRunner());
134 scheduler_helper_->Shutdown();
135 EXPECT_EQ(nullptr, main_task_runner_->default_task_runner());
138 namespace {
139 class MockTaskObserver : public base::MessageLoop::TaskObserver {
140 public:
141 MOCK_METHOD1(DidProcessTask, void(const base::PendingTask& task));
142 MOCK_METHOD1(WillProcessTask, void(const base::PendingTask& task));
145 void NopTask() {}
146 } // namespace
148 TEST_F(SchedulerHelperTest, ObserversNotifiedFor_DefaultTaskRunner) {
149 MockTaskObserver observer;
150 scheduler_helper_->AddTaskObserver(&observer);
152 scheduler_helper_->DefaultTaskRunner()->PostTask(FROM_HERE,
153 base::Bind(&NopTask));
155 EXPECT_CALL(observer, WillProcessTask(_)).Times(1);
156 EXPECT_CALL(observer, DidProcessTask(_)).Times(1);
157 RunUntilIdle();
160 TEST_F(SchedulerHelperTest, ObserversNotNotifiedFor_ControlTaskRunner) {
161 MockTaskObserver observer;
162 scheduler_helper_->AddTaskObserver(&observer);
164 scheduler_helper_->ControlTaskRunner()->PostTask(FROM_HERE,
165 base::Bind(&NopTask));
167 EXPECT_CALL(observer, WillProcessTask(_)).Times(0);
168 EXPECT_CALL(observer, DidProcessTask(_)).Times(0);
169 RunUntilIdle();
172 TEST_F(SchedulerHelperTest,
173 ObserversNotNotifiedFor_ControlAfterWakeUpTaskRunner) {
174 MockTaskObserver observer;
175 scheduler_helper_->AddTaskObserver(&observer);
177 scheduler_helper_->ControlAfterWakeUpTaskRunner()->PostTask(
178 FROM_HERE, base::Bind(&NopTask));
180 EXPECT_CALL(observer, WillProcessTask(_)).Times(0);
181 EXPECT_CALL(observer, DidProcessTask(_)).Times(0);
182 scheduler_helper_->ControlAfterWakeUpTaskRunner()->PumpQueue();
183 RunUntilIdle();
186 } // namespace scheduler