Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(294)

Side by Side Diff: content/public/test/test_browser_thread_bundle.cc

Issue 2860533002: Support base::test::ScopedTaskEnvironment and content::TestBrowserThreadBundle in the same scope. (Closed)
Patch Set: self-review Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/test/scoped_async_task_scheduler.h" 12 #include "base/test/scoped_async_task_scheduler.h"
12 #include "content/browser/browser_thread_impl.h" 13 #include "content/browser/browser_thread_impl.h"
14 #include "content/public/browser/browser_thread.h"
13 #include "content/public/test/test_browser_thread.h" 15 #include "content/public/test/test_browser_thread.h"
14 16
15 namespace content { 17 namespace content {
16 18
17 TestBrowserThreadBundle::TestBrowserThreadBundle() 19 TestBrowserThreadBundle::TestBrowserThreadBundle()
18 : TestBrowserThreadBundle(DEFAULT) {} 20 : TestBrowserThreadBundle(DEFAULT) {}
19 21
20 TestBrowserThreadBundle::TestBrowserThreadBundle(int options) 22 TestBrowserThreadBundle::TestBrowserThreadBundle(int options)
21 : options_(options), threads_created_(false) { 23 : options_(options), threads_created_(false) {
22 Init(); 24 Init();
(...skipping 29 matching lines...) Expand all
52 // it again in case any shutdown tasks have been posted to the pool from the 54 // it again in case any shutdown tasks have been posted to the pool from the
53 // threads above. 55 // threads above.
54 BrowserThreadImpl::FlushThreadPoolHelperForTesting(); 56 BrowserThreadImpl::FlushThreadPoolHelperForTesting();
55 base::RunLoop().RunUntilIdle(); 57 base::RunLoop().RunUntilIdle();
56 ui_thread_->Stop(); 58 ui_thread_->Stop();
57 base::RunLoop().RunUntilIdle(); 59 base::RunLoop().RunUntilIdle();
58 60
59 scoped_async_task_scheduler_.reset(); 61 scoped_async_task_scheduler_.reset();
60 62
61 base::RunLoop().RunUntilIdle(); 63 base::RunLoop().RunUntilIdle();
64 DCHECK(base::MessageLoop::current()->IsIdleForTesting());
62 65
63 // |message_loop_| needs to explicitly go away before fake threads in order 66 // |message_loop_| needs to explicitly go away before fake threads in order
64 // for DestructionObservers hooked to |message_loop_| to be able to invoke 67 // for DestructionObservers hooked to |message_loop_| to be able to invoke
65 // BrowserThread::CurrentlyOn() -- ref. ~TestBrowserThread(). 68 // BrowserThread::CurrentlyOn() -- ref. ~TestBrowserThread().
66 CHECK(message_loop_->IsIdleForTesting());
gab 2017/05/03 15:33:10 Why move (this was checking above comment) and cha
fdoray 2017/05/03 17:03:50 Done.
67 message_loop_.reset(); 69 message_loop_.reset();
68 } 70 }
69 71
70 void TestBrowserThreadBundle::Init() { 72 void TestBrowserThreadBundle::Init() {
71 // Check for conflicting options can't have two IO threads. 73 // Check for conflicting options can't have two IO threads.
72 CHECK(!(options_ & IO_MAINLOOP) || !(options_ & REAL_IO_THREAD)); 74 DCHECK(!(options_ & IO_MAINLOOP) || !(options_ & REAL_IO_THREAD));
gab 2017/05/03 15:33:10 ditto
fdoray 2017/05/03 17:03:50 Done.
73 // There must be a thread to start to use DONT_CREATE_THREADS 75 // There must be a thread to start to use DONT_CREATE_THREADS
74 CHECK((options_ & ~IO_MAINLOOP) != DONT_CREATE_THREADS); 76 DCHECK((options_ & ~IO_MAINLOOP) != DONT_CREATE_THREADS);
75 77
76 // Create the UI thread. In production, this work is done in 78 // Create the UI thread. In production, this work is done in
77 // BrowserMainLoop::MainMessageLoopStart(). 79 // BrowserMainLoop::MainMessageLoopStart().
gab 2017/05/03 15:33:10 Update comment to mention why it could exist alrea
fdoray 2017/05/03 17:03:50 Done.
78 if (options_ & IO_MAINLOOP) { 80 const base::MessageLoop::Type message_loop_type =
79 message_loop_.reset(new base::MessageLoopForIO()); 81 options_ & IO_MAINLOOP ? base::MessageLoop::TYPE_IO
80 } else { 82 : base::MessageLoop::TYPE_UI;
81 message_loop_.reset(new base::MessageLoopForUI()); 83 if (!base::MessageLoop::current())
82 } 84 message_loop_ = base::MakeUnique<base::MessageLoop>(message_loop_type);
85 DCHECK(base::MessageLoop::current()->IsType(message_loop_type));
83 86
84 ui_thread_.reset( 87 // Check that the UI thread hasn't already been initialized. This will fail if
85 new TestBrowserThread(BrowserThread::UI, message_loop_.get())); 88 // multiple TestBrowserThreadBundles are initialized in the same scope. This
89 // needs to be after MessageLoop initialization because
90 // BrowserThread::CurrentlyOn() checks that there is a MessageLoop.
91 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
gab 2017/05/03 15:33:10 You can use BrowserThread::IsThreadInitialized() t
fdoray 2017/05/03 17:03:50 Done.
92
93 ui_thread_ = base::MakeUnique<TestBrowserThread>(
94 BrowserThread::UI, base::MessageLoop::current());
86 95
87 if (!(options_ & DONT_CREATE_THREADS)) 96 if (!(options_ & DONT_CREATE_THREADS))
88 CreateThreads(); 97 CreateThreads();
89 } 98 }
90 99
91 // This method mimics the work done in BrowserMainLoop::CreateThreads(). 100 // This method mimics the work done in BrowserMainLoop::CreateThreads().
92 void TestBrowserThreadBundle::CreateThreads() { 101 void TestBrowserThreadBundle::CreateThreads() {
93 DCHECK(!threads_created_); 102 DCHECK(!threads_created_);
94 103
95 scoped_async_task_scheduler_ = 104 if (!base::TaskScheduler::GetInstance()) {
gab 2017/05/03 15:33:10 Above this: // TaskScheduler can sometimes be ext
fdoray 2017/05/03 17:03:50 Done.
96 base::MakeUnique<base::test::ScopedAsyncTaskScheduler>(); 105 scoped_async_task_scheduler_ =
106 base::MakeUnique<base::test::ScopedAsyncTaskScheduler>();
107 }
97 108
98 if (options_ & REAL_DB_THREAD) { 109 if (options_ & REAL_DB_THREAD) {
99 db_thread_.reset(new TestBrowserThread(BrowserThread::DB)); 110 db_thread_ = base::MakeUnique<TestBrowserThread>(BrowserThread::DB);
100 db_thread_->Start(); 111 db_thread_->Start();
101 } else { 112 } else {
102 db_thread_.reset( 113 db_thread_ = base::MakeUnique<TestBrowserThread>(
103 new TestBrowserThread(BrowserThread::DB, message_loop_.get())); 114 BrowserThread::DB, base::MessageLoop::current());
104 } 115 }
105 116
106 if (options_ & REAL_FILE_THREAD) { 117 if (options_ & REAL_FILE_THREAD) {
107 file_thread_.reset(new TestBrowserThread(BrowserThread::FILE)); 118 file_thread_ = base::MakeUnique<TestBrowserThread>(BrowserThread::FILE);
108 file_thread_->Start(); 119 file_thread_->Start();
109 } else { 120 } else {
110 file_thread_.reset( 121 file_thread_ = base::MakeUnique<TestBrowserThread>(
111 new TestBrowserThread(BrowserThread::FILE, message_loop_.get())); 122 BrowserThread::FILE, base::MessageLoop::current());
112 } 123 }
113 124
114 file_user_blocking_thread_.reset(new TestBrowserThread( 125 file_user_blocking_thread_ = base::MakeUnique<TestBrowserThread>(
115 BrowserThread::FILE_USER_BLOCKING, message_loop_.get())); 126 BrowserThread::FILE_USER_BLOCKING, base::MessageLoop::current());
116 process_launcher_thread_.reset(new TestBrowserThread( 127 process_launcher_thread_ = base::MakeUnique<TestBrowserThread>(
117 BrowserThread::PROCESS_LAUNCHER, message_loop_.get())); 128 BrowserThread::PROCESS_LAUNCHER, base::MessageLoop::current());
118 cache_thread_.reset( 129 cache_thread_ = base::MakeUnique<TestBrowserThread>(
119 new TestBrowserThread(BrowserThread::CACHE, message_loop_.get())); 130 BrowserThread::CACHE, base::MessageLoop::current());
120 131
121 if (options_ & REAL_IO_THREAD) { 132 if (options_ & REAL_IO_THREAD) {
122 io_thread_.reset(new TestBrowserThread(BrowserThread::IO)); 133 io_thread_ = base::MakeUnique<TestBrowserThread>(BrowserThread::IO);
123 io_thread_->StartIOThread(); 134 io_thread_->StartIOThread();
124 } else { 135 } else {
125 io_thread_.reset( 136 io_thread_ = base::MakeUnique<TestBrowserThread>(
126 new TestBrowserThread(BrowserThread::IO, message_loop_.get())); 137 BrowserThread::IO, base::MessageLoop::current());
127 } 138 }
128 139
129 threads_created_ = true; 140 threads_created_ = true;
130 } 141 }
131 142
132 } // namespace content 143 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698