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"
17 using testing::AnyNumber
;
18 using testing::Invoke
;
19 using testing::Return
;
24 void AppendToVectorTestTask(std::vector
<std::string
>* vector
,
26 vector
->push_back(value
);
29 void AppendToVectorReentrantTask(base::SingleThreadTaskRunner
* task_runner
,
30 std::vector
<int>* vector
,
32 int max_reentrant_count
) {
33 vector
->push_back((*reentrant_count
)++);
34 if (*reentrant_count
< max_reentrant_count
) {
35 task_runner
->PostTask(
37 base::Bind(AppendToVectorReentrantTask
, base::Unretained(task_runner
),
38 vector
, reentrant_count
, max_reentrant_count
));
44 class SchedulerHelperTest
: public testing::Test
{
47 : clock_(new base::SimpleTestTickClock()),
48 mock_task_runner_(new cc::OrderedSimpleTaskRunner(clock_
.get(), false)),
50 SchedulerTaskRunnerDelegateForTest::Create(mock_task_runner_
)),
51 scheduler_helper_(new SchedulerHelper(
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(); }
76 static void CallForEachEnumValue(E first
,
78 const char* (*function
)(E
)) {
79 for (E val
= first
; val
< last
;
80 val
= static_cast<E
>(static_cast<int>(val
) + 1)) {
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"));
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
) {
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));
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());
139 class MockTaskObserver
: public base::MessageLoop::TaskObserver
{
141 MOCK_METHOD1(DidProcessTask
, void(const base::PendingTask
& task
));
142 MOCK_METHOD1(WillProcessTask
, void(const base::PendingTask
& task
));
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);
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);
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();
186 } // namespace scheduler