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 // The TestBrowserThreadBundle will also flush the blocking pool on destruction.
     21 // We do this to avoid memory leaks, particularly in the case of threads posting
     22 // tasks to the blocking pool via PostTaskAndReply. By ensuring that the tasks
     23 // are run while the originating TestBroswserThreads still exist, we prevent
     24 // leakage of PostTaskAndReplyRelay objects. We also flush the blocking pool
     25 // again at the point where it would normally be shut down, to better simulate
     26 // the normal thread shutdown process.
     27 //
     28 // Some tests using the IO thread expect a MessageLoopForIO. Passing
     29 // IO_MAINLOOP will use a MessageLoopForIO for the main MessageLoop.
     30 // Most of the time, this avoids needing to use a REAL_IO_THREAD.
     31 
     32 #ifndef CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_TEST_BUNDLE_H_
     33 #define CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_TEST_BUNDLE_H_
     34 
     35 #include "base/memory/scoped_ptr.h"
     36 #include "testing/gtest/include/gtest/gtest.h"
     37 
     38 namespace base {
     39 class MessageLoop;
     40 }  // namespace base
     41 
     42 namespace content {
     43 
     44 class TestBrowserThread;
     45 
     46 class TestBrowserThreadBundle {
     47  public:
     48   // Used to specify the type of MessageLoop that backs the UI thread, and
     49   // which of the named BrowserThreads should be backed by a real
     50   // threads. The UI thread is always the main thread in a unit test.
     51   enum Options {
     52     DEFAULT = 0x00,
     53     IO_MAINLOOP = 0x01,
     54     REAL_DB_THREAD = 0x02,
     55     REAL_FILE_THREAD = 0x08,
     56     REAL_FILE_USER_BLOCKING_THREAD = 0x10,
     57     REAL_PROCESS_LAUNCHER_THREAD = 0x20,
     58     REAL_CACHE_THREAD = 0x40,
     59     REAL_IO_THREAD = 0x80,
     60   };
     61 
     62   TestBrowserThreadBundle();
     63   explicit TestBrowserThreadBundle(int options);
     64 
     65   ~TestBrowserThreadBundle();
     66 
     67  private:
     68   void Init(int options);
     69 
     70   scoped_ptr<base::MessageLoop> message_loop_;
     71   scoped_ptr<TestBrowserThread> ui_thread_;
     72   scoped_ptr<TestBrowserThread> db_thread_;
     73   scoped_ptr<TestBrowserThread> file_thread_;
     74   scoped_ptr<TestBrowserThread> file_user_blocking_thread_;
     75   scoped_ptr<TestBrowserThread> process_launcher_thread_;
     76   scoped_ptr<TestBrowserThread> cache_thread_;
     77   scoped_ptr<TestBrowserThread> io_thread_;
     78 
     79   DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadBundle);
     80 };
     81 
     82 }  // namespace content
     83 
     84 #endif /* CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_TEST_BUNDLE_H_ */
     85