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/run_loop.h"
8 #include "content/browser/frame_host/navigation_entry_impl.h"
9 #include "content/browser/renderer_host/render_view_host_factory.h"
10 #include "content/browser/renderer_host/render_widget_host_impl.h"
11 #include "content/browser/site_instance_impl.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/web_contents.h"
14 #include "content/public/test/mock_render_process_host.h"
15 #include "content/public/test/test_browser_context.h"
16 #include "content/test/test_render_frame_host_factory.h"
17 #include "content/test/test_render_view_host.h"
18 #include "content/test/test_render_view_host_factory.h"
19 #include "content/test/test_web_contents.h"
22 #include "ui/base/win/scoped_ole_initializer.h"
26 #include "ui/aura/test/aura_test_helper.h"
31 // RenderViewHostTester -------------------------------------------------------
34 RenderViewHostTester* RenderViewHostTester::For(RenderViewHost* host) {
35 return static_cast<TestRenderViewHost*>(host);
39 RenderViewHost* RenderViewHostTester::GetPendingForController(
40 NavigationController* controller) {
41 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
42 controller->GetWebContents());
43 return web_contents->GetRenderManagerForTesting()->pending_render_view_host();
47 bool RenderViewHostTester::IsRenderViewHostSwappedOut(RenderViewHost* rvh) {
48 return static_cast<RenderViewHostImpl*>(rvh)->rvh_state() ==
49 RenderViewHostImpl::STATE_SWAPPED_OUT;
53 bool RenderViewHostTester::TestOnMessageReceived(RenderViewHost* rvh,
54 const IPC::Message& msg) {
55 return static_cast<RenderViewHostImpl*>(rvh)->OnMessageReceived(msg);
59 bool RenderViewHostTester::HasTouchEventHandler(RenderViewHost* rvh) {
60 RenderWidgetHostImpl* host_impl = RenderWidgetHostImpl::From(rvh);
61 return host_impl->has_touch_handler();
65 // RenderViewHostTestEnabler --------------------------------------------------
67 RenderViewHostTestEnabler::RenderViewHostTestEnabler()
68 : rph_factory_(new MockRenderProcessHostFactory()),
69 rvh_factory_(new TestRenderViewHostFactory(rph_factory_.get())),
70 rfh_factory_(new TestRenderFrameHostFactory()) {}
72 RenderViewHostTestEnabler::~RenderViewHostTestEnabler() {
76 // RenderViewHostTestHarness --------------------------------------------------
78 RenderViewHostTestHarness::RenderViewHostTestHarness()
79 : thread_bundle_options_(TestBrowserThreadBundle::DEFAULT) {}
81 RenderViewHostTestHarness::~RenderViewHostTestHarness() {
84 NavigationController& RenderViewHostTestHarness::controller() {
85 return web_contents()->GetController();
88 WebContents* RenderViewHostTestHarness::web_contents() {
89 return contents_.get();
92 RenderViewHost* RenderViewHostTestHarness::rvh() {
93 return web_contents()->GetRenderViewHost();
96 RenderViewHost* RenderViewHostTestHarness::pending_rvh() {
97 return static_cast<TestWebContents*>(web_contents())->
98 GetRenderManagerForTesting()->pending_render_view_host();
101 RenderViewHost* RenderViewHostTestHarness::active_rvh() {
102 return pending_rvh() ? pending_rvh() : rvh();
105 RenderFrameHost* RenderViewHostTestHarness::main_rfh() {
106 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
107 this->web_contents());
108 return web_contents->GetFrameTree()->GetMainFrame();
111 BrowserContext* RenderViewHostTestHarness::browser_context() {
112 return browser_context_.get();
115 MockRenderProcessHost* RenderViewHostTestHarness::process() {
116 return static_cast<MockRenderProcessHost*>(active_rvh()->GetProcess());
119 void RenderViewHostTestHarness::DeleteContents() {
123 void RenderViewHostTestHarness::SetContents(WebContents* contents) {
124 contents_.reset(contents);
127 WebContents* RenderViewHostTestHarness::CreateTestWebContents() {
128 // Make sure we ran SetUp() already.
130 DCHECK(ole_initializer_ != NULL);
132 #if defined(USE_AURA)
133 DCHECK(aura_test_helper_ != NULL);
136 // This will be deleted when the WebContentsImpl goes away.
137 SiteInstance* instance = SiteInstance::Create(browser_context_.get());
139 return TestWebContents::Create(browser_context_.get(), instance);
142 void RenderViewHostTestHarness::NavigateAndCommit(const GURL& url) {
143 static_cast<TestWebContents*>(web_contents())->NavigateAndCommit(url);
146 void RenderViewHostTestHarness::Reload() {
147 NavigationEntry* entry = controller().GetLastCommittedEntry();
149 controller().Reload(false);
150 static_cast<TestRenderViewHost*>(
151 rvh())->SendNavigate(entry->GetPageID(), entry->GetURL());
154 void RenderViewHostTestHarness::FailedReload() {
155 NavigationEntry* entry = controller().GetLastCommittedEntry();
157 controller().Reload(false);
158 static_cast<TestRenderViewHost*>(
159 rvh())->SendFailedNavigate(entry->GetPageID(), entry->GetURL());
162 void RenderViewHostTestHarness::SetUp() {
163 thread_bundle_.reset(new TestBrowserThreadBundle(thread_bundle_options_));
166 ole_initializer_.reset(new ui::ScopedOleInitializer());
168 #if defined(USE_AURA)
169 aura_test_helper_.reset(
170 new aura::test::AuraTestHelper(base::MessageLoopForUI::current()));
171 bool allow_test_contexts = true;
172 aura_test_helper_->SetUp(allow_test_contexts);
175 DCHECK(!browser_context_);
176 browser_context_.reset(CreateBrowserContext());
178 SetContents(CreateTestWebContents());
181 void RenderViewHostTestHarness::TearDown() {
183 #if defined(USE_AURA)
184 aura_test_helper_->TearDown();
186 // Make sure that we flush any messages related to WebContentsImpl destruction
187 // before we destroy the browser context.
188 base::RunLoop().RunUntilIdle();
191 ole_initializer_.reset();
194 // Delete any RenderProcessHosts before the BrowserContext goes away.
195 if (rvh_test_enabler_.rph_factory_)
196 rvh_test_enabler_.rph_factory_.reset();
198 // Release the browser context by posting itself on the end of the task
199 // queue. This is preferable to immediate deletion because it will behave
200 // properly if the |rph_factory_| reset above enqueued any tasks which
201 // depend on |browser_context_|.
202 BrowserThread::DeleteSoon(content::BrowserThread::UI,
204 browser_context_.release());
205 thread_bundle_.reset();
208 BrowserContext* RenderViewHostTestHarness::CreateBrowserContext() {
209 return new TestBrowserContext();
212 void RenderViewHostTestHarness::SetRenderProcessHostFactory(
213 RenderProcessHostFactory* factory) {
214 rvh_test_enabler_.rvh_factory_->set_render_process_host_factory(factory);
217 } // namespace content