test_renderer_host.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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. 4 5#include "content/public/test/test_renderer_host.h" 6 7#include "content/browser/renderer_host/render_view_host_factory.h" 8#include "content/browser/renderer_host/render_widget_host_impl.h" 9#include "content/browser/renderer_host/test_render_view_host.h" 10#include "content/browser/site_instance_impl.h" 11#include "content/browser/web_contents/navigation_entry_impl.h" 12#include "content/browser/web_contents/test_web_contents.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_view_host_factory.h" 17 18#if defined(OS_WIN) 19#include "ui/base/win/scoped_ole_initializer.h" 20#endif 21 22#if defined(USE_AURA) 23#include "ui/aura/test/aura_test_helper.h" 24#endif 25 26namespace content { 27 28// RenderViewHostTester ------------------------------------------------------- 29 30// static 31RenderViewHostTester* RenderViewHostTester::For(RenderViewHost* host) { 32 return static_cast<TestRenderViewHost*>(host); 33} 34 35// static 36RenderViewHost* RenderViewHostTester::GetPendingForController( 37 NavigationController* controller) { 38 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>( 39 controller->GetWebContents()); 40 return web_contents->GetRenderManagerForTesting()->pending_render_view_host(); 41} 42 43// static 44bool RenderViewHostTester::IsRenderViewHostSwappedOut(RenderViewHost* rvh) { 45 return static_cast<RenderViewHostImpl*>(rvh)->is_swapped_out(); 46} 47 48// static 49bool RenderViewHostTester::TestOnMessageReceived(RenderViewHost* rvh, 50 const IPC::Message& msg) { 51 return static_cast<RenderViewHostImpl*>(rvh)->OnMessageReceived(msg); 52} 53 54// static 55bool RenderViewHostTester::HasTouchEventHandler(RenderViewHost* rvh) { 56 RenderWidgetHostImpl* host_impl = RenderWidgetHostImpl::From(rvh); 57 return host_impl->has_touch_handler(); 58} 59 60 61// RenderViewHostTestEnabler -------------------------------------------------- 62 63RenderViewHostTestEnabler::RenderViewHostTestEnabler() 64 : rph_factory_(new MockRenderProcessHostFactory()), 65 rvh_factory_(new TestRenderViewHostFactory(rph_factory_.get())) { 66} 67 68RenderViewHostTestEnabler::~RenderViewHostTestEnabler() { 69} 70 71 72// RenderViewHostTestHarness -------------------------------------------------- 73 74RenderViewHostTestHarness::RenderViewHostTestHarness() : contents_(NULL) { 75} 76 77RenderViewHostTestHarness::~RenderViewHostTestHarness() { 78} 79 80NavigationController& RenderViewHostTestHarness::controller() { 81 return web_contents()->GetController(); 82} 83 84WebContents* RenderViewHostTestHarness::web_contents() { 85 return contents_.get(); 86} 87 88RenderViewHost* RenderViewHostTestHarness::rvh() { 89 return web_contents()->GetRenderViewHost(); 90} 91 92RenderViewHost* RenderViewHostTestHarness::pending_rvh() { 93 return static_cast<TestWebContents*>(web_contents())-> 94 GetRenderManagerForTesting()->pending_render_view_host(); 95} 96 97RenderViewHost* RenderViewHostTestHarness::active_rvh() { 98 return pending_rvh() ? pending_rvh() : rvh(); 99} 100 101BrowserContext* RenderViewHostTestHarness::browser_context() { 102 return browser_context_.get(); 103} 104 105MockRenderProcessHost* RenderViewHostTestHarness::process() { 106 return static_cast<MockRenderProcessHost*>(active_rvh()->GetProcess()); 107} 108 109void RenderViewHostTestHarness::DeleteContents() { 110 SetContents(NULL); 111} 112 113void RenderViewHostTestHarness::SetContents(WebContents* contents) { 114 contents_.reset(contents); 115} 116 117WebContents* RenderViewHostTestHarness::CreateTestWebContents() { 118 // Make sure we ran SetUp() already. 119#if defined(OS_WIN) 120 DCHECK(ole_initializer_ != NULL); 121#endif 122#if defined(USE_AURA) 123 DCHECK(aura_test_helper_ != NULL); 124#endif 125 126 // See comment above browser_context_ decl for why we check for NULL here. 127 if (!browser_context_.get()) 128 browser_context_.reset(new TestBrowserContext()); 129 130 // This will be deleted when the WebContentsImpl goes away. 131 SiteInstance* instance = SiteInstance::Create(browser_context_.get()); 132 133 return TestWebContents::Create(browser_context_.get(), instance); 134} 135 136void RenderViewHostTestHarness::NavigateAndCommit(const GURL& url) { 137 static_cast<TestWebContents*>(web_contents())->NavigateAndCommit(url); 138} 139 140void RenderViewHostTestHarness::Reload() { 141 NavigationEntry* entry = controller().GetLastCommittedEntry(); 142 DCHECK(entry); 143 controller().Reload(false); 144 static_cast<TestRenderViewHost*>( 145 rvh())->SendNavigate(entry->GetPageID(), entry->GetURL()); 146} 147 148void RenderViewHostTestHarness::SetUp() { 149#if defined(OS_WIN) 150 ole_initializer_.reset(new ui::ScopedOleInitializer()); 151#endif 152#if defined(USE_AURA) 153 aura_test_helper_.reset(new aura::test::AuraTestHelper(&message_loop_)); 154 aura_test_helper_->SetUp(); 155#endif 156 SetContents(CreateTestWebContents()); 157} 158 159void RenderViewHostTestHarness::TearDown() { 160 SetContents(NULL); 161#if defined(USE_AURA) 162 aura_test_helper_->TearDown(); 163#endif 164 // Make sure that we flush any messages related to WebContentsImpl destruction 165 // before we destroy the browser context. 166 MessageLoop::current()->RunUntilIdle(); 167 168 // Delete any RenderProcessHosts before the BrowserContext goes away. 169 if (rvh_test_enabler_.rph_factory_.get()) 170 rvh_test_enabler_.rph_factory_.reset(); 171 172 // Release the browser context on the UI thread. 173 message_loop_.DeleteSoon(FROM_HERE, browser_context_.release()); 174 message_loop_.RunUntilIdle(); 175 176#if defined(OS_WIN) 177 ole_initializer_.reset(); 178#endif 179} 180 181void RenderViewHostTestHarness::SetRenderProcessHostFactory( 182 RenderProcessHostFactory* factory) { 183 rvh_test_enabler_.rvh_factory_->set_render_process_host_factory(factory); 184} 185 186} // namespace content 187