test_renderer_host.cc revision 5c02ac1a9c1b504631c0a3d2b6e737b5d738bae1
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 "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" 20 21#if defined(OS_WIN) 22#include "ui/base/win/scoped_ole_initializer.h" 23#endif 24 25#if defined(USE_AURA) 26#include "ui/aura/test/aura_test_helper.h" 27#include "ui/compositor/test/context_factories_for_test.h" 28#include "ui/wm/core/default_activation_client.h" 29#endif 30 31namespace content { 32 33// RenderViewHostTester ------------------------------------------------------- 34 35// static 36RenderViewHostTester* RenderViewHostTester::For(RenderViewHost* host) { 37 return static_cast<TestRenderViewHost*>(host); 38} 39 40// static 41RenderViewHost* RenderViewHostTester::GetPendingForController( 42 NavigationController* controller) { 43 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>( 44 controller->GetWebContents()); 45 return web_contents->GetRenderManagerForTesting()->pending_render_view_host(); 46} 47 48// static 49bool RenderViewHostTester::IsRenderViewHostSwappedOut(RenderViewHost* rvh) { 50 return static_cast<RenderViewHostImpl*>(rvh)->rvh_state() == 51 RenderViewHostImpl::STATE_SWAPPED_OUT; 52} 53 54// static 55bool RenderViewHostTester::TestOnMessageReceived(RenderViewHost* rvh, 56 const IPC::Message& msg) { 57 return static_cast<RenderViewHostImpl*>(rvh)->OnMessageReceived(msg); 58} 59 60// static 61bool RenderViewHostTester::HasTouchEventHandler(RenderViewHost* rvh) { 62 RenderWidgetHostImpl* host_impl = RenderWidgetHostImpl::From(rvh); 63 return host_impl->has_touch_handler(); 64} 65 66 67// RenderViewHostTestEnabler -------------------------------------------------- 68 69RenderViewHostTestEnabler::RenderViewHostTestEnabler() 70 : rph_factory_(new MockRenderProcessHostFactory()), 71 rvh_factory_(new TestRenderViewHostFactory(rph_factory_.get())), 72 rfh_factory_(new TestRenderFrameHostFactory()) {} 73 74RenderViewHostTestEnabler::~RenderViewHostTestEnabler() { 75} 76 77 78// RenderViewHostTestHarness -------------------------------------------------- 79 80RenderViewHostTestHarness::RenderViewHostTestHarness() 81 : thread_bundle_options_(TestBrowserThreadBundle::DEFAULT) {} 82 83RenderViewHostTestHarness::~RenderViewHostTestHarness() { 84} 85 86NavigationController& RenderViewHostTestHarness::controller() { 87 return web_contents()->GetController(); 88} 89 90WebContents* RenderViewHostTestHarness::web_contents() { 91 return contents_.get(); 92} 93 94RenderViewHost* RenderViewHostTestHarness::rvh() { 95 return web_contents()->GetRenderViewHost(); 96} 97 98RenderViewHost* RenderViewHostTestHarness::pending_rvh() { 99 return static_cast<TestWebContents*>(web_contents())-> 100 GetRenderManagerForTesting()->pending_render_view_host(); 101} 102 103RenderViewHost* RenderViewHostTestHarness::active_rvh() { 104 return pending_rvh() ? pending_rvh() : rvh(); 105} 106 107RenderFrameHost* RenderViewHostTestHarness::main_rfh() { 108 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>( 109 this->web_contents()); 110 return web_contents->GetFrameTree()->GetMainFrame(); 111} 112 113BrowserContext* RenderViewHostTestHarness::browser_context() { 114 return browser_context_.get(); 115} 116 117MockRenderProcessHost* RenderViewHostTestHarness::process() { 118 return static_cast<MockRenderProcessHost*>(active_rvh()->GetProcess()); 119} 120 121void RenderViewHostTestHarness::DeleteContents() { 122 SetContents(NULL); 123} 124 125void RenderViewHostTestHarness::SetContents(WebContents* contents) { 126 contents_.reset(contents); 127} 128 129WebContents* RenderViewHostTestHarness::CreateTestWebContents() { 130 // Make sure we ran SetUp() already. 131#if defined(OS_WIN) 132 DCHECK(ole_initializer_ != NULL); 133#endif 134#if defined(USE_AURA) 135 DCHECK(aura_test_helper_ != NULL); 136#endif 137 138 // This will be deleted when the WebContentsImpl goes away. 139 SiteInstance* instance = SiteInstance::Create(browser_context_.get()); 140 141 return TestWebContents::Create(browser_context_.get(), instance); 142} 143 144void RenderViewHostTestHarness::NavigateAndCommit(const GURL& url) { 145 static_cast<TestWebContents*>(web_contents())->NavigateAndCommit(url); 146} 147 148void RenderViewHostTestHarness::Reload() { 149 NavigationEntry* entry = controller().GetLastCommittedEntry(); 150 DCHECK(entry); 151 controller().Reload(false); 152 static_cast<TestRenderViewHost*>( 153 rvh())->SendNavigate(entry->GetPageID(), entry->GetURL()); 154} 155 156void RenderViewHostTestHarness::FailedReload() { 157 NavigationEntry* entry = controller().GetLastCommittedEntry(); 158 DCHECK(entry); 159 controller().Reload(false); 160 static_cast<TestRenderViewHost*>( 161 rvh())->SendFailedNavigate(entry->GetPageID(), entry->GetURL()); 162} 163 164void RenderViewHostTestHarness::SetUp() { 165 thread_bundle_.reset(new TestBrowserThreadBundle(thread_bundle_options_)); 166 167#if defined(OS_WIN) 168 ole_initializer_.reset(new ui::ScopedOleInitializer()); 169#endif 170#if defined(USE_AURA) 171 // The ContextFactory must exist before any Compositors are created. 172 bool enable_pixel_output = false; 173 ui::InitializeContextFactoryForTests(enable_pixel_output); 174 175 aura_test_helper_.reset( 176 new aura::test::AuraTestHelper(base::MessageLoopForUI::current())); 177 aura_test_helper_->SetUp(); 178 new wm::DefaultActivationClient(aura_test_helper_->root_window()); 179#endif 180 181 DCHECK(!browser_context_); 182 browser_context_.reset(CreateBrowserContext()); 183 184 SetContents(CreateTestWebContents()); 185} 186 187void RenderViewHostTestHarness::TearDown() { 188 SetContents(NULL); 189#if defined(USE_AURA) 190 aura_test_helper_->TearDown(); 191 ui::TerminateContextFactoryForTests(); 192#endif 193 // Make sure that we flush any messages related to WebContentsImpl destruction 194 // before we destroy the browser context. 195 base::RunLoop().RunUntilIdle(); 196 197#if defined(OS_WIN) 198 ole_initializer_.reset(); 199#endif 200 201 // Delete any RenderProcessHosts before the BrowserContext goes away. 202 if (rvh_test_enabler_.rph_factory_) 203 rvh_test_enabler_.rph_factory_.reset(); 204 205 // Release the browser context by posting itself on the end of the task 206 // queue. This is preferable to immediate deletion because it will behave 207 // properly if the |rph_factory_| reset above enqueued any tasks which 208 // depend on |browser_context_|. 209 BrowserThread::DeleteSoon(content::BrowserThread::UI, 210 FROM_HERE, 211 browser_context_.release()); 212 thread_bundle_.reset(); 213} 214 215BrowserContext* RenderViewHostTestHarness::CreateBrowserContext() { 216 return new TestBrowserContext(); 217} 218 219void RenderViewHostTestHarness::SetRenderProcessHostFactory( 220 RenderProcessHostFactory* factory) { 221 rvh_test_enabler_.rvh_factory_->set_render_process_host_factory(factory); 222} 223 224} // namespace content 225