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