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