blob: 991a7f9e3f791545ef4d5cac08a9cd87064d296b [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//
fdoray8c580522017-05-08 16:59:4157// TestBrowserThreadBundle may be instantiated in a scope where there is already
58// a base::test::ScopedTaskEnvironment. In that case, it will use the
59// MessageLoop and the TaskScheduler provided by this
60// base::test::ScopedTaskEnvironment instead of creating its own. The ability to
61// have a base::test::ScopedTaskEnvironment and a TestBrowserThreadBundle in the
62// same scope is useful when a fixture that inherits from a fixture that
63// provides a base::test::ScopedTaskEnvironment needs to add support for browser
64// threads.
65//
gab1f58e952017-05-04 23:28:5166// Basic usage:
67//
68// class MyTestFixture : public testing::Test {
69// public:
70// (...)
71//
72// protected:
73// // Must be the first member (or at least before any member that cares
74// // about tasks) to be initialized first and destroyed last. protected
75// // instead of private visibility will allow controlling the task
76// // environment (e.g. clock) once such features are added (see
77// // base::test::ScopedTaskEnvironment for details), until then it at least
78// // doesn't hurt :).
79// content::TestBrowserThreadBundle test_browser_thread_bundle_;
80//
81// // Other members go here (or further below in private section.)
82// };
[email protected]ec04d3f2013-06-06 21:31:3983
sdefresne634eec92014-12-18 13:02:5484#ifndef CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_
85#define CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_
[email protected]ec04d3f2013-06-06 21:31:3986
dcheng6003e0b2016-04-09 18:42:3487#include <memory>
88
avi652869c2015-12-25 01:48:4589#include "base/macros.h"
Robert Liao0bde45e2017-06-22 21:16:0390#include "build/build_config.h"
[email protected]ec04d3f2013-06-06 21:31:3991
92namespace base {
93class MessageLoop;
fdorayad58f832016-12-19 18:06:4694namespace test {
fdorayaedd3c2c2017-01-14 00:09:3295class ScopedAsyncTaskScheduler;
fdorayad58f832016-12-19 18:06:4696} // namespace test
Robert Liao0bde45e2017-06-22 21:16:0397#if defined(OS_WIN)
98namespace win {
99class ScopedCOMInitializer;
100} // namespace win
101#endif
[email protected]ec04d3f2013-06-06 21:31:39102} // namespace base
103
104namespace content {
105
106class TestBrowserThread;
107
108class TestBrowserThreadBundle {
109 public:
110 // Used to specify the type of MessageLoop that backs the UI thread, and
111 // which of the named BrowserThreads should be backed by a real
112 // threads. The UI thread is always the main thread in a unit test.
113 enum Options {
fdorayaedd3c2c2017-01-14 00:09:32114 DEFAULT = 0,
115 IO_MAINLOOP = 1 << 0,
116 REAL_DB_THREAD = 1 << 1,
117 REAL_FILE_THREAD = 1 << 2,
118 REAL_IO_THREAD = 1 << 3,
fdoray575c4b22017-04-05 19:25:51119 DONT_CREATE_THREADS = 1 << 4,
[email protected]ec04d3f2013-06-06 21:31:39120 };
121
122 TestBrowserThreadBundle();
123 explicit TestBrowserThreadBundle(int options);
124
fdoray93605d22017-01-13 12:17:25125 // Creates threads; should only be called from other classes if the
126 // DONT_CREATE_THREADS option was used when the bundle was created.
127 void CreateThreads();
aberent8f1ce402016-01-13 12:01:38128
[email protected]ec04d3f2013-06-06 21:31:39129 ~TestBrowserThreadBundle();
130
131 private:
aberent8f1ce402016-01-13 12:01:38132 void Init();
[email protected]ec04d3f2013-06-06 21:31:39133
dcheng6003e0b2016-04-09 18:42:34134 std::unique_ptr<base::MessageLoop> message_loop_;
fdorayaedd3c2c2017-01-14 00:09:32135 std::unique_ptr<base::test::ScopedAsyncTaskScheduler>
136 scoped_async_task_scheduler_;
dcheng6003e0b2016-04-09 18:42:34137 std::unique_ptr<TestBrowserThread> ui_thread_;
138 std::unique_ptr<TestBrowserThread> db_thread_;
139 std::unique_ptr<TestBrowserThread> file_thread_;
140 std::unique_ptr<TestBrowserThread> file_user_blocking_thread_;
141 std::unique_ptr<TestBrowserThread> process_launcher_thread_;
142 std::unique_ptr<TestBrowserThread> cache_thread_;
143 std::unique_ptr<TestBrowserThread> io_thread_;
[email protected]ec04d3f2013-06-06 21:31:39144
aberent8f1ce402016-01-13 12:01:38145 int options_;
fdoray93605d22017-01-13 12:17:25146 bool threads_created_;
aberent8f1ce402016-01-13 12:01:38147
Robert Liao0bde45e2017-06-22 21:16:03148#if defined(OS_WIN)
149 std::unique_ptr<base::win::ScopedCOMInitializer> com_initializer_;
150#endif
151
[email protected]ec04d3f2013-06-06 21:31:39152 DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadBundle);
153};
154
155} // namespace content
156
sdefresne634eec92014-12-18 13:02:54157#endif // CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_