blob: c8f85deb2a5378de47e4d1b1badfc14f6f2b160e [file] [log] [blame]
[email protected]ec04d3f2013-06-06 21:31:391// Copyright 2013 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
5// TestBrowserThreadBundle is a convenience class for creating a set of
fdorayad58f832016-12-19 18:06:466// TestBrowserThreads, a blocking pool, and a task scheduler in unit tests. For
7// most tests, it is sufficient to just instantiate the TestBrowserThreadBundle
8// as a member variable. It is a good idea to put the TestBrowserThreadBundle as
9// the first member variable in test classes, so it is destroyed last, and the
10// test threads always exist from the perspective of other classes.
[email protected]ec04d3f2013-06-06 21:31:3911//
fdoray575c4b22017-04-05 19:25:5112// By default, all of the created TestBrowserThreads will be backed by a single
13// shared MessageLoop. If a test truly needs separate threads, it can do so by
14// passing the appropriate combination of option values during the
15// TestBrowserThreadBundle construction. TaskScheduler and blocking pool tasks
16// always run on dedicated threads.
[email protected]ec04d3f2013-06-06 21:31:3917//
fdoray575c4b22017-04-05 19:25:5118// To synchronously run tasks from the shared MessageLoop:
19//
20// ... until there are no undelayed tasks in the shared MessageLoop:
21// base::RunLoop::RunUntilIdle();
22//
23// ... until there are no undelayed tasks in the shared MessageLoop, in
24// TaskScheduler or in the blocking pool (excluding tasks not posted from the
25// shared MessageLoop's thread, TaskScheduler or the blocking pool):
26// content::RunAllBlockingPoolTasksUntilIdle();
27//
28// ... until a condition is met:
29// base::RunLoop run_loop;
30// // Runs until a task running in the shared MessageLoop calls
31// // run_loop.Quit() or runs run_loop.QuitClosure() (&run_loop or
32// // run_loop.QuitClosure() must be kept somewhere accessible by that task).
33// run_loop.Run();
34//
35// To wait until there are no pending undelayed tasks in TaskScheduler or in the
36// blocking pool, without running tasks from the shared MessageLoop:
37// base::TaskScheduler::GetInstance()->FlushForTesting();
38// // Note: content::BrowserThread::GetBlockingPool()->FlushForTesting() is
39// // equivalent but deprecated.
40//
41// The destructor of TestBrowserThreadBundle runs remaining TestBrowserThreads
42// tasks, remaining blocking pool tasks, and remaining BLOCK_SHUTDOWN task
43// scheduler tasks.
[email protected]ec04d3f2013-06-06 21:31:3944//
fdorayad58f832016-12-19 18:06:4645// If a test needs a MessageLoopForIO on the main thread, it should use the
fdoray575c4b22017-04-05 19:25:5146// IO_MAINLOOP option. Most of the time, IO_MAINLOOP avoids needing to use a
fdorayad58f832016-12-19 18:06:4647// REAL_IO_THREAD.
aberent8f1ce402016-01-13 12:01:3848//
49// For some tests it is important to emulate real browser startup. During real
fdoray93605d22017-01-13 12:17:2550// browser startup, the main MessageLoop is created before other threads.
51// Passing DONT_CREATE_THREADS to constructor will delay creating other threads
52// until the test explicitly calls CreateThreads().
aberent8f1ce402016-01-13 12:01:3853//
fdoray93605d22017-01-13 12:17:2554// DONT_CREATE_THREADS should only be used when the options specify at least
aberent8f1ce402016-01-13 12:01:3855// one real thread other than the main thread.
gab1f58e952017-05-04 23:28:5156//
57// Basic usage:
58//
59// class MyTestFixture : public testing::Test {
60// public:
61// (...)
62//
63// protected:
64// // Must be the first member (or at least before any member that cares
65// // about tasks) to be initialized first and destroyed last. protected
66// // instead of private visibility will allow controlling the task
67// // environment (e.g. clock) once such features are added (see
68// // base::test::ScopedTaskEnvironment for details), until then it at least
69// // doesn't hurt :).
70// content::TestBrowserThreadBundle test_browser_thread_bundle_;
71//
72// // Other members go here (or further below in private section.)
73// };
[email protected]ec04d3f2013-06-06 21:31:3974
sdefresne634eec92014-12-18 13:02:5475#ifndef CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_
76#define CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_
[email protected]ec04d3f2013-06-06 21:31:3977
dcheng6003e0b2016-04-09 18:42:3478#include <memory>
79
avi652869c2015-12-25 01:48:4580#include "base/macros.h"
[email protected]ec04d3f2013-06-06 21:31:3981
82namespace base {
83class MessageLoop;
fdorayad58f832016-12-19 18:06:4684namespace test {
fdorayaedd3c2c2017-01-14 00:09:3285class ScopedAsyncTaskScheduler;
fdorayad58f832016-12-19 18:06:4686} // namespace test
[email protected]ec04d3f2013-06-06 21:31:3987} // namespace base
88
89namespace content {
90
91class TestBrowserThread;
92
93class TestBrowserThreadBundle {
94 public:
95 // Used to specify the type of MessageLoop that backs the UI thread, and
96 // which of the named BrowserThreads should be backed by a real
97 // threads. The UI thread is always the main thread in a unit test.
98 enum Options {
fdorayaedd3c2c2017-01-14 00:09:3299 DEFAULT = 0,
100 IO_MAINLOOP = 1 << 0,
101 REAL_DB_THREAD = 1 << 1,
102 REAL_FILE_THREAD = 1 << 2,
103 REAL_IO_THREAD = 1 << 3,
fdoray575c4b22017-04-05 19:25:51104 DONT_CREATE_THREADS = 1 << 4,
[email protected]ec04d3f2013-06-06 21:31:39105 };
106
107 TestBrowserThreadBundle();
108 explicit TestBrowserThreadBundle(int options);
109
fdoray93605d22017-01-13 12:17:25110 // Creates threads; should only be called from other classes if the
111 // DONT_CREATE_THREADS option was used when the bundle was created.
112 void CreateThreads();
aberent8f1ce402016-01-13 12:01:38113
[email protected]ec04d3f2013-06-06 21:31:39114 ~TestBrowserThreadBundle();
115
116 private:
aberent8f1ce402016-01-13 12:01:38117 void Init();
[email protected]ec04d3f2013-06-06 21:31:39118
dcheng6003e0b2016-04-09 18:42:34119 std::unique_ptr<base::MessageLoop> message_loop_;
fdorayaedd3c2c2017-01-14 00:09:32120 std::unique_ptr<base::test::ScopedAsyncTaskScheduler>
121 scoped_async_task_scheduler_;
dcheng6003e0b2016-04-09 18:42:34122 std::unique_ptr<TestBrowserThread> ui_thread_;
123 std::unique_ptr<TestBrowserThread> db_thread_;
124 std::unique_ptr<TestBrowserThread> file_thread_;
125 std::unique_ptr<TestBrowserThread> file_user_blocking_thread_;
126 std::unique_ptr<TestBrowserThread> process_launcher_thread_;
127 std::unique_ptr<TestBrowserThread> cache_thread_;
128 std::unique_ptr<TestBrowserThread> io_thread_;
[email protected]ec04d3f2013-06-06 21:31:39129
aberent8f1ce402016-01-13 12:01:38130 int options_;
fdoray93605d22017-01-13 12:17:25131 bool threads_created_;
aberent8f1ce402016-01-13 12:01:38132
[email protected]ec04d3f2013-06-06 21:31:39133 DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadBundle);
134};
135
136} // namespace content
137
sdefresne634eec92014-12-18 13:02:54138#endif // CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_