[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 1 | // 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 |
fdoray | ad58f83 | 2016-12-19 18:06:46 | [diff] [blame] | 6 | // 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] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 11 | // |
fdoray | 575c4b2 | 2017-04-05 19:25:51 | [diff] [blame] | 12 | // 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] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 17 | // |
fdoray | 575c4b2 | 2017-04-05 19:25:51 | [diff] [blame] | 18 | // 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] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 44 | // |
fdoray | ad58f83 | 2016-12-19 18:06:46 | [diff] [blame] | 45 | // If a test needs a MessageLoopForIO on the main thread, it should use the |
fdoray | 575c4b2 | 2017-04-05 19:25:51 | [diff] [blame] | 46 | // IO_MAINLOOP option. Most of the time, IO_MAINLOOP avoids needing to use a |
fdoray | ad58f83 | 2016-12-19 18:06:46 | [diff] [blame] | 47 | // REAL_IO_THREAD. |
aberent | 8f1ce40 | 2016-01-13 12:01:38 | [diff] [blame] | 48 | // |
| 49 | // For some tests it is important to emulate real browser startup. During real |
fdoray | 93605d2 | 2017-01-13 12:17:25 | [diff] [blame] | 50 | // 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(). |
aberent | 8f1ce40 | 2016-01-13 12:01:38 | [diff] [blame] | 53 | // |
fdoray | 93605d2 | 2017-01-13 12:17:25 | [diff] [blame] | 54 | // DONT_CREATE_THREADS should only be used when the options specify at least |
aberent | 8f1ce40 | 2016-01-13 12:01:38 | [diff] [blame] | 55 | // one real thread other than the main thread. |
gab | 1f58e95 | 2017-05-04 23:28:51 | [diff] [blame] | 56 | // |
fdoray | 8c58052 | 2017-05-08 16:59:41 | [diff] [blame] | 57 | // 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 | // |
gab | 1f58e95 | 2017-05-04 23:28:51 | [diff] [blame] | 66 | // 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] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 83 | |
sdefresne | 634eec9 | 2014-12-18 13:02:54 | [diff] [blame] | 84 | #ifndef CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ |
| 85 | #define CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 86 | |
dcheng | 6003e0b | 2016-04-09 18:42:34 | [diff] [blame] | 87 | #include <memory> |
| 88 | |
avi | 652869c | 2015-12-25 01:48:45 | [diff] [blame] | 89 | #include "base/macros.h" |
Robert Liao | 0bde45e | 2017-06-22 21:16:03 | [diff] [blame] | 90 | #include "build/build_config.h" |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 91 | |
| 92 | namespace base { |
| 93 | class MessageLoop; |
fdoray | ad58f83 | 2016-12-19 18:06:46 | [diff] [blame] | 94 | namespace test { |
fdoray | aedd3c2c | 2017-01-14 00:09:32 | [diff] [blame] | 95 | class ScopedAsyncTaskScheduler; |
fdoray | ad58f83 | 2016-12-19 18:06:46 | [diff] [blame] | 96 | } // namespace test |
Robert Liao | 0bde45e | 2017-06-22 21:16:03 | [diff] [blame] | 97 | #if defined(OS_WIN) |
| 98 | namespace win { |
| 99 | class ScopedCOMInitializer; |
| 100 | } // namespace win |
| 101 | #endif |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 102 | } // namespace base |
| 103 | |
| 104 | namespace content { |
| 105 | |
| 106 | class TestBrowserThread; |
| 107 | |
| 108 | class 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 { |
fdoray | aedd3c2c | 2017-01-14 00:09:32 | [diff] [blame] | 114 | 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, |
fdoray | 575c4b2 | 2017-04-05 19:25:51 | [diff] [blame] | 119 | DONT_CREATE_THREADS = 1 << 4, |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | TestBrowserThreadBundle(); |
| 123 | explicit TestBrowserThreadBundle(int options); |
| 124 | |
fdoray | 93605d2 | 2017-01-13 12:17:25 | [diff] [blame] | 125 | // 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(); |
aberent | 8f1ce40 | 2016-01-13 12:01:38 | [diff] [blame] | 128 | |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 129 | ~TestBrowserThreadBundle(); |
| 130 | |
| 131 | private: |
aberent | 8f1ce40 | 2016-01-13 12:01:38 | [diff] [blame] | 132 | void Init(); |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 133 | |
dcheng | 6003e0b | 2016-04-09 18:42:34 | [diff] [blame] | 134 | std::unique_ptr<base::MessageLoop> message_loop_; |
fdoray | aedd3c2c | 2017-01-14 00:09:32 | [diff] [blame] | 135 | std::unique_ptr<base::test::ScopedAsyncTaskScheduler> |
| 136 | scoped_async_task_scheduler_; |
dcheng | 6003e0b | 2016-04-09 18:42:34 | [diff] [blame] | 137 | 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] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 144 | |
aberent | 8f1ce40 | 2016-01-13 12:01:38 | [diff] [blame] | 145 | int options_; |
fdoray | 93605d2 | 2017-01-13 12:17:25 | [diff] [blame] | 146 | bool threads_created_; |
aberent | 8f1ce40 | 2016-01-13 12:01:38 | [diff] [blame] | 147 | |
Robert Liao | 0bde45e | 2017-06-22 21:16:03 | [diff] [blame] | 148 | #if defined(OS_WIN) |
| 149 | std::unique_ptr<base::win::ScopedCOMInitializer> com_initializer_; |
| 150 | #endif |
| 151 | |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 152 | DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadBundle); |
| 153 | }; |
| 154 | |
| 155 | } // namespace content |
| 156 | |
sdefresne | 634eec9 | 2014-12-18 13:02:54 | [diff] [blame] | 157 | #endif // CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ |