OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // TestBrowserThreadBundle is a convenience class for creating a set of | 5 // TestBrowserThreadBundle is a convenience class for creating a set of |
6 // TestBrowserThreads, a blocking pool, and a task scheduler in unit tests. For | 6 // TestBrowserThreads, a blocking pool, and a task scheduler in unit tests. For |
7 // most tests, it is sufficient to just instantiate the TestBrowserThreadBundle | 7 // most tests, it is sufficient to just instantiate the TestBrowserThreadBundle |
8 // as a member variable. It is a good idea to put the TestBrowserThreadBundle as | 8 // as a member variable. It is a good idea to put the TestBrowserThreadBundle as |
9 // the first member variable in test classes, so it is destroyed last, and the | 9 // the first member variable in test classes, so it is destroyed last, and the |
10 // test threads always exist from the perspective of other classes. | 10 // test threads always exist from the perspective of other classes. |
11 // | 11 // |
12 // By default, all of the created TestBrowserThreads and the task scheduler will | 12 // By default, all of the created TestBrowserThreads and the task scheduler will |
13 // be backed by a single shared MessageLoop. If a test truly needs separate | 13 // be backed by a single shared MessageLoop. If a test truly needs separate |
14 // threads, it can do so by passing the appropriate combination of option values | 14 // threads, it can do so by passing the appropriate combination of option values |
15 // during the TestBrowserThreadBundle construction. | 15 // during the TestBrowserThreadBundle construction. |
16 // | 16 // |
17 // To synchronously run tasks posted to task scheduler or to TestBrowserThreads | 17 // To synchronously run tasks posted to task scheduler or to TestBrowserThreads |
18 // that use the shared MessageLoop, call RunLoop::Run/RunUntilIdle() on the | 18 // that use the shared MessageLoop, call RunLoop::Run/RunUntilIdle() on the |
19 // thread where the TestBrowserThreadBundle lives. The destructor of | 19 // thread where the TestBrowserThreadBundle lives. The destructor of |
20 // TestBrowserThreadBundle runs remaining TestBrowserThreads tasks, remaining | 20 // TestBrowserThreadBundle runs remaining TestBrowserThreads tasks, remaining |
21 // blocking pool tasks, and remaining BLOCK_SHUTDOWN task scheduler tasks. | 21 // blocking pool tasks, and remaining BLOCK_SHUTDOWN task scheduler tasks. |
22 // | 22 // |
23 // If a test needs a MessageLoopForIO on the main thread, it should use the | 23 // If a test needs a MessageLoopForIO on the main thread, it should use the |
24 // IO_MAINLOOP option. This also allows task scheduler tasks to use | 24 // IO_MAINLOOP option. This also allows task scheduler tasks to use |
25 // FileDescriptorWatcher. Most of the time, IO_MAINLOOP avoids needing to use a | 25 // FileDescriptorWatcher. Most of the time, IO_MAINLOOP avoids needing to use a |
26 // REAL_IO_THREAD. | 26 // REAL_IO_THREAD. |
27 // | 27 // |
28 // For some tests it is important to emulate real browser startup. During real | 28 // For some tests it is important to emulate real browser startup. During real |
29 // browser startup some initialization is done (e.g. creation of thread objects) | 29 // browser startup, the main MessageLoop is created before other threads. |
30 // between creating the main thread message loop, which is bound to the existing | 30 // Passing DONT_CREATE_THREADS to constructor will delay creating other threads |
31 // main thread, and starting the other threads. Passing DONT_START_THREADS to | 31 // until the test explicitly calls CreateThreads(). |
32 // constructor will delay staring these other threads until the test explicitly | |
33 // calls Start(). | |
34 // | 32 // |
35 // DONT_START_THREADS should only be used when the options specify at least | 33 // DONT_CREATE_THREADS should only be used when the options specify at least |
36 // one real thread other than the main thread. | 34 // one real thread other than the main thread. |
37 | 35 |
38 #ifndef CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ | 36 #ifndef CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ |
39 #define CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ | 37 #define CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ |
40 | 38 |
41 #include <memory> | 39 #include <memory> |
42 | 40 |
43 #include "base/macros.h" | 41 #include "base/macros.h" |
44 | 42 |
45 namespace base { | 43 namespace base { |
(...skipping 11 matching lines...) Expand all Loading... |
57 public: | 55 public: |
58 // Used to specify the type of MessageLoop that backs the UI thread, and | 56 // Used to specify the type of MessageLoop that backs the UI thread, and |
59 // which of the named BrowserThreads should be backed by a real | 57 // which of the named BrowserThreads should be backed by a real |
60 // threads. The UI thread is always the main thread in a unit test. | 58 // threads. The UI thread is always the main thread in a unit test. |
61 enum Options { | 59 enum Options { |
62 DEFAULT = 0x00, | 60 DEFAULT = 0x00, |
63 IO_MAINLOOP = 0x01, | 61 IO_MAINLOOP = 0x01, |
64 REAL_DB_THREAD = 0x02, | 62 REAL_DB_THREAD = 0x02, |
65 REAL_FILE_THREAD = 0x08, | 63 REAL_FILE_THREAD = 0x08, |
66 REAL_IO_THREAD = 0x10, | 64 REAL_IO_THREAD = 0x10, |
67 DONT_START_THREADS = 0x20, | 65 DONT_CREATE_THREADS = 0x20, |
68 }; | 66 }; |
69 | 67 |
70 TestBrowserThreadBundle(); | 68 TestBrowserThreadBundle(); |
71 explicit TestBrowserThreadBundle(int options); | 69 explicit TestBrowserThreadBundle(int options); |
72 | 70 |
73 // Start the real threads; should only be called from other classes if the | 71 // Creates threads; should only be called from other classes if the |
74 // DONT_START_THREADS option was used when the bundle was created. | 72 // DONT_CREATE_THREADS option was used when the bundle was created. |
75 void Start(); | 73 void CreateThreads(); |
76 | 74 |
77 ~TestBrowserThreadBundle(); | 75 ~TestBrowserThreadBundle(); |
78 | 76 |
79 private: | 77 private: |
80 void Init(); | 78 void Init(); |
81 | 79 |
82 std::unique_ptr<base::MessageLoop> message_loop_; | 80 std::unique_ptr<base::MessageLoop> message_loop_; |
83 std::unique_ptr<base::test::ScopedTaskScheduler> task_scheduler_; | 81 std::unique_ptr<base::test::ScopedTaskScheduler> task_scheduler_; |
84 std::unique_ptr<TestBrowserThread> ui_thread_; | 82 std::unique_ptr<TestBrowserThread> ui_thread_; |
85 std::unique_ptr<TestBrowserThread> db_thread_; | 83 std::unique_ptr<TestBrowserThread> db_thread_; |
86 std::unique_ptr<TestBrowserThread> file_thread_; | 84 std::unique_ptr<TestBrowserThread> file_thread_; |
87 std::unique_ptr<TestBrowserThread> file_user_blocking_thread_; | 85 std::unique_ptr<TestBrowserThread> file_user_blocking_thread_; |
88 std::unique_ptr<TestBrowserThread> process_launcher_thread_; | 86 std::unique_ptr<TestBrowserThread> process_launcher_thread_; |
89 std::unique_ptr<TestBrowserThread> cache_thread_; | 87 std::unique_ptr<TestBrowserThread> cache_thread_; |
90 std::unique_ptr<TestBrowserThread> io_thread_; | 88 std::unique_ptr<TestBrowserThread> io_thread_; |
91 | 89 |
92 int options_; | 90 int options_; |
93 bool threads_started_; | 91 bool threads_created_; |
94 | 92 |
95 DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadBundle); | 93 DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadBundle); |
96 }; | 94 }; |
97 | 95 |
98 } // namespace content | 96 } // namespace content |
99 | 97 |
100 #endif // CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ | 98 #endif // CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ |
OLD | NEW |