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