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

Side by Side Diff: content/browser/startup_task_runner_unittest.cc

Issue 19957002: Run the later parts of startup as UI thread tasks (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Run the later parts of startup as UI thread tasks - fix jam@'s comments Created 7 years, 4 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
(Empty)
1 // Copyright (c) 2013 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.
4
5 #include "content/browser/startup_task_runner.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/location.h"
11 #include "base/run_loop.h"
12 #include "base/task_runner.h"
13
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace content {
18 namespace {
19
20 using base::Closure;
21 using testing::_;
22 using testing::Assign;
23 using testing::Invoke;
24 using testing::WithArg;
25
26 bool observer_called = false;
27 int observer_result;
28 base::Closure task;
29
30 // I couldn't get gMock's SaveArg to compile, hence had to save the argument
31 // this way
32 bool SaveTaskArg(const Closure& arg) {
33 task = arg;
34 return true;
35 }
36
37 void Observer(int result) {
38 observer_called = true;
39 observer_result = result;
40 }
41
42 class StartupTaskRunnerTest : public testing::Test {
43 public:
44
45 virtual void SetUp() {
46 last_task_ = 0;
47 observer_called = false;
48 }
49
50 int Task1() {
51 last_task_ = 1;
52 return 0;
53 }
54
55 int Task2() {
56 last_task_ = 2;
57 return 0;
58 }
59
60 int FailingTask() {
61 // Task returning failure
62 last_task_ = 3;
63 return 1;
64 }
65
66 int GetLastTask() { return last_task_; }
67
68 private:
69
70 int last_task_;
71 };
72
73 // We can't use the real message loop, even if we want to, since doing so on
74 // Android requires a complex Java infrastructure. The test would have to built
75 // as a content_shell test; but content_shell startup invokes the class we are
76 // trying to test.
77 //
78 // The mocks are not directly in TaskRunnerProxy because reference counted
79 // objects seem to confuse the mocking framework
80
81 class MockTaskRunner {
82 public:
83 MOCK_METHOD3(
84 PostDelayedTask,
85 bool(const tracked_objects::Location&, const Closure&, base::TimeDelta));
86 MOCK_METHOD3(
87 PostNonNestableDelayedTask,
88 bool(const tracked_objects::Location&, const Closure&, base::TimeDelta));
89 };
90
91 class TaskRunnerProxy : public base::SingleThreadTaskRunner {
92 public:
93 TaskRunnerProxy(MockTaskRunner* mock) : mock_(mock) {}
94 virtual bool RunsTasksOnCurrentThread() const OVERRIDE { return true; }
95 virtual bool PostDelayedTask(const tracked_objects::Location& location,
96 const Closure& closure,
97 base::TimeDelta delta) OVERRIDE {
98 return mock_->PostDelayedTask(location, closure, delta);
99 }
100 virtual bool PostNonNestableDelayedTask(
101 const tracked_objects::Location& location,
102 const Closure& closure,
103 base::TimeDelta delta) OVERRIDE {
104 return mock_->PostNonNestableDelayedTask(location, closure, delta);
105 }
106
107 private:
108 MockTaskRunner* mock_;
109 virtual ~TaskRunnerProxy() {}
110 };
111
112 TEST_F(StartupTaskRunnerTest, SynchronousExecution) {
113 MockTaskRunner mock_runner;
114 scoped_refptr<TaskRunnerProxy> proxy = new TaskRunnerProxy(&mock_runner);
115
116 EXPECT_CALL(mock_runner, PostDelayedTask(_, _, _)).Times(0);
117 EXPECT_CALL(mock_runner, PostNonNestableDelayedTask(_, _, _)).Times(0);
118
119 scoped_refptr<StartupTaskRunner> runner =
120 new StartupTaskRunner(false, Observer, proxy);
121
122 StartupTask task1 =
123 base::Bind(&StartupTaskRunnerTest::Task1, base::Unretained(this));
124 runner->AddTask(task1);
125 EXPECT_EQ(GetLastTask(), 0);
126 StartupTask task2 =
127 base::Bind(&StartupTaskRunnerTest::Task2, base::Unretained(this));
128 runner->AddTask(task2);
129
130 // Nothing should run until we tell them to.
131 EXPECT_EQ(GetLastTask(), 0);
132 runner->StartRunningTasks();
133
134 // On an immediate StartupTaskRunner the tasks should now all have run.
135 EXPECT_EQ(GetLastTask(), 2);
136
137 EXPECT_TRUE(observer_called);
138 EXPECT_EQ(observer_result, 0);
139 }
140
141 TEST_F(StartupTaskRunnerTest, SynchronousExecutionFailedTask) {
142 MockTaskRunner mock_runner;
143 scoped_refptr<TaskRunnerProxy> proxy = new TaskRunnerProxy(&mock_runner);
144
145 EXPECT_CALL(mock_runner, PostDelayedTask(_, _, _)).Times(0);
146 EXPECT_CALL(mock_runner, PostNonNestableDelayedTask(_, _, _)).Times(0);
147
148 scoped_refptr<StartupTaskRunner> runner =
149 new StartupTaskRunner(false, Observer, proxy);
150
151 StartupTask task3 =
152 base::Bind(&StartupTaskRunnerTest::FailingTask, base::Unretained(this));
153 runner->AddTask(task3);
154 EXPECT_EQ(GetLastTask(), 0);
155 StartupTask task2 =
156 base::Bind(&StartupTaskRunnerTest::Task2, base::Unretained(this));
157 runner->AddTask(task2);
158
159 // Nothing should run until we tell them to.
160 EXPECT_EQ(GetLastTask(), 0);
161 runner->StartRunningTasks();
162
163 // Only the first task should have run, since it failed
164 EXPECT_EQ(GetLastTask(), 3);
165
166 EXPECT_TRUE(observer_called);
167 EXPECT_EQ(observer_result, 1);
168 }
169
170 TEST_F(StartupTaskRunnerTest, AsynchronousExecution) {
171
172 MockTaskRunner mock_runner;
173 scoped_refptr<TaskRunnerProxy> proxy = new TaskRunnerProxy(&mock_runner);
174
175 EXPECT_CALL(mock_runner, PostDelayedTask(_, _, _)).Times(0);
176 EXPECT_CALL(
177 mock_runner,
178 PostNonNestableDelayedTask(_, _, base::TimeDelta::FromMilliseconds(0)))
179 .Times(testing::Between(2, 3))
180 .WillRepeatedly(WithArg<1>(Invoke(SaveTaskArg)));
181
182 scoped_refptr<StartupTaskRunner> runner =
183 new StartupTaskRunner(true, Observer, proxy);
184
185 StartupTask task1 =
186 base::Bind(&StartupTaskRunnerTest::Task1, base::Unretained(this));
187 runner->AddTask(task1);
188 StartupTask task2 =
189 base::Bind(&StartupTaskRunnerTest::Task2, base::Unretained(this));
190 runner->AddTask(task2);
191
192 // Nothing should run until we tell them to.
193 EXPECT_EQ(GetLastTask(), 0);
194 runner->StartRunningTasks();
195
196 // No tasks should have run yet, since we the message loop hasn't run.
197 EXPECT_EQ(GetLastTask(), 0);
198
199 // Fake the actual message loop. Each time a task is run a new task should
200 // be added to the queue, hence updating "task". The loop should actually run
201 // at most 3 times (once for each task plus possibly once for the observer),
202 // the "4" is a backstop.
203 for (int i = 0; i < 4 && !observer_called; i++) {
204 task.Run();
205 EXPECT_EQ(i + 1, GetLastTask());
206 }
207 EXPECT_TRUE(observer_called);
208 EXPECT_EQ(observer_result, 0);
209 }
210
211 TEST_F(StartupTaskRunnerTest, AsynchronousExecutionFailedTask) {
212
213 MockTaskRunner mock_runner;
214 scoped_refptr<TaskRunnerProxy> proxy = new TaskRunnerProxy(&mock_runner);
215
216 EXPECT_CALL(mock_runner, PostDelayedTask(_, _, _)).Times(0);
217 EXPECT_CALL(
218 mock_runner,
219 PostNonNestableDelayedTask(_, _, base::TimeDelta::FromMilliseconds(0)))
220 .Times(testing::Between(1, 2))
221 .WillRepeatedly(WithArg<1>(Invoke(SaveTaskArg)));
222
223 scoped_refptr<StartupTaskRunner> runner =
224 new StartupTaskRunner(true, Observer, proxy);
225
226 StartupTask task3 =
227 base::Bind(&StartupTaskRunnerTest::FailingTask, base::Unretained(this));
228 runner->AddTask(task3);
229 StartupTask task2 =
230 base::Bind(&StartupTaskRunnerTest::Task2, base::Unretained(this));
231 runner->AddTask(task2);
232
233 // Nothing should run until we tell them to.
234 EXPECT_EQ(GetLastTask(), 0);
235 runner->StartRunningTasks();
236
237 // No tasks should have run yet, since we the message loop hasn't run.
238 EXPECT_EQ(GetLastTask(), 0);
239
240 // Fake the actual message loop. Each time a task is run a new task should
241 // be added to the queue, hence updating "task". The loop should actually run
242 // at most twice (once for the failed task plus possibly once for the
243 // observer), the "4" is a backstop.
244 for (int i = 0; i < 4 && !observer_called; i++) {
245 task.Run();
246 }
247 EXPECT_EQ(GetLastTask(), 3);
248
249 EXPECT_TRUE(observer_called);
250 EXPECT_EQ(observer_result, 1);
251 }
252 } // namespace
253 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698