Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / content / test / test_render_view_host.h
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 #ifndef CONTENT_TEST_TEST_RENDER_VIEW_HOST_H_
6 #define CONTENT_TEST_TEST_RENDER_VIEW_HOST_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "base/basictypes.h"
12 #include "base/gtest_prod_util.h"
13 #include "build/build_config.h"
14 #include "content/browser/renderer_host/render_view_host_impl.h"
15 #include "content/browser/renderer_host/render_widget_host_view_base.h"
16 #include "content/public/common/page_transition_types.h"
17 #include "content/public/test/test_renderer_host.h"
18 #include "content/test/test_render_frame_host.h"
19 #include "ui/base/layout.h"
20 #include "ui/gfx/vector2d_f.h"
21
22 // This file provides a testing framework for mocking out the RenderProcessHost
23 // layer. It allows you to test RenderViewHost, WebContentsImpl,
24 // NavigationController, and other layers above that without running an actual
25 // renderer process.
26 //
27 // To use, derive your test base class from RenderViewHostImplTestHarness.
28
29 struct FrameHostMsg_DidCommitProvisionalLoad_Params;
30
31 namespace gfx {
32 class Rect;
33 }
34
35 namespace content {
36
37 class SiteInstance;
38 class TestRenderFrameHost;
39 class TestWebContents;
40
41 // Utility function to initialize ViewHostMsg_NavigateParams_Params
42 // with given |page_id|, |url| and |transition_type|.
43 void InitNavigateParams(FrameHostMsg_DidCommitProvisionalLoad_Params* params,
44                         int page_id,
45                         const GURL& url,
46                         PageTransition transition_type);
47
48 // TestRenderViewHostView ------------------------------------------------------
49
50 // Subclass the RenderViewHost's view so that we can call Show(), etc.,
51 // without having side-effects.
52 class TestRenderWidgetHostView : public RenderWidgetHostViewBase {
53  public:
54   explicit TestRenderWidgetHostView(RenderWidgetHost* rwh);
55   virtual ~TestRenderWidgetHostView();
56
57   // RenderWidgetHostView implementation.
58   virtual void InitAsChild(gfx::NativeView parent_view) OVERRIDE {}
59   virtual RenderWidgetHost* GetRenderWidgetHost() const OVERRIDE;
60   virtual void SetSize(const gfx::Size& size) OVERRIDE {}
61   virtual void SetBounds(const gfx::Rect& rect) OVERRIDE {}
62   virtual gfx::NativeView GetNativeView() const OVERRIDE;
63   virtual gfx::NativeViewId GetNativeViewId() const OVERRIDE;
64   virtual gfx::NativeViewAccessible GetNativeViewAccessible() OVERRIDE;
65   virtual bool HasFocus() const OVERRIDE;
66   virtual bool IsSurfaceAvailableForCopy() const OVERRIDE;
67   virtual void Show() OVERRIDE;
68   virtual void Hide() OVERRIDE;
69   virtual bool IsShowing() OVERRIDE;
70   virtual gfx::Rect GetViewBounds() const OVERRIDE;
71 #if defined(OS_MACOSX)
72   virtual void SetActive(bool active) OVERRIDE;
73   virtual void SetTakesFocusOnlyOnMouseDown(bool flag) OVERRIDE {}
74   virtual void SetWindowVisibility(bool visible) OVERRIDE {}
75   virtual void WindowFrameChanged() OVERRIDE {}
76   virtual void ShowDefinitionForSelection() OVERRIDE {}
77   virtual bool SupportsSpeech() const OVERRIDE;
78   virtual void SpeakSelection() OVERRIDE;
79   virtual bool IsSpeaking() const OVERRIDE;
80   virtual void StopSpeaking() OVERRIDE;
81 #endif  // defined(OS_MACOSX)
82 #if defined(TOOLKIT_GTK)
83   virtual GdkEventButton* GetLastMouseDown() OVERRIDE;
84   virtual gfx::NativeView BuildInputMethodsGtkMenu() OVERRIDE;
85 #endif  // defined(TOOLKIT_GTK)
86   virtual void OnSwapCompositorFrame(
87       uint32 output_surface_id,
88       scoped_ptr<cc::CompositorFrame> frame) OVERRIDE;
89
90   // RenderWidgetHostViewPort implementation.
91   virtual void InitAsPopup(RenderWidgetHostView* parent_host_view,
92                            const gfx::Rect& pos) OVERRIDE {}
93   virtual void InitAsFullscreen(
94       RenderWidgetHostView* reference_host_view) OVERRIDE {}
95   virtual void WasShown() OVERRIDE {}
96   virtual void WasHidden() OVERRIDE {}
97   virtual void MovePluginWindows(
98       const gfx::Vector2d& scroll_offset,
99       const std::vector<WebPluginGeometry>& moves) OVERRIDE {}
100   virtual void Focus() OVERRIDE {}
101   virtual void Blur() OVERRIDE {}
102   virtual void SetIsLoading(bool is_loading) OVERRIDE {}
103   virtual void UpdateCursor(const WebCursor& cursor) OVERRIDE {}
104   virtual void TextInputTypeChanged(ui::TextInputType type,
105                                     ui::TextInputMode input_mode,
106                                     bool can_compose_inline) OVERRIDE {}
107   virtual void ImeCancelComposition() OVERRIDE {}
108 #if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
109   virtual void ImeCompositionRangeChanged(
110       const gfx::Range& range,
111       const std::vector<gfx::Rect>& character_bounds) OVERRIDE {}
112 #endif
113   virtual void DidUpdateBackingStore(
114       const gfx::Rect& scroll_rect,
115       const gfx::Vector2d& scroll_delta,
116       const std::vector<gfx::Rect>& rects,
117       const std::vector<ui::LatencyInfo>& latency_info) OVERRIDE {}
118   virtual void RenderProcessGone(base::TerminationStatus status,
119                                  int error_code) OVERRIDE;
120   virtual void WillDestroyRenderWidget(RenderWidgetHost* rwh) { }
121   virtual void Destroy() OVERRIDE;
122   virtual void SetTooltipText(const base::string16& tooltip_text) OVERRIDE {}
123   virtual void SelectionBoundsChanged(
124       const ViewHostMsg_SelectionBounds_Params& params) OVERRIDE {}
125 #if defined(OS_ANDROID)
126   virtual void SelectionRootBoundsChanged(const gfx::Rect&) OVERRIDE {}
127 #endif
128   virtual void ScrollOffsetChanged() OVERRIDE {}
129   virtual BackingStore* AllocBackingStore(const gfx::Size& size) OVERRIDE;
130   virtual void CopyFromCompositingSurface(
131       const gfx::Rect& src_subrect,
132       const gfx::Size& dst_size,
133       const base::Callback<void(bool, const SkBitmap&)>& callback,
134       const SkBitmap::Config config) OVERRIDE;
135   virtual void CopyFromCompositingSurfaceToVideoFrame(
136       const gfx::Rect& src_subrect,
137       const scoped_refptr<media::VideoFrame>& target,
138       const base::Callback<void(bool)>& callback) OVERRIDE;
139   virtual bool CanCopyToVideoFrame() const OVERRIDE;
140   virtual void OnAcceleratedCompositingStateChange() OVERRIDE;
141   virtual void AcceleratedSurfaceInitialized(int host_id,
142                                              int route_id) OVERRIDE;
143   virtual void AcceleratedSurfaceBuffersSwapped(
144       const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params,
145       int gpu_host_id) OVERRIDE;
146   virtual void AcceleratedSurfacePostSubBuffer(
147       const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params& params,
148       int gpu_host_id) OVERRIDE;
149   virtual void AcceleratedSurfaceSuspend() OVERRIDE;
150   virtual void AcceleratedSurfaceRelease() OVERRIDE {}
151   virtual bool HasAcceleratedSurface(const gfx::Size& desired_size) OVERRIDE;
152 #if defined(OS_MACOSX)
153   virtual bool PostProcessEventForPluginIme(
154       const NativeWebKeyboardEvent& event) OVERRIDE;
155 #elif defined(OS_ANDROID)
156   virtual void ShowDisambiguationPopup(
157       const gfx::Rect& target_rect,
158       const SkBitmap& zoomed_bitmap) OVERRIDE {}
159 #endif
160   virtual void GetScreenInfo(blink::WebScreenInfo* results) OVERRIDE {}
161   virtual gfx::Rect GetBoundsInRootWindow() OVERRIDE;
162   virtual void SetHasHorizontalScrollbar(
163       bool has_horizontal_scrollbar) OVERRIDE { }
164   virtual void SetScrollOffsetPinning(
165       bool is_pinned_to_left, bool is_pinned_to_right) OVERRIDE { }
166   virtual gfx::GLSurfaceHandle GetCompositingSurface() OVERRIDE;
167   virtual bool LockMouse() OVERRIDE;
168   virtual void UnlockMouse() OVERRIDE;
169 #if defined(OS_WIN)
170   virtual void SetParentNativeViewAccessible(
171       gfx::NativeViewAccessible accessible_parent) OVERRIDE;
172   virtual gfx::NativeViewId GetParentForWindowlessPlugin() const OVERRIDE;
173 #endif
174
175   bool is_showing() const { return is_showing_; }
176   bool did_swap_compositor_frame() const { return did_swap_compositor_frame_; }
177
178  protected:
179   RenderWidgetHostImpl* rwh_;
180
181  private:
182   bool is_showing_;
183   bool did_swap_compositor_frame_;
184 };
185
186 #if defined(COMPILER_MSVC)
187 // See comment for same warning on RenderViewHostImpl.
188 #pragma warning(push)
189 #pragma warning(disable: 4250)
190 #endif
191
192 // TestRenderViewHost ----------------------------------------------------------
193
194 // TODO(brettw) this should use a TestWebContents which should be generalized
195 // from the WebContentsImpl test. We will probably also need that class' version
196 // of CreateRenderViewForRenderManager when more complicated tests start using
197 // this.
198 //
199 // Note that users outside of content must use this class by getting
200 // the separate RenderViewHostTester interface via
201 // RenderViewHostTester::For(rvh) on the RenderViewHost they want to
202 // drive tests on.
203 //
204 // Users within content may directly static_cast from a
205 // RenderViewHost* to a TestRenderViewHost*.
206 //
207 // The reasons we do it this way rather than extending the parallel
208 // inheritance hierarchy we have for RenderWidgetHost/RenderViewHost
209 // vs. RenderWidgetHostImpl/RenderViewHostImpl are:
210 //
211 // a) Extending the parallel class hierarchy further would require
212 // more classes to use virtual inheritance.  This is a complexity that
213 // is better to avoid, especially when it would be introduced in the
214 // production code solely to facilitate testing code.
215 //
216 // b) While users outside of content only need to drive tests on a
217 // RenderViewHost, content needs a test version of the full
218 // RenderViewHostImpl so that it can test all methods on that concrete
219 // class (e.g. overriding a method such as
220 // RenderViewHostImpl::CreateRenderView).  This would have complicated
221 // the dual class hierarchy even further.
222 //
223 // The reason we do it this way instead of using composition is
224 // similar to (b) above, essentially it gets very tricky.  By using
225 // the split interface we avoid complexity within content and maintain
226 // reasonable utility for embedders.
227 class TestRenderViewHost
228     : public RenderViewHostImpl,
229       public RenderViewHostTester {
230  public:
231   TestRenderViewHost(SiteInstance* instance,
232                      RenderViewHostDelegate* delegate,
233                      RenderWidgetHostDelegate* widget_delegate,
234                      int routing_id,
235                      int main_frame_routing_id,
236                      bool swapped_out);
237   virtual ~TestRenderViewHost();
238
239   // RenderViewHostTester implementation.  Note that CreateRenderView
240   // is not specified since it is synonymous with the one from
241   // RenderViewHostImpl, see below.
242   virtual void SendBeforeUnloadACK(bool proceed) OVERRIDE;
243   virtual void SetContentsMimeType(const std::string& mime_type) OVERRIDE;
244   virtual void SimulateSwapOutACK() OVERRIDE;
245   virtual void SimulateWasHidden() OVERRIDE;
246   virtual void SimulateWasShown() OVERRIDE;
247
248   // NOTE: These methods are deprecated and the equivalents in
249   // TestRenderFrameHost should be used.
250   virtual void SendNavigate(int page_id, const GURL& url) OVERRIDE;
251   virtual void SendFailedNavigate(int page_id, const GURL& url) OVERRIDE;
252   virtual void SendNavigateWithTransition(int page_id, const GURL& url,
253                                           PageTransition transition) OVERRIDE;
254
255   // Calls OnNavigate on the RenderViewHost with the given information,
256   // including a custom original request URL.  Sets the rest of the
257   // parameters in the message to the "typical" values.  This is a helper
258   // function for simulating the most common types of loads.
259   void SendNavigateWithOriginalRequestURL(
260       int page_id, const GURL& url, const GURL& original_request_url);
261
262   void SendNavigateWithFile(
263       int page_id, const GURL& url, const base::FilePath& file_path);
264
265   void SendNavigateWithParams(
266       FrameHostMsg_DidCommitProvisionalLoad_Params* params);
267
268   void TestOnUpdateStateWithFile(
269       int process_id, const base::FilePath& file_path);
270
271   void TestOnStartDragging(const DropData& drop_data);
272
273   // If set, *delete_counter is incremented when this object destructs.
274   void set_delete_counter(int* delete_counter) {
275     delete_counter_ = delete_counter;
276   }
277
278   // Sets whether the RenderView currently exists or not. This controls the
279   // return value from IsRenderViewLive, which the rest of the system uses to
280   // check whether the RenderView has crashed or not.
281   void set_render_view_created(bool created) {
282     render_view_created_ = created;
283   }
284
285   // Returns whether the RenderViewHost is currently waiting to hear the result
286   // of a before unload handler from the renderer.
287   bool is_waiting_for_beforeunload_ack() const {
288     return is_waiting_for_beforeunload_ack_;
289   }
290
291   // Sets whether the RenderViewHost is currently swapped out, and thus
292   // filtering messages from the renderer.
293   void set_rvh_state(RenderViewHostImplState rvh_state) {
294     rvh_state_ = rvh_state;
295   }
296
297   // If set, navigations will appear to have loaded through a proxy
298   // (ViewHostMsg_FrameNavigte_Params::was_fetched_via_proxy).
299   // False by default.
300   void set_simulate_fetch_via_proxy(bool proxy);
301
302   // If set, navigations will appear to have cleared the history list in the
303   // RenderView
304   // (FrameHostMsg_DidCommitProvisionalLoad_Params::history_list_was_cleared).
305   // False by default.
306   void set_simulate_history_list_was_cleared(bool cleared);
307
308   // The opener route id passed to CreateRenderView().
309   int opener_route_id() const { return opener_route_id_; }
310
311   // TODO(creis): Remove the need for these methods.
312   TestRenderFrameHost* main_render_frame_host() const {
313     return main_render_frame_host_;
314   }
315   void set_main_render_frame_host(TestRenderFrameHost* rfh) {
316     main_render_frame_host_ = rfh;
317   }
318
319   // RenderViewHost overrides --------------------------------------------------
320
321   virtual bool CreateRenderView(const base::string16& frame_name,
322                                 int opener_route_id,
323                                 int32 max_page_id) OVERRIDE;
324   virtual bool IsRenderViewLive() const OVERRIDE;
325   virtual bool IsFullscreen() const OVERRIDE;
326
327  private:
328   FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest, FilterNavigate);
329
330   void SendNavigateWithTransitionAndResponseCode(int page_id,
331                                                  const GURL& url,
332                                                  PageTransition transition,
333                                                  int response_code);
334
335   // Calls OnNavigate on the RenderViewHost with the given information.
336   // Sets the rest of the parameters in the message to the "typical" values.
337   // This is a helper function for simulating the most common types of loads.
338   void SendNavigateWithParameters(
339       int page_id,
340       const GURL& url,
341       PageTransition transition,
342       const GURL& original_request_url,
343       int response_code,
344       const base::FilePath* file_path_for_history_item);
345
346   // Tracks if the caller thinks if it created the RenderView. This is so we can
347   // respond to IsRenderViewLive appropriately.
348   bool render_view_created_;
349
350   // See set_delete_counter() above. May be NULL.
351   int* delete_counter_;
352
353   // See set_simulate_fetch_via_proxy() above.
354   bool simulate_fetch_via_proxy_;
355
356   // See set_simulate_history_list_was_cleared() above.
357   bool simulate_history_list_was_cleared_;
358
359   // See SetContentsMimeType() above.
360   std::string contents_mime_type_;
361
362   // See opener_route_id() above.
363   int opener_route_id_;
364
365   TestRenderFrameHost* main_render_frame_host_;
366
367   DISALLOW_COPY_AND_ASSIGN(TestRenderViewHost);
368 };
369
370 #if defined(COMPILER_MSVC)
371 #pragma warning(pop)
372 #endif
373
374 // Adds methods to get straight at the impl classes.
375 class RenderViewHostImplTestHarness : public RenderViewHostTestHarness {
376  public:
377   RenderViewHostImplTestHarness();
378   virtual ~RenderViewHostImplTestHarness();
379
380   TestRenderViewHost* test_rvh();
381   TestRenderViewHost* pending_test_rvh();
382   TestRenderViewHost* active_test_rvh();
383   TestRenderFrameHost* main_test_rfh();
384   TestWebContents* contents();
385
386  private:
387   typedef scoped_ptr<ui::test::ScopedSetSupportedScaleFactors>
388       ScopedSetSupportedScaleFactors;
389   ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_;
390   DISALLOW_COPY_AND_ASSIGN(RenderViewHostImplTestHarness);
391 };
392
393 }  // namespace content
394
395 #endif  // CONTENT_TEST_TEST_RENDER_VIEW_HOST_H_