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"
27 #include "ui/compositor/test/context_factories_for_test.h"
32 // RenderViewHostTester -------------------------------------------------------
35 RenderViewHostTester* RenderViewHostTester::For(RenderViewHost* host) {
36 return static_cast<TestRenderViewHost*>(host);
40 RenderViewHost* RenderViewHostTester::GetPendingForController(
41 NavigationController* controller) {
42 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
43 controller->GetWebContents());
44 return web_contents->GetRenderManagerForTesting()->pending_render_view_host();
48 bool RenderViewHostTester::IsRenderViewHostSwappedOut(RenderViewHost* rvh) {
49 return static_cast<RenderViewHostImpl*>(rvh)->rvh_state() ==
50 RenderViewHostImpl::STATE_SWAPPED_OUT;
54 bool RenderViewHostTester::TestOnMessageReceived(RenderViewHost* rvh,
55 const IPC::Message& msg) {
56 return static_cast<RenderViewHostImpl*>(rvh)->OnMessageReceived(msg);
60 bool RenderViewHostTester::HasTouchEventHandler(RenderViewHost* rvh) {
61 RenderWidgetHostImpl* host_impl = RenderWidgetHostImpl::From(rvh);
62 return host_impl->has_touch_handler();
66 // RenderViewHostTestEnabler --------------------------------------------------
68 RenderViewHostTestEnabler::RenderViewHostTestEnabler()
69 : rph_factory_(new MockRenderProcessHostFactory()),
70 rvh_factory_(new TestRenderViewHostFactory(rph_factory_.get())),
71 rfh_factory_(new TestRenderFrameHostFactory()) {}
73 RenderViewHostTestEnabler::~RenderViewHostTestEnabler() {
77 // RenderViewHostTestHarness --------------------------------------------------
79 RenderViewHostTestHarness::RenderViewHostTestHarness()
80 : thread_bundle_options_(TestBrowserThreadBundle::DEFAULT) {}
82 RenderViewHostTestHarness::~RenderViewHostTestHarness() {
85 NavigationController& RenderViewHostTestHarness::controller() {
86 return web_contents()->GetController();
89 WebContents* RenderViewHostTestHarness::web_contents() {
90 return contents_.get();
93 RenderViewHost* RenderViewHostTestHarness::rvh() {
94 return web_contents()->GetRenderViewHost();
97 RenderViewHost* RenderViewHostTestHarness::pending_rvh() {
98 return static_cast<TestWebContents*>(web_contents())->
99 GetRenderManagerForTesting()->pending_render_view_host();
102 RenderViewHost* RenderViewHostTestHarness::active_rvh() {
103 return pending_rvh() ? pending_rvh() : rvh();
106 RenderFrameHost* RenderViewHostTestHarness::main_rfh() {
107 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
108 this->web_contents());
109 return web_contents->GetFrameTree()->GetMainFrame();
112 BrowserContext* RenderViewHostTestHarness::browser_context() {
113 return browser_context_.get();
116 MockRenderProcessHost* RenderViewHostTestHarness::process() {
117 return static_cast<MockRenderProcessHost*>(active_rvh()->GetProcess());
120 void RenderViewHostTestHarness::DeleteContents() {
124 void RenderViewHostTestHarness::SetContents(WebContents* contents) {
125 contents_.reset(contents);
128 WebContents* RenderViewHostTestHarness::CreateTestWebContents() {
129 // Make sure we ran SetUp() already.
131 DCHECK(ole_initializer_ != NULL);
133 #if defined(USE_AURA)
134 DCHECK(aura_test_helper_ != NULL);
137 // This will be deleted when the WebContentsImpl goes away.
138 SiteInstance* instance = SiteInstance::Create(browser_context_.get());
140 return TestWebContents::Create(browser_context_.get(), instance);
143 void RenderViewHostTestHarness::NavigateAndCommit(const GURL& url) {
144 static_cast<TestWebContents*>(web_contents())->NavigateAndCommit(url);
147 void RenderViewHostTestHarness::Reload() {
148 NavigationEntry* entry = controller().GetLastCommittedEntry();
150 controller().Reload(false);
151 static_cast<TestRenderViewHost*>(
152 rvh())->SendNavigate(entry->GetPageID(), entry->GetURL());
155 void RenderViewHostTestHarness::FailedReload() {
156 NavigationEntry* entry = controller().GetLastCommittedEntry();
158 controller().Reload(false);
159 static_cast<TestRenderViewHost*>(
160 rvh())->SendFailedNavigate(entry->GetPageID(), entry->GetURL());
163 void RenderViewHostTestHarness::SetUp() {
164 thread_bundle_.reset(new TestBrowserThreadBundle(thread_bundle_options_));
167 ole_initializer_.reset(new ui::ScopedOleInitializer());
169 #if defined(USE_AURA)
170 // The ContextFactory must exist before any Compositors are created.
171 bool enable_pixel_output = false;
172 ui::InitializeContextFactoryForTests(enable_pixel_output);
174 aura_test_helper_.reset(
175 new aura::test::AuraTestHelper(base::MessageLoopForUI::current()));
176 aura_test_helper_->SetUp();
179 DCHECK(!browser_context_);
180 browser_context_.reset(CreateBrowserContext());
182 SetContents(CreateTestWebContents());
185 void RenderViewHostTestHarness::TearDown() {
187 #if defined(USE_AURA)
188 aura_test_helper_->TearDown();
189 ui::TerminateContextFactoryForTests();
191 // Make sure that we flush any messages related to WebContentsImpl destruction
192 // before we destroy the browser context.
193 base::RunLoop().RunUntilIdle();
196 ole_initializer_.reset();
199 // Delete any RenderProcessHosts before the BrowserContext goes away.
200 if (rvh_test_enabler_.rph_factory_)
201 rvh_test_enabler_.rph_factory_.reset();
203 // Release the browser context by posting itself on the end of the task
204 // queue. This is preferable to immediate deletion because it will behave
205 // properly if the |rph_factory_| reset above enqueued any tasks which
206 // depend on |browser_context_|.
207 BrowserThread::DeleteSoon(content::BrowserThread::UI,
209 browser_context_.release());
210 thread_bundle_.reset();
213 BrowserContext* RenderViewHostTestHarness::CreateBrowserContext() {
214 return new TestBrowserContext();
217 void RenderViewHostTestHarness::SetRenderProcessHostFactory(
218 RenderProcessHostFactory* factory) {
219 rvh_test_enabler_.rvh_factory_->set_render_process_host_factory(factory);
222 } // namespace content