| 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" | |
| 12 #include "base/test/scoped_async_task_scheduler.h" | 11 #include "base/test/scoped_async_task_scheduler.h" |
| 13 #include "content/browser/browser_thread_impl.h" | 12 #include "content/browser/browser_thread_impl.h" |
| 14 #include "content/public/browser/browser_thread.h" | |
| 15 #include "content/public/test/test_browser_thread.h" | 13 #include "content/public/test/test_browser_thread.h" |
| 16 | 14 |
| 17 namespace content { | 15 namespace content { |
| 18 | 16 |
| 19 TestBrowserThreadBundle::TestBrowserThreadBundle() | 17 TestBrowserThreadBundle::TestBrowserThreadBundle() |
| 20 : TestBrowserThreadBundle(DEFAULT) {} | 18 : TestBrowserThreadBundle(DEFAULT) {} |
| 21 | 19 |
| 22 TestBrowserThreadBundle::TestBrowserThreadBundle(int options) | 20 TestBrowserThreadBundle::TestBrowserThreadBundle(int options) |
| 23 : options_(options), threads_created_(false) { | 21 : options_(options), threads_created_(false) { |
| 24 Init(); | 22 Init(); |
| 25 } | 23 } |
| 26 | 24 |
| 27 TestBrowserThreadBundle::~TestBrowserThreadBundle() { | 25 TestBrowserThreadBundle::~TestBrowserThreadBundle() { |
| 28 CHECK(threads_created_); | 26 DCHECK(threads_created_); |
| 29 | 27 |
| 30 // To avoid memory leaks, we must ensure that any tasks posted to the blocking | 28 // To avoid memory leaks, we must ensure that any tasks posted to the blocking |
| 31 // pool via PostTaskAndReply are able to reply back to the originating thread. | 29 // pool via PostTaskAndReply are able to reply back to the originating thread. |
| 32 // Thus we must flush the blocking pool while the browser threads still exist. | 30 // Thus we must flush the blocking pool while the browser threads still exist. |
| 33 base::RunLoop().RunUntilIdle(); | 31 base::RunLoop().RunUntilIdle(); |
| 34 BrowserThreadImpl::FlushThreadPoolHelperForTesting(); | 32 BrowserThreadImpl::FlushThreadPoolHelperForTesting(); |
| 35 | 33 |
| 36 // To ensure a clean teardown, each thread's message loop must be flushed | 34 // To ensure a clean teardown, each thread's message loop must be flushed |
| 37 // just before the thread is destroyed. But stopping a fake thread does not | 35 // just before the thread is destroyed. But stopping a fake thread does not |
| 38 // automatically flush the message loop, so we have to do it manually. | 36 // automatically flush the message loop, so we have to do it manually. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 54 // it again in case any shutdown tasks have been posted to the pool from the | 52 // it again in case any shutdown tasks have been posted to the pool from the |
| 55 // threads above. | 53 // threads above. |
| 56 BrowserThreadImpl::FlushThreadPoolHelperForTesting(); | 54 BrowserThreadImpl::FlushThreadPoolHelperForTesting(); |
| 57 base::RunLoop().RunUntilIdle(); | 55 base::RunLoop().RunUntilIdle(); |
| 58 ui_thread_->Stop(); | 56 ui_thread_->Stop(); |
| 59 base::RunLoop().RunUntilIdle(); | 57 base::RunLoop().RunUntilIdle(); |
| 60 | 58 |
| 61 scoped_async_task_scheduler_.reset(); | 59 scoped_async_task_scheduler_.reset(); |
| 62 | 60 |
| 63 base::RunLoop().RunUntilIdle(); | 61 base::RunLoop().RunUntilIdle(); |
| 64 CHECK(base::MessageLoop::current()->IsIdleForTesting()); | |
| 65 | 62 |
| 66 // |message_loop_| needs to explicitly go away before fake threads in order | 63 // |message_loop_| needs to explicitly go away before fake threads in order |
| 67 // for DestructionObservers hooked to |message_loop_| to be able to invoke | 64 // for DestructionObservers hooked to |message_loop_| to be able to invoke |
| 68 // BrowserThread::CurrentlyOn() -- ref. ~TestBrowserThread(). | 65 // BrowserThread::CurrentlyOn() -- ref. ~TestBrowserThread(). |
| 66 CHECK(message_loop_->IsIdleForTesting()); |
| 69 message_loop_.reset(); | 67 message_loop_.reset(); |
| 70 } | 68 } |
| 71 | 69 |
| 72 void TestBrowserThreadBundle::Init() { | 70 void TestBrowserThreadBundle::Init() { |
| 73 // Check that the UI thread hasn't already been initialized. This will fail if | |
| 74 // multiple TestBrowserThreadBundles are initialized in the same scope. | |
| 75 CHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI)); | |
| 76 | |
| 77 // Check for conflicting options can't have two IO threads. | 71 // Check for conflicting options can't have two IO threads. |
| 78 CHECK(!(options_ & IO_MAINLOOP) || !(options_ & REAL_IO_THREAD)); | 72 CHECK(!(options_ & IO_MAINLOOP) || !(options_ & REAL_IO_THREAD)); |
| 79 // There must be a thread to start to use DONT_CREATE_THREADS | 73 // There must be a thread to start to use DONT_CREATE_THREADS |
| 80 CHECK((options_ & ~IO_MAINLOOP) != DONT_CREATE_THREADS); | 74 CHECK((options_ & ~IO_MAINLOOP) != DONT_CREATE_THREADS); |
| 81 | 75 |
| 82 // Create the main MessageLoop, if it doesn't already exist, and set the | 76 // Create the UI thread. In production, this work is done in |
| 83 // current thread as the UI thread. In production, this work is done in | 77 // BrowserMainLoop::MainMessageLoopStart(). |
| 84 // BrowserMainLoop::MainMessageLoopStart(). The main MessageLoop may already | 78 if (options_ & IO_MAINLOOP) { |
| 85 // exist if this TestBrowserThreadBundle is instantiated in a test whose | 79 message_loop_.reset(new base::MessageLoopForIO()); |
| 86 // parent fixture provides a base::test::ScopedTaskEnvironment. | 80 } else { |
| 87 const base::MessageLoop::Type message_loop_type = | 81 message_loop_.reset(new base::MessageLoopForUI()); |
| 88 options_ & IO_MAINLOOP ? base::MessageLoop::TYPE_IO | 82 } |
| 89 : base::MessageLoop::TYPE_UI; | |
| 90 if (!base::MessageLoop::current()) | |
| 91 message_loop_ = base::MakeUnique<base::MessageLoop>(message_loop_type); | |
| 92 CHECK(base::MessageLoop::current()->IsType(message_loop_type)); | |
| 93 | 83 |
| 94 ui_thread_ = base::MakeUnique<TestBrowserThread>( | 84 ui_thread_.reset( |
| 95 BrowserThread::UI, base::MessageLoop::current()); | 85 new TestBrowserThread(BrowserThread::UI, message_loop_.get())); |
| 96 | 86 |
| 97 if (!(options_ & DONT_CREATE_THREADS)) | 87 if (!(options_ & DONT_CREATE_THREADS)) |
| 98 CreateThreads(); | 88 CreateThreads(); |
| 99 } | 89 } |
| 100 | 90 |
| 101 // This method mimics the work done in BrowserMainLoop::CreateThreads(). | 91 // This method mimics the work done in BrowserMainLoop::CreateThreads(). |
| 102 void TestBrowserThreadBundle::CreateThreads() { | 92 void TestBrowserThreadBundle::CreateThreads() { |
| 103 CHECK(!threads_created_); | 93 DCHECK(!threads_created_); |
| 104 | 94 |
| 105 // TaskScheduler can sometimes be externally provided by a | 95 scoped_async_task_scheduler_ = |
| 106 // base::test::ScopedTaskEnvironment in a parent fixture. In that case it's | 96 base::MakeUnique<base::test::ScopedAsyncTaskScheduler>(); |
| 107 // expected to have provided the MessageLoop as well (in which case | |
| 108 // |message_loop_| remains null in Init()). | |
| 109 CHECK(!base::TaskScheduler::GetInstance() || !message_loop_); | |
| 110 if (!base::TaskScheduler::GetInstance()) { | |
| 111 scoped_async_task_scheduler_ = | |
| 112 base::MakeUnique<base::test::ScopedAsyncTaskScheduler>(); | |
| 113 } | |
| 114 | 97 |
| 115 if (options_ & REAL_DB_THREAD) { | 98 if (options_ & REAL_DB_THREAD) { |
| 116 db_thread_ = base::MakeUnique<TestBrowserThread>(BrowserThread::DB); | 99 db_thread_.reset(new TestBrowserThread(BrowserThread::DB)); |
| 117 db_thread_->Start(); | 100 db_thread_->Start(); |
| 118 } else { | 101 } else { |
| 119 db_thread_ = base::MakeUnique<TestBrowserThread>( | 102 db_thread_.reset( |
| 120 BrowserThread::DB, base::MessageLoop::current()); | 103 new TestBrowserThread(BrowserThread::DB, message_loop_.get())); |
| 121 } | 104 } |
| 122 | 105 |
| 123 if (options_ & REAL_FILE_THREAD) { | 106 if (options_ & REAL_FILE_THREAD) { |
| 124 file_thread_ = base::MakeUnique<TestBrowserThread>(BrowserThread::FILE); | 107 file_thread_.reset(new TestBrowserThread(BrowserThread::FILE)); |
| 125 file_thread_->Start(); | 108 file_thread_->Start(); |
| 126 } else { | 109 } else { |
| 127 file_thread_ = base::MakeUnique<TestBrowserThread>( | 110 file_thread_.reset( |
| 128 BrowserThread::FILE, base::MessageLoop::current()); | 111 new TestBrowserThread(BrowserThread::FILE, message_loop_.get())); |
| 129 } | 112 } |
| 130 | 113 |
| 131 file_user_blocking_thread_ = base::MakeUnique<TestBrowserThread>( | 114 file_user_blocking_thread_.reset(new TestBrowserThread( |
| 132 BrowserThread::FILE_USER_BLOCKING, base::MessageLoop::current()); | 115 BrowserThread::FILE_USER_BLOCKING, message_loop_.get())); |
| 133 process_launcher_thread_ = base::MakeUnique<TestBrowserThread>( | 116 process_launcher_thread_.reset(new TestBrowserThread( |
| 134 BrowserThread::PROCESS_LAUNCHER, base::MessageLoop::current()); | 117 BrowserThread::PROCESS_LAUNCHER, message_loop_.get())); |
| 135 cache_thread_ = base::MakeUnique<TestBrowserThread>( | 118 cache_thread_.reset( |
| 136 BrowserThread::CACHE, base::MessageLoop::current()); | 119 new TestBrowserThread(BrowserThread::CACHE, message_loop_.get())); |
| 137 | 120 |
| 138 if (options_ & REAL_IO_THREAD) { | 121 if (options_ & REAL_IO_THREAD) { |
| 139 io_thread_ = base::MakeUnique<TestBrowserThread>(BrowserThread::IO); | 122 io_thread_.reset(new TestBrowserThread(BrowserThread::IO)); |
| 140 io_thread_->StartIOThread(); | 123 io_thread_->StartIOThread(); |
| 141 } else { | 124 } else { |
| 142 io_thread_ = base::MakeUnique<TestBrowserThread>( | 125 io_thread_.reset( |
| 143 BrowserThread::IO, base::MessageLoop::current()); | 126 new TestBrowserThread(BrowserThread::IO, message_loop_.get())); |
| 144 } | 127 } |
| 145 | 128 |
| 146 threads_created_ = true; | 129 threads_created_ = true; |
| 147 } | 130 } |
| 148 | 131 |
| 149 } // namespace content | 132 } // namespace content |
| OLD | NEW |