1 // Copyright 2014 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.
5 #include "ios/web/public/test/test_web_thread_bundle.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "ios/web/public/test/test_web_thread.h"
10 #include "ios/web/web_thread_impl.h"
14 // Implmentation of TestWebThreadBundle using TestWebThreads.
15 // TODO(stuartmorgan): The only reason this is a separate impl class is to
16 // keep the implementation details out of the header so that it can be shared
17 // with the adapter implementation that uses TestBrowserThreadBundle. Once that
18 // version is gone, fold this into TestWebThreadBundle.
19 class TestWebThreadBundleImpl
{
21 explicit TestWebThreadBundleImpl();
22 explicit TestWebThreadBundleImpl(int options
);
24 ~TestWebThreadBundleImpl();
27 void Init(int options
);
29 scoped_ptr
<base::MessageLoop
> message_loop_
;
30 scoped_ptr
<TestWebThread
> ui_thread_
;
31 scoped_ptr
<TestWebThread
> db_thread_
;
32 scoped_ptr
<TestWebThread
> file_thread_
;
33 scoped_ptr
<TestWebThread
> file_user_blocking_thread_
;
34 scoped_ptr
<TestWebThread
> cache_thread_
;
35 scoped_ptr
<TestWebThread
> io_thread_
;
37 DISALLOW_COPY_AND_ASSIGN(TestWebThreadBundleImpl
);
40 TestWebThreadBundleImpl::TestWebThreadBundleImpl() {
41 Init(TestWebThreadBundle::DEFAULT
);
44 TestWebThreadBundleImpl::TestWebThreadBundleImpl(int options
) {
48 TestWebThreadBundleImpl::~TestWebThreadBundleImpl() {
49 // To avoid memory leaks, ensure that any tasks posted to the blocking pool
50 // via PostTaskAndReply are able to reply back to the originating thread, by
51 // flushing the blocking pool while the browser threads still exist.
52 base::RunLoop().RunUntilIdle();
53 WebThreadImpl::FlushThreadPoolHelperForTesting();
55 // To ensure a clean teardown, each thread's message loop must be flushed
56 // just before the thread is destroyed. But destroying a fake thread does not
57 // automatically flush the message loop, so do it manually.
58 // See http://crbug.com/247525 for discussion.
59 base::RunLoop().RunUntilIdle();
61 base::RunLoop().RunUntilIdle();
62 cache_thread_
.reset();
63 base::RunLoop().RunUntilIdle();
64 file_user_blocking_thread_
.reset();
65 base::RunLoop().RunUntilIdle();
67 base::RunLoop().RunUntilIdle();
69 base::RunLoop().RunUntilIdle();
70 // This is the point at which the thread pool is normally shut down. So flush
71 // it again in case any shutdown tasks have been posted to the pool from the
73 WebThreadImpl::FlushThreadPoolHelperForTesting();
74 base::RunLoop().RunUntilIdle();
76 base::RunLoop().RunUntilIdle();
79 void TestWebThreadBundleImpl::Init(int options
) {
80 if (options
& TestWebThreadBundle::IO_MAINLOOP
) {
81 message_loop_
.reset(new base::MessageLoopForIO());
83 message_loop_
.reset(new base::MessageLoopForUI());
86 ui_thread_
.reset(new TestWebThread(WebThread::UI
, message_loop_
.get()));
88 if (options
& TestWebThreadBundle::REAL_DB_THREAD
) {
89 db_thread_
.reset(new TestWebThread(WebThread::DB
));
92 db_thread_
.reset(new TestWebThread(WebThread::DB
, message_loop_
.get()));
95 if (options
& TestWebThreadBundle::REAL_FILE_THREAD
) {
96 file_thread_
.reset(new TestWebThread(WebThread::FILE));
97 file_thread_
->Start();
99 file_thread_
.reset(new TestWebThread(WebThread::FILE, message_loop_
.get()));
102 if (options
& TestWebThreadBundle::REAL_FILE_USER_BLOCKING_THREAD
) {
103 file_user_blocking_thread_
.reset(
104 new TestWebThread(WebThread::FILE_USER_BLOCKING
));
105 file_user_blocking_thread_
->Start();
107 file_user_blocking_thread_
.reset(
108 new TestWebThread(WebThread::FILE_USER_BLOCKING
, message_loop_
.get()));
111 if (options
& TestWebThreadBundle::REAL_CACHE_THREAD
) {
112 cache_thread_
.reset(new TestWebThread(WebThread::CACHE
));
113 cache_thread_
->Start();
116 new TestWebThread(WebThread::CACHE
, message_loop_
.get()));
119 if (options
& TestWebThreadBundle::REAL_IO_THREAD
) {
120 io_thread_
.reset(new TestWebThread(WebThread::IO
));
121 io_thread_
->StartIOThread();
123 io_thread_
.reset(new TestWebThread(WebThread::IO
, message_loop_
.get()));
127 #pragma mark - TestWebThreadBundle
129 TestWebThreadBundle::TestWebThreadBundle()
130 : impl_(new TestWebThreadBundleImpl()) {
133 TestWebThreadBundle::TestWebThreadBundle(int options
)
134 : impl_(new TestWebThreadBundleImpl(options
)) {
137 TestWebThreadBundle::~TestWebThreadBundle() {