test_browser_thread_bundle.cc revision 9ab5563a3196760eb381d102cbb2bc0f7abc6a50
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#include "content/public/test/test_browser_thread_bundle.h" 6 7#include "base/message_loop/message_loop.h" 8#include "base/run_loop.h" 9#include "content/public/test/test_browser_thread.h" 10 11namespace content { 12 13TestBrowserThreadBundle::TestBrowserThreadBundle() { 14 Init(DEFAULT); 15} 16 17TestBrowserThreadBundle::TestBrowserThreadBundle(int options) { 18 Init(options); 19} 20 21TestBrowserThreadBundle::~TestBrowserThreadBundle() { 22 // To ensure a clean teardown, each thread's message loop must be flushed 23 // just before the thread is destroyed. But destroying a fake thread does not 24 // automatically flush the message loop, so we have to do it manually. 25 // See http://crbug.com/247525 for discussion. 26 base::RunLoop().RunUntilIdle(); 27 io_thread_.reset(); 28 base::RunLoop().RunUntilIdle(); 29 cache_thread_.reset(); 30 base::RunLoop().RunUntilIdle(); 31 process_launcher_thread_.reset(); 32 base::RunLoop().RunUntilIdle(); 33 file_user_blocking_thread_.reset(); 34 base::RunLoop().RunUntilIdle(); 35 file_thread_.reset(); 36 base::RunLoop().RunUntilIdle(); 37 webkit_deprecated_thread_.reset(); 38 base::RunLoop().RunUntilIdle(); 39 db_thread_.reset(); 40 base::RunLoop().RunUntilIdle(); 41 ui_thread_.reset(); 42 base::RunLoop().RunUntilIdle(); 43} 44 45void TestBrowserThreadBundle::Init(int options) { 46 if (options & IO_MAINLOOP) { 47 message_loop_.reset(new base::MessageLoopForIO()); 48 } else { 49 message_loop_.reset(new base::MessageLoopForUI()); 50 } 51 52 ui_thread_.reset(new TestBrowserThread(BrowserThread::UI, 53 message_loop_.get())); 54 55 if (options & REAL_DB_THREAD) { 56 db_thread_.reset(new TestBrowserThread(BrowserThread::DB)); 57 db_thread_->Start(); 58 } else { 59 db_thread_.reset(new TestBrowserThread(BrowserThread::DB, 60 message_loop_.get())); 61 } 62 63 if (options & REAL_WEBKIT_DEPRECATED_THREAD) { 64 webkit_deprecated_thread_.reset( 65 new TestBrowserThread(BrowserThread::WEBKIT_DEPRECATED)); 66 webkit_deprecated_thread_->Start(); 67 } else { 68 webkit_deprecated_thread_.reset( 69 new TestBrowserThread(BrowserThread::WEBKIT_DEPRECATED, 70 message_loop_.get())); 71 } 72 73 if (options & REAL_FILE_THREAD) { 74 file_thread_.reset(new TestBrowserThread(BrowserThread::FILE)); 75 file_thread_->Start(); 76 } else { 77 file_thread_.reset(new TestBrowserThread(BrowserThread::FILE, 78 message_loop_.get())); 79 } 80 81 if (options & REAL_FILE_USER_BLOCKING_THREAD) { 82 file_user_blocking_thread_.reset( 83 new TestBrowserThread(BrowserThread::FILE_USER_BLOCKING)); 84 file_user_blocking_thread_->Start(); 85 } else { 86 file_user_blocking_thread_.reset( 87 new TestBrowserThread(BrowserThread::FILE_USER_BLOCKING, 88 message_loop_.get())); 89 } 90 91 if (options & REAL_PROCESS_LAUNCHER_THREAD) { 92 process_launcher_thread_.reset( 93 new TestBrowserThread(BrowserThread::PROCESS_LAUNCHER)); 94 process_launcher_thread_->Start(); 95 } else { 96 process_launcher_thread_.reset( 97 new TestBrowserThread(BrowserThread::PROCESS_LAUNCHER, 98 message_loop_.get())); 99 } 100 101 if (options & REAL_CACHE_THREAD) { 102 cache_thread_.reset(new TestBrowserThread(BrowserThread::CACHE)); 103 cache_thread_->Start(); 104 } else { 105 cache_thread_.reset(new TestBrowserThread(BrowserThread::CACHE, 106 message_loop_.get())); 107 } 108 109 if (options & REAL_IO_THREAD) { 110 io_thread_.reset(new TestBrowserThread(BrowserThread::IO)); 111 io_thread_->StartIOThread(); 112 } else { 113 io_thread_.reset( 114 new TestBrowserThread(BrowserThread::IO, message_loop_.get())); 115 } 116} 117 118} // namespace content 119