blob: ca69eb094c9bbab16b381ce916fa6b12a4ee7015 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2016 The Chromium Authors
alokp42544a82016-04-12 01:53:492// 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
Carlos Caballerodd8bf7b042019-07-30 14:14:158#include "base/message_loop/message_pump_type.h"
Patrick Monette643cdf62021-10-15 19:13:429#include "base/task/single_thread_task_runner.h"
Carlos Caballerof5b6f91c2019-12-20 14:07:3810#include "base/test/task_environment.h"
alokp42544a82016-04-12 01:53:4911
12namespace 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.
Carlos Caballerof5b6f91c2019-12-20 14:07:3820//
21// TODO(b/891670): Get rid of this and migrate users to
22// SingleThreadTaskEnvironment
alokp42544a82016-04-12 01:53:4923class TestMessageLoop {
24 public:
25 TestMessageLoop();
Carlos Caballerodd8bf7b042019-07-30 14:14:1526 explicit TestMessageLoop(MessagePumpType type);
alokp42544a82016-04-12 01:53:4927 ~TestMessageLoop();
28
Alexander Timin7dc2d042018-11-16 12:27:1529 scoped_refptr<SingleThreadTaskRunner> task_runner() {
Carlos Caballerof5b6f91c2019-12-20 14:07:3830 return task_environment_.GetMainThreadTaskRunner();
danakj8eb035c02016-06-17 22:41:3231 }
32
alokp42544a82016-04-12 01:53:4933 private:
Carlos Caballerof5b6f91c2019-12-20 14:07:3834 test::SingleThreadTaskEnvironment task_environment_;
alokp42544a82016-04-12 01:53:4935};
36
37} // namespace base
38
39#endif // BASE_TEST_TEST_MESSAGE_LOOP_H_