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/user_manager.h"
20 #include "chrome/browser/chromeos/settings/cros_settings.h"
21 #include "chrome/browser/chromeos/settings/device_settings_service.h"
25 #include "ui/base/win/scoped_ole_initializer.h"
47 class NavigationController;
51 // Base class for browser based unit tests. BrowserWithTestWindowTest creates a
52 // Browser with a TestingProfile and TestBrowserWindow. To add a tab use
53 // AddTab. For example, the following adds a tab and navigates to
54 // two URLs that target the TestWebContents:
56 // // Add a new tab and navigate it. This will be at index 0.
57 // AddTab(browser(), GURL("http://foo/1"));
58 // NavigationController* controller =
59 // &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
61 // // Navigate somewhere else.
62 // GURL url2("http://foo/2");
63 // NavigateAndCommit(controller, url2);
65 // // This is equivalent to the above, and lets you test pending navigations.
66 // browser()->OpenURL(OpenURLParams(
67 // GURL("http://foo/2"), GURL(), CURRENT_TAB,
68 // content::PAGE_TRANSITION_TYPED, false));
69 // CommitPendingLoad(controller);
71 // Subclasses must invoke BrowserWithTestWindowTest::SetUp as it is responsible
72 // for creating the various objects of this class.
73 class BrowserWithTestWindowTest : public testing::Test {
75 // Creates a BrowserWithTestWindowTest for which the initial window will be
76 // created on the native desktop.
77 BrowserWithTestWindowTest();
78 virtual ~BrowserWithTestWindowTest();
80 // Sets the desktop on which the initial window will be created. Must be
81 // called before SetUp().
82 void SetHostDesktopType(chrome::HostDesktopType host_desktop_type);
84 virtual void SetUp() OVERRIDE;
85 virtual void TearDown() OVERRIDE;
88 BrowserWindow* window() const { return window_.get(); }
90 Browser* browser() const { return browser_.get(); }
91 void set_browser(Browser* browser) {
92 browser_.reset(browser);
94 Browser* release_browser() WARN_UNUSED_RESULT {
95 return browser_.release();
98 TestingProfile* profile() const { return profile_; }
100 TestingProfile* GetProfile() { return profile_; }
102 BrowserWindow* release_browser_window() WARN_UNUSED_RESULT {
103 return window_.release();
106 // Adds a tab to |browser| with the given URL and commits the load.
107 // This is a convenience function. The new tab will be added at index 0.
108 void AddTab(Browser* browser, const GURL& url);
110 // Commits the pending load on the given controller. It will keep the
111 // URL of the pending load. If there is no pending load, this does nothing.
112 void CommitPendingLoad(content::NavigationController* controller);
114 // Creates a pending navigation on the given navigation controller to the
115 // given URL with the default parameters and the commits the load with a page
116 // ID one larger than any seen. This emulates what happens on a new
118 void NavigateAndCommit(content::NavigationController* controller,
121 // Navigates the current tab. This is a wrapper around NavigateAndCommit.
122 void NavigateAndCommitActiveTab(const GURL& url);
124 // Set the |title| of the current tab.
125 void NavigateAndCommitActiveTabWithTitle(
128 const string16& title);
130 // Destroys the browser, window, and profile created by this class. This is
131 // invoked from the destructor.
132 void DestroyBrowserAndProfile();
134 // Creates the profile used by this test. The caller owns the return value.
135 virtual TestingProfile* CreateProfile();
137 // Destroys the profile which was created through |CreateProfile|.
138 virtual void DestroyProfile(TestingProfile* profile);
140 // Creates the BrowserWindow used by this test. The caller owns the return
141 // value. Can return NULL to use the default window created by Browser.
142 virtual BrowserWindow* CreateBrowserWindow();
145 // We need to create a MessageLoop, otherwise a bunch of things fails.
146 content::TestBrowserThreadBundle thread_bundle_;
147 base::ShadowingAtExitManager at_exit_manager_;
149 #if defined(OS_CHROMEOS)
150 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
151 chromeos::ScopedTestCrosSettings test_cros_settings_;
152 chromeos::ScopedTestUserManager test_user_manager_;
155 // The profile will automatically be destroyed by TearDown using the
156 // |DestroyProfile()| function - which can be overwritten by derived testing
158 TestingProfile* profile_;
159 scoped_ptr<BrowserWindow> window_; // Usually a TestBrowserWindow.
160 scoped_ptr<Browser> browser_;
162 // The existence of this object enables tests via
163 // RenderViewHostTester.
164 content::RenderViewHostTestEnabler rvh_test_enabler_;
167 scoped_ptr<ash::test::AshTestHelper> ash_test_helper_;
169 #if defined(USE_AURA)
170 scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_;
174 ui::ScopedOleInitializer ole_initializer_;
177 // The desktop to create the initial window on.
178 chrome::HostDesktopType host_desktop_type_;
180 DISALLOW_COPY_AND_ASSIGN(BrowserWithTestWindowTest);
183 #endif // CHROME_TEST_BASE_BROWSER_WITH_TEST_WINDOW_TEST_H_