IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / public / test / test_renderer_host.cc
blobc837a76eda2dbce04b2aa7385514cf2cb5fa4848
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"
21 #if defined(OS_WIN)
22 #include "ui/base/win/scoped_ole_initializer.h"
23 #endif
25 #if defined(USE_AURA)
26 #include "ui/aura/test/aura_test_helper.h"
27 #endif
29 namespace content {
31 // RenderViewHostTester -------------------------------------------------------
33 // static
34 RenderViewHostTester* RenderViewHostTester::For(RenderViewHost* host) {
35 return static_cast<TestRenderViewHost*>(host);
38 // static
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();
46 // static
47 bool RenderViewHostTester::IsRenderViewHostSwappedOut(RenderViewHost* rvh) {
48 return static_cast<RenderViewHostImpl*>(rvh)->is_swapped_out();
51 // static
52 bool RenderViewHostTester::TestOnMessageReceived(RenderViewHost* rvh,
53 const IPC::Message& msg) {
54 return static_cast<RenderViewHostImpl*>(rvh)->OnMessageReceived(msg);
57 // static
58 bool RenderViewHostTester::HasTouchEventHandler(RenderViewHost* rvh) {
59 RenderWidgetHostImpl* host_impl = RenderWidgetHostImpl::From(rvh);
60 return host_impl->has_touch_handler();
64 // RenderViewHostTestEnabler --------------------------------------------------
66 RenderViewHostTestEnabler::RenderViewHostTestEnabler()
67 : rph_factory_(new MockRenderProcessHostFactory()),
68 rvh_factory_(new TestRenderViewHostFactory(rph_factory_.get())),
69 rfh_factory_(new TestRenderFrameHostFactory()) {}
71 RenderViewHostTestEnabler::~RenderViewHostTestEnabler() {
75 // RenderViewHostTestHarness --------------------------------------------------
77 RenderViewHostTestHarness::RenderViewHostTestHarness()
78 : thread_bundle_options_(TestBrowserThreadBundle::DEFAULT) {}
80 RenderViewHostTestHarness::~RenderViewHostTestHarness() {
83 NavigationController& RenderViewHostTestHarness::controller() {
84 return web_contents()->GetController();
87 WebContents* RenderViewHostTestHarness::web_contents() {
88 return contents_.get();
91 RenderViewHost* RenderViewHostTestHarness::rvh() {
92 return web_contents()->GetRenderViewHost();
95 RenderViewHost* RenderViewHostTestHarness::pending_rvh() {
96 return static_cast<TestWebContents*>(web_contents())->
97 GetRenderManagerForTesting()->pending_render_view_host();
100 RenderViewHost* RenderViewHostTestHarness::active_rvh() {
101 return pending_rvh() ? pending_rvh() : rvh();
104 RenderFrameHost* RenderViewHostTestHarness::main_rfh() {
105 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
106 this->web_contents());
107 return web_contents->GetFrameTree()->GetMainFrame();
110 BrowserContext* RenderViewHostTestHarness::browser_context() {
111 return browser_context_.get();
114 MockRenderProcessHost* RenderViewHostTestHarness::process() {
115 return static_cast<MockRenderProcessHost*>(active_rvh()->GetProcess());
118 void RenderViewHostTestHarness::DeleteContents() {
119 SetContents(NULL);
122 void RenderViewHostTestHarness::SetContents(WebContents* contents) {
123 contents_.reset(contents);
126 WebContents* RenderViewHostTestHarness::CreateTestWebContents() {
127 // Make sure we ran SetUp() already.
128 #if defined(OS_WIN)
129 DCHECK(ole_initializer_ != NULL);
130 #endif
131 #if defined(USE_AURA)
132 DCHECK(aura_test_helper_ != NULL);
133 #endif
135 // This will be deleted when the WebContentsImpl goes away.
136 SiteInstance* instance = SiteInstance::Create(browser_context_.get());
138 return TestWebContents::Create(browser_context_.get(), instance);
141 void RenderViewHostTestHarness::NavigateAndCommit(const GURL& url) {
142 static_cast<TestWebContents*>(web_contents())->NavigateAndCommit(url);
145 void RenderViewHostTestHarness::Reload() {
146 NavigationEntry* entry = controller().GetLastCommittedEntry();
147 DCHECK(entry);
148 controller().Reload(false);
149 static_cast<TestRenderViewHost*>(
150 rvh())->SendNavigate(entry->GetPageID(), entry->GetURL());
153 void RenderViewHostTestHarness::FailedReload() {
154 NavigationEntry* entry = controller().GetLastCommittedEntry();
155 DCHECK(entry);
156 controller().Reload(false);
157 static_cast<TestRenderViewHost*>(
158 rvh())->SendFailedNavigate(entry->GetPageID(), entry->GetURL());
161 void RenderViewHostTestHarness::SetUp() {
162 thread_bundle_.reset(new TestBrowserThreadBundle(thread_bundle_options_));
164 #if defined(OS_WIN)
165 ole_initializer_.reset(new ui::ScopedOleInitializer());
166 #endif
167 #if defined(USE_AURA)
168 aura_test_helper_.reset(
169 new aura::test::AuraTestHelper(base::MessageLoopForUI::current()));
170 bool allow_test_contexts = true;
171 aura_test_helper_->SetUp(allow_test_contexts);
172 #endif
174 DCHECK(!browser_context_);
175 browser_context_.reset(CreateBrowserContext());
177 SetContents(CreateTestWebContents());
180 void RenderViewHostTestHarness::TearDown() {
181 SetContents(NULL);
182 #if defined(USE_AURA)
183 aura_test_helper_->TearDown();
184 #endif
185 // Make sure that we flush any messages related to WebContentsImpl destruction
186 // before we destroy the browser context.
187 base::RunLoop().RunUntilIdle();
189 #if defined(OS_WIN)
190 ole_initializer_.reset();
191 #endif
193 // Delete any RenderProcessHosts before the BrowserContext goes away.
194 if (rvh_test_enabler_.rph_factory_)
195 rvh_test_enabler_.rph_factory_.reset();
197 // Release the browser context by posting itself on the end of the task
198 // queue. This is preferable to immediate deletion because it will behave
199 // properly if the |rph_factory_| reset above enqueued any tasks which
200 // depend on |browser_context_|.
201 BrowserThread::DeleteSoon(content::BrowserThread::UI,
202 FROM_HERE,
203 browser_context_.release());
204 thread_bundle_.reset();
207 BrowserContext* RenderViewHostTestHarness::CreateBrowserContext() {
208 return new TestBrowserContext();
211 void RenderViewHostTestHarness::SetRenderProcessHostFactory(
212 RenderProcessHostFactory* factory) {
213 rvh_test_enabler_.rvh_factory_->set_render_process_host_factory(factory);
216 } // namespace content