1 // Copyright (c) 2012 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.
5 #ifndef CHROME_TEST_BASE_IN_PROCESS_BROWSER_TEST_H_
6 #define CHROME_TEST_BASE_IN_PROCESS_BROWSER_TEST_H_
8 #include "base/compiler_specific.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/test/browser_test.h"
14 #include "content/public/test/browser_test_base.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "ui/base/page_transition_types.h"
22 #if defined(OS_MACOSX)
24 class ScopedNSAutoreleasePool
;
26 #endif // defined(OS_MACOSX)
30 class ScopedCOMInitializer
;
32 #endif // defined(OS_WIN)
39 class ContentRendererClient
;
42 // Base class for tests wanting to bring up a browser in the unit test process.
43 // Writing tests with InProcessBrowserTest is slightly different than that of
44 // other tests. This is necessitated by InProcessBrowserTest running a message
45 // loop. To use InProcessBrowserTest do the following:
46 // . Use the macro IN_PROC_BROWSER_TEST_F to define your test.
47 // . Your test method is invoked on the ui thread. If you need to block until
48 // state changes you'll need to run the message loop from your test method.
49 // For example, if you need to wait till a find bar has completely been shown
50 // you'll need to invoke content::RunMessageLoop. When the message bar is
51 // shown, invoke MessageLoop::current()->Quit() to return control back to your
53 // . If you subclass and override SetUp, be sure and invoke
54 // InProcessBrowserTest::SetUp. (But see also SetUpOnMainThread,
55 // SetUpInProcessBrowserTestFixture and other related hook methods for a
56 // cleaner alternative).
58 // Following three hook methods are called in sequence before calling
59 // BrowserMain(), thus no browser has been created yet. They are mainly for
60 // setting up the environment for running the browser.
61 // . SetUpUserDataDirectory()
62 // . SetUpCommandLine()
63 // . SetUpInProcessBrowserTestFixture()
65 // SetUpOnMainThread() is called just after creating the default browser object
66 // and before executing the real test code. It's mainly for setting up things
67 // related to the browser object and associated window, like opening a new Tab
68 // with a testing page loaded.
70 // TearDownOnMainThread() is called just after executing the real test code to
71 // do necessary cleanup before the browser is torn down.
73 // TearDownInProcessBrowserTestFixture() is called after BrowserMain() exits to
74 // cleanup things setup for running the browser.
76 // By default InProcessBrowserTest creates a single Browser (as returned from
77 // the CreateBrowser method). You can obviously create more as needed.
79 // InProcessBrowserTest disables the sandbox when running.
81 // See ui_test_utils for a handful of methods designed for use with this class.
83 // It's possible to write browser tests that span a restart by splitting each
84 // run of the browser process into a separate test. Example:
86 // IN_PROC_BROWSER_TEST_F(Foo, PRE_Bar) {
90 // IN_PROC_BROWSER_TEST_F(Foo, Bar) {
91 // verify something persisted from before
94 // This is recursive, so PRE_PRE_Bar would run before PRE_BAR.
95 class InProcessBrowserTest
: public content::BrowserTestBase
{
97 InProcessBrowserTest();
98 ~InProcessBrowserTest() override
;
100 // Configures everything for an in process browser test, then invokes
101 // BrowserMain. BrowserMain ends up invoking RunTestOnMainThreadLoop.
102 void SetUp() override
;
104 // Restores state configured in SetUp.
105 void TearDown() override
;
108 // Returns the browser created by CreateBrowser.
109 Browser
* browser() const { return browser_
; }
111 // Convenience methods for adding tabs to a Browser.
112 void AddTabAtIndexToBrowser(Browser
* browser
,
115 ui::PageTransition transition
,
116 bool check_navigation_success
);
117 void AddTabAtIndex(int index
,
119 ui::PageTransition transition
);
121 // Initializes the contents of the user data directory. Called by SetUp()
122 // after creating the user data directory, but before any browser is launched.
123 // If a test wishes to set up some initial non-empty state in the user data
124 // directory before the browser starts up, it can do so here. Returns true if
126 virtual bool SetUpUserDataDirectory() WARN_UNUSED_RESULT
;
129 void RunTestOnMainThreadLoop() override
;
131 // Creates a browser with a single tab (about:blank), waits for the tab to
132 // finish loading and shows the browser.
134 // This is invoked from Setup.
135 Browser
* CreateBrowser(Profile
* profile
);
137 // Similar to |CreateBrowser|, but creates an incognito browser.
138 Browser
* CreateIncognitoBrowser();
140 // Creates a browser for a popup window with a single tab (about:blank), waits
141 // for the tab to finish loading, and shows the browser.
142 Browser
* CreateBrowserForPopup(Profile
* profile
);
144 // Creates a browser for an application and waits for it to load and shows
146 Browser
* CreateBrowserForApp(const std::string
& app_name
, Profile
* profile
);
148 // Called from the various CreateBrowser methods to add a blank tab, wait for
149 // the navigation to complete, and show the browser's window.
150 void AddBlankTabAndShow(Browser
* browser
);
152 // Enables running of accessibility audit for a particular test case.
153 // - Call in test body to enable/disable for one test case.
154 // - Call in SetUpOnMainThread to enable for all test cases.
155 void EnableAccessibilityChecksForTestCase(bool enabled
) {
156 run_accessibility_checks_for_test_case_
= enabled
;
159 #if !defined OS_MACOSX
160 // Return a CommandLine object that is used to relaunch the browser_test
161 // binary as a browser process. This function is deliberately not defined on
162 // the Mac because re-using an existing browser process when launching from
163 // the command line isn't a concept that we support on the Mac; AppleEvents
164 // are the Mac solution for the same need. Any test based on these functions
165 // doesn't apply to the Mac.
166 base::CommandLine
GetCommandLineForRelaunch();
169 #if defined(OS_MACOSX)
170 // Returns the autorelease pool in use inside RunTestOnMainThreadLoop().
171 base::mac::ScopedNSAutoreleasePool
* AutoreleasePool() const {
172 return autorelease_pool_
;
176 void set_exit_when_last_browser_closes(bool value
) {
177 exit_when_last_browser_closes_
= value
;
180 void set_open_about_blank_on_browser_launch(bool value
) {
181 open_about_blank_on_browser_launch_
= value
;
184 // This must be called before RunTestOnMainThreadLoop() to have any effect.
185 void set_multi_desktop_test(bool multi_desktop_test
) {
186 multi_desktop_test_
= multi_desktop_test
;
189 // Runs accessibility checks and sets |error_message| if it fails.
190 bool RunAccessibilityChecks(std::string
* error_message
);
193 // Creates a user data directory for the test if one is needed. Returns true
195 virtual bool CreateUserDataDirectory() WARN_UNUSED_RESULT
;
197 // Quits all open browsers and waits until there are no more browsers.
200 // Prepare command line that will be used to launch the child browser process
201 // with an in-process test.
202 void PrepareTestCommandLine(base::CommandLine
* command_line
);
204 // Browser created from CreateBrowser.
207 // Temporary user data directory. Used only when a user data directory is not
208 // specified in the command line.
209 base::ScopedTempDir temp_user_data_dir_
;
211 // True if we should exit the tests after the last browser instance closes.
212 bool exit_when_last_browser_closes_
;
214 // True if the about:blank tab should be opened when the browser is launched.
215 bool open_about_blank_on_browser_launch_
;
217 // True if this is a multi-desktop test (in which case this browser test will
218 // not ensure that Browsers are only created on the tested desktop).
219 bool multi_desktop_test_
;
221 // True if the accessibility test should run for a particular test case.
222 // This is reset for every test case.
223 bool run_accessibility_checks_for_test_case_
;
225 #if defined(OS_MACOSX)
226 base::mac::ScopedNSAutoreleasePool
* autorelease_pool_
;
230 scoped_ptr
<base::win::ScopedCOMInitializer
> com_initializer_
;
234 #endif // CHROME_TEST_BASE_IN_PROCESS_BROWSER_TEST_H_