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