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/location.h"
8 #include "base/run_loop.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "chrome/browser/profiles/profile_destroyer.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_navigator.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/common/render_messages.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "content/public/browser/navigation_controller.h"
18 #include "content/public/browser/navigation_entry.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/test/test_renderer_host.h"
21 #include "ui/base/page_transition_types.h"
24 #include "ui/aura/test/aura_test_helper.h"
25 #include "ui/compositor/compositor.h"
26 #include "ui/compositor/test/context_factories_for_test.h"
27 #include "ui/wm/core/default_activation_client.h"
31 #include "ash/test/ash_test_helper.h"
32 #include "ash/test/ash_test_views_delegate.h"
35 #if defined(TOOLKIT_VIEWS)
36 #include "chrome/browser/ui/views/chrome_constrained_window_views_client.h"
37 #include "components/constrained_window/constrained_window_views.h"
38 #include "ui/views/test/test_views_delegate.h"
41 using content::NavigationController
;
42 using content::RenderFrameHost
;
43 using content::RenderFrameHostTester
;
44 using content::WebContents
;
46 BrowserWithTestWindowTest::BrowserWithTestWindowTest()
47 : browser_type_(Browser::TYPE_TABBED
),
48 host_desktop_type_(chrome::HOST_DESKTOP_TYPE_NATIVE
),
52 BrowserWithTestWindowTest::BrowserWithTestWindowTest(
53 Browser::Type browser_type
,
54 chrome::HostDesktopType host_desktop_type
,
56 : browser_type_(browser_type
),
57 host_desktop_type_(host_desktop_type
),
58 hosted_app_(hosted_app
) {
61 BrowserWithTestWindowTest::~BrowserWithTestWindowTest() {
64 void BrowserWithTestWindowTest::SetUp() {
65 testing::Test::SetUp();
66 #if defined(OS_CHROMEOS)
67 // TODO(jamescook): Windows Ash support. This will require refactoring
68 // AshTestHelper and AuraTestHelper so they can be used at the same time,
69 // perhaps by AshTestHelper owning an AuraTestHelper. Also, need to cleanup
70 // CreateViewsDelegate() below when cleanup done.
71 ash_test_helper_
.reset(new ash::test::AshTestHelper(
72 base::MessageLoopForUI::current()));
73 ash_test_helper_
->SetUp(true);
74 #elif defined(USE_AURA)
75 // The ContextFactory must exist before any Compositors are created.
76 bool enable_pixel_output
= false;
77 ui::ContextFactory
* context_factory
=
78 ui::InitializeContextFactoryForTests(enable_pixel_output
);
80 aura_test_helper_
.reset(new aura::test::AuraTestHelper(
81 base::MessageLoopForUI::current()));
82 aura_test_helper_
->SetUp(context_factory
);
83 new wm::DefaultActivationClient(aura_test_helper_
->root_window());
86 #if defined(TOOLKIT_VIEWS)
87 #if !defined(OS_CHROMEOS)
88 views_delegate_
.reset(CreateViewsDelegate());
89 #endif // !OS_CHROMEOS
90 SetConstrainedWindowViewsClient(CreateChromeConstrainedWindowViewsClient());
91 #endif // TOOLKIT_VIEWS
93 // Subclasses can provide their own Profile.
94 profile_
= CreateProfile();
95 // Subclasses can provide their own test BrowserWindow. If they return NULL
96 // then Browser will create the a production BrowserWindow and the subclass
97 // is responsible for cleaning it up (usually by NativeWidget destruction).
98 window_
.reset(CreateBrowserWindow());
100 browser_
.reset(CreateBrowser(profile(), browser_type_
, hosted_app_
,
101 host_desktop_type_
, window_
.get()));
104 void BrowserWithTestWindowTest::TearDown() {
105 // Some tests end up posting tasks to the DB thread that must be completed
106 // before the profile can be destroyed and the test safely shut down.
107 base::RunLoop().RunUntilIdle();
109 // Reset the profile here because some profile keyed services (like the
110 // audio service) depend on test stubs that the helpers below will remove.
111 DestroyBrowserAndProfile();
113 #if defined(OS_CHROMEOS)
114 ash_test_helper_
->TearDown();
115 #elif defined(USE_AURA)
116 aura_test_helper_
->TearDown();
117 ui::TerminateContextFactoryForTests();
119 testing::Test::TearDown();
121 // A Task is leaked if we don't destroy everything, then run the message
123 base::ThreadTaskRunnerHandle::Get()->PostTask(
124 FROM_HERE
, base::MessageLoop::QuitClosure());
125 base::MessageLoop::current()->Run();
127 #if defined(TOOLKIT_VIEWS)
128 constrained_window::SetConstrainedWindowViewsClient(nullptr);
129 views_delegate_
.reset(NULL
);
133 void BrowserWithTestWindowTest::AddTab(Browser
* browser
, const GURL
& url
) {
134 chrome::NavigateParams
params(browser
, url
, ui::PAGE_TRANSITION_TYPED
);
135 params
.tabstrip_index
= 0;
136 params
.disposition
= NEW_FOREGROUND_TAB
;
137 chrome::Navigate(¶ms
);
138 CommitPendingLoad(¶ms
.target_contents
->GetController());
141 void BrowserWithTestWindowTest::CommitPendingLoad(
142 NavigationController
* controller
) {
143 if (!controller
->GetPendingEntry())
144 return; // Nothing to commit.
146 RenderFrameHost
* old_rfh
= controller
->GetWebContents()->GetMainFrame();
148 RenderFrameHost
* pending_rfh
= RenderFrameHostTester::GetPendingForController(
151 // Simulate the BeforeUnload_ACK that is received from the current renderer
152 // for a cross-site navigation.
153 DCHECK_NE(old_rfh
, pending_rfh
);
154 RenderFrameHostTester::For(old_rfh
)->SendBeforeUnloadACK(true);
156 // Commit on the pending_rfh, if one exists.
157 RenderFrameHost
* test_rfh
= pending_rfh
? pending_rfh
: old_rfh
;
158 RenderFrameHostTester
* test_rfh_tester
= RenderFrameHostTester::For(test_rfh
);
160 // Simulate a SwapOut_ACK before the navigation commits.
162 RenderFrameHostTester::For(old_rfh
)->SimulateSwapOutACK();
164 // For new navigations, we need to send a larger page ID. For renavigations,
165 // we need to send the preexisting page ID. We can tell these apart because
166 // renavigations will have a pending_entry_index while new ones won't (they'll
167 // just have a standalong pending_entry that isn't in the list already).
168 if (controller
->GetPendingEntryIndex() >= 0) {
169 test_rfh_tester
->SendNavigateWithTransition(
170 controller
->GetPendingEntry()->GetPageID(),
171 controller
->GetPendingEntry()->GetUniqueID(),
173 controller
->GetPendingEntry()->GetURL(),
174 controller
->GetPendingEntry()->GetTransitionType());
176 test_rfh_tester
->SendNavigateWithTransition(
177 controller
->GetWebContents()->GetMaxPageIDForSiteInstance(
178 test_rfh
->GetSiteInstance()) + 1,
179 controller
->GetPendingEntry()->GetUniqueID(),
181 controller
->GetPendingEntry()->GetURL(),
182 controller
->GetPendingEntry()->GetTransitionType());
186 void BrowserWithTestWindowTest::NavigateAndCommit(
187 NavigationController
* controller
,
190 url
, content::Referrer(), ui::PAGE_TRANSITION_LINK
, std::string());
191 CommitPendingLoad(controller
);
194 void BrowserWithTestWindowTest::NavigateAndCommitActiveTab(const GURL
& url
) {
195 NavigateAndCommit(&browser()->tab_strip_model()->GetActiveWebContents()->
200 void BrowserWithTestWindowTest::NavigateAndCommitActiveTabWithTitle(
201 Browser
* navigating_browser
,
203 const base::string16
& title
) {
204 NavigationController
* controller
= &navigating_browser
->tab_strip_model()->
205 GetActiveWebContents()->GetController();
206 NavigateAndCommit(controller
, url
);
207 controller
->GetActiveEntry()->SetTitle(title
);
210 void BrowserWithTestWindowTest::DestroyBrowserAndProfile() {
211 if (browser_
.get()) {
212 // Make sure we close all tabs, otherwise Browser isn't happy in its
214 browser()->tab_strip_model()->CloseAllTabs();
215 browser_
.reset(NULL
);
218 // Destroy the profile here - otherwise, if the profile is freed in the
219 // destructor, and a test subclass owns a resource that the profile depends
220 // on (such as g_browser_process()->local_state()) there's no way for the
221 // subclass to free it after the profile.
223 DestroyProfile(profile_
);
227 TestingProfile
* BrowserWithTestWindowTest::CreateProfile() {
228 return new TestingProfile();
231 void BrowserWithTestWindowTest::DestroyProfile(TestingProfile
* profile
) {
235 BrowserWindow
* BrowserWithTestWindowTest::CreateBrowserWindow() {
236 return new TestBrowserWindow();
239 Browser
* BrowserWithTestWindowTest::CreateBrowser(
241 Browser::Type browser_type
,
243 chrome::HostDesktopType host_desktop_type
,
244 BrowserWindow
* browser_window
) {
245 Browser::CreateParams
params(profile
, host_desktop_type
);
247 params
= Browser::CreateParams::CreateForApp("Test",
248 true /* trusted_source */,
253 params
.type
= browser_type
;
255 params
.window
= browser_window
;
256 return new Browser(params
);
259 #if !defined(OS_CHROMEOS) && defined(TOOLKIT_VIEWS)
260 views::ViewsDelegate
* BrowserWithTestWindowTest::CreateViewsDelegate() {
262 return new ash::test::AshTestViewsDelegate
;
264 return new views::TestViewsDelegate
;