• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/view_messages.h"
13 #include "content/public/browser/browser_context.h"
14 #include "content/public/browser/navigation_controller.h"
15 #include "content/public/browser/storage_partition.h"
16 #include "content/public/common/content_client.h"
17 #include "content/public/common/page_state.h"
18 #include "content/test/test_backing_store.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 
26 namespace {
27 
28 const int64 kFrameId = 13UL;
29 
30 }  // namespace
31 
32 
InitNavigateParams(ViewHostMsg_FrameNavigate_Params * params,int page_id,const GURL & url,PageTransition transition)33 void InitNavigateParams(ViewHostMsg_FrameNavigate_Params* params,
34                         int page_id,
35                         const GURL& url,
36                         PageTransition transition) {
37   params->page_id = page_id;
38   params->url = url;
39   params->referrer = Referrer();
40   params->transition = transition;
41   params->redirects = std::vector<GURL>();
42   params->should_update_history = false;
43   params->searchable_form_url = GURL();
44   params->searchable_form_encoding = std::string();
45   params->security_info = std::string();
46   params->gesture = NavigationGestureUser;
47   params->was_within_same_page = false;
48   params->is_post = false;
49   params->page_state = PageState::CreateFromURL(url);
50 }
51 
TestRenderWidgetHostView(RenderWidgetHost * rwh)52 TestRenderWidgetHostView::TestRenderWidgetHostView(RenderWidgetHost* rwh)
53     : rwh_(RenderWidgetHostImpl::From(rwh)),
54       is_showing_(false),
55       did_swap_compositor_frame_(false) {
56   rwh_->SetView(this);
57 }
58 
~TestRenderWidgetHostView()59 TestRenderWidgetHostView::~TestRenderWidgetHostView() {
60 }
61 
GetRenderWidgetHost() const62 RenderWidgetHost* TestRenderWidgetHostView::GetRenderWidgetHost() const {
63   return NULL;
64 }
65 
GetNativeView() const66 gfx::NativeView TestRenderWidgetHostView::GetNativeView() const {
67   return NULL;
68 }
69 
GetNativeViewId() const70 gfx::NativeViewId TestRenderWidgetHostView::GetNativeViewId() const {
71   return 0;
72 }
73 
GetNativeViewAccessible()74 gfx::NativeViewAccessible TestRenderWidgetHostView::GetNativeViewAccessible() {
75   return NULL;
76 }
77 
HasFocus() const78 bool TestRenderWidgetHostView::HasFocus() const {
79   return true;
80 }
81 
IsSurfaceAvailableForCopy() const82 bool TestRenderWidgetHostView::IsSurfaceAvailableForCopy() const {
83   return true;
84 }
85 
Show()86 void TestRenderWidgetHostView::Show() {
87   is_showing_ = true;
88 }
89 
Hide()90 void TestRenderWidgetHostView::Hide() {
91   is_showing_ = false;
92 }
93 
IsShowing()94 bool TestRenderWidgetHostView::IsShowing() {
95   return is_showing_;
96 }
97 
RenderProcessGone(base::TerminationStatus status,int error_code)98 void TestRenderWidgetHostView::RenderProcessGone(base::TerminationStatus status,
99                                                  int error_code) {
100   delete this;
101 }
102 
Destroy()103 void TestRenderWidgetHostView::Destroy() { delete this; }
104 
GetViewBounds() const105 gfx::Rect TestRenderWidgetHostView::GetViewBounds() const {
106   return gfx::Rect();
107 }
108 
AllocBackingStore(const gfx::Size & size)109 BackingStore* TestRenderWidgetHostView::AllocBackingStore(
110     const gfx::Size& size) {
111   return new TestBackingStore(rwh_, size);
112 }
113 
CopyFromCompositingSurface(const gfx::Rect & src_subrect,const gfx::Size & dst_size,const base::Callback<void (bool,const SkBitmap &)> & callback)114 void TestRenderWidgetHostView::CopyFromCompositingSurface(
115     const gfx::Rect& src_subrect,
116     const gfx::Size& dst_size,
117     const base::Callback<void(bool, const SkBitmap&)>& callback) {
118   callback.Run(false, SkBitmap());
119 }
120 
CopyFromCompositingSurfaceToVideoFrame(const gfx::Rect & src_subrect,const scoped_refptr<media::VideoFrame> & target,const base::Callback<void (bool)> & callback)121 void TestRenderWidgetHostView::CopyFromCompositingSurfaceToVideoFrame(
122     const gfx::Rect& src_subrect,
123     const scoped_refptr<media::VideoFrame>& target,
124     const base::Callback<void(bool)>& callback) {
125   callback.Run(false);
126 }
127 
CanCopyToVideoFrame() const128 bool TestRenderWidgetHostView::CanCopyToVideoFrame() const {
129   return false;
130 }
131 
OnAcceleratedCompositingStateChange()132 void TestRenderWidgetHostView::OnAcceleratedCompositingStateChange() {
133 }
134 
AcceleratedSurfaceInitialized(int host_id,int route_id)135 void TestRenderWidgetHostView::AcceleratedSurfaceInitialized(int host_id,
136                                                              int route_id) {
137 }
138 
AcceleratedSurfaceBuffersSwapped(const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params & params,int gpu_host_id)139 void TestRenderWidgetHostView::AcceleratedSurfaceBuffersSwapped(
140     const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params,
141     int gpu_host_id) {
142 }
143 
AcceleratedSurfacePostSubBuffer(const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params & params,int gpu_host_id)144 void TestRenderWidgetHostView::AcceleratedSurfacePostSubBuffer(
145     const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params& params,
146     int gpu_host_id) {
147 }
148 
AcceleratedSurfaceSuspend()149 void TestRenderWidgetHostView::AcceleratedSurfaceSuspend() {
150 }
151 
HasAcceleratedSurface(const gfx::Size & desired_size)152 bool TestRenderWidgetHostView::HasAcceleratedSurface(
153       const gfx::Size& desired_size) {
154   return false;
155 }
156 
157 #if defined(OS_MACOSX)
158 
AboutToWaitForBackingStoreMsg()159 void TestRenderWidgetHostView::AboutToWaitForBackingStoreMsg() {
160 }
161 
SetActive(bool active)162 void TestRenderWidgetHostView::SetActive(bool active) {
163   // <viettrungluu@gmail.com>: Do I need to do anything here?
164 }
165 
SupportsSpeech() const166 bool TestRenderWidgetHostView::SupportsSpeech() const {
167   return false;
168 }
169 
SpeakSelection()170 void TestRenderWidgetHostView::SpeakSelection() {
171 }
172 
IsSpeaking() const173 bool TestRenderWidgetHostView::IsSpeaking() const {
174   return false;
175 }
176 
StopSpeaking()177 void TestRenderWidgetHostView::StopSpeaking() {
178 }
179 
PostProcessEventForPluginIme(const NativeWebKeyboardEvent & event)180 bool TestRenderWidgetHostView::PostProcessEventForPluginIme(
181     const NativeWebKeyboardEvent& event) {
182   return false;
183 }
184 
185 #elif defined(OS_WIN) && !defined(USE_AURA)
WillWmDestroy()186 void TestRenderWidgetHostView::WillWmDestroy() {
187 }
188 #endif
189 
GetBoundsInRootWindow()190 gfx::Rect TestRenderWidgetHostView::GetBoundsInRootWindow() {
191   return gfx::Rect();
192 }
193 
194 #if defined(TOOLKIT_GTK)
GetLastMouseDown()195 GdkEventButton* TestRenderWidgetHostView::GetLastMouseDown() {
196   return NULL;
197 }
198 
BuildInputMethodsGtkMenu()199 gfx::NativeView TestRenderWidgetHostView::BuildInputMethodsGtkMenu() {
200   return NULL;
201 }
202 #endif  // defined(TOOLKIT_GTK)
203 
OnSwapCompositorFrame(uint32 output_surface_id,scoped_ptr<cc::CompositorFrame> frame)204 void TestRenderWidgetHostView::OnSwapCompositorFrame(
205     uint32 output_surface_id,
206     scoped_ptr<cc::CompositorFrame> frame) {
207   did_swap_compositor_frame_ = true;
208 }
209 
210 
GetCompositingSurface()211 gfx::GLSurfaceHandle TestRenderWidgetHostView::GetCompositingSurface() {
212   return gfx::GLSurfaceHandle();
213 }
214 
215 #if defined(OS_WIN) && !defined(USE_AURA)
SetClickthroughRegion(SkRegion * region)216 void TestRenderWidgetHostView::SetClickthroughRegion(SkRegion* region) {
217 }
218 #endif
219 
LockMouse()220 bool TestRenderWidgetHostView::LockMouse() {
221   return false;
222 }
223 
UnlockMouse()224 void TestRenderWidgetHostView::UnlockMouse() {
225 }
226 
227 #if defined(OS_WIN) && defined(USE_AURA)
SetParentNativeViewAccessible(gfx::NativeViewAccessible accessible_parent)228 void TestRenderWidgetHostView::SetParentNativeViewAccessible(
229     gfx::NativeViewAccessible accessible_parent) {
230 }
231 
GetParentForWindowlessPlugin() const232 gfx::NativeViewId TestRenderWidgetHostView::GetParentForWindowlessPlugin()
233     const {
234   return 0;
235 }
236 #endif
237 
TestRenderViewHost(SiteInstance * instance,RenderViewHostDelegate * delegate,RenderFrameHostDelegate * frame_delegate,RenderWidgetHostDelegate * widget_delegate,int routing_id,int main_frame_routing_id,bool swapped_out)238 TestRenderViewHost::TestRenderViewHost(
239     SiteInstance* instance,
240     RenderViewHostDelegate* delegate,
241     RenderFrameHostDelegate* frame_delegate,
242     RenderWidgetHostDelegate* widget_delegate,
243     int routing_id,
244     int main_frame_routing_id,
245     bool swapped_out)
246     : RenderViewHostImpl(instance,
247                          delegate,
248                          frame_delegate,
249                          widget_delegate,
250                          routing_id,
251                          main_frame_routing_id,
252                          swapped_out,
253                          false /* hidden */),
254       render_view_created_(false),
255       delete_counter_(NULL),
256       simulate_fetch_via_proxy_(false),
257       simulate_history_list_was_cleared_(false),
258       contents_mime_type_("text/html"),
259       opener_route_id_(MSG_ROUTING_NONE) {
260   // TestRenderWidgetHostView installs itself into this->view_ in its
261   // constructor, and deletes itself when TestRenderWidgetHostView::Destroy() is
262   // called.
263   new TestRenderWidgetHostView(this);
264 
265   main_frame_id_ = kFrameId;
266 }
267 
~TestRenderViewHost()268 TestRenderViewHost::~TestRenderViewHost() {
269   if (delete_counter_)
270     ++*delete_counter_;
271 }
272 
CreateRenderView(const base::string16 & frame_name,int opener_route_id,int32 max_page_id)273 bool TestRenderViewHost::CreateRenderView(
274     const base::string16& frame_name,
275     int opener_route_id,
276     int32 max_page_id) {
277   DCHECK(!render_view_created_);
278   render_view_created_ = true;
279   opener_route_id_ = opener_route_id;
280   return true;
281 }
282 
IsRenderViewLive() const283 bool TestRenderViewHost::IsRenderViewLive() const {
284   return render_view_created_;
285 }
286 
SendNavigate(int page_id,const GURL & url)287 void TestRenderViewHost::SendNavigate(int page_id, const GURL& url) {
288   SendNavigateWithTransition(page_id, url, PAGE_TRANSITION_LINK);
289 }
290 
SendFailedNavigate(int page_id,const GURL & url)291 void TestRenderViewHost::SendFailedNavigate(int page_id, const GURL& url) {
292   SendNavigateWithTransitionAndResponseCode(
293       page_id, url, PAGE_TRANSITION_LINK, 500);
294 }
295 
SendNavigateWithTransition(int page_id,const GURL & url,PageTransition transition)296 void TestRenderViewHost::SendNavigateWithTransition(
297     int page_id, const GURL& url, PageTransition transition) {
298   SendNavigateWithTransitionAndResponseCode(page_id, url, transition, 200);
299 }
300 
SendNavigateWithOriginalRequestURL(int page_id,const GURL & url,const GURL & original_request_url)301 void TestRenderViewHost::SendNavigateWithOriginalRequestURL(
302     int page_id, const GURL& url, const GURL& original_request_url) {
303   main_render_frame_host()->OnDidStartProvisionalLoadForFrame(
304       kFrameId, -1, true, url);
305   SendNavigateWithParameters(page_id, url, PAGE_TRANSITION_LINK,
306                              original_request_url, 200, 0);
307 }
308 
SendNavigateWithFile(int page_id,const GURL & url,const base::FilePath & file_path)309 void TestRenderViewHost::SendNavigateWithFile(
310     int page_id, const GURL& url, const base::FilePath& file_path) {
311   SendNavigateWithParameters(page_id, url, PAGE_TRANSITION_LINK,
312                              url, 200, &file_path);
313 }
314 
SendNavigateWithParams(ViewHostMsg_FrameNavigate_Params * params)315 void TestRenderViewHost::SendNavigateWithParams(
316     ViewHostMsg_FrameNavigate_Params* params) {
317   params->frame_id = kFrameId;
318   ViewHostMsg_FrameNavigate msg(1, *params);
319   OnNavigate(msg);
320 }
321 
SendNavigateWithTransitionAndResponseCode(int page_id,const GURL & url,PageTransition transition,int response_code)322 void TestRenderViewHost::SendNavigateWithTransitionAndResponseCode(
323     int page_id, const GURL& url, PageTransition transition,
324     int response_code) {
325   // DidStartProvisionalLoad may delete the pending entry that holds |url|,
326   // so we keep a copy of it to use in SendNavigateWithParameters.
327   GURL url_copy(url);
328   main_render_frame_host()->OnDidStartProvisionalLoadForFrame(
329       kFrameId, -1, true, url_copy);
330   SendNavigateWithParameters(page_id, url_copy, transition, url_copy,
331                              response_code, 0);
332 }
333 
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)334 void TestRenderViewHost::SendNavigateWithParameters(
335     int page_id, const GURL& url, PageTransition transition,
336     const GURL& original_request_url, int response_code,
337     const base::FilePath* file_path_for_history_item) {
338   ViewHostMsg_FrameNavigate_Params params;
339   params.page_id = page_id;
340   params.frame_id = kFrameId;
341   params.url = url;
342   params.referrer = Referrer();
343   params.transition = transition;
344   params.redirects = std::vector<GURL>();
345   params.should_update_history = true;
346   params.searchable_form_url = GURL();
347   params.searchable_form_encoding = std::string();
348   params.security_info = std::string();
349   params.gesture = NavigationGestureUser;
350   params.contents_mime_type = contents_mime_type_;
351   params.is_post = false;
352   params.was_within_same_page = false;
353   params.http_status_code = response_code;
354   params.socket_address.set_host("2001:db8::1");
355   params.socket_address.set_port(80);
356   params.was_fetched_via_proxy = simulate_fetch_via_proxy_;
357   params.history_list_was_cleared = simulate_history_list_was_cleared_;
358   params.original_request_url = original_request_url;
359 
360   params.page_state = PageState::CreateForTesting(
361       url,
362       false,
363       file_path_for_history_item ? "data" : NULL,
364       file_path_for_history_item);
365 
366   ViewHostMsg_FrameNavigate msg(1, params);
367   OnNavigate(msg);
368 }
369 
SendShouldCloseACK(bool proceed)370 void TestRenderViewHost::SendShouldCloseACK(bool proceed) {
371   base::TimeTicks now = base::TimeTicks::Now();
372   OnShouldCloseACK(proceed, now, now);
373 }
374 
SetContentsMimeType(const std::string & mime_type)375 void TestRenderViewHost::SetContentsMimeType(const std::string& mime_type) {
376   contents_mime_type_ = mime_type;
377 }
378 
SimulateSwapOutACK()379 void TestRenderViewHost::SimulateSwapOutACK() {
380   OnSwappedOut(false);
381 }
382 
SimulateWasHidden()383 void TestRenderViewHost::SimulateWasHidden() {
384   WasHidden();
385 }
386 
SimulateWasShown()387 void TestRenderViewHost::SimulateWasShown() {
388   WasShown();
389 }
390 
TestOnStartDragging(const DropData & drop_data)391 void TestRenderViewHost::TestOnStartDragging(
392     const DropData& drop_data) {
393   blink::WebDragOperationsMask drag_operation = blink::WebDragOperationEvery;
394   DragEventSourceInfo event_info;
395   OnStartDragging(drop_data, drag_operation, SkBitmap(), gfx::Vector2d(),
396                   event_info);
397 }
398 
TestOnUpdateStateWithFile(int process_id,const base::FilePath & file_path)399 void TestRenderViewHost::TestOnUpdateStateWithFile(
400     int process_id,
401     const base::FilePath& file_path) {
402   OnUpdateState(process_id,
403                 PageState::CreateForTesting(GURL("http://www.google.com"),
404                                             false,
405                                             "data",
406                                             &file_path));
407 }
408 
set_simulate_fetch_via_proxy(bool proxy)409 void TestRenderViewHost::set_simulate_fetch_via_proxy(bool proxy) {
410   simulate_fetch_via_proxy_ = proxy;
411 }
412 
set_simulate_history_list_was_cleared(bool cleared)413 void TestRenderViewHost::set_simulate_history_list_was_cleared(bool cleared) {
414   simulate_history_list_was_cleared_ = cleared;
415 }
416 
RenderViewHostImplTestHarness()417 RenderViewHostImplTestHarness::RenderViewHostImplTestHarness() {
418   std::vector<ui::ScaleFactor> scale_factors;
419   scale_factors.push_back(ui::SCALE_FACTOR_100P);
420   scoped_set_supported_scale_factors_.reset(
421       new ui::test::ScopedSetSupportedScaleFactors(scale_factors));
422 }
423 
~RenderViewHostImplTestHarness()424 RenderViewHostImplTestHarness::~RenderViewHostImplTestHarness() {
425 }
426 
test_rvh()427 TestRenderViewHost* RenderViewHostImplTestHarness::test_rvh() {
428   return static_cast<TestRenderViewHost*>(rvh());
429 }
430 
pending_test_rvh()431 TestRenderViewHost* RenderViewHostImplTestHarness::pending_test_rvh() {
432   return static_cast<TestRenderViewHost*>(pending_rvh());
433 }
434 
active_test_rvh()435 TestRenderViewHost* RenderViewHostImplTestHarness::active_test_rvh() {
436   return static_cast<TestRenderViewHost*>(active_rvh());
437 }
438 
main_test_rfh()439 TestRenderFrameHost* RenderViewHostImplTestHarness::main_test_rfh() {
440   return static_cast<TestRenderFrameHost*>(main_rfh());
441 }
442 
contents()443 TestWebContents* RenderViewHostImplTestHarness::contents() {
444   return static_cast<TestWebContents*>(web_contents());
445 }
446 
447 }  // namespace content
448