Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / public / test / test_browser_thread_bundle.h
blobc3abfeb679053b713c0edfe1c2585241a0b9a11e
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.
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.
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.
20 // The TestBrowserThreadBundle will also flush the blocking pool on destruction.
21 // We do this to avoid memory leaks, particularly in the case of threads posting
22 // tasks to the blocking pool via PostTaskAndReply. By ensuring that the tasks
23 // are run while the originating TestBroswserThreads still exist, we prevent
24 // leakage of PostTaskAndReplyRelay objects. We also flush the blocking pool
25 // again at the point where it would normally be shut down, to better simulate
26 // the normal thread shutdown process.
28 // Some tests using the IO thread expect a MessageLoopForIO. Passing
29 // IO_MAINLOOP will use a MessageLoopForIO for the main MessageLoop.
30 // Most of the time, this avoids needing to use a REAL_IO_THREAD.
32 #ifndef CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_
33 #define CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_
35 #include "base/memory/scoped_ptr.h"
37 namespace base {
38 class MessageLoop;
39 } // namespace base
41 namespace content {
43 class TestBrowserThread;
45 class TestBrowserThreadBundle {
46 public:
47 // Used to specify the type of MessageLoop that backs the UI thread, and
48 // which of the named BrowserThreads should be backed by a real
49 // threads. The UI thread is always the main thread in a unit test.
50 enum Options {
51 DEFAULT = 0x00,
52 IO_MAINLOOP = 0x01,
53 REAL_DB_THREAD = 0x02,
54 REAL_FILE_THREAD = 0x08,
55 REAL_FILE_USER_BLOCKING_THREAD = 0x10,
56 REAL_PROCESS_LAUNCHER_THREAD = 0x20,
57 REAL_CACHE_THREAD = 0x40,
58 REAL_IO_THREAD = 0x80,
61 TestBrowserThreadBundle();
62 explicit TestBrowserThreadBundle(int options);
64 ~TestBrowserThreadBundle();
66 private:
67 void Init(int options);
69 scoped_ptr<base::MessageLoop> message_loop_;
70 scoped_ptr<TestBrowserThread> ui_thread_;
71 scoped_ptr<TestBrowserThread> db_thread_;
72 scoped_ptr<TestBrowserThread> file_thread_;
73 scoped_ptr<TestBrowserThread> file_user_blocking_thread_;
74 scoped_ptr<TestBrowserThread> process_launcher_thread_;
75 scoped_ptr<TestBrowserThread> cache_thread_;
76 scoped_ptr<TestBrowserThread> io_thread_;
78 DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadBundle);
81 } // namespace content
83 #endif // CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_