Make sure webrtc::VideoSourceInterface is released on the main render thread.
[chromium-blink-merge.git] / content / public / test / test_renderer_host.cc
blobabaa4cefe2f6dda4520e8ec5bc571349ff2c2dd9
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_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"
21 #if defined(OS_WIN)
22 #include "ui/base/win/scoped_ole_initializer.h"
23 #endif
25 #if defined(USE_AURA)
26 #include "ui/aura/test/aura_test_helper.h"
27 #include "ui/compositor/test/context_factories_for_test.h"
28 #include "ui/wm/core/default_activation_client.h"
29 #endif
31 namespace content {
33 // RenderViewHostTester -------------------------------------------------------
35 // static
36 RenderViewHostTester* RenderViewHostTester::For(RenderViewHost* host) {
37 return static_cast<TestRenderViewHost*>(host);
40 // static
41 RenderViewHost* RenderViewHostTester::GetPendingForController(
42 NavigationController* controller) {
43 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
44 controller->GetWebContents());
45 return web_contents->GetRenderManagerForTesting()->pending_render_view_host();
48 // static
49 bool RenderViewHostTester::IsRenderViewHostSwappedOut(RenderViewHost* rvh) {
50 return static_cast<RenderViewHostImpl*>(rvh)->rvh_state() ==
51 RenderViewHostImpl::STATE_SWAPPED_OUT;
54 // static
55 bool RenderViewHostTester::TestOnMessageReceived(RenderViewHost* rvh,
56 const IPC::Message& msg) {
57 return static_cast<RenderViewHostImpl*>(rvh)->OnMessageReceived(msg);
60 // static
61 bool RenderViewHostTester::HasTouchEventHandler(RenderViewHost* rvh) {
62 RenderWidgetHostImpl* host_impl = RenderWidgetHostImpl::From(rvh);
63 return host_impl->has_touch_handler();
67 // RenderViewHostTestEnabler --------------------------------------------------
69 RenderViewHostTestEnabler::RenderViewHostTestEnabler()
70 : rph_factory_(new MockRenderProcessHostFactory()),
71 rvh_factory_(new TestRenderViewHostFactory(rph_factory_.get())),
72 rfh_factory_(new TestRenderFrameHostFactory()) {}
74 RenderViewHostTestEnabler::~RenderViewHostTestEnabler() {
78 // RenderViewHostTestHarness --------------------------------------------------
80 RenderViewHostTestHarness::RenderViewHostTestHarness()
81 : thread_bundle_options_(TestBrowserThreadBundle::DEFAULT) {}
83 RenderViewHostTestHarness::~RenderViewHostTestHarness() {
86 NavigationController& RenderViewHostTestHarness::controller() {
87 return web_contents()->GetController();
90 WebContents* RenderViewHostTestHarness::web_contents() {
91 return contents_.get();
94 RenderViewHost* RenderViewHostTestHarness::rvh() {
95 return web_contents()->GetRenderViewHost();
98 RenderViewHost* RenderViewHostTestHarness::pending_rvh() {
99 return static_cast<TestWebContents*>(web_contents())->
100 GetRenderManagerForTesting()->pending_render_view_host();
103 RenderViewHost* RenderViewHostTestHarness::active_rvh() {
104 return pending_rvh() ? pending_rvh() : rvh();
107 RenderFrameHost* RenderViewHostTestHarness::main_rfh() {
108 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
109 this->web_contents());
110 return web_contents->GetFrameTree()->GetMainFrame();
113 BrowserContext* RenderViewHostTestHarness::browser_context() {
114 return browser_context_.get();
117 MockRenderProcessHost* RenderViewHostTestHarness::process() {
118 return static_cast<MockRenderProcessHost*>(active_rvh()->GetProcess());
121 void RenderViewHostTestHarness::DeleteContents() {
122 SetContents(NULL);
125 void RenderViewHostTestHarness::SetContents(WebContents* contents) {
126 contents_.reset(contents);
129 WebContents* RenderViewHostTestHarness::CreateTestWebContents() {
130 // Make sure we ran SetUp() already.
131 #if defined(OS_WIN)
132 DCHECK(ole_initializer_ != NULL);
133 #endif
134 #if defined(USE_AURA)
135 DCHECK(aura_test_helper_ != NULL);
136 #endif
138 // This will be deleted when the WebContentsImpl goes away.
139 SiteInstance* instance = SiteInstance::Create(browser_context_.get());
141 return TestWebContents::Create(browser_context_.get(), instance);
144 void RenderViewHostTestHarness::NavigateAndCommit(const GURL& url) {
145 static_cast<TestWebContents*>(web_contents())->NavigateAndCommit(url);
148 void RenderViewHostTestHarness::Reload() {
149 NavigationEntry* entry = controller().GetLastCommittedEntry();
150 DCHECK(entry);
151 controller().Reload(false);
152 static_cast<TestRenderViewHost*>(
153 rvh())->SendNavigate(entry->GetPageID(), entry->GetURL());
156 void RenderViewHostTestHarness::FailedReload() {
157 NavigationEntry* entry = controller().GetLastCommittedEntry();
158 DCHECK(entry);
159 controller().Reload(false);
160 static_cast<TestRenderViewHost*>(
161 rvh())->SendFailedNavigate(entry->GetPageID(), entry->GetURL());
164 void RenderViewHostTestHarness::SetUp() {
165 thread_bundle_.reset(new TestBrowserThreadBundle(thread_bundle_options_));
167 #if defined(OS_WIN)
168 ole_initializer_.reset(new ui::ScopedOleInitializer());
169 #endif
170 #if defined(USE_AURA)
171 // The ContextFactory must exist before any Compositors are created.
172 bool enable_pixel_output = false;
173 ui::InitializeContextFactoryForTests(enable_pixel_output);
175 aura_test_helper_.reset(
176 new aura::test::AuraTestHelper(base::MessageLoopForUI::current()));
177 aura_test_helper_->SetUp();
178 new wm::DefaultActivationClient(aura_test_helper_->root_window());
179 #endif
181 DCHECK(!browser_context_);
182 browser_context_.reset(CreateBrowserContext());
184 SetContents(CreateTestWebContents());
187 void RenderViewHostTestHarness::TearDown() {
188 SetContents(NULL);
189 #if defined(USE_AURA)
190 aura_test_helper_->TearDown();
191 ui::TerminateContextFactoryForTests();
192 #endif
193 // Make sure that we flush any messages related to WebContentsImpl destruction
194 // before we destroy the browser context.
195 base::RunLoop().RunUntilIdle();
197 #if defined(OS_WIN)
198 ole_initializer_.reset();
199 #endif
201 // Delete any RenderProcessHosts before the BrowserContext goes away.
202 if (rvh_test_enabler_.rph_factory_)
203 rvh_test_enabler_.rph_factory_.reset();
205 // Release the browser context by posting itself on the end of the task
206 // queue. This is preferable to immediate deletion because it will behave
207 // properly if the |rph_factory_| reset above enqueued any tasks which
208 // depend on |browser_context_|.
209 BrowserThread::DeleteSoon(content::BrowserThread::UI,
210 FROM_HERE,
211 browser_context_.release());
212 thread_bundle_.reset();
215 BrowserContext* RenderViewHostTestHarness::CreateBrowserContext() {
216 return new TestBrowserContext();
219 void RenderViewHostTestHarness::SetRenderProcessHostFactory(
220 RenderProcessHostFactory* factory) {
221 rvh_test_enabler_.rvh_factory_->set_render_process_host_factory(factory);
224 } // namespace content