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