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 | 29 // Tasks posted through base/task_scheduler/post_task.h run on dedicated threads |
30 // threads. If ExecutionControlMode is QUEUED, they run when RunUntilIdle() or | |
31 // ~ScopedTaskEnvironment is called. If ExecutionControlMode is ASYNC, they run | |
32 // as they are posted. | 30 // as they are posted. |
33 // | 31 // |
34 // All methods of ScopedTaskEnvironment must be called from the same thread. | 32 // All methods of ScopedTaskEnvironment must be called from the same thread. |
35 // | 33 // |
36 // Usage: | 34 // Usage: |
37 // | 35 // |
38 // class MyTestFixture : public testing::Test { | 36 // class MyTestFixture : public testing::Test { |
39 // public: | 37 // public: |
40 // (...) | 38 // (...) |
41 // | 39 // |
(...skipping 14 matching lines...) Expand all Loading... |
56 public: | 54 public: |
57 enum class MainThreadType { | 55 enum class MainThreadType { |
58 // The main thread doesn't pump system messages. | 56 // The main thread doesn't pump system messages. |
59 DEFAULT, | 57 DEFAULT, |
60 // The main thread pumps UI messages. | 58 // The main thread pumps UI messages. |
61 UI, | 59 UI, |
62 // The main thread pumps asynchronous IO messages. | 60 // The main thread pumps asynchronous IO messages. |
63 IO, | 61 IO, |
64 }; | 62 }; |
65 | 63 |
66 enum class ExecutionControlMode { | |
67 // Tasks are queued and executed when RunUntilIdle() is called. | |
68 QUEUED, | |
69 // Tasks run as they are posted. | |
70 ASYNC, | |
71 }; | |
72 | |
73 ScopedTaskEnvironment( | 64 ScopedTaskEnvironment( |
74 MainThreadType main_thread_type = MainThreadType::DEFAULT, | 65 MainThreadType main_thread_type = MainThreadType::DEFAULT); |
75 ExecutionControlMode execution_control_mode = | |
76 ExecutionControlMode::ASYNC); | |
77 | 66 |
78 // Waits until no undelayed TaskScheduler tasks remain. Then, unregisters the | 67 // Waits until no undelayed TaskScheduler tasks remain. Then, unregisters the |
79 // TaskScheduler and the (Thread|Sequenced)TaskRunnerHandle. | 68 // TaskScheduler and the (Thread|Sequenced)TaskRunnerHandle. |
80 ~ScopedTaskEnvironment(); | 69 ~ScopedTaskEnvironment(); |
81 | 70 |
82 // Returns a TaskRunner that schedules tasks on the main thread. | 71 // Returns a TaskRunner that schedules tasks on the main thread. |
83 scoped_refptr<base::SingleThreadTaskRunner> GetMainThreadTaskRunner(); | 72 scoped_refptr<base::SingleThreadTaskRunner> GetMainThreadTaskRunner(); |
84 | 73 |
85 // Runs tasks until both the (Thread|Sequenced)TaskRunnerHandle and the | 74 // Synchronously runs (Thread|Sequenced)TaskRunnerHandle tasks until no |
86 // TaskScheduler queues are empty. | 75 // undelayed (Thread|Sequenced)TaskRunnerHandle or TaskScheduler tasks remain. |
87 void RunUntilIdle(); | 76 void RunUntilIdle(); |
88 | 77 |
89 private: | 78 private: |
90 class TestTaskTracker; | |
91 | |
92 const ExecutionControlMode execution_control_mode_; | |
93 | |
94 // Note: |message_loop_| is an implementation detail and will be replaced in | 79 // Note: |message_loop_| is an implementation detail and will be replaced in |
95 // the future, do NOT rely on the presence of a MessageLoop beyond | 80 // the future, do NOT rely on the presence of a MessageLoop beyond |
96 // (Thread|Sequenced)TaskRunnerHandle and RunLoop. | 81 // (Thread|Sequenced)TaskRunnerHandle and RunLoop. |
97 MessageLoop message_loop_; | 82 MessageLoop message_loop_; |
98 | 83 |
99 const TaskScheduler* task_scheduler_ = nullptr; | 84 const TaskScheduler* task_scheduler_ = nullptr; |
100 | 85 |
101 // Owned by |task_scheduler_|. | |
102 TestTaskTracker* const task_tracker_; | |
103 | |
104 DISALLOW_COPY_AND_ASSIGN(ScopedTaskEnvironment); | 86 DISALLOW_COPY_AND_ASSIGN(ScopedTaskEnvironment); |
105 }; | 87 }; |
106 | 88 |
107 } // namespace test | 89 } // namespace test |
108 } // namespace base | 90 } // namespace base |
109 | 91 |
110 #endif // BASE_TEST_SCOPED_ASYNC_TASK_SCHEDULER_H_ | 92 #endif // BASE_TEST_SCOPED_ASYNC_TASK_SCHEDULER_H_ |
OLD | NEW |