OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 #ifndef BASE_TEST_SCOPED_TASK_ENVIRONMENT_H_ | 5 #ifndef BASE_TEST_SCOPED_TASK_ENVIRONMENT_H_ |
6 #define BASE_TEST_SCOPED_TASK_ENVIRONMENT_H_ | 6 #define BASE_TEST_SCOPED_TASK_ENVIRONMENT_H_ |
7 | 7 |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 | 13 |
14 class TaskScheduler; | 14 class TaskScheduler; |
15 | 15 |
16 namespace test { | 16 namespace test { |
17 | 17 |
18 // ScopedTaskEnvironment allows usage of these APIs within its scope: | 18 // ScopedTaskEnvironment allows usage of these APIs within its scope: |
19 // - (Thread|Sequenced)TaskRunnerHandle, on the thread where it lives | 19 // - (Thread|Sequenced)TaskRunnerHandle, on the thread where it lives |
20 // - base/task_scheduler/post_task.h, on any thread | 20 // - base/task_scheduler/post_task.h, on any thread |
21 // | 21 // |
22 // Tests that need either of these APIs should instantiate a | 22 // Tests that need either of these APIs should instantiate a |
23 // ScopedTaskEnvironment. | 23 // ScopedTaskEnvironment. |
24 // | 24 // |
25 // Tasks posted to the (Thread|Sequenced)TaskRunnerHandle run synchronously when | 25 // Tasks posted to the (Thread|Sequenced)TaskRunnerHandle run synchronously when |
26 // RunLoop::Run(UntilIdle) or ScopedTaskEnvironment::RunUntilIdle is called on | 26 // RunLoop::Run(UntilIdle) or ScopedTaskEnvironment::RunUntilIdle is called on |
27 // the thread where the ScopedTaskEnvironment lives. | 27 // the thread where the ScopedTaskEnvironment lives. |
28 // | 28 // |
29 // Tasks posted through base/task_scheduler/post_task.h run on dedicated threads | 29 // Tasks posted through base/task_scheduler/post_task.h run on dedicated |
| 30 // threads. If ExecutionMode is QUEUED, they run when RunUntilIdle() or |
| 31 // ~ScopedTaskEnvironment is called. If ExecutionMode is ASYNC, they run |
30 // as they are posted. | 32 // as they are posted. |
31 // | 33 // |
32 // All methods of ScopedTaskEnvironment must be called from the same thread. | 34 // All methods of ScopedTaskEnvironment must be called from the same thread. |
33 // | 35 // |
34 // Usage: | 36 // Usage: |
35 // | 37 // |
36 // class MyTestFixture : public testing::Test { | 38 // class MyTestFixture : public testing::Test { |
37 // public: | 39 // public: |
38 // (...) | 40 // (...) |
39 // | 41 // |
(...skipping 14 matching lines...) Expand all Loading... |
54 public: | 56 public: |
55 enum class MainThreadType { | 57 enum class MainThreadType { |
56 // The main thread doesn't pump system messages. | 58 // The main thread doesn't pump system messages. |
57 DEFAULT, | 59 DEFAULT, |
58 // The main thread pumps UI messages. | 60 // The main thread pumps UI messages. |
59 UI, | 61 UI, |
60 // The main thread pumps asynchronous IO messages. | 62 // The main thread pumps asynchronous IO messages. |
61 IO, | 63 IO, |
62 }; | 64 }; |
63 | 65 |
| 66 enum class ExecutionMode { |
| 67 // Tasks are queued and only executed when RunUntilIdle() is explicitly |
| 68 // called. |
| 69 QUEUED, |
| 70 // Tasks run as they are posted. RunUntilIdle() can still be used to block |
| 71 // until done. |
| 72 ASYNC, |
| 73 }; |
| 74 |
64 ScopedTaskEnvironment( | 75 ScopedTaskEnvironment( |
65 MainThreadType main_thread_type = MainThreadType::DEFAULT); | 76 MainThreadType main_thread_type = MainThreadType::DEFAULT, |
| 77 ExecutionMode execution_control_mode = ExecutionMode::ASYNC); |
66 | 78 |
67 // Waits until no undelayed TaskScheduler tasks remain. Then, unregisters the | 79 // Waits until no undelayed TaskScheduler tasks remain. Then, unregisters the |
68 // TaskScheduler and the (Thread|Sequenced)TaskRunnerHandle. | 80 // TaskScheduler and the (Thread|Sequenced)TaskRunnerHandle. |
69 ~ScopedTaskEnvironment(); | 81 ~ScopedTaskEnvironment(); |
70 | 82 |
71 // Returns a TaskRunner that schedules tasks on the main thread. | 83 // Returns a TaskRunner that schedules tasks on the main thread. |
72 scoped_refptr<base::SingleThreadTaskRunner> GetMainThreadTaskRunner(); | 84 scoped_refptr<base::SingleThreadTaskRunner> GetMainThreadTaskRunner(); |
73 | 85 |
74 // Synchronously runs (Thread|Sequenced)TaskRunnerHandle tasks until no | 86 // Runs tasks until both the (Thread|Sequenced)TaskRunnerHandle and the |
75 // undelayed (Thread|Sequenced)TaskRunnerHandle or TaskScheduler tasks remain. | 87 // TaskScheduler queues are empty. |
76 void RunUntilIdle(); | 88 void RunUntilIdle(); |
77 | 89 |
78 private: | 90 private: |
| 91 class TestTaskTracker; |
| 92 |
| 93 const ExecutionMode execution_control_mode_; |
| 94 |
79 // Note: |message_loop_| is an implementation detail and will be replaced in | 95 // Note: |message_loop_| is an implementation detail and will be replaced in |
80 // the future, do NOT rely on the presence of a MessageLoop beyond | 96 // the future, do NOT rely on the presence of a MessageLoop beyond |
81 // (Thread|Sequenced)TaskRunnerHandle and RunLoop. | 97 // (Thread|Sequenced)TaskRunnerHandle and RunLoop. |
82 MessageLoop message_loop_; | 98 MessageLoop message_loop_; |
83 | 99 |
84 const TaskScheduler* task_scheduler_ = nullptr; | 100 const TaskScheduler* task_scheduler_ = nullptr; |
85 | 101 |
| 102 // Owned by |task_scheduler_|. |
| 103 TestTaskTracker* const task_tracker_; |
| 104 |
86 DISALLOW_COPY_AND_ASSIGN(ScopedTaskEnvironment); | 105 DISALLOW_COPY_AND_ASSIGN(ScopedTaskEnvironment); |
87 }; | 106 }; |
88 | 107 |
89 } // namespace test | 108 } // namespace test |
90 } // namespace base | 109 } // namespace base |
91 | 110 |
92 #endif // BASE_TEST_SCOPED_ASYNC_TASK_SCHEDULER_H_ | 111 #endif // BASE_TEST_SCOPED_ASYNC_TASK_SCHEDULER_H_ |
OLD | NEW |