1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// TestBrowserThreadBundle is a convenience class for creating a set of
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// TestBrowserThreads in unit tests.  For most tests, it is sufficient to
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// just instantiate the TestBrowserThreadBundle as a member variable.
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// By default, all of the created TestBrowserThreads will be backed by a single
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// shared MessageLoop. If a test truly needs separate threads, it can do
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// so by passing the appropriate combination of RealThreadsMask values during
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// the TestBrowserThreadBundle construction.
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// The TestBrowserThreadBundle will attempt to drain the MessageLoop on
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// destruction. Sometimes a test needs to drain currently enqueued tasks
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// mid-test. Browser tests should call content::RunAllPendingInMessageLoop().
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Unit tests should use base::RunLoop (e.g., base::RunLoop().RunUntilIdle()).
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// TODO(phajdan.jr): Revise this comment after switch to Aura.
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//
203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// The TestBrowserThreadBundle will also flush the blocking pool on destruction.
213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// We do this to avoid memory leaks, particularly in the case of threads posting
223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// tasks to the blocking pool via PostTaskAndReply. By ensuring that the tasks
233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// are run while the originating TestBroswserThreads still exist, we prevent
243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// leakage of PostTaskAndReplyRelay objects. We also flush the blocking pool
253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// again at the point where it would normally be shut down, to better simulate
263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// the normal thread shutdown process.
273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)//
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Some tests using the IO thread expect a MessageLoopForIO. Passing
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// IO_MAINLOOP will use a MessageLoopForIO for the main MessageLoop.
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Most of the time, this avoids needing to use a REAL_IO_THREAD.
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#ifndef CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_TEST_BUNDLE_H_
33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#define CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_TEST_BUNDLE_H_
34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace base {
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class MessageLoop;
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace base
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace content {
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class TestBrowserThread;
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class TestBrowserThreadBundle {
47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Used to specify the type of MessageLoop that backs the UI thread, and
49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // which of the named BrowserThreads should be backed by a real
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // threads. The UI thread is always the main thread in a unit test.
51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  enum Options {
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    DEFAULT = 0x00,
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    IO_MAINLOOP = 0x01,
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    REAL_DB_THREAD = 0x02,
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    REAL_FILE_THREAD = 0x08,
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    REAL_FILE_USER_BLOCKING_THREAD = 0x10,
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    REAL_PROCESS_LAUNCHER_THREAD = 0x20,
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    REAL_CACHE_THREAD = 0x40,
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    REAL_IO_THREAD = 0x80,
60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  };
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  TestBrowserThreadBundle();
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  explicit TestBrowserThreadBundle(int options);
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ~TestBrowserThreadBundle();
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void Init(int options);
69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<base::MessageLoop> message_loop_;
71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<TestBrowserThread> ui_thread_;
72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<TestBrowserThread> db_thread_;
73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<TestBrowserThread> file_thread_;
74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<TestBrowserThread> file_user_blocking_thread_;
75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<TestBrowserThread> process_launcher_thread_;
76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<TestBrowserThread> cache_thread_;
77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<TestBrowserThread> io_thread_;
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadBundle);
80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace content
83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#endif /* CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_TEST_BUNDLE_H_ */
85