Home | History | Annotate | Download | only in test
      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.
      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, 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.
     13 //
     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.
     19 //
     20 // Some tests using the IO thread expect a MessageLoopForIO. Passing
     21 // IO_MAINLOOP will use a MessageLoopForIO for the main MessageLoop.
     22 // Most of the time, this avoids needing to use a REAL_IO_THREAD.
     23 
     24 #ifndef CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_TEST_BUNDLE_H_
     25 #define CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_TEST_BUNDLE_H_
     26 
     27 #include "base/memory/scoped_ptr.h"
     28 #include "testing/gtest/include/gtest/gtest.h"
     29 
     30 namespace base {
     31 class MessageLoop;
     32 }  // namespace base
     33 
     34 namespace content {
     35 
     36 class TestBrowserThread;
     37 
     38 class TestBrowserThreadBundle {
     39  public:
     40   // Used to specify the type of MessageLoop that backs the UI thread, and
     41   // which of the named BrowserThreads should be backed by a real
     42   // threads. The UI thread is always the main thread in a unit test.
     43   enum Options {
     44     DEFAULT = 0x00,
     45     IO_MAINLOOP = 0x01,
     46     REAL_DB_THREAD = 0x02,
     47     REAL_FILE_THREAD = 0x08,
     48     REAL_FILE_USER_BLOCKING_THREAD = 0x10,
     49     REAL_PROCESS_LAUNCHER_THREAD = 0x20,
     50     REAL_CACHE_THREAD = 0x40,
     51     REAL_IO_THREAD = 0x80,
     52   };
     53 
     54   TestBrowserThreadBundle();
     55   explicit TestBrowserThreadBundle(int options);
     56 
     57   ~TestBrowserThreadBundle();
     58 
     59  private:
     60   void Init(int options);
     61 
     62   scoped_ptr<base::MessageLoop> message_loop_;
     63   scoped_ptr<TestBrowserThread> ui_thread_;
     64   scoped_ptr<TestBrowserThread> db_thread_;
     65   scoped_ptr<TestBrowserThread> file_thread_;
     66   scoped_ptr<TestBrowserThread> file_user_blocking_thread_;
     67   scoped_ptr<TestBrowserThread> process_launcher_thread_;
     68   scoped_ptr<TestBrowserThread> cache_thread_;
     69   scoped_ptr<TestBrowserThread> io_thread_;
     70 
     71   DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadBundle);
     72 };
     73 
     74 }  // namespace content
     75 
     76 #endif /* CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_TEST_BUNDLE_H_ */
     77