Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / scheduler / renderer / webthread_impl_for_renderer_scheduler_unittest.cc
blobc29dbb30d251ff0bea84b1ca43536e3e90736d1b
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 "components/scheduler/renderer/webthread_impl_for_renderer_scheduler.h"
7 #include "base/location.h"
8 #include "base/run_loop.h"
9 #include "base/single_thread_task_runner.h"
10 #include "components/scheduler/child/scheduler_task_runner_delegate_impl.h"
11 #include "components/scheduler/renderer/renderer_scheduler_impl.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/WebKit/public/platform/WebTaskRunner.h"
15 #include "third_party/WebKit/public/platform/WebTraceLocation.h"
17 namespace scheduler {
18 namespace {
20 const int kWorkBatchSize = 2;
22 class MockTask : public blink::WebTaskRunner::Task {
23 public:
24 MOCK_METHOD0(run, void());
27 class MockTaskObserver : public blink::WebThread::TaskObserver {
28 public:
29 MOCK_METHOD0(willProcessTask, void());
30 MOCK_METHOD0(didProcessTask, void());
32 } // namespace
34 class WebThreadImplForRendererSchedulerTest : public testing::Test {
35 public:
36 WebThreadImplForRendererSchedulerTest()
37 : scheduler_(SchedulerTaskRunnerDelegateImpl::Create(&message_loop_)),
38 default_task_runner_(scheduler_.DefaultTaskRunner()),
39 thread_(&scheduler_) {}
41 ~WebThreadImplForRendererSchedulerTest() override {}
43 void SetWorkBatchSizeForTesting(size_t work_batch_size) {
44 scheduler_.GetSchedulerHelperForTesting()->SetWorkBatchSizeForTesting(
45 work_batch_size);
48 void TearDown() override { scheduler_.Shutdown(); }
50 protected:
51 base::MessageLoop message_loop_;
52 RendererSchedulerImpl scheduler_;
53 scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_;
54 WebThreadImplForRendererScheduler thread_;
56 DISALLOW_COPY_AND_ASSIGN(WebThreadImplForRendererSchedulerTest);
59 TEST_F(WebThreadImplForRendererSchedulerTest, TestTaskObserver) {
60 MockTaskObserver observer;
61 thread_.addTaskObserver(&observer);
62 scoped_ptr<MockTask> task(new MockTask());
65 testing::InSequence sequence;
66 EXPECT_CALL(observer, willProcessTask());
67 EXPECT_CALL(*task, run());
68 EXPECT_CALL(observer, didProcessTask());
71 thread_.taskRunner()->postTask(blink::WebTraceLocation(), task.release());
72 message_loop_.RunUntilIdle();
73 thread_.removeTaskObserver(&observer);
76 TEST_F(WebThreadImplForRendererSchedulerTest, TestWorkBatchWithOneTask) {
77 MockTaskObserver observer;
78 thread_.addTaskObserver(&observer);
79 scoped_ptr<MockTask> task(new MockTask());
81 SetWorkBatchSizeForTesting(kWorkBatchSize);
83 testing::InSequence sequence;
84 EXPECT_CALL(observer, willProcessTask());
85 EXPECT_CALL(*task, run());
86 EXPECT_CALL(observer, didProcessTask());
89 thread_.taskRunner()->postTask(blink::WebTraceLocation(), task.release());
90 message_loop_.RunUntilIdle();
91 thread_.removeTaskObserver(&observer);
94 TEST_F(WebThreadImplForRendererSchedulerTest, TestWorkBatchWithTwoTasks) {
95 MockTaskObserver observer;
96 thread_.addTaskObserver(&observer);
97 scoped_ptr<MockTask> task1(new MockTask());
98 scoped_ptr<MockTask> task2(new MockTask());
100 SetWorkBatchSizeForTesting(kWorkBatchSize);
102 testing::InSequence sequence;
103 EXPECT_CALL(observer, willProcessTask());
104 EXPECT_CALL(*task1, run());
105 EXPECT_CALL(observer, didProcessTask());
107 EXPECT_CALL(observer, willProcessTask());
108 EXPECT_CALL(*task2, run());
109 EXPECT_CALL(observer, didProcessTask());
112 thread_.taskRunner()->postTask(blink::WebTraceLocation(), task1.release());
113 thread_.taskRunner()->postTask(blink::WebTraceLocation(), task2.release());
114 message_loop_.RunUntilIdle();
115 thread_.removeTaskObserver(&observer);
118 TEST_F(WebThreadImplForRendererSchedulerTest, TestWorkBatchWithThreeTasks) {
119 MockTaskObserver observer;
120 thread_.addTaskObserver(&observer);
121 scoped_ptr<MockTask> task1(new MockTask());
122 scoped_ptr<MockTask> task2(new MockTask());
123 scoped_ptr<MockTask> task3(new MockTask());
125 SetWorkBatchSizeForTesting(kWorkBatchSize);
127 testing::InSequence sequence;
128 EXPECT_CALL(observer, willProcessTask());
129 EXPECT_CALL(*task1, run());
130 EXPECT_CALL(observer, didProcessTask());
132 EXPECT_CALL(observer, willProcessTask());
133 EXPECT_CALL(*task2, run());
134 EXPECT_CALL(observer, didProcessTask());
136 EXPECT_CALL(observer, willProcessTask());
137 EXPECT_CALL(*task3, run());
138 EXPECT_CALL(observer, didProcessTask());
141 thread_.taskRunner()->postTask(blink::WebTraceLocation(), task1.release());
142 thread_.taskRunner()->postTask(blink::WebTraceLocation(), task2.release());
143 thread_.taskRunner()->postTask(blink::WebTraceLocation(), task3.release());
144 message_loop_.RunUntilIdle();
145 thread_.removeTaskObserver(&observer);
148 class ExitRunLoopTask : public blink::WebTaskRunner::Task {
149 public:
150 ExitRunLoopTask(base::RunLoop* run_loop) : run_loop_(run_loop) {}
152 virtual void run() { run_loop_->Quit(); }
154 private:
155 base::RunLoop* run_loop_;
158 void EnterRunLoop(base::MessageLoop* message_loop, blink::WebThread* thread) {
159 // Note: WebThreads do not support nested run loops, which is why we use a
160 // run loop directly.
161 base::RunLoop run_loop;
162 thread->taskRunner()->postTask(blink::WebTraceLocation(),
163 new ExitRunLoopTask(&run_loop));
164 message_loop->SetNestableTasksAllowed(true);
165 run_loop.Run();
168 TEST_F(WebThreadImplForRendererSchedulerTest, TestNestedRunLoop) {
169 MockTaskObserver observer;
170 thread_.addTaskObserver(&observer);
173 testing::InSequence sequence;
175 // One callback for EnterRunLoop.
176 EXPECT_CALL(observer, willProcessTask());
178 // A pair for ExitRunLoopTask.
179 EXPECT_CALL(observer, willProcessTask());
180 EXPECT_CALL(observer, didProcessTask());
182 // A final callback for EnterRunLoop.
183 EXPECT_CALL(observer, didProcessTask());
186 message_loop_.task_runner()->PostTask(
187 FROM_HERE, base::Bind(&EnterRunLoop, base::Unretained(&message_loop_),
188 base::Unretained(&thread_)));
189 message_loop_.RunUntilIdle();
190 thread_.removeTaskObserver(&observer);
193 } // namespace scheduler