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