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/test/test_render_view_host.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "content/browser/dom_storage/dom_storage_context_wrapper.h"
9 #include "content/browser/dom_storage/session_storage_namespace_impl.h"
10 #include "content/browser/site_instance_impl.h"
11 #include "content/common/dom_storage/dom_storage_types.h"
12 #include "content/common/frame_messages.h"
13 #include "content/common/view_messages.h"
14 #include "content/public/browser/browser_context.h"
15 #include "content/public/browser/navigation_controller.h"
16 #include "content/public/browser/storage_partition.h"
17 #include "content/public/common/content_client.h"
18 #include "content/public/common/page_state.h"
19 #include "content/test/test_web_contents.h"
20 #include "media/base/video_frame.h"
21 #include "ui/gfx/rect.h"
22 #include "webkit/common/webpreferences.h"
23
24 namespace content {
25
InitNavigateParams(FrameHostMsg_DidCommitProvisionalLoad_Params * params,int page_id,const GURL & url,PageTransition transition)26 void InitNavigateParams(FrameHostMsg_DidCommitProvisionalLoad_Params* params,
27 int page_id,
28 const GURL& url,
29 PageTransition transition) {
30 params->page_id = page_id;
31 params->url = url;
32 params->referrer = Referrer();
33 params->transition = transition;
34 params->redirects = std::vector<GURL>();
35 params->should_update_history = false;
36 params->searchable_form_url = GURL();
37 params->searchable_form_encoding = std::string();
38 params->security_info = std::string();
39 params->gesture = NavigationGestureUser;
40 params->was_within_same_page = false;
41 params->is_post = false;
42 params->page_state = PageState::CreateFromURL(url);
43 }
44
TestRenderWidgetHostView(RenderWidgetHost * rwh)45 TestRenderWidgetHostView::TestRenderWidgetHostView(RenderWidgetHost* rwh)
46 : rwh_(RenderWidgetHostImpl::From(rwh)),
47 is_showing_(false),
48 did_swap_compositor_frame_(false) {
49 rwh_->SetView(this);
50 }
51
~TestRenderWidgetHostView()52 TestRenderWidgetHostView::~TestRenderWidgetHostView() {
53 }
54
GetRenderWidgetHost() const55 RenderWidgetHost* TestRenderWidgetHostView::GetRenderWidgetHost() const {
56 return NULL;
57 }
58
GetNativeView() const59 gfx::NativeView TestRenderWidgetHostView::GetNativeView() const {
60 return NULL;
61 }
62
GetNativeViewId() const63 gfx::NativeViewId TestRenderWidgetHostView::GetNativeViewId() const {
64 return 0;
65 }
66
GetNativeViewAccessible()67 gfx::NativeViewAccessible TestRenderWidgetHostView::GetNativeViewAccessible() {
68 return NULL;
69 }
70
GetTextInputClient()71 ui::TextInputClient* TestRenderWidgetHostView::GetTextInputClient() {
72 return &text_input_client_;
73 }
74
HasFocus() const75 bool TestRenderWidgetHostView::HasFocus() const {
76 return true;
77 }
78
IsSurfaceAvailableForCopy() const79 bool TestRenderWidgetHostView::IsSurfaceAvailableForCopy() const {
80 return true;
81 }
82
Show()83 void TestRenderWidgetHostView::Show() {
84 is_showing_ = true;
85 }
86
Hide()87 void TestRenderWidgetHostView::Hide() {
88 is_showing_ = false;
89 }
90
IsShowing()91 bool TestRenderWidgetHostView::IsShowing() {
92 return is_showing_;
93 }
94
RenderProcessGone(base::TerminationStatus status,int error_code)95 void TestRenderWidgetHostView::RenderProcessGone(base::TerminationStatus status,
96 int error_code) {
97 delete this;
98 }
99
Destroy()100 void TestRenderWidgetHostView::Destroy() { delete this; }
101
GetViewBounds() const102 gfx::Rect TestRenderWidgetHostView::GetViewBounds() const {
103 return gfx::Rect();
104 }
105
CopyFromCompositingSurface(const gfx::Rect & src_subrect,const gfx::Size & dst_size,const base::Callback<void (bool,const SkBitmap &)> & callback,const SkBitmap::Config config)106 void TestRenderWidgetHostView::CopyFromCompositingSurface(
107 const gfx::Rect& src_subrect,
108 const gfx::Size& dst_size,
109 const base::Callback<void(bool, const SkBitmap&)>& callback,
110 const SkBitmap::Config config) {
111 callback.Run(false, SkBitmap());
112 }
113
CopyFromCompositingSurfaceToVideoFrame(const gfx::Rect & src_subrect,const scoped_refptr<media::VideoFrame> & target,const base::Callback<void (bool)> & callback)114 void TestRenderWidgetHostView::CopyFromCompositingSurfaceToVideoFrame(
115 const gfx::Rect& src_subrect,
116 const scoped_refptr<media::VideoFrame>& target,
117 const base::Callback<void(bool)>& callback) {
118 callback.Run(false);
119 }
120
CanCopyToVideoFrame() const121 bool TestRenderWidgetHostView::CanCopyToVideoFrame() const {
122 return false;
123 }
124
AcceleratedSurfaceInitialized(int host_id,int route_id)125 void TestRenderWidgetHostView::AcceleratedSurfaceInitialized(int host_id,
126 int route_id) {
127 }
128
AcceleratedSurfaceBuffersSwapped(const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params & params,int gpu_host_id)129 void TestRenderWidgetHostView::AcceleratedSurfaceBuffersSwapped(
130 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params,
131 int gpu_host_id) {
132 }
133
AcceleratedSurfacePostSubBuffer(const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params & params,int gpu_host_id)134 void TestRenderWidgetHostView::AcceleratedSurfacePostSubBuffer(
135 const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params& params,
136 int gpu_host_id) {
137 }
138
AcceleratedSurfaceSuspend()139 void TestRenderWidgetHostView::AcceleratedSurfaceSuspend() {
140 }
141
HasAcceleratedSurface(const gfx::Size & desired_size)142 bool TestRenderWidgetHostView::HasAcceleratedSurface(
143 const gfx::Size& desired_size) {
144 return false;
145 }
146
147 #if defined(OS_MACOSX)
148
SetActive(bool active)149 void TestRenderWidgetHostView::SetActive(bool active) {
150 // <viettrungluu@gmail.com>: Do I need to do anything here?
151 }
152
SupportsSpeech() const153 bool TestRenderWidgetHostView::SupportsSpeech() const {
154 return false;
155 }
156
SpeakSelection()157 void TestRenderWidgetHostView::SpeakSelection() {
158 }
159
IsSpeaking() const160 bool TestRenderWidgetHostView::IsSpeaking() const {
161 return false;
162 }
163
StopSpeaking()164 void TestRenderWidgetHostView::StopSpeaking() {
165 }
166
PostProcessEventForPluginIme(const NativeWebKeyboardEvent & event)167 bool TestRenderWidgetHostView::PostProcessEventForPluginIme(
168 const NativeWebKeyboardEvent& event) {
169 return false;
170 }
171
172 #endif
173
GetBoundsInRootWindow()174 gfx::Rect TestRenderWidgetHostView::GetBoundsInRootWindow() {
175 return gfx::Rect();
176 }
177
OnSwapCompositorFrame(uint32 output_surface_id,scoped_ptr<cc::CompositorFrame> frame)178 void TestRenderWidgetHostView::OnSwapCompositorFrame(
179 uint32 output_surface_id,
180 scoped_ptr<cc::CompositorFrame> frame) {
181 did_swap_compositor_frame_ = true;
182 }
183
184
GetCompositingSurface()185 gfx::GLSurfaceHandle TestRenderWidgetHostView::GetCompositingSurface() {
186 return gfx::GLSurfaceHandle();
187 }
188
LockMouse()189 bool TestRenderWidgetHostView::LockMouse() {
190 return false;
191 }
192
UnlockMouse()193 void TestRenderWidgetHostView::UnlockMouse() {
194 }
195
196 #if defined(OS_WIN)
SetParentNativeViewAccessible(gfx::NativeViewAccessible accessible_parent)197 void TestRenderWidgetHostView::SetParentNativeViewAccessible(
198 gfx::NativeViewAccessible accessible_parent) {
199 }
200
GetParentForWindowlessPlugin() const201 gfx::NativeViewId TestRenderWidgetHostView::GetParentForWindowlessPlugin()
202 const {
203 return 0;
204 }
205 #endif
206
TestRenderViewHost(SiteInstance * instance,RenderViewHostDelegate * delegate,RenderWidgetHostDelegate * widget_delegate,int routing_id,int main_frame_routing_id,bool swapped_out)207 TestRenderViewHost::TestRenderViewHost(
208 SiteInstance* instance,
209 RenderViewHostDelegate* delegate,
210 RenderWidgetHostDelegate* widget_delegate,
211 int routing_id,
212 int main_frame_routing_id,
213 bool swapped_out)
214 : RenderViewHostImpl(instance,
215 delegate,
216 widget_delegate,
217 routing_id,
218 main_frame_routing_id,
219 swapped_out,
220 false /* hidden */),
221 render_view_created_(false),
222 delete_counter_(NULL),
223 simulate_fetch_via_proxy_(false),
224 simulate_history_list_was_cleared_(false),
225 contents_mime_type_("text/html"),
226 opener_route_id_(MSG_ROUTING_NONE),
227 main_render_frame_host_(NULL) {
228 // TestRenderWidgetHostView installs itself into this->view_ in its
229 // constructor, and deletes itself when TestRenderWidgetHostView::Destroy() is
230 // called.
231 new TestRenderWidgetHostView(this);
232 }
233
~TestRenderViewHost()234 TestRenderViewHost::~TestRenderViewHost() {
235 if (delete_counter_)
236 ++*delete_counter_;
237 }
238
CreateRenderView(const base::string16 & frame_name,int opener_route_id,int proxy_route_id,int32 max_page_id,bool window_was_created_with_opener)239 bool TestRenderViewHost::CreateRenderView(
240 const base::string16& frame_name,
241 int opener_route_id,
242 int proxy_route_id,
243 int32 max_page_id,
244 bool window_was_created_with_opener) {
245 DCHECK(!render_view_created_);
246 render_view_created_ = true;
247 opener_route_id_ = opener_route_id;
248 return true;
249 }
250
IsRenderViewLive() const251 bool TestRenderViewHost::IsRenderViewLive() const {
252 return render_view_created_;
253 }
254
IsFullscreen() const255 bool TestRenderViewHost::IsFullscreen() const {
256 return RenderViewHostImpl::IsFullscreen();
257 }
258
SendNavigate(int page_id,const GURL & url)259 void TestRenderViewHost::SendNavigate(int page_id, const GURL& url) {
260 main_render_frame_host_->SendNavigate(page_id, url);
261 }
262
SendFailedNavigate(int page_id,const GURL & url)263 void TestRenderViewHost::SendFailedNavigate(int page_id, const GURL& url) {
264 main_render_frame_host_->SendFailedNavigate(page_id, url);
265 }
266
SendNavigateWithTransition(int page_id,const GURL & url,PageTransition transition)267 void TestRenderViewHost::SendNavigateWithTransition(
268 int page_id,
269 const GURL& url,
270 PageTransition transition) {
271 main_render_frame_host_->SendNavigateWithTransition(page_id, url, transition);
272 }
273
SendNavigateWithOriginalRequestURL(int page_id,const GURL & url,const GURL & original_request_url)274 void TestRenderViewHost::SendNavigateWithOriginalRequestURL(
275 int page_id,
276 const GURL& url,
277 const GURL& original_request_url) {
278 main_render_frame_host_->SendNavigateWithOriginalRequestURL(
279 page_id, url, original_request_url);
280 }
281
SendNavigateWithFile(int page_id,const GURL & url,const base::FilePath & file_path)282 void TestRenderViewHost::SendNavigateWithFile(
283 int page_id,
284 const GURL& url,
285 const base::FilePath& file_path) {
286 main_render_frame_host_->SendNavigateWithFile(page_id, url, file_path);
287 }
288
SendNavigateWithParams(FrameHostMsg_DidCommitProvisionalLoad_Params * params)289 void TestRenderViewHost::SendNavigateWithParams(
290 FrameHostMsg_DidCommitProvisionalLoad_Params* params) {
291 main_render_frame_host_->SendNavigateWithParams(params);
292 }
293
SendNavigateWithTransitionAndResponseCode(int page_id,const GURL & url,PageTransition transition,int response_code)294 void TestRenderViewHost::SendNavigateWithTransitionAndResponseCode(
295 int page_id,
296 const GURL& url,
297 PageTransition transition,
298 int response_code) {
299 main_render_frame_host_->SendNavigateWithTransitionAndResponseCode(
300 page_id, url, transition, response_code);
301 }
302
SendNavigateWithParameters(int page_id,const GURL & url,PageTransition transition,const GURL & original_request_url,int response_code,const base::FilePath * file_path_for_history_item)303 void TestRenderViewHost::SendNavigateWithParameters(
304 int page_id,
305 const GURL& url,
306 PageTransition transition,
307 const GURL& original_request_url,
308 int response_code,
309 const base::FilePath* file_path_for_history_item) {
310
311 main_render_frame_host_->SendNavigateWithParameters(
312 page_id, url, transition, original_request_url, response_code,
313 file_path_for_history_item, std::vector<GURL>());
314 }
315
SendBeforeUnloadACK(bool proceed)316 void TestRenderViewHost::SendBeforeUnloadACK(bool proceed) {
317 // TODO(creis): Move this whole method to TestRenderFrameHost.
318 base::TimeTicks now = base::TimeTicks::Now();
319 main_render_frame_host_->OnBeforeUnloadACK(proceed, now, now);
320 }
321
SetContentsMimeType(const std::string & mime_type)322 void TestRenderViewHost::SetContentsMimeType(const std::string& mime_type) {
323 contents_mime_type_ = mime_type;
324 main_render_frame_host_->set_contents_mime_type(mime_type);
325 }
326
SimulateSwapOutACK()327 void TestRenderViewHost::SimulateSwapOutACK() {
328 OnSwappedOut(false);
329 }
330
SimulateWasHidden()331 void TestRenderViewHost::SimulateWasHidden() {
332 WasHidden();
333 }
334
SimulateWasShown()335 void TestRenderViewHost::SimulateWasShown() {
336 WasShown();
337 }
338
TestOnStartDragging(const DropData & drop_data)339 void TestRenderViewHost::TestOnStartDragging(
340 const DropData& drop_data) {
341 blink::WebDragOperationsMask drag_operation = blink::WebDragOperationEvery;
342 DragEventSourceInfo event_info;
343 OnStartDragging(drop_data, drag_operation, SkBitmap(), gfx::Vector2d(),
344 event_info);
345 }
346
TestOnUpdateStateWithFile(int process_id,const base::FilePath & file_path)347 void TestRenderViewHost::TestOnUpdateStateWithFile(
348 int process_id,
349 const base::FilePath& file_path) {
350 OnUpdateState(process_id,
351 PageState::CreateForTesting(GURL("http://www.google.com"),
352 false,
353 "data",
354 &file_path));
355 }
356
set_simulate_fetch_via_proxy(bool proxy)357 void TestRenderViewHost::set_simulate_fetch_via_proxy(bool proxy) {
358 simulate_fetch_via_proxy_ = proxy;
359 }
360
set_simulate_history_list_was_cleared(bool cleared)361 void TestRenderViewHost::set_simulate_history_list_was_cleared(bool cleared) {
362 simulate_history_list_was_cleared_ = cleared;
363 main_render_frame_host_->set_simulate_history_list_was_cleared(cleared);
364 }
365
RenderViewHostImplTestHarness()366 RenderViewHostImplTestHarness::RenderViewHostImplTestHarness() {
367 std::vector<ui::ScaleFactor> scale_factors;
368 scale_factors.push_back(ui::SCALE_FACTOR_100P);
369 scoped_set_supported_scale_factors_.reset(
370 new ui::test::ScopedSetSupportedScaleFactors(scale_factors));
371 }
372
~RenderViewHostImplTestHarness()373 RenderViewHostImplTestHarness::~RenderViewHostImplTestHarness() {
374 }
375
test_rvh()376 TestRenderViewHost* RenderViewHostImplTestHarness::test_rvh() {
377 return static_cast<TestRenderViewHost*>(rvh());
378 }
379
pending_test_rvh()380 TestRenderViewHost* RenderViewHostImplTestHarness::pending_test_rvh() {
381 return static_cast<TestRenderViewHost*>(pending_rvh());
382 }
383
active_test_rvh()384 TestRenderViewHost* RenderViewHostImplTestHarness::active_test_rvh() {
385 return static_cast<TestRenderViewHost*>(active_rvh());
386 }
387
main_test_rfh()388 TestRenderFrameHost* RenderViewHostImplTestHarness::main_test_rfh() {
389 return static_cast<TestRenderFrameHost*>(main_rfh());
390 }
391
contents()392 TestWebContents* RenderViewHostImplTestHarness::contents() {
393 return static_cast<TestWebContents*>(web_contents());
394 }
395
396 } // namespace content
397