Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / test / base / browser_with_test_window_test.h
blob6d0788d6270358d34d95fa6898a01217c033020b
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_BROWSER_WITH_TEST_WINDOW_TEST_H_
6 #define CHROME_TEST_BASE_BROWSER_WITH_TEST_WINDOW_TEST_H_
8 #include "base/at_exit.h"
9 #include "base/message_loop/message_loop.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/host_desktop.h"
12 #include "chrome/test/base/test_browser_window.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "content/public/test/test_renderer_host.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 #if defined(OS_CHROMEOS)
19 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
20 #include "chrome/browser/chromeos/settings/cros_settings.h"
21 #include "chrome/browser/chromeos/settings/device_settings_service.h"
22 #endif
24 #if defined(OS_WIN)
25 #include "ui/base/win/scoped_ole_initializer.h"
26 #endif
28 class GURL;
30 #if defined(OS_CHROMEOS)
31 namespace ash {
32 namespace test {
33 class AshTestHelper;
36 #elif defined(TOOLKIT_VIEWS)
37 namespace views {
38 class ScopedViewsTestHelper;
40 #endif
42 namespace content {
43 class NavigationController;
44 class WebContents;
47 // Base class for browser based unit tests. BrowserWithTestWindowTest creates a
48 // Browser with a TestingProfile and TestBrowserWindow. To add a tab use
49 // AddTab. For example, the following adds a tab and navigates to
50 // two URLs that target the TestWebContents:
52 // // Add a new tab and navigate it. This will be at index 0.
53 // AddTab(browser(), GURL("http://foo/1"));
54 // NavigationController* controller =
55 // &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
57 // // Navigate somewhere else.
58 // GURL url2("http://foo/2");
59 // NavigateAndCommit(controller, url2);
61 // // This is equivalent to the above, and lets you test pending navigations.
62 // browser()->OpenURL(OpenURLParams(
63 // GURL("http://foo/2"), GURL(), CURRENT_TAB,
64 // ui::PAGE_TRANSITION_TYPED, false));
65 // CommitPendingLoad(controller);
67 // Subclasses must invoke BrowserWithTestWindowTest::SetUp as it is responsible
68 // for creating the various objects of this class.
69 class BrowserWithTestWindowTest : public testing::Test {
70 public:
71 // Creates a BrowserWithTestWindowTest for which the initial window will be
72 // a tabbed browser created on the native desktop, which is not a hosted app.
73 BrowserWithTestWindowTest();
75 // Creates a BrowserWithTestWindowTest for which the initial window will be
76 // the specified type.
77 BrowserWithTestWindowTest(Browser::Type browser_type,
78 chrome::HostDesktopType host_desktop_type,
79 bool hosted_app);
81 ~BrowserWithTestWindowTest() override;
83 void SetUp() override;
84 void TearDown() override;
86 protected:
87 BrowserWindow* window() const { return window_.get(); }
89 Browser* browser() const { return browser_.get(); }
90 void set_browser(Browser* browser) {
91 browser_.reset(browser);
93 Browser* release_browser() WARN_UNUSED_RESULT {
94 return browser_.release();
97 TestingProfile* profile() const { return profile_; }
99 TestingProfile* GetProfile() { return profile_; }
101 BrowserWindow* release_browser_window() WARN_UNUSED_RESULT {
102 return window_.release();
105 // The context to help determine desktop type when creating new Widgets.
106 gfx::NativeWindow GetContext();
108 // Adds a tab to |browser| with the given URL and commits the load.
109 // This is a convenience function. The new tab will be added at index 0.
110 void AddTab(Browser* browser, const GURL& url);
112 // Commits the pending load on the given controller. It will keep the
113 // URL of the pending load. If there is no pending load, this does nothing.
114 void CommitPendingLoad(content::NavigationController* controller);
116 // Creates a pending navigation on the given navigation controller to the
117 // given URL with the default parameters and the commits the load with a page
118 // ID one larger than any seen. This emulates what happens on a new
119 // navigation.
120 void NavigateAndCommit(content::NavigationController* controller,
121 const GURL& url);
123 // Navigates the current tab. This is a wrapper around NavigateAndCommit.
124 void NavigateAndCommitActiveTab(const GURL& url);
126 // Set the |title| of the current tab.
127 void NavigateAndCommitActiveTabWithTitle(Browser* browser,
128 const GURL& url,
129 const base::string16& title);
131 // Destroys the browser, window, and profile created by this class. This is
132 // invoked from the destructor.
133 void DestroyBrowserAndProfile();
135 // Creates the profile used by this test. The caller owns the return value.
136 virtual TestingProfile* CreateProfile();
138 // Destroys the profile which was created through |CreateProfile|.
139 virtual void DestroyProfile(TestingProfile* profile);
141 // Creates the BrowserWindow used by this test. The caller owns the return
142 // value. Can return NULL to use the default window created by Browser.
143 virtual BrowserWindow* CreateBrowserWindow();
145 // Creates the browser given |profile|, |browser_type|, |hosted_app|,
146 // |host_desktop_type| and |browser_window|. The caller owns the return value.
147 virtual Browser* CreateBrowser(Profile* profile,
148 Browser::Type browser_type,
149 bool hosted_app,
150 chrome::HostDesktopType host_desktop_type,
151 BrowserWindow* browser_window);
153 private:
154 // We need to create a MessageLoop, otherwise a bunch of things fails.
155 content::TestBrowserThreadBundle thread_bundle_;
156 base::ShadowingAtExitManager at_exit_manager_;
158 #if defined(OS_CHROMEOS)
159 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
160 chromeos::ScopedTestCrosSettings test_cros_settings_;
161 chromeos::ScopedTestUserManager test_user_manager_;
162 #endif
164 // The profile will automatically be destroyed by TearDown using the
165 // |DestroyProfile()| function - which can be overwritten by derived testing
166 // frameworks.
167 TestingProfile* profile_;
168 scoped_ptr<BrowserWindow> window_; // Usually a TestBrowserWindow.
169 scoped_ptr<Browser> browser_;
171 // The existence of this object enables tests via
172 // RenderViewHostTester.
173 content::RenderViewHostTestEnabler rvh_test_enabler_;
175 #if defined(OS_CHROMEOS)
176 scoped_ptr<ash::test::AshTestHelper> ash_test_helper_;
177 #elif defined(TOOLKIT_VIEWS)
178 scoped_ptr<views::ScopedViewsTestHelper> views_test_helper_;
179 #endif
181 #if defined(OS_WIN)
182 ui::ScopedOleInitializer ole_initializer_;
183 #endif
185 // The type of browser to create (tabbed or popup).
186 Browser::Type browser_type_;
188 // The desktop to create the initial window on.
189 chrome::HostDesktopType host_desktop_type_;
191 // Whether the browser is part of a hosted app.
192 bool hosted_app_;
194 DISALLOW_COPY_AND_ASSIGN(BrowserWithTestWindowTest);
197 #endif // CHROME_TEST_BASE_BROWSER_WITH_TEST_WINDOW_TEST_H_