OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_TEST_RENDER_VIEW_HOST_H_ | 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_TEST_RENDER_VIEW_HOST_H_ |
6 #define CONTENT_BROWSER_RENDERER_HOST_TEST_RENDER_VIEW_HOST_H_ | 6 #define CONTENT_BROWSER_RENDERER_HOST_TEST_RENDER_VIEW_HOST_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/gtest_prod_util.h" | 10 #include "base/gtest_prod_util.h" |
11 #include "base/message_loop.h" | |
12 #include "build/build_config.h" | 11 #include "build/build_config.h" |
13 #include "content/browser/renderer_host/mock_render_process_host.h" | |
14 #include "content/browser/renderer_host/render_view_host_factory.h" | |
15 #include "content/browser/renderer_host/render_view_host_impl.h" | 12 #include "content/browser/renderer_host/render_view_host_impl.h" |
16 #include "content/browser/renderer_host/render_widget_host_view_base.h" | 13 #include "content/browser/renderer_host/render_widget_host_view_base.h" |
17 #include "content/public/common/page_transition_types.h" | 14 #include "content/public/common/page_transition_types.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "content/test/test_renderer_host.h" |
19 | 16 |
20 // This file provides a testing framework for mocking out the RenderProcessHost | 17 // This file provides a testing framework for mocking out the RenderProcessHost |
21 // layer. It allows you to test RenderViewHost, TabContents, | 18 // layer. It allows you to test RenderViewHost, TabContents, |
22 // NavigationController, and other layers above that without running an actual | 19 // NavigationController, and other layers above that without running an actual |
23 // renderer process. | 20 // renderer process. |
24 // | 21 // |
25 // To use, derive your test base class from RenderViewHostTestHarness. | 22 // To use, derive your test base class from RenderViewHostTestHarness. |
26 | 23 |
27 #if defined(USE_AURA) | |
28 namespace aura { | |
29 class RootWindow; | |
30 namespace test { | |
31 class TestStackingClient; | |
32 } | |
33 } | |
34 #endif | |
35 | |
36 namespace content { | 24 namespace content { |
37 class BrowserContext; | |
38 class NavigationController; | |
39 class RenderProcessHostFactory; | |
40 class SiteInstance; | 25 class SiteInstance; |
41 } | 26 } |
42 | 27 |
43 namespace gfx { | 28 namespace gfx { |
44 class Rect; | 29 class Rect; |
45 } | 30 } |
46 | 31 |
47 class TestTabContents; | |
48 struct ViewHostMsg_FrameNavigate_Params; | 32 struct ViewHostMsg_FrameNavigate_Params; |
49 | 33 |
50 namespace content { | 34 namespace content { |
51 | 35 |
52 // Utility function to initialize ViewHostMsg_NavigateParams_Params | 36 // Utility function to initialize ViewHostMsg_NavigateParams_Params |
53 // with given |page_id|, |url| and |transition_type|. | 37 // with given |page_id|, |url| and |transition_type|. |
54 void InitNavigateParams(ViewHostMsg_FrameNavigate_Params* params, | 38 void InitNavigateParams(ViewHostMsg_FrameNavigate_Params* params, |
55 int page_id, | 39 int page_id, |
56 const GURL& url, | 40 const GURL& url, |
57 PageTransition transition_type); | 41 PageTransition transition_type); |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 // See comment for same warning on RenderViewHostImpl. | 174 // See comment for same warning on RenderViewHostImpl. |
191 #pragma warning(push) | 175 #pragma warning(push) |
192 #pragma warning(disable: 4250) | 176 #pragma warning(disable: 4250) |
193 #endif | 177 #endif |
194 | 178 |
195 // TestRenderViewHost ---------------------------------------------------------- | 179 // TestRenderViewHost ---------------------------------------------------------- |
196 | 180 |
197 // TODO(brettw) this should use a TestTabContents which should be generalized | 181 // TODO(brettw) this should use a TestTabContents which should be generalized |
198 // from the TabContents test. We will probably also need that class' version of | 182 // from the TabContents test. We will probably also need that class' version of |
199 // CreateRenderViewForRenderManager when more complicate tests start using this. | 183 // CreateRenderViewForRenderManager when more complicate tests start using this. |
200 class TestRenderViewHost : public RenderViewHostImpl { | 184 // |
| 185 // Note that users outside of content must use this class by getting |
| 186 // the separate content::RenderViewHostTester interface via |
| 187 // RenderViewHostTester::For(rvh) on the RenderViewHost they want to |
| 188 // drive tests on. |
| 189 // |
| 190 // Users within content may directly static_cast from a |
| 191 // RenderViewHost* to a TestRenderViewHost*. |
| 192 // |
| 193 // The reasons we do it this way rather than extending the parallel |
| 194 // inheritance hierarchy we have for RenderWidgetHost/RenderViewHost |
| 195 // vs. RenderWidgetHostImpl/RenderViewHostImpl are: |
| 196 // |
| 197 // a) Extending the parallel class hierarchy further would require |
| 198 // more classes to use virtual inheritance. This is a complexity that |
| 199 // is better to avoid, especially when it would be introduced in the |
| 200 // production code solely to facilitate testing code. |
| 201 // |
| 202 // b) While users outside of content only need to drive tests on a |
| 203 // RenderViewHost, content needs a test version of the full |
| 204 // RenderViewHostImpl so that it can test all methods on that concrete |
| 205 // class (e.g. overriding a method such as |
| 206 // RenderViewHostImpl::CreateRenderView). This would have complicated |
| 207 // the dual class hierarchy even further. |
| 208 // |
| 209 // The reason we do it this way instead of using composition is |
| 210 // similar to (b) above, essentially it gets very tricky. By using |
| 211 // the split interface we avoid complexity within content and maintain |
| 212 // reasonable utility for embedders. |
| 213 class TestRenderViewHost |
| 214 : public RenderViewHostImpl, |
| 215 public RenderViewHostTester { |
201 public: | 216 public: |
202 // If the given TabContnets has a pending RVH, returns it, otherwise NULL. | |
203 static TestRenderViewHost* GetPendingForController( | |
204 NavigationController* controller); | |
205 | |
206 TestRenderViewHost(SiteInstance* instance, | 217 TestRenderViewHost(SiteInstance* instance, |
207 RenderViewHostDelegate* delegate, | 218 RenderViewHostDelegate* delegate, |
208 int routing_id); | 219 int routing_id); |
209 virtual ~TestRenderViewHost(); | 220 virtual ~TestRenderViewHost(); |
210 | 221 |
211 // Testing functions --------------------------------------------------------- | 222 // RenderViewHostTester implementation. Note that CreateRenderView |
212 | 223 // is not specified since it is synonymous with the one from |
213 // Calls the RenderViewHosts' private OnMessageReceived function with the | 224 // RenderViewHostImpl, see below. |
214 // given message. | 225 virtual void SendNavigate(int page_id, const GURL& url) OVERRIDE; |
215 bool TestOnMessageReceived(const IPC::Message& msg); | 226 virtual void SendNavigateWithTransition(int page_id, const GURL& url, |
216 | 227 PageTransition transition) OVERRIDE; |
217 // Calls OnMsgNavigate on the RenderViewHost with the given information, | 228 virtual void SendShouldCloseACK(bool proceed) OVERRIDE; |
218 // setting the rest of the parameters in the message to the "typical" values. | 229 virtual void SetContentsMimeType(const std::string& mime_type) OVERRIDE; |
219 // This is a helper function for simulating the most common types of loads. | 230 virtual void SimulateSwapOutACK() OVERRIDE; |
220 void SendNavigate(int page_id, const GURL& url); | 231 virtual void SimulateWasHidden() OVERRIDE; |
221 | 232 virtual void SimulateWasRestored() OVERRIDE; |
222 // Calls OnMsgNavigate on the RenderViewHost with the given information, | 233 virtual bool TestOnMessageReceived(const IPC::Message& msg) OVERRIDE; |
223 // including a custom PageTransition. Sets the rest of the | |
224 // parameters in the message to the "typical" values. This is a helper | |
225 // function for simulating the most common types of loads. | |
226 void SendNavigateWithTransition(int page_id, const GURL& url, | |
227 PageTransition transition); | |
228 | |
229 // Calls OnMsgShouldCloseACK on the RenderViewHost with the given parameter. | |
230 void SendShouldCloseACK(bool proceed); | |
231 | 234 |
232 void TestOnMsgStartDragging(const WebDropData& drop_data); | 235 void TestOnMsgStartDragging(const WebDropData& drop_data); |
233 | 236 |
234 // If set, *delete_counter is incremented when this object destructs. | 237 // If set, *delete_counter is incremented when this object destructs. |
235 void set_delete_counter(int* delete_counter) { | 238 void set_delete_counter(int* delete_counter) { |
236 delete_counter_ = delete_counter; | 239 delete_counter_ = delete_counter; |
237 } | 240 } |
238 | 241 |
239 // Sets whether the RenderView currently exists or not. This controls the | 242 // Sets whether the RenderView currently exists or not. This controls the |
240 // return value from IsRenderViewLive, which the rest of the system uses to | 243 // return value from IsRenderViewLive, which the rest of the system uses to |
(...skipping 18 matching lines...) Expand all Loading... |
259 // filtering messages from the renderer. | 262 // filtering messages from the renderer. |
260 void set_is_swapped_out(bool is_swapped_out) { | 263 void set_is_swapped_out(bool is_swapped_out) { |
261 is_swapped_out_ = is_swapped_out; | 264 is_swapped_out_ = is_swapped_out; |
262 } | 265 } |
263 | 266 |
264 // If set, navigations will appear to have loaded through a proxy | 267 // If set, navigations will appear to have loaded through a proxy |
265 // (ViewHostMsg_FrameNavigte_Params::was_fetched_via_proxy). | 268 // (ViewHostMsg_FrameNavigte_Params::was_fetched_via_proxy). |
266 // False by default. | 269 // False by default. |
267 void set_simulate_fetch_via_proxy(bool proxy); | 270 void set_simulate_fetch_via_proxy(bool proxy); |
268 | 271 |
269 // If set, future loads will have |mime_type| set as the mime type. | |
270 // If not set, the mime type will default to "text/html". | |
271 void set_contents_mime_type(const std::string& mime_type); | |
272 | |
273 // RenderViewHost overrides -------------------------------------------------- | 272 // RenderViewHost overrides -------------------------------------------------- |
274 | 273 |
275 virtual bool CreateRenderView(const string16& frame_name, | 274 virtual bool CreateRenderView(const string16& frame_name, |
276 int32 max_page_id) OVERRIDE; | 275 int32 max_page_id) OVERRIDE; |
277 virtual bool IsRenderViewLive() const OVERRIDE; | 276 virtual bool IsRenderViewLive() const OVERRIDE; |
278 | 277 |
279 // This removes the need to expose | |
280 // RenderViewHostImpl::is_swapped_out() outside of content. | |
281 static bool IsRenderViewHostSwappedOut(RenderViewHost* rwh); | |
282 | |
283 // This removes the need to expose | |
284 // RenderViewHostImpl::set_send_accessibility_updated_notifications() | |
285 // outside of content. | |
286 static void EnableAccessibilityUpdatedNotifications(RenderViewHost* rwh); | |
287 | |
288 private: | 278 private: |
289 FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest, FilterNavigate); | 279 FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest, FilterNavigate); |
290 | 280 |
291 // Tracks if the caller thinks if it created the RenderView. This is so we can | 281 // Tracks if the caller thinks if it created the RenderView. This is so we can |
292 // respond to IsRenderViewLive appropriately. | 282 // respond to IsRenderViewLive appropriately. |
293 bool render_view_created_; | 283 bool render_view_created_; |
294 | 284 |
295 // See set_delete_counter() above. May be NULL. | 285 // See set_delete_counter() above. May be NULL. |
296 int* delete_counter_; | 286 int* delete_counter_; |
297 | 287 |
298 // See set_simulate_fetch_via_proxy() above. | 288 // See set_simulate_fetch_via_proxy() above. |
299 bool simulate_fetch_via_proxy_; | 289 bool simulate_fetch_via_proxy_; |
300 | 290 |
301 // See set_contents_mime_type() above. | 291 // See SetContentsMimeType() above. |
302 std::string contents_mime_type_; | 292 std::string contents_mime_type_; |
303 | 293 |
304 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHost); | 294 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHost); |
305 }; | 295 }; |
306 | 296 |
307 #if defined(COMPILER_MSVC) | 297 #if defined(COMPILER_MSVC) |
308 #pragma warning(pop) | 298 #pragma warning(pop) |
309 #endif | 299 #endif |
310 | 300 |
| 301 // Adds methods to get straight at the impl classes. |
| 302 class RenderViewHostImplTestHarness : public RenderViewHostTestHarness { |
| 303 public: |
| 304 RenderViewHostImplTestHarness(); |
| 305 virtual ~RenderViewHostImplTestHarness(); |
| 306 |
| 307 TestRenderViewHost* test_rvh(); |
| 308 TestRenderViewHost* pending_test_rvh(); |
| 309 TestRenderViewHost* active_test_rvh(); |
| 310 |
| 311 private: |
| 312 DISALLOW_COPY_AND_ASSIGN(RenderViewHostImplTestHarness); |
| 313 }; |
| 314 |
311 } // namespace content | 315 } // namespace content |
312 | 316 |
313 // TestRenderViewHostFactory --------------------------------------------------- | |
314 | |
315 // Manages creation of the RenderViewHosts using our special subclass. This | |
316 // automatically registers itself when it goes in scope, and unregisters itself | |
317 // when it goes out of scope. Since you can't have more than one factory | |
318 // registered at a time, you can only have one of these objects at a time. | |
319 class TestRenderViewHostFactory : public RenderViewHostFactory { | |
320 public: | |
321 explicit TestRenderViewHostFactory( | |
322 content::RenderProcessHostFactory* rph_factory); | |
323 virtual ~TestRenderViewHostFactory(); | |
324 | |
325 virtual void set_render_process_host_factory( | |
326 content::RenderProcessHostFactory* rph_factory); | |
327 virtual content::RenderViewHost* CreateRenderViewHost( | |
328 content::SiteInstance* instance, | |
329 content::RenderViewHostDelegate* delegate, | |
330 int routing_id, | |
331 content::SessionStorageNamespace* session_storage) OVERRIDE; | |
332 | |
333 private: | |
334 // This is a bit of a hack. With the current design of the site instances / | |
335 // browsing instances, it's difficult to pass a RenderProcessHostFactory | |
336 // around properly. | |
337 // | |
338 // Instead, we set it right before we create a new RenderViewHost, which | |
339 // happens before the RenderProcessHost is created. This way, the instance | |
340 // has the correct factory and creates our special RenderProcessHosts. | |
341 content::RenderProcessHostFactory* render_process_host_factory_; | |
342 | |
343 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHostFactory); | |
344 }; | |
345 | |
346 // RenderViewHostTestHarness --------------------------------------------------- | |
347 | |
348 class RenderViewHostTestHarness : public testing::Test { | |
349 public: | |
350 RenderViewHostTestHarness(); | |
351 virtual ~RenderViewHostTestHarness(); | |
352 | |
353 content::NavigationController& controller(); | |
354 virtual TestTabContents* contents(); | |
355 content::TestRenderViewHost* rvh(); | |
356 content::TestRenderViewHost* pending_rvh(); | |
357 content::TestRenderViewHost* active_rvh(); | |
358 content::BrowserContext* browser_context(); | |
359 MockRenderProcessHost* process(); | |
360 | |
361 // Frees the current tab contents for tests that want to test destruction. | |
362 void DeleteContents(); | |
363 | |
364 // Sets the current tab contents for tests that want to alter it. Takes | |
365 // ownership of the TestTabContents passed. | |
366 virtual void SetContents(TestTabContents* contents); | |
367 | |
368 // Creates a new TestTabContents. Ownership passes to the caller. | |
369 TestTabContents* CreateTestTabContents(); | |
370 | |
371 // Cover for |contents()->NavigateAndCommit(url)|. See | |
372 // TestTabContents::NavigateAndCommit for details. | |
373 void NavigateAndCommit(const GURL& url); | |
374 | |
375 // Simulates a reload of the current page. | |
376 void Reload(); | |
377 | |
378 protected: | |
379 // testing::Test | |
380 virtual void SetUp() OVERRIDE; | |
381 virtual void TearDown() OVERRIDE; | |
382 | |
383 // This browser context will be created in SetUp if it has not already been | |
384 // created. This allows tests to override the browser context if they so | |
385 // choose in their own SetUp function before calling the base class's (us) | |
386 // SetUp(). | |
387 scoped_ptr<content::BrowserContext> browser_context_; | |
388 | |
389 MessageLoopForUI message_loop_; | |
390 | |
391 MockRenderProcessHostFactory rph_factory_; | |
392 TestRenderViewHostFactory rvh_factory_; | |
393 | |
394 private: | |
395 scoped_ptr<TestTabContents> contents_; | |
396 #if defined(USE_AURA) | |
397 scoped_ptr<aura::RootWindow> root_window_; | |
398 scoped_ptr<aura::test::TestStackingClient> test_stacking_client_; | |
399 #endif | |
400 | |
401 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestHarness); | |
402 }; | |
403 | |
404 #endif // CONTENT_BROWSER_RENDERER_HOST_TEST_RENDER_VIEW_HOST_H_ | 317 #endif // CONTENT_BROWSER_RENDERER_HOST_TEST_RENDER_VIEW_HOST_H_ |
OLD | NEW |