OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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 CHROME_BROWSER_RENDERER_HOST_TEST_TEST_RENDER_VIEW_HOST_H_ | |
6 #define CHROME_BROWSER_RENDERER_HOST_TEST_TEST_RENDER_VIEW_HOST_H_ | |
7 #pragma once | |
8 | |
9 #include "base/basictypes.h" | |
10 #include "base/gtest_prod_util.h" | |
11 #include "base/message_loop.h" | |
12 #include "build/build_config.h" | |
13 #include "chrome/browser/renderer_host/mock_render_process_host.h" | |
14 #include "chrome/browser/renderer_host/render_widget_host_view.h" | |
15 #include "chrome/browser/renderer_host/render_view_host.h" | |
16 #include "chrome/browser/renderer_host/render_view_host_factory.h" | |
17 #include "chrome/common/page_transition_types.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 namespace gfx { | |
21 class Rect; | |
22 } | |
23 | |
24 class NavigationController; | |
25 class SiteInstance; | |
26 class TestingProfile; | |
27 class TestTabContents; | |
28 struct WebMenuItem; | |
29 struct ViewHostMsg_FrameNavigate_Params; | |
30 | |
31 // Utility function to initialize ViewHostMsg_NavigateParams_Params | |
32 // with given |page_id|, |url| and |transition_type|. | |
33 void InitNavigateParams(ViewHostMsg_FrameNavigate_Params* params, | |
34 int page_id, | |
35 const GURL& url, | |
36 PageTransition::Type transition_type); | |
37 | |
38 // This file provides a testing framework for mocking out the RenderProcessHost | |
39 // layer. It allows you to test RenderViewHost, TabContents, | |
40 // NavigationController, and other layers above that without running an actual | |
41 // renderer process. | |
42 // | |
43 // To use, derive your test base class from RenderViewHostTestHarness. | |
44 | |
45 // TestRenderViewHostView ------------------------------------------------------ | |
46 | |
47 // Subclass the RenderViewHost's view so that we can call Show(), etc., | |
48 // without having side-effects. | |
49 class TestRenderWidgetHostView : public RenderWidgetHostView { | |
50 public: | |
51 explicit TestRenderWidgetHostView(RenderWidgetHost* rwh); | |
52 virtual ~TestRenderWidgetHostView(); | |
53 | |
54 virtual void InitAsPopup(RenderWidgetHostView* parent_host_view, | |
55 const gfx::Rect& pos) {} | |
56 virtual void InitAsFullscreen() {} | |
57 virtual RenderWidgetHost* GetRenderWidgetHost() const; | |
58 virtual void DidBecomeSelected() {} | |
59 virtual void WasHidden() {} | |
60 virtual void SetSize(const gfx::Size& size) {} | |
61 virtual gfx::NativeView GetNativeView(); | |
62 virtual void MovePluginWindows( | |
63 const std::vector<webkit::npapi::WebPluginGeometry>& moves) {} | |
64 #if defined(OS_WIN) | |
65 virtual void ForwardMouseEventToRenderer(UINT message, | |
66 WPARAM wparam, | |
67 LPARAM lparam) {} | |
68 #endif | |
69 virtual void Focus() {} | |
70 virtual void Blur() {} | |
71 virtual bool HasFocus(); | |
72 virtual void AdvanceFocus(bool reverse) {} | |
73 virtual void Show(); | |
74 virtual void Hide(); | |
75 virtual bool IsShowing(); | |
76 virtual gfx::Rect GetViewBounds() const; | |
77 virtual void SetIsLoading(bool is_loading) {} | |
78 virtual void UpdateCursor(const WebCursor& cursor) {} | |
79 virtual void UpdateCursorIfOverSelf() {} | |
80 virtual void ImeUpdateTextInputState(WebKit::WebTextInputType state, | |
81 const gfx::Rect& caret_rect) {} | |
82 virtual void ImeCancelComposition() {} | |
83 virtual void DidUpdateBackingStore( | |
84 const gfx::Rect& scroll_rect, int scroll_dx, int scroll_dy, | |
85 const std::vector<gfx::Rect>& rects) {} | |
86 virtual void RenderViewGone(base::TerminationStatus status, | |
87 int error_code); | |
88 virtual void WillDestroyRenderWidget(RenderWidgetHost* rwh) { } | |
89 virtual void Destroy() {} | |
90 virtual void PrepareToDestroy() {} | |
91 virtual void SetTooltipText(const std::wstring& tooltip_text) {} | |
92 virtual BackingStore* AllocBackingStore(const gfx::Size& size); | |
93 #if defined(OS_MACOSX) | |
94 virtual void SetTakesFocusOnlyOnMouseDown(bool flag) {} | |
95 virtual void ShowPopupWithItems(gfx::Rect bounds, | |
96 int item_height, | |
97 double item_font_size, | |
98 int selected_item, | |
99 const std::vector<WebMenuItem>& items, | |
100 bool right_aligned); | |
101 virtual gfx::Rect GetViewCocoaBounds() const; | |
102 virtual gfx::Rect GetRootWindowRect(); | |
103 virtual void SetActive(bool active); | |
104 virtual void SetWindowVisibility(bool visible) {} | |
105 virtual void WindowFrameChanged() {} | |
106 virtual void PluginFocusChanged(bool focused, int plugin_id); | |
107 virtual void StartPluginIme(); | |
108 virtual bool PostProcessEventForPluginIme( | |
109 const NativeWebKeyboardEvent& event); | |
110 virtual gfx::PluginWindowHandle AllocateFakePluginWindowHandle( | |
111 bool opaque, | |
112 bool root); | |
113 virtual void DestroyFakePluginWindowHandle(gfx::PluginWindowHandle window); | |
114 virtual void AcceleratedSurfaceSetIOSurface(gfx::PluginWindowHandle window, | |
115 int32 width, | |
116 int32 height, | |
117 uint64 surface_id); | |
118 virtual void AcceleratedSurfaceSetTransportDIB( | |
119 gfx::PluginWindowHandle window, | |
120 int32 width, | |
121 int32 height, | |
122 TransportDIB::Handle transport_dib); | |
123 virtual void AcceleratedSurfaceBuffersSwapped( | |
124 gfx::PluginWindowHandle window, | |
125 uint64 surface_id, | |
126 int renderer_id, | |
127 int32 route_id, | |
128 int gpu_host_id, | |
129 uint64 swap_buffers_count); | |
130 virtual void GpuRenderingStateDidChange(); | |
131 #elif defined(OS_WIN) | |
132 virtual void WillWmDestroy(); | |
133 virtual void ShowCompositorHostWindow(bool show); | |
134 #endif | |
135 virtual void SetVisuallyDeemphasized(const SkColor* color, bool animate) { } | |
136 | |
137 #if defined(TOOLKIT_USES_GTK) | |
138 virtual void CreatePluginContainer(gfx::PluginWindowHandle id) { } | |
139 virtual void DestroyPluginContainer(gfx::PluginWindowHandle id) { } | |
140 virtual void AcceleratedCompositingActivated(bool activated) { } | |
141 #endif | |
142 | |
143 virtual gfx::PluginWindowHandle AcquireCompositingSurface(); | |
144 virtual void ReleaseCompositingSurface(gfx::PluginWindowHandle surface) { } | |
145 | |
146 virtual bool ContainsNativeView(gfx::NativeView native_view) const; | |
147 | |
148 bool is_showing() const { return is_showing_; } | |
149 | |
150 private: | |
151 RenderWidgetHost* rwh_; | |
152 bool is_showing_; | |
153 }; | |
154 | |
155 // TestRenderViewHost ---------------------------------------------------------- | |
156 | |
157 // TODO(brettw) this should use a TestTabContents which should be generalized | |
158 // from the TabContents test. We will probably also need that class' version of | |
159 // CreateRenderViewForRenderManager when more complicate tests start using this. | |
160 class TestRenderViewHost : public RenderViewHost { | |
161 public: | |
162 TestRenderViewHost(SiteInstance* instance, | |
163 RenderViewHostDelegate* delegate, | |
164 int routing_id); | |
165 virtual ~TestRenderViewHost(); | |
166 | |
167 // Testing functions --------------------------------------------------------- | |
168 | |
169 // Calls the RenderViewHosts' private OnMessageReceived function with the | |
170 // given message. | |
171 bool TestOnMessageReceived(const IPC::Message& msg); | |
172 | |
173 // Calls OnMsgNavigate on the RenderViewHost with the given information, | |
174 // setting the rest of the parameters in the message to the "typical" values. | |
175 // This is a helper function for simulating the most common types of loads. | |
176 void SendNavigate(int page_id, const GURL& url); | |
177 | |
178 // Calls OnMsgNavigate on the RenderViewHost with the given information, | |
179 // including a custom PageTransition::Type. Sets the rest of the parameters | |
180 // in the message to the "typical" values. | |
181 // This is a helper function for simulating the most common types of loads. | |
182 void SendNavigateWithTransition(int page_id, const GURL& url, | |
183 PageTransition::Type transition); | |
184 | |
185 // If set, *delete_counter is incremented when this object destructs. | |
186 void set_delete_counter(int* delete_counter) { | |
187 delete_counter_ = delete_counter; | |
188 } | |
189 | |
190 // Sets whether the RenderView currently exists or not. This controls the | |
191 // return value from IsRenderViewLive, which the rest of the system uses to | |
192 // check whether the RenderView has crashed or not. | |
193 void set_render_view_created(bool created) { | |
194 render_view_created_ = created; | |
195 } | |
196 | |
197 // Returns whether the RenderViewHost is currently waiting to hear the result | |
198 // of a before unload handler from the renderer. | |
199 bool is_waiting_for_beforeunload_ack() const { | |
200 return is_waiting_for_beforeunload_ack_; | |
201 } | |
202 | |
203 // RenderViewHost overrides -------------------------------------------------- | |
204 | |
205 virtual bool CreateRenderView(const string16& frame_name); | |
206 virtual bool IsRenderViewLive() const; | |
207 | |
208 private: | |
209 FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest, FilterNavigate); | |
210 | |
211 // Tracks if the caller thinks if it created the RenderView. This is so we can | |
212 // respond to IsRenderViewLive appropriately. | |
213 bool render_view_created_; | |
214 | |
215 // See set_delete_counter() above. May be NULL. | |
216 int* delete_counter_; | |
217 | |
218 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHost); | |
219 }; | |
220 | |
221 // TestRenderViewHostFactory --------------------------------------------------- | |
222 | |
223 // Manages creation of the RenderViewHosts using our special subclass. This | |
224 // automatically registers itself when it goes in scope, and unregisters itself | |
225 // when it goes out of scope. Since you can't have more than one factory | |
226 // registered at a time, you can only have one of these objects at a time. | |
227 class TestRenderViewHostFactory : public RenderViewHostFactory { | |
228 public: | |
229 explicit TestRenderViewHostFactory(RenderProcessHostFactory* rph_factory); | |
230 virtual ~TestRenderViewHostFactory(); | |
231 | |
232 virtual void set_render_process_host_factory( | |
233 RenderProcessHostFactory* rph_factory); | |
234 virtual RenderViewHost* CreateRenderViewHost( | |
235 SiteInstance* instance, | |
236 RenderViewHostDelegate* delegate, | |
237 int routing_id, | |
238 SessionStorageNamespace* session_storage); | |
239 | |
240 private: | |
241 // This is a bit of a hack. With the current design of the site instances / | |
242 // browsing instances, it's difficult to pass a RenderProcessHostFactory | |
243 // around properly. | |
244 // | |
245 // Instead, we set it right before we create a new RenderViewHost, which | |
246 // happens before the RenderProcessHost is created. This way, the instance | |
247 // has the correct factory and creates our special RenderProcessHosts. | |
248 RenderProcessHostFactory* render_process_host_factory_; | |
249 | |
250 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHostFactory); | |
251 }; | |
252 | |
253 // RenderViewHostTestHarness --------------------------------------------------- | |
254 | |
255 class RenderViewHostTestHarness : public testing::Test { | |
256 public: | |
257 RenderViewHostTestHarness(); | |
258 virtual ~RenderViewHostTestHarness(); | |
259 | |
260 NavigationController& controller(); | |
261 TestTabContents* contents(); | |
262 TestRenderViewHost* rvh(); | |
263 TestRenderViewHost* pending_rvh(); | |
264 TestRenderViewHost* active_rvh(); | |
265 TestingProfile* profile(); | |
266 MockRenderProcessHost* process(); | |
267 | |
268 // Frees the current tab contents for tests that want to test destruction. | |
269 void DeleteContents(); | |
270 | |
271 // Creates a new TestTabContents. Ownership passes to the caller. | |
272 TestTabContents* CreateTestTabContents(); | |
273 | |
274 // Cover for |contents()->NavigateAndCommit(url)|. See | |
275 // TestTabContents::NavigateAndCommit for details. | |
276 void NavigateAndCommit(const GURL& url); | |
277 | |
278 // Simulates a reload of the current page. | |
279 void Reload(); | |
280 | |
281 protected: | |
282 // testing::Test | |
283 virtual void SetUp(); | |
284 virtual void TearDown(); | |
285 | |
286 // This profile will be created in SetUp if it has not already been created. | |
287 // This allows tests to override the profile if they so choose in their own | |
288 // SetUp function before calling the base class's (us) SetUp(). | |
289 scoped_ptr<TestingProfile> profile_; | |
290 | |
291 MessageLoopForUI message_loop_; | |
292 | |
293 MockRenderProcessHostFactory rph_factory_; | |
294 TestRenderViewHostFactory rvh_factory_; | |
295 | |
296 scoped_ptr<TestTabContents> contents_; | |
297 | |
298 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestHarness); | |
299 }; | |
300 | |
301 #endif // CHROME_BROWSER_RENDERER_HOST_TEST_TEST_RENDER_VIEW_HOST_H_ | |
OLD | NEW |