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.
5 #include "content/browser/renderer_host/test_render_view_host.h"
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/renderer_host/test_backing_store.h"
11 #include "content/browser/site_instance_impl.h"
12 #include "content/common/dom_storage/dom_storage_types.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"
28 const int64 kFrameId = 13UL;
33 void InitNavigateParams(ViewHostMsg_FrameNavigate_Params* params,
36 PageTransition transition) {
37 params->page_id = page_id;
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);
52 TestRenderWidgetHostView::TestRenderWidgetHostView(RenderWidgetHost* rwh)
53 : rwh_(RenderWidgetHostImpl::From(rwh)),
55 did_swap_compositor_frame_(false) {
59 TestRenderWidgetHostView::~TestRenderWidgetHostView() {
62 RenderWidgetHost* TestRenderWidgetHostView::GetRenderWidgetHost() const {
66 gfx::NativeView TestRenderWidgetHostView::GetNativeView() const {
70 gfx::NativeViewId TestRenderWidgetHostView::GetNativeViewId() const {
74 gfx::NativeViewAccessible TestRenderWidgetHostView::GetNativeViewAccessible() {
78 bool TestRenderWidgetHostView::HasFocus() const {
82 bool TestRenderWidgetHostView::IsSurfaceAvailableForCopy() const {
86 void TestRenderWidgetHostView::Show() {
90 void TestRenderWidgetHostView::Hide() {
94 bool TestRenderWidgetHostView::IsShowing() {
98 void TestRenderWidgetHostView::RenderProcessGone(base::TerminationStatus status,
103 void TestRenderWidgetHostView::Destroy() { delete this; }
105 gfx::Rect TestRenderWidgetHostView::GetViewBounds() const {
109 BackingStore* TestRenderWidgetHostView::AllocBackingStore(
110 const gfx::Size& size) {
111 return new TestBackingStore(rwh_, size);
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());
121 void TestRenderWidgetHostView::CopyFromCompositingSurfaceToVideoFrame(
122 const gfx::Rect& src_subrect,
123 const scoped_refptr<media::VideoFrame>& target,
124 const base::Callback<void(bool)>& callback) {
128 bool TestRenderWidgetHostView::CanCopyToVideoFrame() const {
132 void TestRenderWidgetHostView::OnAcceleratedCompositingStateChange() {
135 void TestRenderWidgetHostView::AcceleratedSurfaceBuffersSwapped(
136 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params,
140 void TestRenderWidgetHostView::AcceleratedSurfacePostSubBuffer(
141 const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params& params,
145 void TestRenderWidgetHostView::AcceleratedSurfaceSuspend() {
148 bool TestRenderWidgetHostView::HasAcceleratedSurface(
149 const gfx::Size& desired_size) {
153 #if defined(OS_MACOSX)
155 void TestRenderWidgetHostView::AboutToWaitForBackingStoreMsg() {
158 void TestRenderWidgetHostView::SetActive(bool active) {
159 // <viettrungluu@gmail.com>: Do I need to do anything here?
162 bool TestRenderWidgetHostView::SupportsSpeech() const {
166 void TestRenderWidgetHostView::SpeakSelection() {
169 bool TestRenderWidgetHostView::IsSpeaking() const {
173 void TestRenderWidgetHostView::StopSpeaking() {
176 bool TestRenderWidgetHostView::PostProcessEventForPluginIme(
177 const NativeWebKeyboardEvent& event) {
181 #elif defined(OS_WIN) && !defined(USE_AURA)
182 void TestRenderWidgetHostView::WillWmDestroy() {
186 gfx::Rect TestRenderWidgetHostView::GetBoundsInRootWindow() {
190 #if defined(TOOLKIT_GTK)
191 GdkEventButton* TestRenderWidgetHostView::GetLastMouseDown() {
195 gfx::NativeView TestRenderWidgetHostView::BuildInputMethodsGtkMenu() {
198 #endif // defined(TOOLKIT_GTK)
200 void TestRenderWidgetHostView::OnSwapCompositorFrame(
201 uint32 output_surface_id,
202 scoped_ptr<cc::CompositorFrame> frame) {
203 did_swap_compositor_frame_ = true;
207 gfx::GLSurfaceHandle TestRenderWidgetHostView::GetCompositingSurface() {
208 return gfx::GLSurfaceHandle();
211 #if defined(OS_WIN) && !defined(USE_AURA)
212 void TestRenderWidgetHostView::SetClickthroughRegion(SkRegion* region) {
216 bool TestRenderWidgetHostView::LockMouse() {
220 void TestRenderWidgetHostView::UnlockMouse() {
223 #if defined(OS_WIN) && defined(USE_AURA)
224 void TestRenderWidgetHostView::SetParentNativeViewAccessible(
225 gfx::NativeViewAccessible accessible_parent) {
229 TestRenderViewHost::TestRenderViewHost(
230 SiteInstance* instance,
231 RenderViewHostDelegate* delegate,
232 RenderWidgetHostDelegate* widget_delegate,
234 int main_frame_routing_id,
236 : RenderViewHostImpl(instance,
240 main_frame_routing_id,
243 render_view_created_(false),
244 delete_counter_(NULL),
245 simulate_fetch_via_proxy_(false),
246 simulate_history_list_was_cleared_(false),
247 contents_mime_type_("text/html"),
248 opener_route_id_(MSG_ROUTING_NONE) {
249 // TestRenderWidgetHostView installs itself into this->view_ in its
250 // constructor, and deletes itself when TestRenderWidgetHostView::Destroy() is
252 new TestRenderWidgetHostView(this);
254 main_frame_id_ = kFrameId;
257 TestRenderViewHost::~TestRenderViewHost() {
262 bool TestRenderViewHost::CreateRenderView(
263 const string16& frame_name,
266 DCHECK(!render_view_created_);
267 render_view_created_ = true;
268 opener_route_id_ = opener_route_id;
272 bool TestRenderViewHost::IsRenderViewLive() const {
273 return render_view_created_;
276 void TestRenderViewHost::SendNavigate(int page_id, const GURL& url) {
277 SendNavigateWithTransition(page_id, url, PAGE_TRANSITION_LINK);
280 void TestRenderViewHost::SendFailedNavigate(int page_id, const GURL& url) {
281 SendNavigateWithTransitionAndResponseCode(
282 page_id, url, PAGE_TRANSITION_LINK, 500);
285 void TestRenderViewHost::SendNavigateWithTransition(
286 int page_id, const GURL& url, PageTransition transition) {
287 SendNavigateWithTransitionAndResponseCode(page_id, url, transition, 200);
290 void TestRenderViewHost::SendNavigateWithOriginalRequestURL(
291 int page_id, const GURL& url, const GURL& original_request_url) {
292 OnDidStartProvisionalLoadForFrame(kFrameId, -1, true, url);
293 SendNavigateWithParameters(page_id, url, PAGE_TRANSITION_LINK,
294 original_request_url, 200, 0);
297 void TestRenderViewHost::SendNavigateWithFile(
298 int page_id, const GURL& url, const base::FilePath& file_path) {
299 SendNavigateWithParameters(page_id, url, PAGE_TRANSITION_LINK,
300 url, 200, &file_path);
303 void TestRenderViewHost::SendNavigateWithTransitionAndResponseCode(
304 int page_id, const GURL& url, PageTransition transition,
306 // DidStartProvisionalLoad may delete the pending entry that holds |url|,
307 // so we keep a copy of it to use in SendNavigateWithParameters.
309 OnDidStartProvisionalLoadForFrame(kFrameId, -1, true, url_copy);
310 SendNavigateWithParameters(page_id, url_copy, transition, url_copy,
314 void TestRenderViewHost::SendNavigateWithParameters(
315 int page_id, const GURL& url, PageTransition transition,
316 const GURL& original_request_url, int response_code,
317 const base::FilePath* file_path_for_history_item) {
318 ViewHostMsg_FrameNavigate_Params params;
319 params.page_id = page_id;
320 params.frame_id = kFrameId;
322 params.referrer = Referrer();
323 params.transition = transition;
324 params.redirects = std::vector<GURL>();
325 params.should_update_history = true;
326 params.searchable_form_url = GURL();
327 params.searchable_form_encoding = std::string();
328 params.security_info = std::string();
329 params.gesture = NavigationGestureUser;
330 params.contents_mime_type = contents_mime_type_;
331 params.is_post = false;
332 params.was_within_same_page = false;
333 params.http_status_code = response_code;
334 params.socket_address.set_host("2001:db8::1");
335 params.socket_address.set_port(80);
336 params.was_fetched_via_proxy = simulate_fetch_via_proxy_;
337 params.history_list_was_cleared = simulate_history_list_was_cleared_;
338 params.original_request_url = original_request_url;
340 params.page_state = PageState::CreateForTesting(
343 file_path_for_history_item ? "data" : NULL,
344 file_path_for_history_item);
346 ViewHostMsg_FrameNavigate msg(1, params);
350 void TestRenderViewHost::SendShouldCloseACK(bool proceed) {
351 base::TimeTicks now = base::TimeTicks::Now();
352 OnShouldCloseACK(proceed, now, now);
355 void TestRenderViewHost::SetContentsMimeType(const std::string& mime_type) {
356 contents_mime_type_ = mime_type;
359 void TestRenderViewHost::SimulateSwapOutACK() {
363 void TestRenderViewHost::SimulateWasHidden() {
367 void TestRenderViewHost::SimulateWasShown() {
371 void TestRenderViewHost::TestOnStartDragging(
372 const DropData& drop_data) {
373 WebKit::WebDragOperationsMask drag_operation = WebKit::WebDragOperationEvery;
374 DragEventSourceInfo event_info;
375 OnStartDragging(drop_data, drag_operation, SkBitmap(), gfx::Vector2d(),
379 void TestRenderViewHost::TestOnUpdateStateWithFile(
381 const base::FilePath& file_path) {
382 OnUpdateState(process_id,
383 PageState::CreateForTesting(GURL("http://www.google.com"),
389 void TestRenderViewHost::set_simulate_fetch_via_proxy(bool proxy) {
390 simulate_fetch_via_proxy_ = proxy;
393 void TestRenderViewHost::set_simulate_history_list_was_cleared(bool cleared) {
394 simulate_history_list_was_cleared_ = cleared;
397 RenderViewHostImplTestHarness::RenderViewHostImplTestHarness() {
398 std::vector<ui::ScaleFactor> scale_factors;
399 scale_factors.push_back(ui::SCALE_FACTOR_100P);
400 scoped_set_supported_scale_factors_.reset(
401 new ui::test::ScopedSetSupportedScaleFactors(scale_factors));
404 RenderViewHostImplTestHarness::~RenderViewHostImplTestHarness() {
407 TestRenderViewHost* RenderViewHostImplTestHarness::test_rvh() {
408 return static_cast<TestRenderViewHost*>(rvh());
411 TestRenderViewHost* RenderViewHostImplTestHarness::pending_test_rvh() {
412 return static_cast<TestRenderViewHost*>(pending_rvh());
415 TestRenderViewHost* RenderViewHostImplTestHarness::active_test_rvh() {
416 return static_cast<TestRenderViewHost*>(active_rvh());
419 TestWebContents* RenderViewHostImplTestHarness::contents() {
420 return static_cast<TestWebContents*>(web_contents());
423 } // namespace content