Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
alokp | 42544a8 | 2016-04-12 01:53:49 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_TEST_TEST_MESSAGE_LOOP_H_ |
| 6 | #define BASE_TEST_TEST_MESSAGE_LOOP_H_ |
| 7 | |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 8 | #include "base/message_loop/message_pump_type.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 9 | #include "base/task/single_thread_task_runner.h" |
Carlos Caballero | f5b6f91c | 2019-12-20 14:07:38 | [diff] [blame] | 10 | #include "base/test/task_environment.h" |
alokp | 42544a8 | 2016-04-12 01:53:49 | [diff] [blame] | 11 | |
| 12 | namespace base { |
| 13 | |
| 14 | // TestMessageLoop is a convenience class for unittests that need to create a |
| 15 | // message loop without a real thread backing it. For most tests, |
| 16 | // it is sufficient to just instantiate TestMessageLoop as a member variable. |
| 17 | // |
| 18 | // TestMessageLoop will attempt to drain the underlying MessageLoop on |
| 19 | // destruction for clean teardown of tests. |
Carlos Caballero | f5b6f91c | 2019-12-20 14:07:38 | [diff] [blame] | 20 | // |
| 21 | // TODO(b/891670): Get rid of this and migrate users to |
| 22 | // SingleThreadTaskEnvironment |
alokp | 42544a8 | 2016-04-12 01:53:49 | [diff] [blame] | 23 | class TestMessageLoop { |
| 24 | public: |
| 25 | TestMessageLoop(); |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 26 | explicit TestMessageLoop(MessagePumpType type); |
alokp | 42544a8 | 2016-04-12 01:53:49 | [diff] [blame] | 27 | ~TestMessageLoop(); |
| 28 | |
Alexander Timin | 7dc2d04 | 2018-11-16 12:27:15 | [diff] [blame] | 29 | scoped_refptr<SingleThreadTaskRunner> task_runner() { |
Carlos Caballero | f5b6f91c | 2019-12-20 14:07:38 | [diff] [blame] | 30 | return task_environment_.GetMainThreadTaskRunner(); |
danakj | 8eb035c0 | 2016-06-17 22:41:32 | [diff] [blame] | 31 | } |
| 32 | |
alokp | 42544a8 | 2016-04-12 01:53:49 | [diff] [blame] | 33 | private: |
Carlos Caballero | f5b6f91c | 2019-12-20 14:07:38 | [diff] [blame] | 34 | test::SingleThreadTaskEnvironment task_environment_; |
alokp | 42544a8 | 2016-04-12 01:53:49 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | } // namespace base |
| 38 | |
| 39 | #endif // BASE_TEST_TEST_MESSAGE_LOOP_H_ |