| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/public/test/test_browser_thread_bundle.h" | 5 #include "content/public/test/test_browser_thread_bundle.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/task_scheduler/task_scheduler.h" | 11 #include "base/task_scheduler/task_scheduler.h" |
| 12 #include "base/test/scoped_async_task_scheduler.h" | 12 #include "base/test/scoped_async_task_scheduler.h" |
| 13 #include "base/threading/sequenced_worker_pool.h" |
| 13 #include "content/browser/browser_thread_impl.h" | 14 #include "content/browser/browser_thread_impl.h" |
| 14 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/test/test_browser_thread.h" | 16 #include "content/public/test/test_browser_thread.h" |
| 16 #include "content/public/test/test_utils.h" | 17 #include "content/public/test/test_utils.h" |
| 17 | 18 |
| 18 namespace content { | 19 namespace content { |
| 19 | 20 |
| 20 TestBrowserThreadBundle::TestBrowserThreadBundle() | 21 TestBrowserThreadBundle::TestBrowserThreadBundle() |
| 21 : TestBrowserThreadBundle(DEFAULT) {} | 22 : TestBrowserThreadBundle(DEFAULT) {} |
| 22 | 23 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 // by flushing we guarantee it will blow up). | 72 // by flushing we guarantee it will blow up). |
| 72 RunAllBlockingPoolTasksUntilIdle(); | 73 RunAllBlockingPoolTasksUntilIdle(); |
| 73 | 74 |
| 74 scoped_async_task_scheduler_.reset(); | 75 scoped_async_task_scheduler_.reset(); |
| 75 CHECK(base::MessageLoop::current()->IsIdleForTesting()); | 76 CHECK(base::MessageLoop::current()->IsIdleForTesting()); |
| 76 | 77 |
| 77 // |message_loop_| needs to explicitly go away before fake threads in order | 78 // |message_loop_| needs to explicitly go away before fake threads in order |
| 78 // for DestructionObservers hooked to |message_loop_| to be able to invoke | 79 // for DestructionObservers hooked to |message_loop_| to be able to invoke |
| 79 // BrowserThread::CurrentlyOn() -- ref. ~TestBrowserThread(). | 80 // BrowserThread::CurrentlyOn() -- ref. ~TestBrowserThread(). |
| 80 message_loop_.reset(); | 81 message_loop_.reset(); |
| 82 |
| 83 // Disable redirection of SequencedWorkerPools to TaskScheduler. This is |
| 84 // required in order to reset global state so that tests following this one in |
| 85 // this process can still manage their own SequencedWorkerPool without using |
| 86 // TestBrowserThreadBundle. |
| 87 base::SequencedWorkerPool::EnableForProcess(); |
| 81 } | 88 } |
| 82 | 89 |
| 83 void TestBrowserThreadBundle::Init() { | 90 void TestBrowserThreadBundle::Init() { |
| 84 // Check that the UI thread hasn't already been initialized. This will fail if | 91 // Check that the UI thread hasn't already been initialized. This will fail if |
| 85 // multiple TestBrowserThreadBundles are initialized in the same scope. | 92 // multiple TestBrowserThreadBundles are initialized in the same scope. |
| 86 CHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI)); | 93 CHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI)); |
| 87 | 94 |
| 88 // Check for conflicting options can't have two IO threads. | 95 // Check for conflicting options can't have two IO threads. |
| 89 CHECK(!(options_ & IO_MAINLOOP) || !(options_ & REAL_IO_THREAD)); | 96 CHECK(!(options_ & IO_MAINLOOP) || !(options_ & REAL_IO_THREAD)); |
| 90 // There must be a thread to start to use DONT_CREATE_THREADS | 97 // There must be a thread to start to use DONT_CREATE_THREADS |
| (...skipping 25 matching lines...) Expand all Loading... |
| 116 // TaskScheduler can sometimes be externally provided by a | 123 // TaskScheduler can sometimes be externally provided by a |
| 117 // base::test::ScopedTaskEnvironment in a parent fixture. In that case it's | 124 // base::test::ScopedTaskEnvironment in a parent fixture. In that case it's |
| 118 // expected to have provided the MessageLoop as well (in which case | 125 // expected to have provided the MessageLoop as well (in which case |
| 119 // |message_loop_| remains null in Init()). | 126 // |message_loop_| remains null in Init()). |
| 120 CHECK(!base::TaskScheduler::GetInstance() || !message_loop_); | 127 CHECK(!base::TaskScheduler::GetInstance() || !message_loop_); |
| 121 if (!base::TaskScheduler::GetInstance()) { | 128 if (!base::TaskScheduler::GetInstance()) { |
| 122 scoped_async_task_scheduler_ = | 129 scoped_async_task_scheduler_ = |
| 123 base::MakeUnique<base::test::ScopedAsyncTaskScheduler>(); | 130 base::MakeUnique<base::test::ScopedAsyncTaskScheduler>(); |
| 124 } | 131 } |
| 125 | 132 |
| 133 // Enable redirection of SequencedWorkerPools to TaskScheduler. |
| 134 base::SequencedWorkerPool::EnableWithRedirectionToTaskSchedulerForProcess(); |
| 135 |
| 126 if (options_ & REAL_DB_THREAD) { | 136 if (options_ & REAL_DB_THREAD) { |
| 127 db_thread_ = base::MakeUnique<TestBrowserThread>(BrowserThread::DB); | 137 db_thread_ = base::MakeUnique<TestBrowserThread>(BrowserThread::DB); |
| 128 db_thread_->Start(); | 138 db_thread_->Start(); |
| 129 } else { | 139 } else { |
| 130 db_thread_ = base::MakeUnique<TestBrowserThread>( | 140 db_thread_ = base::MakeUnique<TestBrowserThread>( |
| 131 BrowserThread::DB, base::MessageLoop::current()); | 141 BrowserThread::DB, base::MessageLoop::current()); |
| 132 } | 142 } |
| 133 | 143 |
| 134 if (options_ & REAL_FILE_THREAD) { | 144 if (options_ & REAL_FILE_THREAD) { |
| 135 file_thread_ = base::MakeUnique<TestBrowserThread>(BrowserThread::FILE); | 145 file_thread_ = base::MakeUnique<TestBrowserThread>(BrowserThread::FILE); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 151 io_thread_->StartIOThread(); | 161 io_thread_->StartIOThread(); |
| 152 } else { | 162 } else { |
| 153 io_thread_ = base::MakeUnique<TestBrowserThread>( | 163 io_thread_ = base::MakeUnique<TestBrowserThread>( |
| 154 BrowserThread::IO, base::MessageLoop::current()); | 164 BrowserThread::IO, base::MessageLoop::current()); |
| 155 } | 165 } |
| 156 | 166 |
| 157 threads_created_ = true; | 167 threads_created_ = true; |
| 158 } | 168 } |
| 159 | 169 |
| 160 } // namespace content | 170 } // namespace content |
| OLD | NEW |