• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28 class GURL;
29 
30 #if defined(USE_ASH)
31 namespace ash {
32 namespace test {
33 class AshTestHelper;
34 }
35 }
36 #endif
37 
38 #if defined(USE_AURA)
39 namespace aura {
40 namespace test {
41 class AuraTestHelper;
42 }
43 }
44 #endif
45 
46 namespace content {
47 class NavigationController;
48 class 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.
73 class 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:
window()88   BrowserWindow* window() const { return window_.get(); }
89 
browser()90   Browser* browser() const { return browser_.get(); }
set_browser(Browser * browser)91   void set_browser(Browser* browser) {
92     browser_.reset(browser);
93   }
release_browser()94   Browser* release_browser() WARN_UNUSED_RESULT {
95     return browser_.release();
96   }
97 
profile()98   TestingProfile* profile() const { return profile_; }
99 
GetProfile()100   TestingProfile* GetProfile() { return profile_; }
101 
release_browser_window()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   // Destroys the profile which was created through |CreateProfile|.
138   virtual void DestroyProfile(TestingProfile* profile);
139 
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();
143 
144   // Creates the browser given |profile|, |host_desktop_type| and
145   // |browser_window|. The caller owns the return value.
146   virtual Browser* CreateBrowser(Profile* profile,
147                                  chrome::HostDesktopType host_desktop_type,
148                                  BrowserWindow* browser_window);
149 
150  private:
151   // We need to create a MessageLoop, otherwise a bunch of things fails.
152   content::TestBrowserThreadBundle thread_bundle_;
153   base::ShadowingAtExitManager at_exit_manager_;
154 
155 #if defined(OS_CHROMEOS)
156   chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
157   chromeos::ScopedTestCrosSettings test_cros_settings_;
158   chromeos::ScopedTestUserManager test_user_manager_;
159 #endif
160 
161   // The profile will automatically be destroyed by TearDown using the
162   // |DestroyProfile()| function - which can be overwritten by derived testing
163   // frameworks.
164   TestingProfile* profile_;
165   scoped_ptr<BrowserWindow> window_;  // Usually a TestBrowserWindow.
166   scoped_ptr<Browser> browser_;
167 
168   // The existence of this object enables tests via
169   // RenderViewHostTester.
170   content::RenderViewHostTestEnabler rvh_test_enabler_;
171 
172 #if defined(USE_ASH)
173   scoped_ptr<ash::test::AshTestHelper> ash_test_helper_;
174 #endif
175 #if defined(USE_AURA)
176   scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_;
177 #endif
178 
179 #if defined(OS_WIN)
180   ui::ScopedOleInitializer ole_initializer_;
181 #endif
182 
183   // The desktop to create the initial window on.
184   chrome::HostDesktopType host_desktop_type_;
185 
186   DISALLOW_COPY_AND_ASSIGN(BrowserWithTestWindowTest);
187 };
188 
189 #endif  // CHROME_TEST_BASE_BROWSER_WITH_TEST_WINDOW_TEST_H_
190