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