Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / public / test / test_renderer_host.cc
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.
4
5 #include "content/public/test/test_renderer_host.h"
6
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"
20
21 #if defined(OS_WIN)
22 #include "ui/base/win/scoped_ole_initializer.h"
23 #endif
24
25 #if defined(USE_AURA)
26 #include "ui/aura/test/aura_test_helper.h"
27 #endif
28
29 namespace content {
30
31 // RenderViewHostTester -------------------------------------------------------
32
33 // static
34 RenderViewHostTester* RenderViewHostTester::For(RenderViewHost* host) {
35   return static_cast<TestRenderViewHost*>(host);
36 }
37
38 // static
39 RenderViewHost* RenderViewHostTester::GetPendingForController(
40     NavigationController* controller) {
41   WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
42       controller->GetWebContents());
43   return web_contents->GetRenderManagerForTesting()->pending_render_view_host();
44 }
45
46 // static
47 bool RenderViewHostTester::IsRenderViewHostSwappedOut(RenderViewHost* rvh) {
48   return static_cast<RenderViewHostImpl*>(rvh)->rvh_state() ==
49          RenderViewHostImpl::STATE_SWAPPED_OUT;
50 }
51
52 // static
53 bool RenderViewHostTester::TestOnMessageReceived(RenderViewHost* rvh,
54                                                  const IPC::Message& msg) {
55   return static_cast<RenderViewHostImpl*>(rvh)->OnMessageReceived(msg);
56 }
57
58 // static
59 bool RenderViewHostTester::HasTouchEventHandler(RenderViewHost* rvh) {
60   RenderWidgetHostImpl* host_impl = RenderWidgetHostImpl::From(rvh);
61   return host_impl->has_touch_handler();
62 }
63
64
65 // RenderViewHostTestEnabler --------------------------------------------------
66
67 RenderViewHostTestEnabler::RenderViewHostTestEnabler()
68     : rph_factory_(new MockRenderProcessHostFactory()),
69       rvh_factory_(new TestRenderViewHostFactory(rph_factory_.get())),
70       rfh_factory_(new TestRenderFrameHostFactory()) {}
71
72 RenderViewHostTestEnabler::~RenderViewHostTestEnabler() {
73 }
74
75
76 // RenderViewHostTestHarness --------------------------------------------------
77
78 RenderViewHostTestHarness::RenderViewHostTestHarness()
79     : thread_bundle_options_(TestBrowserThreadBundle::DEFAULT) {}
80
81 RenderViewHostTestHarness::~RenderViewHostTestHarness() {
82 }
83
84 NavigationController& RenderViewHostTestHarness::controller() {
85   return web_contents()->GetController();
86 }
87
88 WebContents* RenderViewHostTestHarness::web_contents() {
89   return contents_.get();
90 }
91
92 RenderViewHost* RenderViewHostTestHarness::rvh() {
93   return web_contents()->GetRenderViewHost();
94 }
95
96 RenderViewHost* RenderViewHostTestHarness::pending_rvh() {
97   return static_cast<TestWebContents*>(web_contents())->
98       GetRenderManagerForTesting()->pending_render_view_host();
99 }
100
101 RenderViewHost* RenderViewHostTestHarness::active_rvh() {
102   return pending_rvh() ? pending_rvh() : rvh();
103 }
104
105 RenderFrameHost* RenderViewHostTestHarness::main_rfh() {
106   WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
107       this->web_contents());
108   return web_contents->GetFrameTree()->GetMainFrame();
109 }
110
111 BrowserContext* RenderViewHostTestHarness::browser_context() {
112   return browser_context_.get();
113 }
114
115 MockRenderProcessHost* RenderViewHostTestHarness::process() {
116   return static_cast<MockRenderProcessHost*>(active_rvh()->GetProcess());
117 }
118
119 void RenderViewHostTestHarness::DeleteContents() {
120   SetContents(NULL);
121 }
122
123 void RenderViewHostTestHarness::SetContents(WebContents* contents) {
124   contents_.reset(contents);
125 }
126
127 WebContents* RenderViewHostTestHarness::CreateTestWebContents() {
128   // Make sure we ran SetUp() already.
129 #if defined(OS_WIN)
130   DCHECK(ole_initializer_ != NULL);
131 #endif
132 #if defined(USE_AURA)
133   DCHECK(aura_test_helper_ != NULL);
134 #endif
135
136   // This will be deleted when the WebContentsImpl goes away.
137   SiteInstance* instance = SiteInstance::Create(browser_context_.get());
138
139   return TestWebContents::Create(browser_context_.get(), instance);
140 }
141
142 void RenderViewHostTestHarness::NavigateAndCommit(const GURL& url) {
143   static_cast<TestWebContents*>(web_contents())->NavigateAndCommit(url);
144 }
145
146 void RenderViewHostTestHarness::Reload() {
147   NavigationEntry* entry = controller().GetLastCommittedEntry();
148   DCHECK(entry);
149   controller().Reload(false);
150   static_cast<TestRenderViewHost*>(
151       rvh())->SendNavigate(entry->GetPageID(), entry->GetURL());
152 }
153
154 void RenderViewHostTestHarness::FailedReload() {
155   NavigationEntry* entry = controller().GetLastCommittedEntry();
156   DCHECK(entry);
157   controller().Reload(false);
158   static_cast<TestRenderViewHost*>(
159       rvh())->SendFailedNavigate(entry->GetPageID(), entry->GetURL());
160 }
161
162 void RenderViewHostTestHarness::SetUp() {
163   thread_bundle_.reset(new TestBrowserThreadBundle(thread_bundle_options_));
164
165 #if defined(OS_WIN)
166   ole_initializer_.reset(new ui::ScopedOleInitializer());
167 #endif
168 #if defined(USE_AURA)
169   aura_test_helper_.reset(
170       new aura::test::AuraTestHelper(base::MessageLoopForUI::current()));
171   bool allow_test_contexts = true;
172   aura_test_helper_->SetUp(allow_test_contexts);
173 #endif
174
175   DCHECK(!browser_context_);
176   browser_context_.reset(CreateBrowserContext());
177
178   SetContents(CreateTestWebContents());
179 }
180
181 void RenderViewHostTestHarness::TearDown() {
182   SetContents(NULL);
183 #if defined(USE_AURA)
184   aura_test_helper_->TearDown();
185 #endif
186   // Make sure that we flush any messages related to WebContentsImpl destruction
187   // before we destroy the browser context.
188   base::RunLoop().RunUntilIdle();
189
190 #if defined(OS_WIN)
191   ole_initializer_.reset();
192 #endif
193
194   // Delete any RenderProcessHosts before the BrowserContext goes away.
195   if (rvh_test_enabler_.rph_factory_)
196     rvh_test_enabler_.rph_factory_.reset();
197
198   // Release the browser context by posting itself on the end of the task
199   // queue. This is preferable to immediate deletion because it will behave
200   // properly if the |rph_factory_| reset above enqueued any tasks which
201   // depend on |browser_context_|.
202   BrowserThread::DeleteSoon(content::BrowserThread::UI,
203                             FROM_HERE,
204                             browser_context_.release());
205   thread_bundle_.reset();
206 }
207
208 BrowserContext* RenderViewHostTestHarness::CreateBrowserContext() {
209   return new TestBrowserContext();
210 }
211
212 void RenderViewHostTestHarness::SetRenderProcessHostFactory(
213     RenderProcessHostFactory* factory) {
214     rvh_test_enabler_.rvh_factory_->set_render_process_host_factory(factory);
215 }
216
217 }  // namespace content