OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "base/test/scoped_task_scheduler.h" | 5 #include "base/test/scoped_task_scheduler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 | 250 |
251 RunLoop().RunUntilIdle(); | 251 RunLoop().RunUntilIdle(); |
252 | 252 |
253 EXPECT_TRUE(com_task_ran); | 253 EXPECT_TRUE(com_task_ran); |
254 } | 254 } |
255 #endif // defined(OS_WIN) | 255 #endif // defined(OS_WIN) |
256 | 256 |
257 TEST(ScopedTaskSchedulerTest, NonBlockShutdownTasksPostedAfterShutdownDontRun) { | 257 TEST(ScopedTaskSchedulerTest, NonBlockShutdownTasksPostedAfterShutdownDontRun) { |
258 ScopedTaskScheduler scoped_task_scheduler; | 258 ScopedTaskScheduler scoped_task_scheduler; |
259 TaskScheduler::GetInstance()->Shutdown(); | 259 TaskScheduler::GetInstance()->Shutdown(); |
260 PostTaskWithTraits(FROM_HERE, | 260 PostTaskWithTraits(FROM_HERE, {TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, |
261 TaskTraits().WithShutdownBehavior( | |
262 TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN), | |
263 BindOnce([]() { | 261 BindOnce([]() { |
264 ADD_FAILURE() | 262 ADD_FAILURE() |
265 << "CONTINUE_ON_SHUTDOWN task should not run"; | 263 << "CONTINUE_ON_SHUTDOWN task should not run"; |
266 })); | 264 })); |
267 PostTaskWithTraits( | 265 PostTaskWithTraits(FROM_HERE, {TaskShutdownBehavior::SKIP_ON_SHUTDOWN}, |
268 FROM_HERE, | 266 BindOnce([]() { |
269 TaskTraits().WithShutdownBehavior(TaskShutdownBehavior::SKIP_ON_SHUTDOWN), | 267 ADD_FAILURE() << "SKIP_ON_SHUTDOWN task should not run"; |
270 BindOnce( | 268 })); |
271 []() { ADD_FAILURE() << "SKIP_ON_SHUTDOWN task should not run"; })); | |
272 | 269 |
273 // This should not run anything. | 270 // This should not run anything. |
274 RunLoop().RunUntilIdle(); | 271 RunLoop().RunUntilIdle(); |
275 } | 272 } |
276 | 273 |
277 TEST(ScopedTaskSchedulerTest, DestructorRunsBlockShutdownTasksOnly) { | 274 TEST(ScopedTaskSchedulerTest, DestructorRunsBlockShutdownTasksOnly) { |
278 bool block_shutdown_task_ran = false; | 275 bool block_shutdown_task_ran = false; |
279 { | 276 { |
280 ScopedTaskScheduler scoped_task_scheduler; | 277 ScopedTaskScheduler scoped_task_scheduler; |
281 PostTaskWithTraits(FROM_HERE, | 278 PostTaskWithTraits(FROM_HERE, {TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, |
282 TaskTraits().WithShutdownBehavior( | |
283 TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN), | |
284 BindOnce([]() { | 279 BindOnce([]() { |
285 ADD_FAILURE() | 280 ADD_FAILURE() |
286 << "CONTINUE_ON_SHUTDOWN task should not run"; | 281 << "CONTINUE_ON_SHUTDOWN task should not run"; |
287 })); | 282 })); |
288 PostTaskWithTraits(FROM_HERE, | 283 PostTaskWithTraits(FROM_HERE, {TaskShutdownBehavior::SKIP_ON_SHUTDOWN}, |
289 TaskTraits().WithShutdownBehavior( | |
290 TaskShutdownBehavior::SKIP_ON_SHUTDOWN), | |
291 BindOnce([]() { | 284 BindOnce([]() { |
292 ADD_FAILURE() | 285 ADD_FAILURE() |
293 << "SKIP_ON_SHUTDOWN task should not run"; | 286 << "SKIP_ON_SHUTDOWN task should not run"; |
294 })); | 287 })); |
295 PostTaskWithTraits( | 288 PostTaskWithTraits(FROM_HERE, {TaskShutdownBehavior::BLOCK_SHUTDOWN}, |
296 FROM_HERE, | 289 BindOnce( |
297 TaskTraits().WithShutdownBehavior(TaskShutdownBehavior::BLOCK_SHUTDOWN), | 290 [](bool* block_shutdown_task_ran) { |
298 BindOnce( | 291 *block_shutdown_task_ran = true; |
299 [](bool* block_shutdown_task_ran) { | 292 }, |
300 *block_shutdown_task_ran = true; | 293 Unretained(&block_shutdown_task_ran))); |
301 }, | |
302 Unretained(&block_shutdown_task_ran))); | |
303 } | 294 } |
304 EXPECT_TRUE(block_shutdown_task_ran); | 295 EXPECT_TRUE(block_shutdown_task_ran); |
305 } | 296 } |
306 | 297 |
307 TEST(ScopedTaskSchedulerTest, ReassignCurrentTaskRunner) { | 298 TEST(ScopedTaskSchedulerTest, ReassignCurrentTaskRunner) { |
308 bool first_task_ran = false; | 299 bool first_task_ran = false; |
309 bool second_task_ran = false; | 300 bool second_task_ran = false; |
310 | 301 |
311 auto TestTaskRan = [](bool* task_ran) { *task_ran = true; }; | 302 auto TestTaskRan = [](bool* task_ran) { *task_ran = true; }; |
312 | 303 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 BindOnce([](bool* task_ran) { *task_ran = true; }, | 336 BindOnce([](bool* task_ran) { *task_ran = true; }, |
346 Unretained(task_ran))); | 337 Unretained(task_ran))); |
347 }, | 338 }, |
348 Unretained(&task_ran))); | 339 Unretained(&task_ran))); |
349 RunLoop().RunUntilIdle(); | 340 RunLoop().RunUntilIdle(); |
350 EXPECT_TRUE(task_ran); | 341 EXPECT_TRUE(task_ran); |
351 } | 342 } |
352 | 343 |
353 } // namespace test | 344 } // namespace test |
354 } // namespace base | 345 } // namespace base |
OLD | NEW |