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)18test::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 } 35 } 36 37 } // namespace 38 39 TestMessageLoop::TestMessageLoop() = default; 40 TestMessageLoop(MessagePumpType type)41TestMessageLoop::TestMessageLoop(MessagePumpType type) 42 : task_environment_(GetMainThreadType(type)) {} 43 ~TestMessageLoop()44TestMessageLoop::~TestMessageLoop() { 45 RunLoop().RunUntilIdle(); 46 } 47 48 } // namespace base 49