| 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 in unit tests. For most tests, it is sufficient to | 6 // TestBrowserThreads, a blocking pool, and a task scheduler in unit tests. For |
| 7 // just instantiate the TestBrowserThreadBundle as a member variable. | 7 // most tests, it is sufficient to just instantiate the TestBrowserThreadBundle |
| 8 // It is a good idea to put the TestBrowserThreadBundle as the first member | 8 // as a member variable. It is a good idea to put the TestBrowserThreadBundle as |
| 9 // variable in test classes, so it is destroyed last, and the test threads | 9 // the first member variable in test classes, so it is destroyed last, and the |
| 10 // 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 will be backed by a single | 12 // By default, all of the created TestBrowserThreads and the task scheduler will |
| 13 // shared MessageLoop. If a test truly needs separate threads, it can do | 13 // be backed by a single shared MessageLoop. If a test truly needs separate |
| 14 // so by passing the appropriate combination of option values during | 14 // threads, it can do so by passing the appropriate combination of option values |
| 15 // the TestBrowserThreadBundle construction. | 15 // during the TestBrowserThreadBundle construction. |
| 16 // | 16 // |
| 17 // The TestBrowserThreadBundle will attempt to drain the MessageLoop on | 17 // To synchronously run tasks posted to task scheduler or to TestBrowserThreads |
| 18 // destruction. Sometimes a test needs to drain currently enqueued tasks | 18 // that use the shared MessageLoop, call RunLoop::Run/RunUntilIdle() on the |
| 19 // mid-test. Browser tests should call content::RunAllPendingInMessageLoop(). | 19 // thread where the TestBrowserThreadBundle lives. The destructor of |
| 20 // Unit tests should use base::RunLoop (e.g., base::RunLoop().RunUntilIdle()). | 20 // TestBrowserThreadBundle runs remaining TestBrowserThreads tasks, remaining |
| 21 // TODO(phajdan.jr): Revise this comment after switch to Aura. | 21 // blocking pool tasks, and remaining BLOCK_SHUTDOWN task scheduler tasks. |
| 22 // | 22 // |
| 23 // The TestBrowserThreadBundle will also flush the blocking pool on destruction. | 23 // If a test needs a MessageLoopForIO on the main thread, it should use the |
| 24 // We do this to avoid memory leaks, particularly in the case of threads posting | 24 // IO_MAINLOOP option. This also allows task scheduler tasks to use |
| 25 // tasks to the blocking pool via PostTaskAndReply. By ensuring that the tasks | 25 // FileDescriptorWatcher. Most of the time, IO_MAINLOOP avoids needing to use a |
| 26 // are run while the originating TestBroswserThreads still exist, we prevent | 26 // REAL_IO_THREAD. |
| 27 // leakage of PostTaskAndReplyRelay objects. We also flush the blocking pool | |
| 28 // again at the point where it would normally be shut down, to better simulate | |
| 29 // the normal thread shutdown process. | |
| 30 // | |
| 31 // Some tests using the IO thread expect a MessageLoopForIO. Passing | |
| 32 // IO_MAINLOOP will use a MessageLoopForIO for the main MessageLoop. | |
| 33 // Most of the time, this avoids needing to use a REAL_IO_THREAD. | |
| 34 // | 27 // |
| 35 // 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 |
| 36 // browser startup some initialization is done (e.g. creation of thread objects) | 29 // browser startup some initialization is done (e.g. creation of thread objects) |
| 37 // between creating the main thread message loop, which is bound to the existing | 30 // between creating the main thread message loop, which is bound to the existing |
| 38 // main thread, and starting the other threads. Passing DONT_START_THREADS to | 31 // main thread, and starting the other threads. Passing DONT_START_THREADS to |
| 39 // constructor will delay staring these other threads until the test explicitly | 32 // constructor will delay staring these other threads until the test explicitly |
| 40 // calls Start(). | 33 // calls Start(). |
| 41 // | 34 // |
| 42 // DONT_START_THREADS should only be used when the options specify at least | 35 // DONT_START_THREADS should only be used when the options specify at least |
| 43 // one real thread other than the main thread. | 36 // one real thread other than the main thread. |
| 44 | 37 |
| 45 #ifndef CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ | 38 #ifndef CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ |
| 46 #define CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ | 39 #define CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ |
| 47 | 40 |
| 48 #include <memory> | 41 #include <memory> |
| 49 | 42 |
| 50 #include "base/macros.h" | 43 #include "base/macros.h" |
| 51 | 44 |
| 52 namespace base { | 45 namespace base { |
| 53 class MessageLoop; | 46 class MessageLoop; |
| 47 namespace test { |
| 48 class ScopedTaskScheduler; |
| 49 } // namespace test |
| 54 } // namespace base | 50 } // namespace base |
| 55 | 51 |
| 56 namespace content { | 52 namespace content { |
| 57 | 53 |
| 58 class TestBrowserThread; | 54 class TestBrowserThread; |
| 59 | 55 |
| 60 class TestBrowserThreadBundle { | 56 class TestBrowserThreadBundle { |
| 61 public: | 57 public: |
| 62 // Used to specify the type of MessageLoop that backs the UI thread, and | 58 // Used to specify the type of MessageLoop that backs the UI thread, and |
| 63 // which of the named BrowserThreads should be backed by a real | 59 // which of the named BrowserThreads should be backed by a real |
| (...skipping 13 matching lines...) Expand all Loading... |
| 77 // Start the real threads; should only be called from other classes if the | 73 // Start the real threads; should only be called from other classes if the |
| 78 // DONT_START_THREADS option was used when the bundle was created. | 74 // DONT_START_THREADS option was used when the bundle was created. |
| 79 void Start(); | 75 void Start(); |
| 80 | 76 |
| 81 ~TestBrowserThreadBundle(); | 77 ~TestBrowserThreadBundle(); |
| 82 | 78 |
| 83 private: | 79 private: |
| 84 void Init(); | 80 void Init(); |
| 85 | 81 |
| 86 std::unique_ptr<base::MessageLoop> message_loop_; | 82 std::unique_ptr<base::MessageLoop> message_loop_; |
| 83 std::unique_ptr<base::test::ScopedTaskScheduler> task_scheduler_; |
| 87 std::unique_ptr<TestBrowserThread> ui_thread_; | 84 std::unique_ptr<TestBrowserThread> ui_thread_; |
| 88 std::unique_ptr<TestBrowserThread> db_thread_; | 85 std::unique_ptr<TestBrowserThread> db_thread_; |
| 89 std::unique_ptr<TestBrowserThread> file_thread_; | 86 std::unique_ptr<TestBrowserThread> file_thread_; |
| 90 std::unique_ptr<TestBrowserThread> file_user_blocking_thread_; | 87 std::unique_ptr<TestBrowserThread> file_user_blocking_thread_; |
| 91 std::unique_ptr<TestBrowserThread> process_launcher_thread_; | 88 std::unique_ptr<TestBrowserThread> process_launcher_thread_; |
| 92 std::unique_ptr<TestBrowserThread> cache_thread_; | 89 std::unique_ptr<TestBrowserThread> cache_thread_; |
| 93 std::unique_ptr<TestBrowserThread> io_thread_; | 90 std::unique_ptr<TestBrowserThread> io_thread_; |
| 94 | 91 |
| 95 int options_; | 92 int options_; |
| 96 bool threads_started_; | 93 bool threads_started_; |
| 97 | 94 |
| 98 DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadBundle); | 95 DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadBundle); |
| 99 }; | 96 }; |
| 100 | 97 |
| 101 } // namespace content | 98 } // namespace content |
| 102 | 99 |
| 103 #endif // CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ | 100 #endif // CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ |
| OLD | NEW |