test_renderer_host.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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::ContextFactory* context_factory =
174      ui::InitializeContextFactoryForTests(enable_pixel_output);
175
176  aura_test_helper_.reset(
177      new aura::test::AuraTestHelper(base::MessageLoopForUI::current()));
178  aura_test_helper_->SetUp(context_factory);
179  new wm::DefaultActivationClient(aura_test_helper_->root_window());
180#endif
181
182  DCHECK(!browser_context_);
183  browser_context_.reset(CreateBrowserContext());
184
185  SetContents(CreateTestWebContents());
186}
187
188void RenderViewHostTestHarness::TearDown() {
189  SetContents(NULL);
190#if defined(USE_AURA)
191  aura_test_helper_->TearDown();
192  ui::TerminateContextFactoryForTests();
193#endif
194  // Make sure that we flush any messages related to WebContentsImpl destruction
195  // before we destroy the browser context.
196  base::RunLoop().RunUntilIdle();
197
198#if defined(OS_WIN)
199  ole_initializer_.reset();
200#endif
201
202  // Delete any RenderProcessHosts before the BrowserContext goes away.
203  if (rvh_test_enabler_.rph_factory_)
204    rvh_test_enabler_.rph_factory_.reset();
205
206  // Release the browser context by posting itself on the end of the task
207  // queue. This is preferable to immediate deletion because it will behave
208  // properly if the |rph_factory_| reset above enqueued any tasks which
209  // depend on |browser_context_|.
210  BrowserThread::DeleteSoon(content::BrowserThread::UI,
211                            FROM_HERE,
212                            browser_context_.release());
213  thread_bundle_.reset();
214}
215
216BrowserContext* RenderViewHostTestHarness::CreateBrowserContext() {
217  return new TestBrowserContext();
218}
219
220void RenderViewHostTestHarness::SetRenderProcessHostFactory(
221    RenderProcessHostFactory* factory) {
222    rvh_test_enabler_.rvh_factory_->set_render_process_host_factory(factory);
223}
224
225}  // namespace content
226