- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / test_render_view_host.cc
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 "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"
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::AcceleratedSurfaceBuffersSwapped(
136     const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params,
137     int gpu_host_id) {
138 }
139
140 void TestRenderWidgetHostView::AcceleratedSurfacePostSubBuffer(
141     const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params& params,
142     int gpu_host_id) {
143 }
144
145 void TestRenderWidgetHostView::AcceleratedSurfaceSuspend() {
146 }
147
148 bool TestRenderWidgetHostView::HasAcceleratedSurface(
149       const gfx::Size& desired_size) {
150   return false;
151 }
152
153 #if defined(OS_MACOSX)
154
155 void TestRenderWidgetHostView::AboutToWaitForBackingStoreMsg() {
156 }
157
158 void TestRenderWidgetHostView::SetActive(bool active) {
159   // <viettrungluu@gmail.com>: Do I need to do anything here?
160 }
161
162 bool TestRenderWidgetHostView::SupportsSpeech() const {
163   return false;
164 }
165
166 void TestRenderWidgetHostView::SpeakSelection() {
167 }
168
169 bool TestRenderWidgetHostView::IsSpeaking() const {
170   return false;
171 }
172
173 void TestRenderWidgetHostView::StopSpeaking() {
174 }
175
176 bool TestRenderWidgetHostView::PostProcessEventForPluginIme(
177     const NativeWebKeyboardEvent& event) {
178   return false;
179 }
180
181 #elif defined(OS_WIN) && !defined(USE_AURA)
182 void TestRenderWidgetHostView::WillWmDestroy() {
183 }
184 #endif
185
186 gfx::Rect TestRenderWidgetHostView::GetBoundsInRootWindow() {
187   return gfx::Rect();
188 }
189
190 #if defined(TOOLKIT_GTK)
191 GdkEventButton* TestRenderWidgetHostView::GetLastMouseDown() {
192   return NULL;
193 }
194
195 gfx::NativeView TestRenderWidgetHostView::BuildInputMethodsGtkMenu() {
196   return NULL;
197 }
198 #endif  // defined(TOOLKIT_GTK)
199
200 void TestRenderWidgetHostView::OnSwapCompositorFrame(
201     uint32 output_surface_id,
202     scoped_ptr<cc::CompositorFrame> frame) {
203   did_swap_compositor_frame_ = true;
204 }
205
206
207 gfx::GLSurfaceHandle TestRenderWidgetHostView::GetCompositingSurface() {
208   return gfx::GLSurfaceHandle();
209 }
210
211 #if defined(OS_WIN) && !defined(USE_AURA)
212 void TestRenderWidgetHostView::SetClickthroughRegion(SkRegion* region) {
213 }
214 #endif
215
216 bool TestRenderWidgetHostView::LockMouse() {
217   return false;
218 }
219
220 void TestRenderWidgetHostView::UnlockMouse() {
221 }
222
223 #if defined(OS_WIN) && defined(USE_AURA)
224 void TestRenderWidgetHostView::SetParentNativeViewAccessible(
225     gfx::NativeViewAccessible accessible_parent) {
226 }
227 #endif
228
229 TestRenderViewHost::TestRenderViewHost(
230     SiteInstance* instance,
231     RenderViewHostDelegate* delegate,
232     RenderWidgetHostDelegate* widget_delegate,
233     int routing_id,
234     int main_frame_routing_id,
235     bool swapped_out)
236     : RenderViewHostImpl(instance,
237                          delegate,
238                          widget_delegate,
239                          routing_id,
240                          main_frame_routing_id,
241                          swapped_out,
242                          false /* hidden */),
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
251   // called.
252   new TestRenderWidgetHostView(this);
253
254   main_frame_id_ = kFrameId;
255 }
256
257 TestRenderViewHost::~TestRenderViewHost() {
258   if (delete_counter_)
259     ++*delete_counter_;
260 }
261
262 bool TestRenderViewHost::CreateRenderView(
263     const string16& frame_name,
264     int opener_route_id,
265     int32 max_page_id) {
266   DCHECK(!render_view_created_);
267   render_view_created_ = true;
268   opener_route_id_ = opener_route_id;
269   return true;
270 }
271
272 bool TestRenderViewHost::IsRenderViewLive() const {
273   return render_view_created_;
274 }
275
276 void TestRenderViewHost::SendNavigate(int page_id, const GURL& url) {
277   SendNavigateWithTransition(page_id, url, PAGE_TRANSITION_LINK);
278 }
279
280 void TestRenderViewHost::SendFailedNavigate(int page_id, const GURL& url) {
281   SendNavigateWithTransitionAndResponseCode(
282       page_id, url, PAGE_TRANSITION_LINK, 500);
283 }
284
285 void TestRenderViewHost::SendNavigateWithTransition(
286     int page_id, const GURL& url, PageTransition transition) {
287   SendNavigateWithTransitionAndResponseCode(page_id, url, transition, 200);
288 }
289
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);
295 }
296
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);
301 }
302
303 void TestRenderViewHost::SendNavigateWithTransitionAndResponseCode(
304     int page_id, const GURL& url, PageTransition transition,
305     int response_code) {
306   // DidStartProvisionalLoad may delete the pending entry that holds |url|,
307   // so we keep a copy of it to use in SendNavigateWithParameters.
308   GURL url_copy(url);
309   OnDidStartProvisionalLoadForFrame(kFrameId, -1, true, url_copy);
310   SendNavigateWithParameters(page_id, url_copy, transition, url_copy,
311                              response_code, 0);
312 }
313
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;
321   params.url = url;
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;
339
340   params.page_state = PageState::CreateForTesting(
341       url,
342       false,
343       file_path_for_history_item ? "data" : NULL,
344       file_path_for_history_item);
345
346   ViewHostMsg_FrameNavigate msg(1, params);
347   OnNavigate(msg);
348 }
349
350 void TestRenderViewHost::SendShouldCloseACK(bool proceed) {
351   base::TimeTicks now = base::TimeTicks::Now();
352   OnShouldCloseACK(proceed, now, now);
353 }
354
355 void TestRenderViewHost::SetContentsMimeType(const std::string& mime_type) {
356   contents_mime_type_ = mime_type;
357 }
358
359 void TestRenderViewHost::SimulateSwapOutACK() {
360   OnSwappedOut(false);
361 }
362
363 void TestRenderViewHost::SimulateWasHidden() {
364   WasHidden();
365 }
366
367 void TestRenderViewHost::SimulateWasShown() {
368   WasShown();
369 }
370
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(),
376                   event_info);
377 }
378
379 void TestRenderViewHost::TestOnUpdateStateWithFile(
380     int process_id,
381     const base::FilePath& file_path) {
382   OnUpdateState(process_id,
383                 PageState::CreateForTesting(GURL("http://www.google.com"),
384                                             false,
385                                             "data",
386                                             &file_path));
387 }
388
389 void TestRenderViewHost::set_simulate_fetch_via_proxy(bool proxy) {
390   simulate_fetch_via_proxy_ = proxy;
391 }
392
393 void TestRenderViewHost::set_simulate_history_list_was_cleared(bool cleared) {
394   simulate_history_list_was_cleared_ = cleared;
395 }
396
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));
402 }
403
404 RenderViewHostImplTestHarness::~RenderViewHostImplTestHarness() {
405 }
406
407 TestRenderViewHost* RenderViewHostImplTestHarness::test_rvh() {
408   return static_cast<TestRenderViewHost*>(rvh());
409 }
410
411 TestRenderViewHost* RenderViewHostImplTestHarness::pending_test_rvh() {
412   return static_cast<TestRenderViewHost*>(pending_rvh());
413 }
414
415 TestRenderViewHost* RenderViewHostImplTestHarness::active_test_rvh() {
416   return static_cast<TestRenderViewHost*>(active_rvh());
417 }
418
419 TestWebContents* RenderViewHostImplTestHarness::contents() {
420   return static_cast<TestWebContents*>(web_contents());
421 }
422
423 }  // namespace content