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 "content/public/test/test_renderer_host.h"
7 #include "base/command_line.h"
8 #include "base/run_loop.h"
9 #include "content/browser/frame_host/navigation_entry_impl.h"
10 #include "content/browser/renderer_host/render_view_host_factory.h"
11 #include "content/browser/renderer_host/render_widget_host_impl.h"
12 #include "content/browser/site_instance_impl.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/common/content_switches.h"
16 #include "content/public/test/content_browser_sanity_checker.h"
17 #include "content/public/test/mock_render_process_host.h"
18 #include "content/public/test/test_browser_context.h"
19 #include "content/test/browser_side_navigation_test_utils.h"
20 #include "content/test/test_render_frame_host.h"
21 #include "content/test/test_render_frame_host_factory.h"
22 #include "content/test/test_render_view_host.h"
23 #include "content/test/test_render_view_host_factory.h"
24 #include "content/test/test_web_contents.h"
27 #include "ui/base/win/scoped_ole_initializer.h"
31 #include "ui/aura/test/aura_test_helper.h"
32 #include "ui/compositor/test/context_factories_for_test.h"
33 #include "ui/wm/core/default_activation_client.h"
38 // RenderFrameHostTester ------------------------------------------------------
41 RenderFrameHostTester
* RenderFrameHostTester::For(RenderFrameHost
* host
) {
42 return static_cast<TestRenderFrameHost
*>(host
);
46 RenderFrameHost
* RenderFrameHostTester::GetPendingForController(
47 NavigationController
* controller
) {
48 WebContentsImpl
* web_contents
= static_cast<WebContentsImpl
*>(
49 controller
->GetWebContents());
50 return web_contents
->GetRenderManagerForTesting()->pending_frame_host();
54 bool RenderFrameHostTester::IsRenderFrameHostSwappedOut(RenderFrameHost
* rfh
) {
55 return static_cast<RenderFrameHostImpl
*>(rfh
)->is_swapped_out();
58 // RenderViewHostTester -------------------------------------------------------
61 RenderViewHostTester
* RenderViewHostTester::For(RenderViewHost
* host
) {
62 return static_cast<TestRenderViewHost
*>(host
);
66 bool RenderViewHostTester::TestOnMessageReceived(RenderViewHost
* rvh
,
67 const IPC::Message
& msg
) {
68 return static_cast<RenderViewHostImpl
*>(rvh
)->OnMessageReceived(msg
);
72 bool RenderViewHostTester::HasTouchEventHandler(RenderViewHost
* rvh
) {
73 RenderWidgetHostImpl
* host_impl
= RenderWidgetHostImpl::From(rvh
);
74 return host_impl
->has_touch_handler();
78 // RenderViewHostTestEnabler --------------------------------------------------
80 RenderViewHostTestEnabler::RenderViewHostTestEnabler()
81 : rph_factory_(new MockRenderProcessHostFactory()),
82 rvh_factory_(new TestRenderViewHostFactory(rph_factory_
.get())),
83 rfh_factory_(new TestRenderFrameHostFactory()) {}
85 RenderViewHostTestEnabler::~RenderViewHostTestEnabler() {
89 // RenderViewHostTestHarness --------------------------------------------------
91 RenderViewHostTestHarness::RenderViewHostTestHarness()
92 : thread_bundle_options_(TestBrowserThreadBundle::DEFAULT
) {}
94 RenderViewHostTestHarness::~RenderViewHostTestHarness() {
97 NavigationController
& RenderViewHostTestHarness::controller() {
98 return web_contents()->GetController();
101 WebContents
* RenderViewHostTestHarness::web_contents() {
102 return contents_
.get();
105 RenderViewHost
* RenderViewHostTestHarness::rvh() {
106 RenderViewHost
* result
= web_contents()->GetRenderViewHost();
107 CHECK_EQ(result
, web_contents()->GetMainFrame()->GetRenderViewHost());
111 RenderViewHost
* RenderViewHostTestHarness::pending_rvh() {
112 return pending_main_rfh() ? pending_main_rfh()->GetRenderViewHost() : NULL
;
115 RenderViewHost
* RenderViewHostTestHarness::active_rvh() {
116 return pending_rvh() ? pending_rvh() : rvh();
119 RenderFrameHost
* RenderViewHostTestHarness::main_rfh() {
120 return web_contents()->GetMainFrame();
123 RenderFrameHost
* RenderViewHostTestHarness::pending_main_rfh() {
124 return WebContentsTester::For(web_contents())->GetPendingMainFrame();
127 BrowserContext
* RenderViewHostTestHarness::browser_context() {
128 return browser_context_
.get();
131 MockRenderProcessHost
* RenderViewHostTestHarness::process() {
132 return static_cast<MockRenderProcessHost
*>(active_rvh()->GetProcess());
135 void RenderViewHostTestHarness::DeleteContents() {
139 void RenderViewHostTestHarness::SetContents(WebContents
* contents
) {
140 contents_
.reset(contents
);
143 WebContents
* RenderViewHostTestHarness::CreateTestWebContents() {
144 // Make sure we ran SetUp() already.
146 DCHECK(ole_initializer_
!= NULL
);
148 #if defined(USE_AURA)
149 DCHECK(aura_test_helper_
!= NULL
);
152 // This will be deleted when the WebContentsImpl goes away.
153 SiteInstance
* instance
= SiteInstance::Create(browser_context_
.get());
155 return TestWebContents::Create(browser_context_
.get(), instance
);
158 void RenderViewHostTestHarness::NavigateAndCommit(const GURL
& url
) {
159 static_cast<TestWebContents
*>(web_contents())->NavigateAndCommit(url
);
162 void RenderViewHostTestHarness::Reload() {
163 NavigationEntry
* entry
= controller().GetLastCommittedEntry();
165 controller().Reload(false);
166 RenderFrameHostTester::For(main_rfh())->SendNavigateWithTransition(
167 entry
->GetPageID(), entry
->GetURL(), ui::PAGE_TRANSITION_RELOAD
);
170 void RenderViewHostTestHarness::FailedReload() {
171 NavigationEntry
* entry
= controller().GetLastCommittedEntry();
173 controller().Reload(false);
174 RenderFrameHostTester::For(main_rfh())->SendFailedNavigate(entry
->GetPageID(),
178 void RenderViewHostTestHarness::SetUp() {
179 thread_bundle_
.reset(new TestBrowserThreadBundle(thread_bundle_options_
));
182 ole_initializer_
.reset(new ui::ScopedOleInitializer());
184 #if defined(USE_AURA)
185 // The ContextFactory must exist before any Compositors are created.
186 bool enable_pixel_output
= false;
187 ui::ContextFactory
* context_factory
=
188 ui::InitializeContextFactoryForTests(enable_pixel_output
);
190 aura_test_helper_
.reset(
191 new aura::test::AuraTestHelper(base::MessageLoopForUI::current()));
192 aura_test_helper_
->SetUp(context_factory
);
193 new wm::DefaultActivationClient(aura_test_helper_
->root_window());
196 sanity_checker_
.reset(new ContentBrowserSanityChecker());
198 DCHECK(!browser_context_
);
199 browser_context_
.reset(CreateBrowserContext());
201 SetContents(CreateTestWebContents());
203 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
204 switches::kEnableBrowserSideNavigation
)) {
205 BrowserSideNavigationSetUp();
209 void RenderViewHostTestHarness::TearDown() {
210 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
211 switches::kEnableBrowserSideNavigation
)) {
212 BrowserSideNavigationTearDown();
216 #if defined(USE_AURA)
217 aura_test_helper_
->TearDown();
218 ui::TerminateContextFactoryForTests();
220 // Make sure that we flush any messages related to WebContentsImpl destruction
221 // before we destroy the browser context.
222 base::RunLoop().RunUntilIdle();
225 ole_initializer_
.reset();
228 // Delete any RenderProcessHosts before the BrowserContext goes away.
229 if (rvh_test_enabler_
.rph_factory_
)
230 rvh_test_enabler_
.rph_factory_
.reset();
232 // Release the browser context by posting itself on the end of the task
233 // queue. This is preferable to immediate deletion because it will behave
234 // properly if the |rph_factory_| reset above enqueued any tasks which
235 // depend on |browser_context_|.
236 BrowserThread::DeleteSoon(content::BrowserThread::UI
,
238 browser_context_
.release());
239 thread_bundle_
.reset();
242 BrowserContext
* RenderViewHostTestHarness::CreateBrowserContext() {
243 return new TestBrowserContext();
246 void RenderViewHostTestHarness::SetRenderProcessHostFactory(
247 RenderProcessHostFactory
* factory
) {
248 rvh_test_enabler_
.rvh_factory_
->set_render_process_host_factory(factory
);
251 } // namespace content