Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / test / base / in_process_browser_test.h
blob9192ec2288b61919268b6a71faf66f79bece4f34
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"
18 namespace base {
20 class CommandLine;
22 #if defined(OS_MACOSX)
23 namespace mac {
24 class ScopedNSAutoreleasePool;
25 } // namespace mac
26 #endif // defined(OS_MACOSX)
28 #if defined(OS_WIN)
29 namespace win {
30 class ScopedCOMInitializer;
32 #endif // defined(OS_WIN)
33 } // namespace base
35 class Browser;
36 class Profile;
37 #if defined(OS_MACOSX)
38 class ScopedBundleSwizzlerMac;
39 #endif // defined(OS_MACOSX)
41 namespace content {
42 class ContentRendererClient;
45 // Base class for tests wanting to bring up a browser in the unit test process.
46 // Writing tests with InProcessBrowserTest is slightly different than that of
47 // other tests. This is necessitated by InProcessBrowserTest running a message
48 // loop. To use InProcessBrowserTest do the following:
49 // . Use the macro IN_PROC_BROWSER_TEST_F to define your test.
50 // . Your test method is invoked on the ui thread. If you need to block until
51 // state changes you'll need to run the message loop from your test method.
52 // For example, if you need to wait till a find bar has completely been shown
53 // you'll need to invoke content::RunMessageLoop. When the message bar is
54 // shown, invoke MessageLoop::current()->Quit() to return control back to your
55 // test method.
56 // . If you subclass and override SetUp, be sure and invoke
57 // InProcessBrowserTest::SetUp. (But see also SetUpOnMainThread,
58 // SetUpInProcessBrowserTestFixture and other related hook methods for a
59 // cleaner alternative).
61 // Following three hook methods are called in sequence before calling
62 // BrowserMain(), thus no browser has been created yet. They are mainly for
63 // setting up the environment for running the browser.
64 // . SetUpUserDataDirectory()
65 // . SetUpCommandLine()
66 // . SetUpInProcessBrowserTestFixture()
68 // SetUpOnMainThread() is called just after creating the default browser object
69 // and before executing the real test code. It's mainly for setting up things
70 // related to the browser object and associated window, like opening a new Tab
71 // with a testing page loaded.
73 // TearDownOnMainThread() is called just after executing the real test code to
74 // do necessary cleanup before the browser is torn down.
76 // TearDownInProcessBrowserTestFixture() is called after BrowserMain() exits to
77 // cleanup things setup for running the browser.
79 // By default InProcessBrowserTest creates a single Browser (as returned from
80 // the CreateBrowser method). You can obviously create more as needed.
82 // InProcessBrowserTest disables the sandbox when running.
84 // See ui_test_utils for a handful of methods designed for use with this class.
86 // It's possible to write browser tests that span a restart by splitting each
87 // run of the browser process into a separate test. Example:
89 // IN_PROC_BROWSER_TEST_F(Foo, PRE_Bar) {
90 // do something
91 // }
93 // IN_PROC_BROWSER_TEST_F(Foo, Bar) {
94 // verify something persisted from before
95 // }
97 // This is recursive, so PRE_PRE_Bar would run before PRE_BAR.
98 class InProcessBrowserTest : public content::BrowserTestBase {
99 public:
100 InProcessBrowserTest();
101 ~InProcessBrowserTest() override;
103 // Configures everything for an in process browser test, then invokes
104 // BrowserMain. BrowserMain ends up invoking RunTestOnMainThreadLoop.
105 void SetUp() override;
107 // Restores state configured in SetUp.
108 void TearDown() override;
110 protected:
111 // Returns the browser created by CreateBrowser.
112 Browser* browser() const { return browser_; }
114 // Closes the given browser and waits for it to release all its resources.
115 void CloseBrowserSynchronously(Browser* browser);
117 // Closes the browser without waiting for it to release all its resources.
118 // WARNING: This may leave tasks posted, but not yet run, in the message
119 // loops. Prefer CloseBrowserSynchronously over this method.
120 void CloseBrowserAsynchronously(Browser* browser);
122 // Closes all browsers. No guarantees are made about the destruction of
123 // outstanding resources.
124 void CloseAllBrowsers();
126 // Convenience methods for adding tabs to a Browser.
127 void AddTabAtIndexToBrowser(Browser* browser,
128 int index,
129 const GURL& url,
130 ui::PageTransition transition,
131 bool check_navigation_success);
132 void AddTabAtIndex(int index,
133 const GURL& url,
134 ui::PageTransition transition);
136 // Initializes the contents of the user data directory. Called by SetUp()
137 // after creating the user data directory, but before any browser is launched.
138 // If a test wishes to set up some initial non-empty state in the user data
139 // directory before the browser starts up, it can do so here. Returns true if
140 // successful.
141 virtual bool SetUpUserDataDirectory() WARN_UNUSED_RESULT;
143 // BrowserTestBase:
144 void RunTestOnMainThreadLoop() override;
146 // Ensures that no devtools are open, and then opens the devtools.
147 void OpenDevToolsWindow(content::WebContents* web_contents);
149 // Opens |url| in an incognito browser window with the incognito profile of
150 // |profile|, blocking until the navigation finishes. This will create a new
151 // browser if a browser with the incognito profile does not exist. Returns the
152 // incognito window Browser.
153 Browser* OpenURLOffTheRecord(Profile* profile, const GURL& url);
155 // Creates a browser with a single tab (about:blank), waits for the tab to
156 // finish loading and shows the browser.
158 // This is invoked from Setup.
159 Browser* CreateBrowser(Profile* profile);
161 // Similar to |CreateBrowser|, but creates an incognito browser.
162 Browser* CreateIncognitoBrowser();
164 // Creates a browser for a popup window with a single tab (about:blank), waits
165 // for the tab to finish loading, and shows the browser.
166 Browser* CreateBrowserForPopup(Profile* profile);
168 // Creates a browser for an application and waits for it to load and shows
169 // the browser.
170 Browser* CreateBrowserForApp(const std::string& app_name, Profile* profile);
172 // Called from the various CreateBrowser methods to add a blank tab, wait for
173 // the navigation to complete, and show the browser's window.
174 void AddBlankTabAndShow(Browser* browser);
176 // Enables running of accessibility audit for a particular test case.
177 // - Call in test body to enable/disable for one test case.
178 // - Call in SetUpOnMainThread to enable for all test cases.
179 void EnableAccessibilityChecksForTestCase(bool enabled) {
180 run_accessibility_checks_for_test_case_ = enabled;
183 #if !defined OS_MACOSX
184 // Return a CommandLine object that is used to relaunch the browser_test
185 // binary as a browser process. This function is deliberately not defined on
186 // the Mac because re-using an existing browser process when launching from
187 // the command line isn't a concept that we support on the Mac; AppleEvents
188 // are the Mac solution for the same need. Any test based on these functions
189 // doesn't apply to the Mac.
190 base::CommandLine GetCommandLineForRelaunch();
191 #endif
193 #if defined(OS_MACOSX)
194 // Returns the autorelease pool in use inside RunTestOnMainThreadLoop().
195 base::mac::ScopedNSAutoreleasePool* AutoreleasePool() const {
196 return autorelease_pool_;
198 #endif // OS_MACOSX
200 void set_exit_when_last_browser_closes(bool value) {
201 exit_when_last_browser_closes_ = value;
204 void set_open_about_blank_on_browser_launch(bool value) {
205 open_about_blank_on_browser_launch_ = value;
208 // This must be called before RunTestOnMainThreadLoop() to have any effect.
209 void set_multi_desktop_test(bool multi_desktop_test) {
210 multi_desktop_test_ = multi_desktop_test;
213 // Runs accessibility checks and sets |error_message| if it fails.
214 bool RunAccessibilityChecks(std::string* error_message);
216 private:
217 // Creates a user data directory for the test if one is needed. Returns true
218 // if successful.
219 virtual bool CreateUserDataDirectory() WARN_UNUSED_RESULT;
221 // Quits all open browsers and waits until there are no more browsers.
222 void QuitBrowsers();
224 // Prepare command line that will be used to launch the child browser process
225 // with an in-process test.
226 void PrepareTestCommandLine(base::CommandLine* command_line);
228 // Browser created from CreateBrowser.
229 Browser* browser_;
231 // Temporary user data directory. Used only when a user data directory is not
232 // specified in the command line.
233 base::ScopedTempDir temp_user_data_dir_;
235 // True if we should exit the tests after the last browser instance closes.
236 bool exit_when_last_browser_closes_;
238 // True if the about:blank tab should be opened when the browser is launched.
239 bool open_about_blank_on_browser_launch_;
241 // True if this is a multi-desktop test (in which case this browser test will
242 // not ensure that Browsers are only created on the tested desktop).
243 bool multi_desktop_test_;
245 // True if the accessibility test should run for a particular test case.
246 // This is reset for every test case.
247 bool run_accessibility_checks_for_test_case_;
249 #if defined(OS_MACOSX)
250 base::mac::ScopedNSAutoreleasePool* autorelease_pool_;
251 scoped_ptr<ScopedBundleSwizzlerMac> bundle_swizzler_;
252 #endif // OS_MACOSX
254 #if defined(OS_WIN)
255 scoped_ptr<base::win::ScopedCOMInitializer> com_initializer_;
256 #endif
259 #endif // CHROME_TEST_BASE_IN_PROCESS_BROWSER_TEST_H_