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