blob: 8adc61ba58b87b1d6a37c33aa113e0e389390d37 [file] [log] [blame]
[email protected]ec04d3f2013-06-06 21:31:391// 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// TestBrowserThreadBundle is a convenience class for creating a set of
6// TestBrowserThreads in unit tests. For most tests, it is sufficient to
7// just instantiate the TestBrowserThreadBundle as a member variable.
8//
9// By default, all of the created TestBrowserThreads will be backed by a single
10// shared MessageLoop. If a test truly needs separate threads, it can do
11// so by passing the appropriate combination of RealThreadsMask values during
12// the TestBrowserThreadBundle construction.
13//
14// The TestBrowserThreadBundle will attempt to drain the MessageLoop on
15// destruction. Sometimes a test needs to drain currently enqueued tasks
16// mid-test. Browser tests should call content::RunAllPendingInMessageLoop().
17// Unit tests should use base::RunLoop (e.g., base::RunLoop().RunUntilIdle()).
18// TODO(phajdan.jr): Revise this comment after switch to Aura.
19//
20// Some tests using the IO thread expect a MessageLoopForIO. Passing
21// IO_MAINLOOP will use a MessageLoopForIO for the main MessageLoop.
22// Most of the time, this avoids needing to use a REAL_IO_THREAD.
23
24#ifndef CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_TEST_BUNDLE_H_
25#define CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_TEST_BUNDLE_H_
26
27#include "base/memory/scoped_ptr.h"
28#include "testing/gtest/include/gtest/gtest.h"
29
30namespace base {
31class MessageLoop;
32} // namespace base
33
34namespace content {
35
36class TestBrowserThread;
37
38class TestBrowserThreadBundle {
39 public:
40 // Used to specify the type of MessageLoop that backs the UI thread, and
41 // which of the named BrowserThreads should be backed by a real
42 // threads. The UI thread is always the main thread in a unit test.
43 enum Options {
44 DEFAULT = 0x00,
45 IO_MAINLOOP = 0x01,
46 REAL_DB_THREAD = 0x02,
47 REAL_WEBKIT_DEPRECATED_THREAD = 0x04,
48 REAL_FILE_THREAD = 0x08,
49 REAL_FILE_USER_BLOCKING_THREAD = 0x10,
50 REAL_PROCESS_LAUNCHER_THREAD = 0x20,
51 REAL_CACHE_THREAD = 0x40,
52 REAL_IO_THREAD = 0x80,
53 };
54
55 TestBrowserThreadBundle();
56 explicit TestBrowserThreadBundle(int options);
57
58 ~TestBrowserThreadBundle();
59
60 private:
61 void Init(int options);
62
63 scoped_ptr<base::MessageLoop> message_loop_;
64 scoped_ptr<TestBrowserThread> ui_thread_;
65 scoped_ptr<TestBrowserThread> db_thread_;
66 scoped_ptr<TestBrowserThread> webkit_deprecated_thread_;
67 scoped_ptr<TestBrowserThread> file_thread_;
68 scoped_ptr<TestBrowserThread> file_user_blocking_thread_;
69 scoped_ptr<TestBrowserThread> process_launcher_thread_;
70 scoped_ptr<TestBrowserThread> cache_thread_;
71 scoped_ptr<TestBrowserThread> io_thread_;
72
73 DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadBundle);
74};
75
76} // namespace content
77
78#endif /* CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_TEST_BUNDLE_H_ */