OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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, each of the created TestBrowserThreads will be backed by |
| 10 // all of the created TestBrowserThreads will be backed by a single |
| 11 // shared MessageLoop. If a test truly needs separate threads, it can do |
| 12 // so by passing the apprpriate combination of RealThreadsMask values during |
| 13 // the TestBrowserThreadBundle construction. |
| 14 // |
| 15 // The TestBrowserThreadBundle will attempt to drain the MessageLoop on |
| 16 // destruction. If a test needs to drain currently enqueued tasks, the best |
| 17 // pattern is to instantiate base::RunLoop and call the appropriate methods |
| 18 // the RunLoop (eg., base::RunLoop().RunUntilIdle()). Very rarely should a |
| 19 // test talk directly to the underlying MessageLoop. |
| 20 // |
| 21 // Some tests using the IO thread expect a MessageLoopForIO. Currently, the |
| 22 // best workaround is to create a REAL_IO_THREAD to get the appropriate mesasge |
| 23 // loop. This class could conceivably create a MessageLoopForIO instead of |
| 24 // a MessageLoopForUI as the main loop, but there are not enough cases to |
| 25 // make it work the API complexity. |
| 26 |
| 27 #ifndef BROWSER_THREAD_TEST_BUNDLE_H_ |
| 28 #define BROWSER_THREAD_TEST_BUNDLE_H_ |
| 29 |
| 30 #include "base/message_loop.h" |
| 31 #include "content/public/test/test_browser_thread.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" |
| 33 |
| 34 namespace content { |
| 35 |
| 36 class TestBrowserThreadBundle { |
| 37 public: |
| 38 // Used to specifying which named BrowserThreads should be backed by a real |
| 39 // thread rather than main main thread. The UI thread is always the main |
| 40 // thread in a unit test so it is left out of this enum. |
| 41 enum RealThreadsMask { |
| 42 NO_REAL_THREAD = 0x0, |
| 43 REAL_DB_THREAD = 0x01, |
| 44 REAL_FILE_THREAD = 0x2, |
| 45 REAL_FILE_USER_BLOCKING_THREAD = 0x4, |
| 46 REAL_IO_THREAD = 0x8 |
| 47 }; |
| 48 |
| 49 TestBrowserThreadBundle(); |
| 50 explicit TestBrowserThreadBundle(int real_threads_mask); |
| 51 |
| 52 ~TestBrowserThreadBundle(); |
| 53 |
| 54 TestBrowserThread* ui_thread() { return &ui_thread_; } |
| 55 TestBrowserThread* db_thread() { return &db_thread_; } |
| 56 TestBrowserThread* file_thread() { return &file_thread_; } |
| 57 TestBrowserThread* file_user_blocking_thread() { |
| 58 return &file_user_blocking_thread_; |
| 59 } |
| 60 TestBrowserThread* io_thread() { return &io_thread_; } |
| 61 |
| 62 private: |
| 63 MessageLoopForUI message_loop_; |
| 64 TestBrowserThread ui_thread_; |
| 65 TestBrowserThread db_thread_; |
| 66 TestBrowserThread file_thread_; |
| 67 TestBrowserThread file_user_blocking_thread_; |
| 68 TestBrowserThread io_thread_; |
| 69 |
| 70 DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadBundle); |
| 71 }; |
| 72 |
| 73 } // namespace content |
| 74 |
| 75 #endif /* BROWSER_THREAD_TEST_BUNDLE_H_ */ |
OLD | NEW |