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