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.
5 #include "content/public/test/test_renderer_host.h"
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.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"
23 #include "ui/base/win/scoped_ole_initializer.h"
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"
34 // RenderFrameHostTester ------------------------------------------------------
37 RenderFrameHostTester
* RenderFrameHostTester::For(RenderFrameHost
* host
) {
38 return static_cast<TestRenderFrameHost
*>(host
);
41 // RenderViewHostTester -------------------------------------------------------
44 RenderViewHostTester
* RenderViewHostTester::For(RenderViewHost
* host
) {
45 return static_cast<TestRenderViewHost
*>(host
);
49 RenderViewHost
* RenderViewHostTester::GetPendingForController(
50 NavigationController
* controller
) {
51 WebContentsImpl
* web_contents
= static_cast<WebContentsImpl
*>(
52 controller
->GetWebContents());
53 return web_contents
->GetRenderManagerForTesting()->pending_render_view_host();
57 bool RenderViewHostTester::IsRenderViewHostSwappedOut(RenderViewHost
* rvh
) {
58 return static_cast<RenderViewHostImpl
*>(rvh
)->rvh_state() ==
59 RenderViewHostImpl::STATE_SWAPPED_OUT
;
63 bool RenderViewHostTester::TestOnMessageReceived(RenderViewHost
* rvh
,
64 const IPC::Message
& msg
) {
65 return static_cast<RenderViewHostImpl
*>(rvh
)->OnMessageReceived(msg
);
69 bool RenderViewHostTester::HasTouchEventHandler(RenderViewHost
* rvh
) {
70 RenderWidgetHostImpl
* host_impl
= RenderWidgetHostImpl::From(rvh
);
71 return host_impl
->has_touch_handler();
75 // RenderViewHostTestEnabler --------------------------------------------------
77 RenderViewHostTestEnabler::RenderViewHostTestEnabler()
78 : rph_factory_(new MockRenderProcessHostFactory()),
79 rvh_factory_(new TestRenderViewHostFactory(rph_factory_
.get())),
80 rfh_factory_(new TestRenderFrameHostFactory()) {}
82 RenderViewHostTestEnabler::~RenderViewHostTestEnabler() {
86 // RenderViewHostTestHarness --------------------------------------------------
88 RenderViewHostTestHarness::RenderViewHostTestHarness()
89 : thread_bundle_options_(TestBrowserThreadBundle::DEFAULT
) {}
91 RenderViewHostTestHarness::~RenderViewHostTestHarness() {
94 NavigationController
& RenderViewHostTestHarness::controller() {
95 return web_contents()->GetController();
98 WebContents
* RenderViewHostTestHarness::web_contents() {
99 return contents_
.get();
102 RenderViewHost
* RenderViewHostTestHarness::rvh() {
103 RenderViewHost
* result
= web_contents()->GetRenderViewHost();
104 CHECK_EQ(result
, web_contents()->GetMainFrame()->GetRenderViewHost());
108 RenderViewHost
* RenderViewHostTestHarness::pending_rvh() {
109 return pending_main_rfh() ? pending_main_rfh()->GetRenderViewHost() : NULL
;
112 RenderViewHost
* RenderViewHostTestHarness::active_rvh() {
113 return pending_rvh() ? pending_rvh() : rvh();
116 RenderFrameHost
* RenderViewHostTestHarness::main_rfh() {
117 return web_contents()->GetMainFrame();
120 RenderFrameHost
* RenderViewHostTestHarness::pending_main_rfh() {
121 return WebContentsTester::For(web_contents())->GetPendingMainFrame();
124 BrowserContext
* RenderViewHostTestHarness::browser_context() {
125 return browser_context_
.get();
128 MockRenderProcessHost
* RenderViewHostTestHarness::process() {
129 return static_cast<MockRenderProcessHost
*>(active_rvh()->GetProcess());
132 void RenderViewHostTestHarness::DeleteContents() {
136 void RenderViewHostTestHarness::SetContents(WebContents
* contents
) {
137 contents_
.reset(contents
);
140 WebContents
* RenderViewHostTestHarness::CreateTestWebContents() {
141 // Make sure we ran SetUp() already.
143 DCHECK(ole_initializer_
!= NULL
);
145 #if defined(USE_AURA)
146 DCHECK(aura_test_helper_
!= NULL
);
149 // This will be deleted when the WebContentsImpl goes away.
150 SiteInstance
* instance
= SiteInstance::Create(browser_context_
.get());
152 return TestWebContents::Create(browser_context_
.get(), instance
);
155 void RenderViewHostTestHarness::NavigateAndCommit(const GURL
& url
) {
156 static_cast<TestWebContents
*>(web_contents())->NavigateAndCommit(url
);
159 void RenderViewHostTestHarness::Reload() {
160 NavigationEntry
* entry
= controller().GetLastCommittedEntry();
162 controller().Reload(false);
163 static_cast<TestRenderViewHost
*>(
164 rvh())->SendNavigateWithTransition(
165 entry
->GetPageID(), entry
->GetURL(), PAGE_TRANSITION_RELOAD
);
168 void RenderViewHostTestHarness::FailedReload() {
169 NavigationEntry
* entry
= controller().GetLastCommittedEntry();
171 controller().Reload(false);
172 static_cast<TestRenderViewHost
*>(
173 rvh())->SendFailedNavigate(entry
->GetPageID(), entry
->GetURL());
176 void RenderViewHostTestHarness::SetUp() {
177 thread_bundle_
.reset(new TestBrowserThreadBundle(thread_bundle_options_
));
180 ole_initializer_
.reset(new ui::ScopedOleInitializer());
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
);
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());
194 DCHECK(!browser_context_
);
195 browser_context_
.reset(CreateBrowserContext());
197 SetContents(CreateTestWebContents());
200 void RenderViewHostTestHarness::TearDown() {
202 #if defined(USE_AURA)
203 aura_test_helper_
->TearDown();
204 ui::TerminateContextFactoryForTests();
206 // Make sure that we flush any messages related to WebContentsImpl destruction
207 // before we destroy the browser context.
208 base::RunLoop().RunUntilIdle();
211 ole_initializer_
.reset();
214 // Delete any RenderProcessHosts before the BrowserContext goes away.
215 if (rvh_test_enabler_
.rph_factory_
)
216 rvh_test_enabler_
.rph_factory_
.reset();
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
,
224 browser_context_
.release());
225 thread_bundle_
.reset();
228 BrowserContext
* RenderViewHostTestHarness::CreateBrowserContext() {
229 return new TestBrowserContext();
232 void RenderViewHostTestHarness::SetRenderProcessHostFactory(
233 RenderProcessHostFactory
* factory
) {
234 rvh_test_enabler_
.rvh_factory_
->set_render_process_host_factory(factory
);
237 } // namespace content