Merge html-office-public repo into src
[chromium-blink-merge.git] / chrome / test / base / browser_with_test_window_test.cc
blob09f57d5bb5ac6671de86ffc0761a16a3b4a50716
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 #include "chrome/test/base/browser_with_test_window_test.h"
7 #include "base/run_loop.h"
8 #include "chrome/browser/profiles/profile_destroyer.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_navigator.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/common/render_messages.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "content/public/browser/navigation_controller.h"
15 #include "content/public/browser/navigation_entry.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/test/test_renderer_host.h"
18 #include "ui/base/page_transition_types.h"
20 #if defined(USE_AURA)
21 #include "ui/aura/test/aura_test_helper.h"
22 #include "ui/compositor/compositor.h"
23 #include "ui/compositor/test/context_factories_for_test.h"
24 #include "ui/wm/core/default_activation_client.h"
25 #endif
27 #if defined(USE_ASH)
28 #include "ash/test/ash_test_helper.h"
29 #include "ash/test/ash_test_views_delegate.h"
30 #endif
32 #if defined(TOOLKIT_VIEWS)
33 #include "chrome/browser/ui/views/chrome_constrained_window_views_client.h"
34 #include "components/constrained_window/constrained_window_views.h"
35 #include "ui/views/test/test_views_delegate.h"
36 #endif
38 using content::NavigationController;
39 using content::RenderFrameHost;
40 using content::RenderFrameHostTester;
41 using content::WebContents;
43 BrowserWithTestWindowTest::BrowserWithTestWindowTest()
44 : browser_type_(Browser::TYPE_TABBED),
45 host_desktop_type_(chrome::HOST_DESKTOP_TYPE_NATIVE),
46 hosted_app_(false) {
49 BrowserWithTestWindowTest::BrowserWithTestWindowTest(
50 Browser::Type browser_type,
51 chrome::HostDesktopType host_desktop_type,
52 bool hosted_app)
53 : browser_type_(browser_type),
54 host_desktop_type_(host_desktop_type),
55 hosted_app_(hosted_app) {
58 BrowserWithTestWindowTest::~BrowserWithTestWindowTest() {
61 void BrowserWithTestWindowTest::SetUp() {
62 testing::Test::SetUp();
63 #if defined(OS_CHROMEOS)
64 // TODO(jamescook): Windows Ash support. This will require refactoring
65 // AshTestHelper and AuraTestHelper so they can be used at the same time,
66 // perhaps by AshTestHelper owning an AuraTestHelper. Also, need to cleanup
67 // CreateViewsDelegate() below when cleanup done.
68 ash_test_helper_.reset(new ash::test::AshTestHelper(
69 base::MessageLoopForUI::current()));
70 ash_test_helper_->SetUp(true);
71 #elif defined(USE_AURA)
72 // The ContextFactory must exist before any Compositors are created.
73 bool enable_pixel_output = false;
74 ui::ContextFactory* context_factory =
75 ui::InitializeContextFactoryForTests(enable_pixel_output);
77 aura_test_helper_.reset(new aura::test::AuraTestHelper(
78 base::MessageLoopForUI::current()));
79 aura_test_helper_->SetUp(context_factory);
80 new wm::DefaultActivationClient(aura_test_helper_->root_window());
81 #endif // USE_AURA
83 #if defined(TOOLKIT_VIEWS)
84 #if !defined(OS_CHROMEOS)
85 views_delegate_.reset(CreateViewsDelegate());
86 #endif // !OS_CHROMEOS
87 SetConstrainedWindowViewsClient(CreateChromeConstrainedWindowViewsClient());
88 #endif // TOOLKIT_VIEWS
90 // Subclasses can provide their own Profile.
91 profile_ = CreateProfile();
92 // Subclasses can provide their own test BrowserWindow. If they return NULL
93 // then Browser will create the a production BrowserWindow and the subclass
94 // is responsible for cleaning it up (usually by NativeWidget destruction).
95 window_.reset(CreateBrowserWindow());
97 browser_.reset(CreateBrowser(profile(), browser_type_, hosted_app_,
98 host_desktop_type_, window_.get()));
101 void BrowserWithTestWindowTest::TearDown() {
102 // Some tests end up posting tasks to the DB thread that must be completed
103 // before the profile can be destroyed and the test safely shut down.
104 base::RunLoop().RunUntilIdle();
106 // Reset the profile here because some profile keyed services (like the
107 // audio service) depend on test stubs that the helpers below will remove.
108 DestroyBrowserAndProfile();
110 #if defined(OS_CHROMEOS)
111 ash_test_helper_->TearDown();
112 #elif defined(USE_AURA)
113 aura_test_helper_->TearDown();
114 ui::TerminateContextFactoryForTests();
115 #endif
116 testing::Test::TearDown();
118 // A Task is leaked if we don't destroy everything, then run the message
119 // loop.
120 base::MessageLoop::current()->PostTask(FROM_HERE,
121 base::MessageLoop::QuitClosure());
122 base::MessageLoop::current()->Run();
124 #if defined(TOOLKIT_VIEWS)
125 constrained_window::SetConstrainedWindowViewsClient(nullptr);
126 views_delegate_.reset(NULL);
127 #endif
130 void BrowserWithTestWindowTest::AddTab(Browser* browser, const GURL& url) {
131 chrome::NavigateParams params(browser, url, ui::PAGE_TRANSITION_TYPED);
132 params.tabstrip_index = 0;
133 params.disposition = NEW_FOREGROUND_TAB;
134 chrome::Navigate(&params);
135 CommitPendingLoad(&params.target_contents->GetController());
138 void BrowserWithTestWindowTest::CommitPendingLoad(
139 NavigationController* controller) {
140 if (!controller->GetPendingEntry())
141 return; // Nothing to commit.
143 RenderFrameHost* old_rfh = controller->GetWebContents()->GetMainFrame();
145 RenderFrameHost* pending_rfh = RenderFrameHostTester::GetPendingForController(
146 controller);
147 if (pending_rfh) {
148 // Simulate the BeforeUnload_ACK that is received from the current renderer
149 // for a cross-site navigation.
150 DCHECK_NE(old_rfh, pending_rfh);
151 RenderFrameHostTester::For(old_rfh)->SendBeforeUnloadACK(true);
153 // Commit on the pending_rfh, if one exists.
154 RenderFrameHost* test_rfh = pending_rfh ? pending_rfh : old_rfh;
155 RenderFrameHostTester* test_rfh_tester = RenderFrameHostTester::For(test_rfh);
157 // Simulate a SwapOut_ACK before the navigation commits.
158 if (pending_rfh)
159 RenderFrameHostTester::For(old_rfh)->SimulateSwapOutACK();
161 // For new navigations, we need to send a larger page ID. For renavigations,
162 // we need to send the preexisting page ID. We can tell these apart because
163 // renavigations will have a pending_entry_index while new ones won't (they'll
164 // just have a standalong pending_entry that isn't in the list already).
165 if (controller->GetPendingEntryIndex() >= 0) {
166 test_rfh_tester->SendNavigateWithTransition(
167 controller->GetPendingEntry()->GetPageID(),
168 controller->GetPendingEntry()->GetURL(),
169 controller->GetPendingEntry()->GetTransitionType());
170 } else {
171 test_rfh_tester->SendNavigateWithTransition(
172 controller->GetWebContents()->GetMaxPageIDForSiteInstance(
173 test_rfh->GetSiteInstance()) + 1,
174 controller->GetPendingEntry()->GetURL(),
175 controller->GetPendingEntry()->GetTransitionType());
179 void BrowserWithTestWindowTest::NavigateAndCommit(
180 NavigationController* controller,
181 const GURL& url) {
182 controller->LoadURL(
183 url, content::Referrer(), ui::PAGE_TRANSITION_LINK, std::string());
184 CommitPendingLoad(controller);
187 void BrowserWithTestWindowTest::NavigateAndCommitActiveTab(const GURL& url) {
188 NavigateAndCommit(&browser()->tab_strip_model()->GetActiveWebContents()->
189 GetController(),
190 url);
193 void BrowserWithTestWindowTest::NavigateAndCommitActiveTabWithTitle(
194 Browser* navigating_browser,
195 const GURL& url,
196 const base::string16& title) {
197 NavigationController* controller = &navigating_browser->tab_strip_model()->
198 GetActiveWebContents()->GetController();
199 NavigateAndCommit(controller, url);
200 controller->GetActiveEntry()->SetTitle(title);
203 void BrowserWithTestWindowTest::DestroyBrowserAndProfile() {
204 if (browser_.get()) {
205 // Make sure we close all tabs, otherwise Browser isn't happy in its
206 // destructor.
207 browser()->tab_strip_model()->CloseAllTabs();
208 browser_.reset(NULL);
210 window_.reset(NULL);
211 // Destroy the profile here - otherwise, if the profile is freed in the
212 // destructor, and a test subclass owns a resource that the profile depends
213 // on (such as g_browser_process()->local_state()) there's no way for the
214 // subclass to free it after the profile.
215 if (profile_)
216 DestroyProfile(profile_);
217 profile_ = NULL;
220 TestingProfile* BrowserWithTestWindowTest::CreateProfile() {
221 return new TestingProfile();
224 void BrowserWithTestWindowTest::DestroyProfile(TestingProfile* profile) {
225 delete profile;
228 BrowserWindow* BrowserWithTestWindowTest::CreateBrowserWindow() {
229 return new TestBrowserWindow();
232 Browser* BrowserWithTestWindowTest::CreateBrowser(
233 Profile* profile,
234 Browser::Type browser_type,
235 bool hosted_app,
236 chrome::HostDesktopType host_desktop_type,
237 BrowserWindow* browser_window) {
238 Browser::CreateParams params(profile, host_desktop_type);
239 if (hosted_app) {
240 params = Browser::CreateParams::CreateForApp("Test",
241 true /* trusted_source */,
242 gfx::Rect(),
243 profile,
244 host_desktop_type);
245 } else {
246 params.type = browser_type;
248 params.window = browser_window;
249 return new Browser(params);
252 #if !defined(OS_CHROMEOS) && defined(TOOLKIT_VIEWS)
253 views::ViewsDelegate* BrowserWithTestWindowTest::CreateViewsDelegate() {
254 #if defined(USE_ASH)
255 return new ash::test::AshTestViewsDelegate;
256 #else
257 return new views::TestViewsDelegate;
258 #endif
260 #endif