test_message_loop.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. #ifndef BASE_TEST_TEST_MESSAGE_LOOP_H_
  5. #define BASE_TEST_TEST_MESSAGE_LOOP_H_
  6. #include "base/message_loop/message_pump_type.h"
  7. #include "base/single_thread_task_runner.h"
  8. #include "base/test/task_environment.h"
  9. namespace base {
  10. // TestMessageLoop is a convenience class for unittests that need to create a
  11. // message loop without a real thread backing it. For most tests,
  12. // it is sufficient to just instantiate TestMessageLoop as a member variable.
  13. //
  14. // TestMessageLoop will attempt to drain the underlying MessageLoop on
  15. // destruction for clean teardown of tests.
  16. //
  17. // TODO(b/891670): Get rid of this and migrate users to
  18. // SingleThreadTaskEnvironment
  19. class TestMessageLoop {
  20. public:
  21. TestMessageLoop();
  22. explicit TestMessageLoop(MessagePumpType type);
  23. ~TestMessageLoop();
  24. scoped_refptr<SingleThreadTaskRunner> task_runner() {
  25. return task_environment_.GetMainThreadTaskRunner();
  26. }
  27. private:
  28. test::SingleThreadTaskEnvironment task_environment_;
  29. };
  30. } // namespace base
  31. #endif // BASE_TEST_TEST_MESSAGE_LOOP_H_