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. 4 5#ifndef CHROME_TEST_BASE_BROWSER_WITH_TEST_WINDOW_TEST_H_ 6#define CHROME_TEST_BASE_BROWSER_WITH_TEST_WINDOW_TEST_H_ 7 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" 17 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" 22#endif 23 24#if defined(OS_WIN) 25#include "ui/base/win/scoped_ole_initializer.h" 26#endif 27 28class GURL; 29 30#if defined(USE_ASH) 31namespace ash { 32namespace test { 33class AshTestHelper; 34} 35} 36#endif 37 38#if defined(USE_AURA) 39namespace aura { 40namespace test { 41class AuraTestHelper; 42} 43} 44#endif 45 46namespace content { 47class NavigationController; 48class WebContents; 49} 50 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: 55// 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(); 60// 61// // Navigate somewhere else. 62// GURL url2("http://foo/2"); 63// NavigateAndCommit(controller, url2); 64// 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); 70// 71// Subclasses must invoke BrowserWithTestWindowTest::SetUp as it is responsible 72// for creating the various objects of this class. 73class BrowserWithTestWindowTest : public testing::Test { 74 public: 75 // Creates a BrowserWithTestWindowTest for which the initial window will be 76 // created on the native desktop. 77 BrowserWithTestWindowTest(); 78 virtual ~BrowserWithTestWindowTest(); 79 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); 83 84 virtual void SetUp() OVERRIDE; 85 virtual void TearDown() OVERRIDE; 86 87 protected: 88 BrowserWindow* window() const { return window_.get(); } 89 90 Browser* browser() const { return browser_.get(); } 91 void set_browser(Browser* browser) { 92 browser_.reset(browser); 93 } 94 Browser* release_browser() WARN_UNUSED_RESULT { 95 return browser_.release(); 96 } 97 98 TestingProfile* profile() const { return profile_.get(); } 99 100 TestingProfile* GetProfile() { return profile_.get(); } 101 102 BrowserWindow* release_browser_window() WARN_UNUSED_RESULT { 103 return window_.release(); 104 } 105 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); 109 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); 113 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 117 // navigation. 118 void NavigateAndCommit(content::NavigationController* controller, 119 const GURL& url); 120 121 // Navigates the current tab. This is a wrapper around NavigateAndCommit. 122 void NavigateAndCommitActiveTab(const GURL& url); 123 124 // Set the |title| of the current tab. 125 void NavigateAndCommitActiveTabWithTitle( 126 Browser* browser, 127 const GURL& url, 128 const string16& title); 129 130 // Destroys the browser, window, and profile created by this class. This is 131 // invoked from the destructor. 132 void DestroyBrowserAndProfile(); 133 134 // Creates the profile used by this test. The caller owns the return value. 135 virtual TestingProfile* CreateProfile(); 136 137 // Creates the BrowserWindow used by this test. The caller owns the return 138 // value. Can return NULL to use the default window created by Browser. 139 virtual BrowserWindow* CreateBrowserWindow(); 140 141 private: 142 // We need to create a MessageLoop, otherwise a bunch of things fails. 143 content::TestBrowserThreadBundle thread_bundle_; 144 base::ShadowingAtExitManager at_exit_manager_; 145 146#if defined(OS_CHROMEOS) 147 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; 148 chromeos::ScopedTestCrosSettings test_cros_settings_; 149 chromeos::ScopedTestUserManager test_user_manager_; 150#endif 151 152 scoped_ptr<TestingProfile> profile_; 153 scoped_ptr<BrowserWindow> window_; // Usually a TestBrowserWindow. 154 scoped_ptr<Browser> browser_; 155 156 // The existence of this object enables tests via 157 // RenderViewHostTester. 158 content::RenderViewHostTestEnabler rvh_test_enabler_; 159 160#if defined(USE_ASH) 161 scoped_ptr<ash::test::AshTestHelper> ash_test_helper_; 162#endif 163#if defined(USE_AURA) 164 scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_; 165#endif 166 167#if defined(OS_WIN) 168 ui::ScopedOleInitializer ole_initializer_; 169#endif 170 171 // The desktop to create the initial window on. 172 chrome::HostDesktopType host_desktop_type_; 173 174 DISALLOW_COPY_AND_ASSIGN(BrowserWithTestWindowTest); 175}; 176 177#endif // CHROME_TEST_BASE_BROWSER_WITH_TEST_WINDOW_TEST_H_ 178