Home | History | Annotate | Download | only in test
      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 
     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 
     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 
     59 TestRenderWidgetHostView::~TestRenderWidgetHostView() {
     60 }
     61 
     62 RenderWidgetHost* TestRenderWidgetHostView::GetRenderWidgetHost() const {
     63   return NULL;
     64 }
     65 
     66 gfx::NativeView TestRenderWidgetHostView::GetNativeView() const {
     67   return NULL;
     68 }
     69 
     70 gfx::NativeViewId TestRenderWidgetHostView::GetNativeViewId() const {
     71   return 0;
     72 }
     73 
     74 gfx::NativeViewAccessible TestRenderWidgetHostView::GetNativeViewAccessible() {
     75   return NULL;
     76 }
     77 
     78 bool TestRenderWidgetHostView::HasFocus() const {
     79   return true;
     80 }
     81 
     82 bool TestRenderWidgetHostView::IsSurfaceAvailableForCopy() const {
     83   return true;
     84 }
     85 
     86 void TestRenderWidgetHostView::Show() {
     87   is_showing_ = true;
     88 }
     89 
     90 void TestRenderWidgetHostView::Hide() {
     91   is_showing_ = false;
     92 }
     93 
     94 bool TestRenderWidgetHostView::IsShowing() {
     95   return is_showing_;
     96 }
     97 
     98 void TestRenderWidgetHostView::RenderProcessGone(base::TerminationStatus status,
     99                                                  int error_code) {
    100   delete this;
    101 }
    102 
    103 void TestRenderWidgetHostView::Destroy() { delete this; }
    104 
    105 gfx::Rect TestRenderWidgetHostView::GetViewBounds() const {
    106   return gfx::Rect();
    107 }
    108 
    109 BackingStore* TestRenderWidgetHostView::AllocBackingStore(
    110     const gfx::Size& size) {
    111   return new TestBackingStore(rwh_, size);
    112 }
    113 
    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 
    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 
    128 bool TestRenderWidgetHostView::CanCopyToVideoFrame() const {
    129   return false;
    130 }
    131 
    132 void TestRenderWidgetHostView::OnAcceleratedCompositingStateChange() {
    133 }
    134 
    135 void TestRenderWidgetHostView::AcceleratedSurfaceInitialized(int host_id,
    136                                                              int route_id) {
    137 }
    138 
    139 void TestRenderWidgetHostView::AcceleratedSurfaceBuffersSwapped(
    140     const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params,
    141     int gpu_host_id) {
    142 }
    143 
    144 void TestRenderWidgetHostView::AcceleratedSurfacePostSubBuffer(
    145     const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params& params,
    146     int gpu_host_id) {
    147 }
    148 
    149 void TestRenderWidgetHostView::AcceleratedSurfaceSuspend() {
    150 }
    151 
    152 bool TestRenderWidgetHostView::HasAcceleratedSurface(
    153       const gfx::Size& desired_size) {
    154   return false;
    155 }
    156 
    157 #if defined(OS_MACOSX)
    158 
    159 void TestRenderWidgetHostView::AboutToWaitForBackingStoreMsg() {
    160 }
    161 
    162 void TestRenderWidgetHostView::SetActive(bool active) {
    163   // <viettrungluu (at) gmail.com>: Do I need to do anything here?
    164 }
    165 
    166 bool TestRenderWidgetHostView::SupportsSpeech() const {
    167   return false;
    168 }
    169 
    170 void TestRenderWidgetHostView::SpeakSelection() {
    171 }
    172 
    173 bool TestRenderWidgetHostView::IsSpeaking() const {
    174   return false;
    175 }
    176 
    177 void TestRenderWidgetHostView::StopSpeaking() {
    178 }
    179 
    180 bool TestRenderWidgetHostView::PostProcessEventForPluginIme(
    181     const NativeWebKeyboardEvent& event) {
    182   return false;
    183 }
    184 
    185 #elif defined(OS_WIN) && !defined(USE_AURA)
    186 void TestRenderWidgetHostView::WillWmDestroy() {
    187 }
    188 #endif
    189 
    190 gfx::Rect TestRenderWidgetHostView::GetBoundsInRootWindow() {
    191   return gfx::Rect();
    192 }
    193 
    194 #if defined(TOOLKIT_GTK)
    195 GdkEventButton* TestRenderWidgetHostView::GetLastMouseDown() {
    196   return NULL;
    197 }
    198 
    199 gfx::NativeView TestRenderWidgetHostView::BuildInputMethodsGtkMenu() {
    200   return NULL;
    201 }
    202 #endif  // defined(TOOLKIT_GTK)
    203 
    204 void TestRenderWidgetHostView::OnSwapCompositorFrame(
    205     uint32 output_surface_id,
    206     scoped_ptr<cc::CompositorFrame> frame) {
    207   did_swap_compositor_frame_ = true;
    208 }
    209 
    210 
    211 gfx::GLSurfaceHandle TestRenderWidgetHostView::GetCompositingSurface() {
    212   return gfx::GLSurfaceHandle();
    213 }
    214 
    215 #if defined(OS_WIN) && !defined(USE_AURA)
    216 void TestRenderWidgetHostView::SetClickthroughRegion(SkRegion* region) {
    217 }
    218 #endif
    219 
    220 bool TestRenderWidgetHostView::LockMouse() {
    221   return false;
    222 }
    223 
    224 void TestRenderWidgetHostView::UnlockMouse() {
    225 }
    226 
    227 #if defined(OS_WIN) && defined(USE_AURA)
    228 void TestRenderWidgetHostView::SetParentNativeViewAccessible(
    229     gfx::NativeViewAccessible accessible_parent) {
    230 }
    231 
    232 gfx::NativeViewId TestRenderWidgetHostView::GetParentForWindowlessPlugin()
    233     const {
    234   return 0;
    235 }
    236 #endif
    237 
    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 
    268 TestRenderViewHost::~TestRenderViewHost() {
    269   if (delete_counter_)
    270     ++*delete_counter_;
    271 }
    272 
    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 
    283 bool TestRenderViewHost::IsRenderViewLive() const {
    284   return render_view_created_;
    285 }
    286 
    287 void TestRenderViewHost::SendNavigate(int page_id, const GURL& url) {
    288   SendNavigateWithTransition(page_id, url, PAGE_TRANSITION_LINK);
    289 }
    290 
    291 void TestRenderViewHost::SendFailedNavigate(int page_id, const GURL& url) {
    292   SendNavigateWithTransitionAndResponseCode(
    293       page_id, url, PAGE_TRANSITION_LINK, 500);
    294 }
    295 
    296 void TestRenderViewHost::SendNavigateWithTransition(
    297     int page_id, const GURL& url, PageTransition transition) {
    298   SendNavigateWithTransitionAndResponseCode(page_id, url, transition, 200);
    299 }
    300 
    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 
    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 
    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 
    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 
    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 
    370 void TestRenderViewHost::SendShouldCloseACK(bool proceed) {
    371   base::TimeTicks now = base::TimeTicks::Now();
    372   OnShouldCloseACK(proceed, now, now);
    373 }
    374 
    375 void TestRenderViewHost::SetContentsMimeType(const std::string& mime_type) {
    376   contents_mime_type_ = mime_type;
    377 }
    378 
    379 void TestRenderViewHost::SimulateSwapOutACK() {
    380   OnSwappedOut(false);
    381 }
    382 
    383 void TestRenderViewHost::SimulateWasHidden() {
    384   WasHidden();
    385 }
    386 
    387 void TestRenderViewHost::SimulateWasShown() {
    388   WasShown();
    389 }
    390 
    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 
    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 
    409 void TestRenderViewHost::set_simulate_fetch_via_proxy(bool proxy) {
    410   simulate_fetch_via_proxy_ = proxy;
    411 }
    412 
    413 void TestRenderViewHost::set_simulate_history_list_was_cleared(bool cleared) {
    414   simulate_history_list_was_cleared_ = cleared;
    415 }
    416 
    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 
    424 RenderViewHostImplTestHarness::~RenderViewHostImplTestHarness() {
    425 }
    426 
    427 TestRenderViewHost* RenderViewHostImplTestHarness::test_rvh() {
    428   return static_cast<TestRenderViewHost*>(rvh());
    429 }
    430 
    431 TestRenderViewHost* RenderViewHostImplTestHarness::pending_test_rvh() {
    432   return static_cast<TestRenderViewHost*>(pending_rvh());
    433 }
    434 
    435 TestRenderViewHost* RenderViewHostImplTestHarness::active_test_rvh() {
    436   return static_cast<TestRenderViewHost*>(active_rvh());
    437 }
    438 
    439 TestRenderFrameHost* RenderViewHostImplTestHarness::main_test_rfh() {
    440   return static_cast<TestRenderFrameHost*>(main_rfh());
    441 }
    442 
    443 TestWebContents* RenderViewHostImplTestHarness::contents() {
    444   return static_cast<TestWebContents*>(web_contents());
    445 }
    446 
    447 }  // namespace content
    448