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