xref: /aosp_15_r20/external/cronet/base/test/test_message_loop.cc (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 #include "base/test/test_message_loop.h"
6 
7 #include "base/compiler_specific.h"
8 #include "base/message_loop/message_pump_type.h"
9 #include "base/notreached.h"
10 #include "base/run_loop.h"
11 #include "base/test/task_environment.h"
12 #include "build/build_config.h"
13 
14 namespace base {
15 
16 namespace {
17 
GetMainThreadType(MessagePumpType type)18 test::SingleThreadTaskEnvironment::MainThreadType GetMainThreadType(
19     MessagePumpType type) {
20   switch (type) {
21     case MessagePumpType::DEFAULT:
22       return test::SingleThreadTaskEnvironment::MainThreadType::DEFAULT;
23     case MessagePumpType::IO:
24       return test::SingleThreadTaskEnvironment::MainThreadType::IO;
25     case MessagePumpType::UI:
26       return test::SingleThreadTaskEnvironment::MainThreadType::UI;
27     case MessagePumpType::CUSTOM:
28 #if BUILDFLAG(IS_ANDROID)
29     case MessagePumpType::JAVA:
30 #elif BUILDFLAG(IS_APPLE)
31     case MessagePumpType::NS_RUNLOOP:
32 #endif
33       NOTREACHED();
34       return test::SingleThreadTaskEnvironment::MainThreadType::DEFAULT;
35   }
36 }
37 
38 }  // namespace
39 
40 TestMessageLoop::TestMessageLoop() = default;
41 
TestMessageLoop(MessagePumpType type)42 TestMessageLoop::TestMessageLoop(MessagePumpType type)
43     : task_environment_(GetMainThreadType(type)) {}
44 
~TestMessageLoop()45 TestMessageLoop::~TestMessageLoop() {
46   RunLoop().RunUntilIdle();
47 }
48 
49 }  // namespace base
50