| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef IOS_WEB_PUBLIC_TEST_TEST_WEB_THREAD_BUNDLE_H_ | 5 #ifndef IOS_WEB_PUBLIC_TEST_TEST_WEB_THREAD_BUNDLE_H_ |
| 6 #define IOS_WEB_PUBLIC_TEST_TEST_WEB_THREAD_BUNDLE_H_ | 6 #define IOS_WEB_PUBLIC_TEST_TEST_WEB_THREAD_BUNDLE_H_ |
| 7 | 7 |
| 8 // TestWebThreadBundle is a convenience class for creating a set of | 8 // TestWebThreadBundle is a convenience class for creating a set of |
| 9 // TestWebThreads, a blocking pool, and a task scheduler in unit tests. For | 9 // TestWebThreads, a blocking pool, and a task scheduler in unit tests. For |
| 10 // most tests, it is sufficient to just instantiate the TestWebThreadBundle as a | 10 // most tests, it is sufficient to just instantiate the TestWebThreadBundle as a |
| 11 // member variable. It is a good idea to put the TestWebThreadBundle as the | 11 // member variable. It is a good idea to put the TestWebThreadBundle as the |
| 12 // first member variable in test classes, so it is destroyed last, and the test | 12 // first member variable in test classes, so it is destroyed last, and the test |
| 13 // threads always exist from the perspective of other classes. | 13 // threads always exist from the perspective of other classes. |
| 14 // | 14 // |
| 15 // By default, all of the created TestWebThreads and the task scheduler will | 15 // By default, all of the created TestWebThreads will be backed by a single |
| 16 // be backed by a single shared MessageLoop. If a test truly needs separate | 16 // shared MessageLoop. If a test truly needs separate threads, it can do so by |
| 17 // threads, it can do so by passing the appropriate combination of option values | 17 // passing the appropriate combination of option values during the |
| 18 // during the TestWebThreadBundle construction. | 18 // TestWebThreadBundle construction. |
| 19 // | 19 // |
| 20 // To synchronously run tasks posted to task scheduler or to TestWebThreads | 20 // To synchronously run tasks posted to TestWebThreads that use the shared |
| 21 // that use the shared MessageLoop, call RunLoop::Run/RunUntilIdle() on the | 21 // MessageLoop, call RunLoop::Run/RunUntilIdle() on the thread where the |
| 22 // thread where the TestWebThreadBundle lives. The destructor of | 22 // TestWebThreadBundle lives. The destructor of TestWebThreadBundle runs |
| 23 // TestWebThreadBundle runs remaining TestWebThreads tasks, remaining | 23 // remaining TestWebThreads tasks, remaining blocking pool tasks, and remaining |
| 24 // blocking pool tasks, and remaining BLOCK_SHUTDOWN task scheduler tasks. | 24 // BLOCK_SHUTDOWN task scheduler tasks. |
| 25 // | 25 // |
| 26 // Some tests using the IO thread expect a MessageLoopForIO. Passing | 26 // Some tests using the IO thread expect a MessageLoopForIO. Passing |
| 27 // IO_MAINLOOP will use a MessageLoopForIO for the main MessageLoop. | 27 // IO_MAINLOOP will use a MessageLoopForIO for the main MessageLoop. |
| 28 // Most of the time, this avoids needing to use a REAL_IO_THREAD. | 28 // Most of the time, this avoids needing to use a REAL_IO_THREAD. |
| 29 | 29 |
| 30 #include <memory> | 30 #include <memory> |
| 31 | 31 |
| 32 #include "base/macros.h" | 32 #include "base/macros.h" |
| 33 | 33 |
| 34 namespace base { | 34 namespace base { |
| 35 class MessageLoop; | 35 class MessageLoop; |
| 36 namespace test { | 36 namespace test { |
| 37 class ScopedAsyncTaskScheduler; | 37 class ScopedAsyncTaskScheduler; |
| 38 class ScopedTaskScheduler; | |
| 39 } // namespace test | 38 } // namespace test |
| 40 } // namespace base | 39 } // namespace base |
| 41 | 40 |
| 42 namespace web { | 41 namespace web { |
| 43 | 42 |
| 44 class TestWebThread; | 43 class TestWebThread; |
| 45 | 44 |
| 46 class TestWebThreadBundle { | 45 class TestWebThreadBundle { |
| 47 public: | 46 public: |
| 48 // Used to specify the type of MessageLoop that backs the UI thread, and | 47 // Used to specify the type of MessageLoop that backs the UI thread, and |
| 49 // which of the named WebThreads should be backed by a real | 48 // which of the named WebThreads should be backed by a real |
| 50 // threads. The UI thread is always the main thread in a unit test. | 49 // threads. The UI thread is always the main thread in a unit test. |
| 51 enum Options { | 50 enum Options { |
| 52 DEFAULT = 0, | 51 DEFAULT = 0, |
| 53 IO_MAINLOOP = 1 << 0, | 52 IO_MAINLOOP = 1 << 0, |
| 54 REAL_DB_THREAD = 1 << 1, | 53 REAL_DB_THREAD = 1 << 1, |
| 55 REAL_FILE_THREAD = 1 << 2, | 54 REAL_FILE_THREAD = 1 << 2, |
| 56 REAL_IO_THREAD = 1 << 3, | 55 REAL_IO_THREAD = 1 << 3, |
| 57 REAL_TASK_SCHEDULER = 1 << 4, | |
| 58 }; | 56 }; |
| 59 | 57 |
| 60 TestWebThreadBundle(); | 58 TestWebThreadBundle(); |
| 61 explicit TestWebThreadBundle(int options); | 59 explicit TestWebThreadBundle(int options); |
| 62 | 60 |
| 63 ~TestWebThreadBundle(); | 61 ~TestWebThreadBundle(); |
| 64 | 62 |
| 65 private: | 63 private: |
| 66 void Init(int options); | 64 void Init(int options); |
| 67 | 65 |
| 68 std::unique_ptr<base::MessageLoop> message_loop_; | 66 std::unique_ptr<base::MessageLoop> message_loop_; |
| 69 std::unique_ptr<base::test::ScopedAsyncTaskScheduler> | 67 std::unique_ptr<base::test::ScopedAsyncTaskScheduler> |
| 70 scoped_async_task_scheduler_; | 68 scoped_async_task_scheduler_; |
| 71 std::unique_ptr<base::test::ScopedTaskScheduler> scoped_task_scheduler_; | |
| 72 std::unique_ptr<TestWebThread> ui_thread_; | 69 std::unique_ptr<TestWebThread> ui_thread_; |
| 73 std::unique_ptr<TestWebThread> db_thread_; | 70 std::unique_ptr<TestWebThread> db_thread_; |
| 74 std::unique_ptr<TestWebThread> file_thread_; | 71 std::unique_ptr<TestWebThread> file_thread_; |
| 75 std::unique_ptr<TestWebThread> file_user_blocking_thread_; | 72 std::unique_ptr<TestWebThread> file_user_blocking_thread_; |
| 76 std::unique_ptr<TestWebThread> cache_thread_; | 73 std::unique_ptr<TestWebThread> cache_thread_; |
| 77 std::unique_ptr<TestWebThread> io_thread_; | 74 std::unique_ptr<TestWebThread> io_thread_; |
| 78 | 75 |
| 79 DISALLOW_COPY_AND_ASSIGN(TestWebThreadBundle); | 76 DISALLOW_COPY_AND_ASSIGN(TestWebThreadBundle); |
| 80 }; | 77 }; |
| 81 | 78 |
| 82 } // namespace web | 79 } // namespace web |
| 83 | 80 |
| 84 #endif // IOS_WEB_PUBLIC_TEST_TEST_WEB_THREAD_BUNDLE_H_ | 81 #endif // IOS_WEB_PUBLIC_TEST_TEST_WEB_THREAD_BUNDLE_H_ |
| OLD | NEW |