[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / content / public / test / test_renderer_host.cc
blobd8847791a77f9dd1449664c8978b8450184646e1
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/content_browser_sanity_checker.h"
20 #include "content/test/test_render_frame_host.h"
21 #include "content/test/test_render_frame_host_factory.h"
22 #include "content/test/test_render_view_host.h"
23 #include "content/test/test_render_view_host_factory.h"
24 #include "content/test/test_web_contents.h"
26 #if defined(OS_WIN)
27 #include "ui/base/win/scoped_ole_initializer.h"
28 #endif
30 #if defined(USE_AURA)
31 #include "ui/aura/test/aura_test_helper.h"
32 #include "ui/compositor/test/context_factories_for_test.h"
33 #include "ui/wm/core/default_activation_client.h"
34 #endif
36 namespace content {
38 // RenderFrameHostTester ------------------------------------------------------
40 // static
41 RenderFrameHostTester* RenderFrameHostTester::For(RenderFrameHost* host) {
42 return static_cast<TestRenderFrameHost*>(host);
45 // static
46 RenderFrameHost* RenderFrameHostTester::GetPendingForController(
47 NavigationController* controller) {
48 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
49 controller->GetWebContents());
50 return web_contents->GetRenderManagerForTesting()->pending_frame_host();
53 // static
54 bool RenderFrameHostTester::IsRenderFrameHostSwappedOut(RenderFrameHost* rfh) {
55 return static_cast<RenderFrameHostImpl*>(rfh)->is_swapped_out();
58 // RenderViewHostTester -------------------------------------------------------
60 // static
61 RenderViewHostTester* RenderViewHostTester::For(RenderViewHost* host) {
62 return static_cast<TestRenderViewHost*>(host);
65 // static
66 bool RenderViewHostTester::TestOnMessageReceived(RenderViewHost* rvh,
67 const IPC::Message& msg) {
68 return static_cast<RenderViewHostImpl*>(rvh)->OnMessageReceived(msg);
71 // static
72 bool RenderViewHostTester::HasTouchEventHandler(RenderViewHost* rvh) {
73 RenderWidgetHostImpl* host_impl = RenderWidgetHostImpl::From(rvh);
74 return host_impl->has_touch_handler();
78 // RenderViewHostTestEnabler --------------------------------------------------
80 RenderViewHostTestEnabler::RenderViewHostTestEnabler()
81 : rph_factory_(new MockRenderProcessHostFactory()),
82 rvh_factory_(new TestRenderViewHostFactory(rph_factory_.get())),
83 rfh_factory_(new TestRenderFrameHostFactory()) {}
85 RenderViewHostTestEnabler::~RenderViewHostTestEnabler() {
89 // RenderViewHostTestHarness --------------------------------------------------
91 RenderViewHostTestHarness::RenderViewHostTestHarness()
92 : thread_bundle_options_(TestBrowserThreadBundle::DEFAULT) {}
94 RenderViewHostTestHarness::~RenderViewHostTestHarness() {
97 NavigationController& RenderViewHostTestHarness::controller() {
98 return web_contents()->GetController();
101 WebContents* RenderViewHostTestHarness::web_contents() {
102 return contents_.get();
105 RenderViewHost* RenderViewHostTestHarness::rvh() {
106 RenderViewHost* result = web_contents()->GetRenderViewHost();
107 CHECK_EQ(result, web_contents()->GetMainFrame()->GetRenderViewHost());
108 return result;
111 RenderViewHost* RenderViewHostTestHarness::pending_rvh() {
112 return pending_main_rfh() ? pending_main_rfh()->GetRenderViewHost() : NULL;
115 RenderViewHost* RenderViewHostTestHarness::active_rvh() {
116 return pending_rvh() ? pending_rvh() : rvh();
119 RenderFrameHost* RenderViewHostTestHarness::main_rfh() {
120 return web_contents()->GetMainFrame();
123 RenderFrameHost* RenderViewHostTestHarness::pending_main_rfh() {
124 return WebContentsTester::For(web_contents())->GetPendingMainFrame();
127 BrowserContext* RenderViewHostTestHarness::browser_context() {
128 return browser_context_.get();
131 MockRenderProcessHost* RenderViewHostTestHarness::process() {
132 return static_cast<MockRenderProcessHost*>(active_rvh()->GetProcess());
135 void RenderViewHostTestHarness::DeleteContents() {
136 SetContents(NULL);
139 void RenderViewHostTestHarness::SetContents(WebContents* contents) {
140 contents_.reset(contents);
143 WebContents* RenderViewHostTestHarness::CreateTestWebContents() {
144 // Make sure we ran SetUp() already.
145 #if defined(OS_WIN)
146 DCHECK(ole_initializer_ != NULL);
147 #endif
148 #if defined(USE_AURA)
149 DCHECK(aura_test_helper_ != NULL);
150 #endif
152 // This will be deleted when the WebContentsImpl goes away.
153 SiteInstance* instance = SiteInstance::Create(browser_context_.get());
154 instance->GetProcess()->Init();
156 return TestWebContents::Create(browser_context_.get(), instance);
159 void RenderViewHostTestHarness::NavigateAndCommit(const GURL& url) {
160 static_cast<TestWebContents*>(web_contents())->NavigateAndCommit(url);
163 void RenderViewHostTestHarness::Reload() {
164 NavigationEntry* entry = controller().GetLastCommittedEntry();
165 DCHECK(entry);
166 controller().Reload(false);
167 RenderFrameHostTester::For(main_rfh())
168 ->SendNavigateWithTransition(entry->GetPageID(), entry->GetUniqueID(),
169 false, entry->GetURL(),
170 ui::PAGE_TRANSITION_RELOAD);
173 void RenderViewHostTestHarness::FailedReload() {
174 NavigationEntry* entry = controller().GetLastCommittedEntry();
175 DCHECK(entry);
176 controller().Reload(false);
177 RenderFrameHostTester::For(main_rfh())
178 ->SendFailedNavigate(entry->GetPageID(), entry->GetUniqueID(), false,
179 entry->GetURL());
182 void RenderViewHostTestHarness::SetUp() {
183 thread_bundle_.reset(new TestBrowserThreadBundle(thread_bundle_options_));
185 #if defined(OS_WIN)
186 ole_initializer_.reset(new ui::ScopedOleInitializer());
187 #endif
188 #if defined(USE_AURA)
189 // The ContextFactory must exist before any Compositors are created.
190 bool enable_pixel_output = false;
191 ui::ContextFactory* context_factory =
192 ui::InitializeContextFactoryForTests(enable_pixel_output);
194 aura_test_helper_.reset(
195 new aura::test::AuraTestHelper(base::MessageLoopForUI::current()));
196 aura_test_helper_->SetUp(context_factory);
197 new wm::DefaultActivationClient(aura_test_helper_->root_window());
198 #endif
200 sanity_checker_.reset(new ContentBrowserSanityChecker());
202 DCHECK(!browser_context_);
203 browser_context_.reset(CreateBrowserContext());
205 SetContents(CreateTestWebContents());
207 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
208 switches::kEnableBrowserSideNavigation)) {
209 BrowserSideNavigationSetUp();
213 void RenderViewHostTestHarness::TearDown() {
214 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
215 switches::kEnableBrowserSideNavigation)) {
216 BrowserSideNavigationTearDown();
219 SetContents(NULL);
220 #if defined(USE_AURA)
221 aura_test_helper_->TearDown();
222 ui::TerminateContextFactoryForTests();
223 #endif
224 // Make sure that we flush any messages related to WebContentsImpl destruction
225 // before we destroy the browser context.
226 base::RunLoop().RunUntilIdle();
228 #if defined(OS_WIN)
229 ole_initializer_.reset();
230 #endif
232 // Delete any RenderProcessHosts before the BrowserContext goes away.
233 if (rvh_test_enabler_.rph_factory_)
234 rvh_test_enabler_.rph_factory_.reset();
236 // Release the browser context by posting itself on the end of the task
237 // queue. This is preferable to immediate deletion because it will behave
238 // properly if the |rph_factory_| reset above enqueued any tasks which
239 // depend on |browser_context_|.
240 BrowserThread::DeleteSoon(content::BrowserThread::UI,
241 FROM_HERE,
242 browser_context_.release());
243 thread_bundle_.reset();
246 BrowserContext* RenderViewHostTestHarness::CreateBrowserContext() {
247 return new TestBrowserContext();
250 void RenderViewHostTestHarness::SetRenderProcessHostFactory(
251 RenderProcessHostFactory* factory) {
252 rvh_test_enabler_.rvh_factory_->set_render_process_host_factory(factory);
255 } // namespace content