Make callers of CommandLine use it via the base:: namespace.
[chromium-blink-merge.git] / content / public / test / test_renderer_host.cc
blobfd9d35601597825d00feabc1001d3379ba39e762
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/command_line.h"
8 #include "base/run_loop.h"
9 #include "content/browser/frame_host/navigation_entry_impl.h"
10 #include "content/browser/renderer_host/render_view_host_factory.h"
11 #include "content/browser/renderer_host/render_widget_host_impl.h"
12 #include "content/browser/site_instance_impl.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/common/content_switches.h"
16 #include "content/public/test/mock_render_process_host.h"
17 #include "content/public/test/test_browser_context.h"
18 #include "content/test/browser_side_navigation_test_utils.h"
19 #include "content/test/test_render_frame_host.h"
20 #include "content/test/test_render_frame_host_factory.h"
21 #include "content/test/test_render_view_host.h"
22 #include "content/test/test_render_view_host_factory.h"
23 #include "content/test/test_web_contents.h"
25 #if defined(OS_WIN)
26 #include "ui/base/win/scoped_ole_initializer.h"
27 #endif
29 #if defined(USE_AURA)
30 #include "ui/aura/test/aura_test_helper.h"
31 #include "ui/compositor/test/context_factories_for_test.h"
32 #include "ui/wm/core/default_activation_client.h"
33 #endif
35 namespace content {
37 // RenderFrameHostTester ------------------------------------------------------
39 // static
40 RenderFrameHostTester* RenderFrameHostTester::For(RenderFrameHost* host) {
41 return static_cast<TestRenderFrameHost*>(host);
44 // static
45 RenderFrameHost* RenderFrameHostTester::GetPendingForController(
46 NavigationController* controller) {
47 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
48 controller->GetWebContents());
49 return web_contents->GetRenderManagerForTesting()->pending_frame_host();
52 // static
53 bool RenderFrameHostTester::IsRenderFrameHostSwappedOut(RenderFrameHost* rfh) {
54 return static_cast<RenderFrameHostImpl*>(rfh)->is_swapped_out();
57 // RenderViewHostTester -------------------------------------------------------
59 // static
60 RenderViewHostTester* RenderViewHostTester::For(RenderViewHost* host) {
61 return static_cast<TestRenderViewHost*>(host);
64 // static
65 bool RenderViewHostTester::TestOnMessageReceived(RenderViewHost* rvh,
66 const IPC::Message& msg) {
67 return static_cast<RenderViewHostImpl*>(rvh)->OnMessageReceived(msg);
70 // static
71 bool RenderViewHostTester::HasTouchEventHandler(RenderViewHost* rvh) {
72 RenderWidgetHostImpl* host_impl = RenderWidgetHostImpl::From(rvh);
73 return host_impl->has_touch_handler();
77 // RenderViewHostTestEnabler --------------------------------------------------
79 RenderViewHostTestEnabler::RenderViewHostTestEnabler()
80 : rph_factory_(new MockRenderProcessHostFactory()),
81 rvh_factory_(new TestRenderViewHostFactory(rph_factory_.get())),
82 rfh_factory_(new TestRenderFrameHostFactory()) {}
84 RenderViewHostTestEnabler::~RenderViewHostTestEnabler() {
88 // RenderViewHostTestHarness --------------------------------------------------
90 RenderViewHostTestHarness::RenderViewHostTestHarness()
91 : thread_bundle_options_(TestBrowserThreadBundle::DEFAULT) {}
93 RenderViewHostTestHarness::~RenderViewHostTestHarness() {
96 NavigationController& RenderViewHostTestHarness::controller() {
97 return web_contents()->GetController();
100 WebContents* RenderViewHostTestHarness::web_contents() {
101 return contents_.get();
104 RenderViewHost* RenderViewHostTestHarness::rvh() {
105 RenderViewHost* result = web_contents()->GetRenderViewHost();
106 CHECK_EQ(result, web_contents()->GetMainFrame()->GetRenderViewHost());
107 return result;
110 RenderViewHost* RenderViewHostTestHarness::pending_rvh() {
111 return pending_main_rfh() ? pending_main_rfh()->GetRenderViewHost() : NULL;
114 RenderViewHost* RenderViewHostTestHarness::active_rvh() {
115 return pending_rvh() ? pending_rvh() : rvh();
118 RenderFrameHost* RenderViewHostTestHarness::main_rfh() {
119 return web_contents()->GetMainFrame();
122 RenderFrameHost* RenderViewHostTestHarness::pending_main_rfh() {
123 return WebContentsTester::For(web_contents())->GetPendingMainFrame();
126 BrowserContext* RenderViewHostTestHarness::browser_context() {
127 return browser_context_.get();
130 MockRenderProcessHost* RenderViewHostTestHarness::process() {
131 return static_cast<MockRenderProcessHost*>(active_rvh()->GetProcess());
134 void RenderViewHostTestHarness::DeleteContents() {
135 SetContents(NULL);
138 void RenderViewHostTestHarness::SetContents(WebContents* contents) {
139 contents_.reset(contents);
142 WebContents* RenderViewHostTestHarness::CreateTestWebContents() {
143 // Make sure we ran SetUp() already.
144 #if defined(OS_WIN)
145 DCHECK(ole_initializer_ != NULL);
146 #endif
147 #if defined(USE_AURA)
148 DCHECK(aura_test_helper_ != NULL);
149 #endif
151 // This will be deleted when the WebContentsImpl goes away.
152 SiteInstance* instance = SiteInstance::Create(browser_context_.get());
154 return TestWebContents::Create(browser_context_.get(), instance);
157 void RenderViewHostTestHarness::NavigateAndCommit(const GURL& url) {
158 static_cast<TestWebContents*>(web_contents())->NavigateAndCommit(url);
161 void RenderViewHostTestHarness::Reload() {
162 NavigationEntry* entry = controller().GetLastCommittedEntry();
163 DCHECK(entry);
164 controller().Reload(false);
165 RenderFrameHostTester::For(main_rfh())->SendNavigateWithTransition(
166 entry->GetPageID(), entry->GetURL(), ui::PAGE_TRANSITION_RELOAD);
169 void RenderViewHostTestHarness::FailedReload() {
170 NavigationEntry* entry = controller().GetLastCommittedEntry();
171 DCHECK(entry);
172 controller().Reload(false);
173 RenderFrameHostTester::For(main_rfh())->SendFailedNavigate(entry->GetPageID(),
174 entry->GetURL());
177 void RenderViewHostTestHarness::SetUp() {
178 thread_bundle_.reset(new TestBrowserThreadBundle(thread_bundle_options_));
180 #if defined(OS_WIN)
181 ole_initializer_.reset(new ui::ScopedOleInitializer());
182 #endif
183 #if defined(USE_AURA)
184 // The ContextFactory must exist before any Compositors are created.
185 bool enable_pixel_output = false;
186 ui::ContextFactory* context_factory =
187 ui::InitializeContextFactoryForTests(enable_pixel_output);
189 aura_test_helper_.reset(
190 new aura::test::AuraTestHelper(base::MessageLoopForUI::current()));
191 aura_test_helper_->SetUp(context_factory);
192 new wm::DefaultActivationClient(aura_test_helper_->root_window());
193 #endif
195 DCHECK(!browser_context_);
196 browser_context_.reset(CreateBrowserContext());
198 SetContents(CreateTestWebContents());
200 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
201 switches::kEnableBrowserSideNavigation)) {
202 BrowserSideNavigationSetUp();
206 void RenderViewHostTestHarness::TearDown() {
207 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
208 switches::kEnableBrowserSideNavigation)) {
209 BrowserSideNavigationTearDown();
212 SetContents(NULL);
213 #if defined(USE_AURA)
214 aura_test_helper_->TearDown();
215 ui::TerminateContextFactoryForTests();
216 #endif
217 // Make sure that we flush any messages related to WebContentsImpl destruction
218 // before we destroy the browser context.
219 base::RunLoop().RunUntilIdle();
221 #if defined(OS_WIN)
222 ole_initializer_.reset();
223 #endif
225 // Delete any RenderProcessHosts before the BrowserContext goes away.
226 if (rvh_test_enabler_.rph_factory_)
227 rvh_test_enabler_.rph_factory_.reset();
229 // Release the browser context by posting itself on the end of the task
230 // queue. This is preferable to immediate deletion because it will behave
231 // properly if the |rph_factory_| reset above enqueued any tasks which
232 // depend on |browser_context_|.
233 BrowserThread::DeleteSoon(content::BrowserThread::UI,
234 FROM_HERE,
235 browser_context_.release());
236 thread_bundle_.reset();
239 BrowserContext* RenderViewHostTestHarness::CreateBrowserContext() {
240 return new TestBrowserContext();
243 void RenderViewHostTestHarness::SetRenderProcessHostFactory(
244 RenderProcessHostFactory* factory) {
245 rvh_test_enabler_.rvh_factory_->set_render_process_host_factory(factory);
248 } // namespace content