test_message_loop.cc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/test/test_message_loop.h"
  5. #include "base/compiler_specific.h"
  6. #include "base/message_loop/message_pump_type.h"
  7. #include "base/notreached.h"
  8. #include "base/run_loop.h"
  9. #include "base/test/task_environment.h"
  10. #include "build/build_config.h"
  11. namespace base {
  12. namespace {
  13. test::SingleThreadTaskEnvironment::MainThreadType GetMainThreadType(
  14. MessagePumpType type) {
  15. switch (type) {
  16. case MessagePumpType::DEFAULT:
  17. return test::SingleThreadTaskEnvironment::MainThreadType::DEFAULT;
  18. case MessagePumpType::IO:
  19. return test::SingleThreadTaskEnvironment::MainThreadType::IO;
  20. case MessagePumpType::UI:
  21. return test::SingleThreadTaskEnvironment::MainThreadType::UI;
  22. case MessagePumpType::CUSTOM:
  23. #if BUILDFLAG(IS_ANDROID)
  24. case MessagePumpType::JAVA:
  25. #elif BUILDFLAG(IS_APPLE)
  26. case MessagePumpType::NS_RUNLOOP:
  27. #endif
  28. NOTREACHED();
  29. return test::SingleThreadTaskEnvironment::MainThreadType::DEFAULT;
  30. }
  31. }
  32. } // namespace
  33. TestMessageLoop::TestMessageLoop() = default;
  34. TestMessageLoop::TestMessageLoop(MessagePumpType type)
  35. : task_environment_(GetMainThreadType(type)) {}
  36. TestMessageLoop::~TestMessageLoop() {
  37. RunLoop().RunUntilIdle();
  38. }
  39. } // namespace base