xref: /aosp_15_r20/external/cronet/base/test/test_message_loop.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2016 The Chromium Authors
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 
8 #include "base/message_loop/message_pump_type.h"
9 #include "base/task/single_thread_task_runner.h"
10 #include "base/test/task_environment.h"
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.
20 //
21 // TODO(b/891670): Get rid of this and migrate users to
22 // SingleThreadTaskEnvironment
23 class TestMessageLoop {
24  public:
25   TestMessageLoop();
26   explicit TestMessageLoop(MessagePumpType type);
27   ~TestMessageLoop();
28 
task_runner()29   scoped_refptr<SingleThreadTaskRunner> task_runner() {
30     return task_environment_.GetMainThreadTaskRunner();
31   }
32 
33  private:
34   test::SingleThreadTaskEnvironment task_environment_;
35 };
36 
37 }  // namespace base
38 
39 #endif  // BASE_TEST_TEST_MESSAGE_LOOP_H_
40