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