test_browser_thread_bundle.cc revision ca12bfac764ba476d6cd062bf1dde12cc64c3f40
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  db_thread_.reset();
38  base::RunLoop().RunUntilIdle();
39  ui_thread_.reset();
40  base::RunLoop().RunUntilIdle();
41}
42
43void TestBrowserThreadBundle::Init(int options) {
44  if (options & IO_MAINLOOP) {
45    message_loop_.reset(new base::MessageLoopForIO());
46  } else {
47    message_loop_.reset(new base::MessageLoopForUI());
48  }
49
50  ui_thread_.reset(new TestBrowserThread(BrowserThread::UI,
51                                         message_loop_.get()));
52
53  if (options & REAL_DB_THREAD) {
54    db_thread_.reset(new TestBrowserThread(BrowserThread::DB));
55    db_thread_->Start();
56  } else {
57    db_thread_.reset(new TestBrowserThread(BrowserThread::DB,
58                                           message_loop_.get()));
59  }
60
61  if (options & REAL_FILE_THREAD) {
62    file_thread_.reset(new TestBrowserThread(BrowserThread::FILE));
63    file_thread_->Start();
64  } else {
65    file_thread_.reset(new TestBrowserThread(BrowserThread::FILE,
66                                             message_loop_.get()));
67  }
68
69  if (options & REAL_FILE_USER_BLOCKING_THREAD) {
70    file_user_blocking_thread_.reset(
71        new TestBrowserThread(BrowserThread::FILE_USER_BLOCKING));
72    file_user_blocking_thread_->Start();
73  } else {
74    file_user_blocking_thread_.reset(
75        new TestBrowserThread(BrowserThread::FILE_USER_BLOCKING,
76                              message_loop_.get()));
77  }
78
79  if (options & REAL_PROCESS_LAUNCHER_THREAD) {
80    process_launcher_thread_.reset(
81        new TestBrowserThread(BrowserThread::PROCESS_LAUNCHER));
82    process_launcher_thread_->Start();
83  } else {
84    process_launcher_thread_.reset(
85        new TestBrowserThread(BrowserThread::PROCESS_LAUNCHER,
86                              message_loop_.get()));
87  }
88
89  if (options & REAL_CACHE_THREAD) {
90    cache_thread_.reset(new TestBrowserThread(BrowserThread::CACHE));
91    cache_thread_->Start();
92  } else {
93    cache_thread_.reset(new TestBrowserThread(BrowserThread::CACHE,
94                                              message_loop_.get()));
95  }
96
97  if (options & REAL_IO_THREAD) {
98    io_thread_.reset(new TestBrowserThread(BrowserThread::IO));
99    io_thread_->StartIOThread();
100  } else {
101    io_thread_.reset(
102        new TestBrowserThread(BrowserThread::IO, message_loop_.get()));
103  }
104}
105
106}  // namespace content
107