OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/startup_task_runner.h" | 5 #include "content/browser/startup_task_runner.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 virtual ~TaskRunnerProxy() {} | 109 virtual ~TaskRunnerProxy() {} |
110 }; | 110 }; |
111 | 111 |
112 TEST_F(StartupTaskRunnerTest, SynchronousExecution) { | 112 TEST_F(StartupTaskRunnerTest, SynchronousExecution) { |
113 MockTaskRunner mock_runner; | 113 MockTaskRunner mock_runner; |
114 scoped_refptr<TaskRunnerProxy> proxy = new TaskRunnerProxy(&mock_runner); | 114 scoped_refptr<TaskRunnerProxy> proxy = new TaskRunnerProxy(&mock_runner); |
115 | 115 |
116 EXPECT_CALL(mock_runner, PostDelayedTask(_, _, _)).Times(0); | 116 EXPECT_CALL(mock_runner, PostDelayedTask(_, _, _)).Times(0); |
117 EXPECT_CALL(mock_runner, PostNonNestableDelayedTask(_, _, _)).Times(0); | 117 EXPECT_CALL(mock_runner, PostNonNestableDelayedTask(_, _, _)).Times(0); |
118 | 118 |
119 scoped_refptr<StartupTaskRunner> runner = | 119 StartupTaskRunner runner(base::Bind(&Observer), proxy); |
120 new StartupTaskRunner(false, base::Bind(&Observer), proxy); | |
121 | 120 |
122 StartupTask task1 = | 121 StartupTask task1 = |
123 base::Bind(&StartupTaskRunnerTest::Task1, base::Unretained(this)); | 122 base::Bind(&StartupTaskRunnerTest::Task1, base::Unretained(this)); |
124 runner->AddTask(task1); | 123 runner.AddTask(task1); |
125 EXPECT_EQ(GetLastTask(), 0); | 124 EXPECT_EQ(GetLastTask(), 0); |
126 StartupTask task2 = | 125 StartupTask task2 = |
127 base::Bind(&StartupTaskRunnerTest::Task2, base::Unretained(this)); | 126 base::Bind(&StartupTaskRunnerTest::Task2, base::Unretained(this)); |
128 runner->AddTask(task2); | 127 runner.AddTask(task2); |
129 | 128 |
130 // Nothing should run until we tell them to. | 129 // Nothing should run until we tell them to. |
131 EXPECT_EQ(GetLastTask(), 0); | 130 EXPECT_EQ(GetLastTask(), 0); |
132 runner->StartRunningTasks(); | 131 runner.RunAllTasksNow(); |
133 | 132 |
134 // On an immediate StartupTaskRunner the tasks should now all have run. | 133 // On an immediate StartupTaskRunner the tasks should now all have run. |
135 EXPECT_EQ(GetLastTask(), 2); | 134 EXPECT_EQ(GetLastTask(), 2); |
136 | 135 |
137 EXPECT_TRUE(observer_called); | 136 EXPECT_TRUE(observer_called); |
138 EXPECT_EQ(observer_result, 0); | 137 EXPECT_EQ(observer_result, 0); |
139 } | 138 } |
140 | 139 |
141 TEST_F(StartupTaskRunnerTest, NullObserver) { | 140 TEST_F(StartupTaskRunnerTest, NullObserver) { |
142 MockTaskRunner mock_runner; | 141 MockTaskRunner mock_runner; |
143 scoped_refptr<TaskRunnerProxy> proxy = new TaskRunnerProxy(&mock_runner); | 142 scoped_refptr<TaskRunnerProxy> proxy = new TaskRunnerProxy(&mock_runner); |
144 | 143 |
145 EXPECT_CALL(mock_runner, PostDelayedTask(_, _, _)).Times(0); | 144 EXPECT_CALL(mock_runner, PostDelayedTask(_, _, _)).Times(0); |
146 EXPECT_CALL(mock_runner, PostNonNestableDelayedTask(_, _, _)).Times(0); | 145 EXPECT_CALL(mock_runner, PostNonNestableDelayedTask(_, _, _)).Times(0); |
147 | 146 |
148 scoped_refptr<StartupTaskRunner> runner = | 147 StartupTaskRunner runner(base::Callback<void(int)>(), proxy); |
149 new StartupTaskRunner(false, base::Callback<void(int)>(), proxy); | |
150 | 148 |
151 StartupTask task1 = | 149 StartupTask task1 = |
152 base::Bind(&StartupTaskRunnerTest::Task1, base::Unretained(this)); | 150 base::Bind(&StartupTaskRunnerTest::Task1, base::Unretained(this)); |
153 runner->AddTask(task1); | 151 runner.AddTask(task1); |
154 EXPECT_EQ(GetLastTask(), 0); | 152 EXPECT_EQ(GetLastTask(), 0); |
155 StartupTask task2 = | 153 StartupTask task2 = |
156 base::Bind(&StartupTaskRunnerTest::Task2, base::Unretained(this)); | 154 base::Bind(&StartupTaskRunnerTest::Task2, base::Unretained(this)); |
157 runner->AddTask(task2); | 155 runner.AddTask(task2); |
158 | 156 |
159 // Nothing should run until we tell them to. | 157 // Nothing should run until we tell them to. |
160 EXPECT_EQ(GetLastTask(), 0); | 158 EXPECT_EQ(GetLastTask(), 0); |
161 runner->StartRunningTasks(); | 159 runner.RunAllTasksNow(); |
162 | 160 |
163 // On an immediate StartupTaskRunner the tasks should now all have run. | 161 // On an immediate StartupTaskRunner the tasks should now all have run. |
164 EXPECT_EQ(GetLastTask(), 2); | 162 EXPECT_EQ(GetLastTask(), 2); |
165 | 163 |
166 EXPECT_FALSE(observer_called); | 164 EXPECT_FALSE(observer_called); |
167 } | 165 } |
168 | 166 |
169 TEST_F(StartupTaskRunnerTest, SynchronousExecutionFailedTask) { | 167 TEST_F(StartupTaskRunnerTest, SynchronousExecutionFailedTask) { |
170 MockTaskRunner mock_runner; | 168 MockTaskRunner mock_runner; |
171 scoped_refptr<TaskRunnerProxy> proxy = new TaskRunnerProxy(&mock_runner); | 169 scoped_refptr<TaskRunnerProxy> proxy = new TaskRunnerProxy(&mock_runner); |
172 | 170 |
173 EXPECT_CALL(mock_runner, PostDelayedTask(_, _, _)).Times(0); | 171 EXPECT_CALL(mock_runner, PostDelayedTask(_, _, _)).Times(0); |
174 EXPECT_CALL(mock_runner, PostNonNestableDelayedTask(_, _, _)).Times(0); | 172 EXPECT_CALL(mock_runner, PostNonNestableDelayedTask(_, _, _)).Times(0); |
175 | 173 |
176 scoped_refptr<StartupTaskRunner> runner = | 174 StartupTaskRunner runner(base::Bind(&Observer), proxy); |
177 new StartupTaskRunner(false, base::Bind(&Observer), proxy); | |
178 | 175 |
179 StartupTask task3 = | 176 StartupTask task3 = |
180 base::Bind(&StartupTaskRunnerTest::FailingTask, base::Unretained(this)); | 177 base::Bind(&StartupTaskRunnerTest::FailingTask, base::Unretained(this)); |
181 runner->AddTask(task3); | 178 runner.AddTask(task3); |
182 EXPECT_EQ(GetLastTask(), 0); | 179 EXPECT_EQ(GetLastTask(), 0); |
183 StartupTask task2 = | 180 StartupTask task2 = |
184 base::Bind(&StartupTaskRunnerTest::Task2, base::Unretained(this)); | 181 base::Bind(&StartupTaskRunnerTest::Task2, base::Unretained(this)); |
185 runner->AddTask(task2); | 182 runner.AddTask(task2); |
186 | 183 |
187 // Nothing should run until we tell them to. | 184 // Nothing should run until we tell them to. |
188 EXPECT_EQ(GetLastTask(), 0); | 185 EXPECT_EQ(GetLastTask(), 0); |
189 runner->StartRunningTasks(); | 186 runner.RunAllTasksNow(); |
190 | 187 |
191 // Only the first task should have run, since it failed | 188 // Only the first task should have run, since it failed |
192 EXPECT_EQ(GetLastTask(), 3); | 189 EXPECT_EQ(GetLastTask(), 3); |
193 | 190 |
194 EXPECT_TRUE(observer_called); | 191 EXPECT_TRUE(observer_called); |
195 EXPECT_EQ(observer_result, 1); | 192 EXPECT_EQ(observer_result, 1); |
196 } | 193 } |
197 | 194 |
198 TEST_F(StartupTaskRunnerTest, AsynchronousExecution) { | 195 TEST_F(StartupTaskRunnerTest, AsynchronousExecution) { |
199 | 196 |
200 MockTaskRunner mock_runner; | 197 MockTaskRunner mock_runner; |
201 scoped_refptr<TaskRunnerProxy> proxy = new TaskRunnerProxy(&mock_runner); | 198 scoped_refptr<TaskRunnerProxy> proxy = new TaskRunnerProxy(&mock_runner); |
202 | 199 |
203 EXPECT_CALL(mock_runner, PostDelayedTask(_, _, _)).Times(0); | 200 EXPECT_CALL(mock_runner, PostDelayedTask(_, _, _)).Times(0); |
204 EXPECT_CALL( | 201 EXPECT_CALL( |
205 mock_runner, | 202 mock_runner, |
206 PostNonNestableDelayedTask(_, _, base::TimeDelta::FromMilliseconds(0))) | 203 PostNonNestableDelayedTask(_, _, base::TimeDelta::FromMilliseconds(0))) |
207 .Times(testing::Between(2, 3)) | 204 .Times(testing::Between(2, 3)) |
208 .WillRepeatedly(WithArg<1>(Invoke(SaveTaskArg))); | 205 .WillRepeatedly(WithArg<1>(Invoke(SaveTaskArg))); |
209 | 206 |
210 scoped_refptr<StartupTaskRunner> runner = | 207 StartupTaskRunner runner(base::Bind(&Observer), proxy); |
211 new StartupTaskRunner(true, base::Bind(&Observer), proxy); | |
212 | 208 |
213 StartupTask task1 = | 209 StartupTask task1 = |
214 base::Bind(&StartupTaskRunnerTest::Task1, base::Unretained(this)); | 210 base::Bind(&StartupTaskRunnerTest::Task1, base::Unretained(this)); |
215 runner->AddTask(task1); | 211 runner.AddTask(task1); |
216 StartupTask task2 = | 212 StartupTask task2 = |
217 base::Bind(&StartupTaskRunnerTest::Task2, base::Unretained(this)); | 213 base::Bind(&StartupTaskRunnerTest::Task2, base::Unretained(this)); |
218 runner->AddTask(task2); | 214 runner.AddTask(task2); |
219 | 215 |
220 // Nothing should run until we tell them to. | 216 // Nothing should run until we tell them to. |
221 EXPECT_EQ(GetLastTask(), 0); | 217 EXPECT_EQ(GetLastTask(), 0); |
222 runner->StartRunningTasks(); | 218 runner.StartRunningTasksAsync(); |
223 | 219 |
224 // No tasks should have run yet, since we the message loop hasn't run. | 220 // No tasks should have run yet, since we the message loop hasn't run. |
225 EXPECT_EQ(GetLastTask(), 0); | 221 EXPECT_EQ(GetLastTask(), 0); |
226 | 222 |
227 // Fake the actual message loop. Each time a task is run a new task should | 223 // Fake the actual message loop. Each time a task is run a new task should |
228 // be added to the queue, hence updating "task". The loop should actually run | 224 // be added to the queue, hence updating "task". The loop should actually run |
229 // at most 3 times (once for each task plus possibly once for the observer), | 225 // at most 3 times (once for each task plus possibly once for the observer), |
230 // the "4" is a backstop. | 226 // the "4" is a backstop. |
231 for (int i = 0; i < 4 && !observer_called; i++) { | 227 for (int i = 0; i < 4 && !observer_called; i++) { |
232 task.Run(); | 228 task.Run(); |
233 EXPECT_EQ(i + 1, GetLastTask()); | 229 EXPECT_EQ(i + 1, GetLastTask()); |
234 } | 230 } |
235 EXPECT_TRUE(observer_called); | 231 EXPECT_TRUE(observer_called); |
236 EXPECT_EQ(observer_result, 0); | 232 EXPECT_EQ(observer_result, 0); |
237 } | 233 } |
238 | 234 |
239 TEST_F(StartupTaskRunnerTest, AsynchronousExecutionFailedTask) { | 235 TEST_F(StartupTaskRunnerTest, AsynchronousExecutionFailedTask) { |
240 | 236 |
241 MockTaskRunner mock_runner; | 237 MockTaskRunner mock_runner; |
242 scoped_refptr<TaskRunnerProxy> proxy = new TaskRunnerProxy(&mock_runner); | 238 scoped_refptr<TaskRunnerProxy> proxy = new TaskRunnerProxy(&mock_runner); |
243 | 239 |
244 EXPECT_CALL(mock_runner, PostDelayedTask(_, _, _)).Times(0); | 240 EXPECT_CALL(mock_runner, PostDelayedTask(_, _, _)).Times(0); |
245 EXPECT_CALL( | 241 EXPECT_CALL( |
246 mock_runner, | 242 mock_runner, |
247 PostNonNestableDelayedTask(_, _, base::TimeDelta::FromMilliseconds(0))) | 243 PostNonNestableDelayedTask(_, _, base::TimeDelta::FromMilliseconds(0))) |
248 .Times(testing::Between(1, 2)) | 244 .Times(testing::Between(1, 2)) |
249 .WillRepeatedly(WithArg<1>(Invoke(SaveTaskArg))); | 245 .WillRepeatedly(WithArg<1>(Invoke(SaveTaskArg))); |
250 | 246 |
251 scoped_refptr<StartupTaskRunner> runner = | 247 StartupTaskRunner runner(base::Bind(&Observer), proxy); |
252 new StartupTaskRunner(true, base::Bind(&Observer), proxy); | |
253 | 248 |
254 StartupTask task3 = | 249 StartupTask task3 = |
255 base::Bind(&StartupTaskRunnerTest::FailingTask, base::Unretained(this)); | 250 base::Bind(&StartupTaskRunnerTest::FailingTask, base::Unretained(this)); |
256 runner->AddTask(task3); | 251 runner.AddTask(task3); |
257 StartupTask task2 = | 252 StartupTask task2 = |
258 base::Bind(&StartupTaskRunnerTest::Task2, base::Unretained(this)); | 253 base::Bind(&StartupTaskRunnerTest::Task2, base::Unretained(this)); |
259 runner->AddTask(task2); | 254 runner.AddTask(task2); |
260 | 255 |
261 // Nothing should run until we tell them to. | 256 // Nothing should run until we tell them to. |
262 EXPECT_EQ(GetLastTask(), 0); | 257 EXPECT_EQ(GetLastTask(), 0); |
263 runner->StartRunningTasks(); | 258 runner.StartRunningTasksAsync(); |
264 | 259 |
265 // No tasks should have run yet, since we the message loop hasn't run. | 260 // No tasks should have run yet, since we the message loop hasn't run. |
266 EXPECT_EQ(GetLastTask(), 0); | 261 EXPECT_EQ(GetLastTask(), 0); |
267 | 262 |
268 // Fake the actual message loop. Each time a task is run a new task should | 263 // Fake the actual message loop. Each time a task is run a new task should |
269 // be added to the queue, hence updating "task". The loop should actually run | 264 // be added to the queue, hence updating "task". The loop should actually run |
270 // at most twice (once for the failed task plus possibly once for the | 265 // at most twice (once for the failed task plus possibly once for the |
271 // observer), the "4" is a backstop. | 266 // observer), the "4" is a backstop. |
272 for (int i = 0; i < 4 && !observer_called; i++) { | 267 for (int i = 0; i < 4 && !observer_called; i++) { |
273 task.Run(); | 268 task.Run(); |
274 } | 269 } |
275 EXPECT_EQ(GetLastTask(), 3); | 270 EXPECT_EQ(GetLastTask(), 3); |
276 | 271 |
277 EXPECT_TRUE(observer_called); | 272 EXPECT_TRUE(observer_called); |
278 EXPECT_EQ(observer_result, 1); | 273 EXPECT_EQ(observer_result, 1); |
279 } | 274 } |
280 } // namespace | 275 } // namespace |
281 } // namespace content | 276 } // namespace content |
OLD | NEW |