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);
42 RenderFrameHost* RenderFrameHostTester::GetPendingForController(
43 NavigationController* controller) {
44 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
45 controller->GetWebContents());
46 return web_contents->GetRenderManagerForTesting()->pending_frame_host();
50 bool RenderFrameHostTester::IsRenderFrameHostSwappedOut(RenderFrameHost* rfh) {
51 return static_cast<RenderFrameHostImpl*>(rfh)->is_swapped_out();
54 // RenderViewHostTester -------------------------------------------------------
57 RenderViewHostTester* RenderViewHostTester::For(RenderViewHost* host) {
58 return static_cast<TestRenderViewHost*>(host);
62 bool RenderViewHostTester::TestOnMessageReceived(RenderViewHost* rvh,
63 const IPC::Message& msg) {
64 return static_cast<RenderViewHostImpl*>(rvh)->OnMessageReceived(msg);
68 bool RenderViewHostTester::HasTouchEventHandler(RenderViewHost* rvh) {
69 RenderWidgetHostImpl* host_impl = RenderWidgetHostImpl::From(rvh);
70 return host_impl->has_touch_handler();
74 // RenderViewHostTestEnabler --------------------------------------------------
76 RenderViewHostTestEnabler::RenderViewHostTestEnabler()
77 : rph_factory_(new MockRenderProcessHostFactory()),
78 rvh_factory_(new TestRenderViewHostFactory(rph_factory_.get())),
79 rfh_factory_(new TestRenderFrameHostFactory()) {}
81 RenderViewHostTestEnabler::~RenderViewHostTestEnabler() {
85 // RenderViewHostTestHarness --------------------------------------------------
87 RenderViewHostTestHarness::RenderViewHostTestHarness()
88 : thread_bundle_options_(TestBrowserThreadBundle::DEFAULT) {}
90 RenderViewHostTestHarness::~RenderViewHostTestHarness() {
93 NavigationController& RenderViewHostTestHarness::controller() {
94 return web_contents()->GetController();
97 WebContents* RenderViewHostTestHarness::web_contents() {
98 return contents_.get();
101 RenderViewHost* RenderViewHostTestHarness::rvh() {
102 RenderViewHost* result = web_contents()->GetRenderViewHost();
103 CHECK_EQ(result, web_contents()->GetMainFrame()->GetRenderViewHost());
107 RenderViewHost* RenderViewHostTestHarness::pending_rvh() {
108 return pending_main_rfh() ? pending_main_rfh()->GetRenderViewHost() : NULL;
111 RenderViewHost* RenderViewHostTestHarness::active_rvh() {
112 return pending_rvh() ? pending_rvh() : rvh();
115 RenderFrameHost* RenderViewHostTestHarness::main_rfh() {
116 return web_contents()->GetMainFrame();
119 RenderFrameHost* RenderViewHostTestHarness::pending_main_rfh() {
120 return WebContentsTester::For(web_contents())->GetPendingMainFrame();
123 BrowserContext* RenderViewHostTestHarness::browser_context() {
124 return browser_context_.get();
127 MockRenderProcessHost* RenderViewHostTestHarness::process() {
128 return static_cast<MockRenderProcessHost*>(active_rvh()->GetProcess());
131 void RenderViewHostTestHarness::DeleteContents() {
135 void RenderViewHostTestHarness::SetContents(WebContents* contents) {
136 contents_.reset(contents);
139 WebContents* RenderViewHostTestHarness::CreateTestWebContents() {
140 // Make sure we ran SetUp() already.
142 DCHECK(ole_initializer_ != NULL);
144 #if defined(USE_AURA)
145 DCHECK(aura_test_helper_ != NULL);
148 // This will be deleted when the WebContentsImpl goes away.
149 SiteInstance* instance = SiteInstance::Create(browser_context_.get());
151 return TestWebContents::Create(browser_context_.get(), instance);
154 void RenderViewHostTestHarness::NavigateAndCommit(const GURL& url) {
155 static_cast<TestWebContents*>(web_contents())->NavigateAndCommit(url);
158 void RenderViewHostTestHarness::Reload() {
159 NavigationEntry* entry = controller().GetLastCommittedEntry();
161 controller().Reload(false);
162 RenderFrameHostTester::For(main_rfh())->SendNavigateWithTransition(
163 entry->GetPageID(), entry->GetURL(), ui::PAGE_TRANSITION_RELOAD);
166 void RenderViewHostTestHarness::FailedReload() {
167 NavigationEntry* entry = controller().GetLastCommittedEntry();
169 controller().Reload(false);
170 RenderFrameHostTester::For(main_rfh())->SendFailedNavigate(entry->GetPageID(),
174 void RenderViewHostTestHarness::SetUp() {
175 thread_bundle_.reset(new TestBrowserThreadBundle(thread_bundle_options_));
178 ole_initializer_.reset(new ui::ScopedOleInitializer());
180 #if defined(USE_AURA)
181 // The ContextFactory must exist before any Compositors are created.
182 bool enable_pixel_output = false;
183 ui::ContextFactory* context_factory =
184 ui::InitializeContextFactoryForTests(enable_pixel_output);
186 aura_test_helper_.reset(
187 new aura::test::AuraTestHelper(base::MessageLoopForUI::current()));
188 aura_test_helper_->SetUp(context_factory);
189 new wm::DefaultActivationClient(aura_test_helper_->root_window());
192 DCHECK(!browser_context_);
193 browser_context_.reset(CreateBrowserContext());
195 SetContents(CreateTestWebContents());
198 void RenderViewHostTestHarness::TearDown() {
200 #if defined(USE_AURA)
201 aura_test_helper_->TearDown();
202 ui::TerminateContextFactoryForTests();
204 // Make sure that we flush any messages related to WebContentsImpl destruction
205 // before we destroy the browser context.
206 base::RunLoop().RunUntilIdle();
209 ole_initializer_.reset();
212 // Delete any RenderProcessHosts before the BrowserContext goes away.
213 if (rvh_test_enabler_.rph_factory_)
214 rvh_test_enabler_.rph_factory_.reset();
216 // Release the browser context by posting itself on the end of the task
217 // queue. This is preferable to immediate deletion because it will behave
218 // properly if the |rph_factory_| reset above enqueued any tasks which
219 // depend on |browser_context_|.
220 BrowserThread::DeleteSoon(content::BrowserThread::UI,
222 browser_context_.release());
223 thread_bundle_.reset();
226 BrowserContext* RenderViewHostTestHarness::CreateBrowserContext() {
227 return new TestBrowserContext();
230 void RenderViewHostTestHarness::SetRenderProcessHostFactory(
231 RenderProcessHostFactory* factory) {
232 rvh_test_enabler_.rvh_factory_->set_render_process_host_factory(factory);
235 } // namespace content