Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / renderer_host / test_render_view_host.cc
blob9f12960d73d042073eba5253be07725679932714
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 "content/browser/dom_storage/dom_storage_context_impl.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/view_messages.h"
12 #include "content/public/browser/browser_context.h"
13 #include "content/public/browser/navigation_controller.h"
14 #include "content/public/browser/storage_partition.h"
15 #include "content/public/common/content_client.h"
16 #include "content/public/common/page_state.h"
17 #include "content/public/common/password_form.h"
18 #include "content/test/test_web_contents.h"
19 #include "media/base/video_frame.h"
20 #include "ui/gfx/rect.h"
21 #include "webkit/common/dom_storage/dom_storage_types.h"
22 #include "webkit/common/webpreferences.h"
24 namespace content {
26 namespace {
27 // Normally this is done by the NavigationController, but we'll fake it out
28 // here for testing.
29 SessionStorageNamespaceImpl* CreateSessionStorageNamespace(
30 SiteInstance* instance) {
31 RenderProcessHost* process_host = instance->GetProcess();
32 DOMStorageContext* dom_storage_context =
33 BrowserContext::GetStoragePartition(process_host->GetBrowserContext(),
34 instance)->GetDOMStorageContext();
35 return new SessionStorageNamespaceImpl(
36 static_cast<DOMStorageContextImpl*>(dom_storage_context));
39 const int64 kFrameId = 13UL;
41 } // namespace
44 void InitNavigateParams(ViewHostMsg_FrameNavigate_Params* params,
45 int page_id,
46 const GURL& url,
47 PageTransition transition) {
48 params->page_id = page_id;
49 params->url = url;
50 params->referrer = Referrer();
51 params->transition = transition;
52 params->redirects = std::vector<GURL>();
53 params->should_update_history = false;
54 params->searchable_form_url = GURL();
55 params->searchable_form_encoding = std::string();
56 params->password_form = PasswordForm();
57 params->security_info = std::string();
58 params->gesture = NavigationGestureUser;
59 params->was_within_same_page = false;
60 params->is_post = false;
61 params->page_state = PageState::CreateFromURL(url);
64 TestRenderWidgetHostView::TestRenderWidgetHostView(RenderWidgetHost* rwh)
65 : rwh_(RenderWidgetHostImpl::From(rwh)),
66 is_showing_(false),
67 did_swap_compositor_frame_(false) {
68 rwh_->SetView(this);
71 TestRenderWidgetHostView::~TestRenderWidgetHostView() {
74 RenderWidgetHost* TestRenderWidgetHostView::GetRenderWidgetHost() const {
75 return NULL;
78 gfx::NativeView TestRenderWidgetHostView::GetNativeView() const {
79 return NULL;
82 gfx::NativeViewId TestRenderWidgetHostView::GetNativeViewId() const {
83 return 0;
86 gfx::NativeViewAccessible TestRenderWidgetHostView::GetNativeViewAccessible() {
87 return NULL;
90 bool TestRenderWidgetHostView::HasFocus() const {
91 return true;
94 bool TestRenderWidgetHostView::IsSurfaceAvailableForCopy() const {
95 return true;
98 void TestRenderWidgetHostView::Show() {
99 is_showing_ = true;
102 void TestRenderWidgetHostView::Hide() {
103 is_showing_ = false;
106 bool TestRenderWidgetHostView::IsShowing() {
107 return is_showing_;
110 void TestRenderWidgetHostView::RenderViewGone(base::TerminationStatus status,
111 int error_code) {
112 delete this;
115 void TestRenderWidgetHostView::Destroy() { delete this; }
117 gfx::Rect TestRenderWidgetHostView::GetViewBounds() const {
118 return gfx::Rect();
121 BackingStore* TestRenderWidgetHostView::AllocBackingStore(
122 const gfx::Size& size) {
123 return new TestBackingStore(rwh_, size);
126 void TestRenderWidgetHostView::CopyFromCompositingSurface(
127 const gfx::Rect& src_subrect,
128 const gfx::Size& dst_size,
129 const base::Callback<void(bool, const SkBitmap&)>& callback) {
130 callback.Run(false, SkBitmap());
133 void TestRenderWidgetHostView::CopyFromCompositingSurfaceToVideoFrame(
134 const gfx::Rect& src_subrect,
135 const scoped_refptr<media::VideoFrame>& target,
136 const base::Callback<void(bool)>& callback) {
137 callback.Run(false);
140 bool TestRenderWidgetHostView::CanCopyToVideoFrame() const {
141 return false;
144 void TestRenderWidgetHostView::OnAcceleratedCompositingStateChange() {
147 void TestRenderWidgetHostView::AcceleratedSurfaceBuffersSwapped(
148 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params,
149 int gpu_host_id) {
152 void TestRenderWidgetHostView::AcceleratedSurfacePostSubBuffer(
153 const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params& params,
154 int gpu_host_id) {
157 void TestRenderWidgetHostView::AcceleratedSurfaceSuspend() {
160 bool TestRenderWidgetHostView::HasAcceleratedSurface(
161 const gfx::Size& desired_size) {
162 return false;
165 #if defined(OS_MACOSX)
167 void TestRenderWidgetHostView::AboutToWaitForBackingStoreMsg() {
170 void TestRenderWidgetHostView::SetActive(bool active) {
171 // <viettrungluu@gmail.com>: Do I need to do anything here?
174 bool TestRenderWidgetHostView::SupportsSpeech() const {
175 return false;
178 void TestRenderWidgetHostView::SpeakSelection() {
181 bool TestRenderWidgetHostView::IsSpeaking() const {
182 return false;
185 void TestRenderWidgetHostView::StopSpeaking() {
188 bool TestRenderWidgetHostView::PostProcessEventForPluginIme(
189 const NativeWebKeyboardEvent& event) {
190 return false;
193 #elif defined(OS_WIN) && !defined(USE_AURA)
194 void TestRenderWidgetHostView::WillWmDestroy() {
196 #endif
198 gfx::Rect TestRenderWidgetHostView::GetBoundsInRootWindow() {
199 return gfx::Rect();
202 #if defined(TOOLKIT_GTK)
203 GdkEventButton* TestRenderWidgetHostView::GetLastMouseDown() {
204 return NULL;
207 gfx::NativeView TestRenderWidgetHostView::BuildInputMethodsGtkMenu() {
208 return NULL;
210 #endif // defined(TOOLKIT_GTK)
212 void TestRenderWidgetHostView::OnSwapCompositorFrame(
213 scoped_ptr<cc::CompositorFrame> frame) {
214 did_swap_compositor_frame_ = true;
218 gfx::GLSurfaceHandle TestRenderWidgetHostView::GetCompositingSurface() {
219 return gfx::GLSurfaceHandle();
222 #if defined(OS_WIN) && !defined(USE_AURA)
223 void TestRenderWidgetHostView::SetClickthroughRegion(SkRegion* region) {
225 #endif
227 #if defined(OS_WIN) && defined(USE_AURA)
228 gfx::NativeViewAccessible
229 TestRenderWidgetHostView::AccessibleObjectFromChildId(long child_id) {
230 NOTIMPLEMENTED();
231 return NULL;
233 #endif
235 bool TestRenderWidgetHostView::LockMouse() {
236 return false;
239 void TestRenderWidgetHostView::UnlockMouse() {
242 #if defined(OS_WIN) && defined(USE_AURA)
243 void TestRenderWidgetHostView::SetParentNativeViewAccessible(
244 gfx::NativeViewAccessible accessible_parent) {
246 #endif
248 TestRenderViewHost::TestRenderViewHost(
249 SiteInstance* instance,
250 RenderViewHostDelegate* delegate,
251 RenderWidgetHostDelegate* widget_delegate,
252 int routing_id,
253 int main_frame_routing_id,
254 bool swapped_out)
255 : RenderViewHostImpl(instance,
256 delegate,
257 widget_delegate,
258 routing_id,
259 main_frame_routing_id,
260 swapped_out,
261 CreateSessionStorageNamespace(instance)),
262 render_view_created_(false),
263 delete_counter_(NULL),
264 simulate_fetch_via_proxy_(false),
265 simulate_history_list_was_cleared_(false),
266 contents_mime_type_("text/html") {
267 // TestRenderWidgetHostView installs itself into this->view_ in its
268 // constructor, and deletes itself when TestRenderWidgetHostView::Destroy() is
269 // called.
270 new TestRenderWidgetHostView(this);
272 main_frame_id_ = kFrameId;
275 TestRenderViewHost::~TestRenderViewHost() {
276 if (delete_counter_)
277 ++*delete_counter_;
280 bool TestRenderViewHost::CreateRenderView(
281 const string16& frame_name,
282 int opener_route_id,
283 int32 max_page_id) {
284 DCHECK(!render_view_created_);
285 render_view_created_ = true;
286 return true;
289 bool TestRenderViewHost::IsRenderViewLive() const {
290 return render_view_created_;
293 void TestRenderViewHost::SendNavigate(int page_id, const GURL& url) {
294 SendNavigateWithTransition(page_id, url, PAGE_TRANSITION_LINK);
297 void TestRenderViewHost::SendFailedNavigate(int page_id, const GURL& url) {
298 SendNavigateWithTransitionAndResponseCode(
299 page_id, url, PAGE_TRANSITION_LINK, 500);
302 void TestRenderViewHost::SendNavigateWithTransition(
303 int page_id, const GURL& url, PageTransition transition) {
304 SendNavigateWithTransitionAndResponseCode(page_id, url, transition, 200);
307 void TestRenderViewHost::SendNavigateWithOriginalRequestURL(
308 int page_id, const GURL& url, const GURL& original_request_url) {
309 OnDidStartProvisionalLoadForFrame(kFrameId, -1, true, url);
310 SendNavigateWithParameters(page_id, url, PAGE_TRANSITION_LINK,
311 original_request_url, 200, 0);
314 void TestRenderViewHost::SendNavigateWithFile(
315 int page_id, const GURL& url, const base::FilePath& file_path) {
316 SendNavigateWithParameters(page_id, url, PAGE_TRANSITION_LINK,
317 url, 200, &file_path);
320 void TestRenderViewHost::SendNavigateWithTransitionAndResponseCode(
321 int page_id, const GURL& url, PageTransition transition,
322 int response_code) {
323 OnDidStartProvisionalLoadForFrame(kFrameId, -1, true, url);
324 SendNavigateWithParameters(page_id, url, transition, url, response_code, 0);
327 void TestRenderViewHost::SendNavigateWithParameters(
328 int page_id, const GURL& url, PageTransition transition,
329 const GURL& original_request_url, int response_code,
330 const base::FilePath* file_path_for_history_item) {
331 ViewHostMsg_FrameNavigate_Params params;
332 params.page_id = page_id;
333 params.frame_id = kFrameId;
334 params.url = url;
335 params.referrer = Referrer();
336 params.transition = transition;
337 params.redirects = std::vector<GURL>();
338 params.should_update_history = true;
339 params.searchable_form_url = GURL();
340 params.searchable_form_encoding = std::string();
341 params.password_form = PasswordForm();
342 params.security_info = std::string();
343 params.gesture = NavigationGestureUser;
344 params.contents_mime_type = contents_mime_type_;
345 params.is_post = false;
346 params.was_within_same_page = false;
347 params.http_status_code = response_code;
348 params.socket_address.set_host("2001:db8::1");
349 params.socket_address.set_port(80);
350 params.was_fetched_via_proxy = simulate_fetch_via_proxy_;
351 params.history_list_was_cleared = simulate_history_list_was_cleared_;
352 params.original_request_url = original_request_url;
354 params.page_state = PageState::CreateForTesting(
355 url,
356 false,
357 file_path_for_history_item ? "data" : NULL,
358 file_path_for_history_item);
360 ViewHostMsg_FrameNavigate msg(1, params);
361 OnNavigate(msg);
364 void TestRenderViewHost::SendShouldCloseACK(bool proceed) {
365 base::TimeTicks now = base::TimeTicks::Now();
366 OnShouldCloseACK(proceed, now, now);
369 void TestRenderViewHost::SetContentsMimeType(const std::string& mime_type) {
370 contents_mime_type_ = mime_type;
373 void TestRenderViewHost::SimulateSwapOutACK() {
374 OnSwappedOut(false);
377 void TestRenderViewHost::SimulateWasHidden() {
378 WasHidden();
381 void TestRenderViewHost::SimulateWasShown() {
382 WasShown();
385 void TestRenderViewHost::TestOnStartDragging(
386 const WebDropData& drop_data) {
387 WebKit::WebDragOperationsMask drag_operation = WebKit::WebDragOperationEvery;
388 DragEventSourceInfo event_info;
389 OnStartDragging(drop_data, drag_operation, SkBitmap(), gfx::Vector2d(),
390 event_info);
393 void TestRenderViewHost::TestOnUpdateStateWithFile(
394 int process_id,
395 const base::FilePath& file_path) {
396 OnUpdateState(process_id,
397 PageState::CreateForTesting(GURL("http://www.google.com"),
398 false,
399 "data",
400 &file_path));
403 void TestRenderViewHost::set_simulate_fetch_via_proxy(bool proxy) {
404 simulate_fetch_via_proxy_ = proxy;
407 void TestRenderViewHost::set_simulate_history_list_was_cleared(bool cleared) {
408 simulate_history_list_was_cleared_ = cleared;
411 RenderViewHostImplTestHarness::RenderViewHostImplTestHarness() {
414 RenderViewHostImplTestHarness::~RenderViewHostImplTestHarness() {
417 TestRenderViewHost* RenderViewHostImplTestHarness::test_rvh() {
418 return static_cast<TestRenderViewHost*>(rvh());
421 TestRenderViewHost* RenderViewHostImplTestHarness::pending_test_rvh() {
422 return static_cast<TestRenderViewHost*>(pending_rvh());
425 TestRenderViewHost* RenderViewHostImplTestHarness::active_test_rvh() {
426 return static_cast<TestRenderViewHost*>(active_rvh());
429 TestWebContents* RenderViewHostImplTestHarness::contents() {
430 return static_cast<TestWebContents*>(web_contents());
433 } // namespace content