test_renderer_host.h revision 9ab5563a3196760eb381d102cbb2bc0f7abc6a50
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_PUBLIC_TEST_TEST_RENDERER_HOST_H_
6#define CONTENT_PUBLIC_TEST_TEST_RENDERER_HOST_H_
7
8#include "base/memory/scoped_ptr.h"
9#include "base/message_loop/message_loop.h"
10#include "content/public/browser/render_view_host.h"
11#include "content/public/common/page_transition_types.h"
12#include "content/public/test/test_browser_thread_bundle.h"
13#include "testing/gtest/include/gtest/gtest.h"
14
15#if defined(USE_AURA)
16#include "ui/aura/test/aura_test_helper.h"
17#endif
18
19namespace aura {
20namespace test {
21class AuraTestHelper;
22}
23}
24
25namespace ui {
26class ScopedOleInitializer;
27}
28
29namespace content {
30
31class BrowserContext;
32class MockRenderProcessHost;
33class MockRenderProcessHostFactory;
34class NavigationController;
35class RenderProcessHostFactory;
36class RenderViewHostDelegate;
37class TestRenderViewHostFactory;
38class WebContents;
39
40// An interface and utility for driving tests of RenderViewHost.
41class RenderViewHostTester {
42 public:
43  // Retrieves the RenderViewHostTester that drives the specified
44  // RenderViewHost.  The RenderViewHost must have been created while
45  // RenderViewHost testing was enabled; use a
46  // RenderViewHostTestEnabler instance (see below) to do this.
47  static RenderViewHostTester* For(RenderViewHost* host);
48
49  // If the given WebContentsImpl has a pending RVH, returns it, otherwise NULL.
50  static RenderViewHost* GetPendingForController(
51      NavigationController* controller);
52
53  // This removes the need to expose
54  // RenderViewHostImpl::is_swapped_out() outside of content.
55  //
56  // This is safe to call on any RenderViewHost, not just ones
57  // constructed while a RenderViewHostTestEnabler is in play.
58  static bool IsRenderViewHostSwappedOut(RenderViewHost* rvh);
59
60  // Calls the RenderViewHosts' private OnMessageReceived function with the
61  // given message.
62  static bool TestOnMessageReceived(RenderViewHost* rvh,
63                                    const IPC::Message& msg);
64
65  // Returns whether the underlying web-page has any touch-event handlers.
66  static bool HasTouchEventHandler(RenderViewHost* rvh);
67
68  virtual ~RenderViewHostTester() {}
69
70  // Gives tests access to RenderViewHostImpl::CreateRenderView.
71  virtual bool CreateRenderView(const string16& frame_name,
72                                int opener_route_id,
73                                int32 max_page_id) = 0;
74
75  // Calls OnMsgNavigate on the RenderViewHost with the given information,
76  // setting the rest of the parameters in the message to the "typical" values.
77  // This is a helper function for simulating the most common types of loads.
78  virtual void SendNavigate(int page_id, const GURL& url) = 0;
79  virtual void SendFailedNavigate(int page_id, const GURL& url) = 0;
80
81  // Calls OnMsgNavigate on the RenderViewHost with the given information,
82  // including a custom PageTransition.  Sets the rest of the
83  // parameters in the message to the "typical" values. This is a helper
84  // function for simulating the most common types of loads.
85  virtual void SendNavigateWithTransition(int page_id, const GURL& url,
86                                          PageTransition transition) = 0;
87
88  // Calls OnMsgShouldCloseACK on the RenderViewHost with the given parameter.
89  virtual void SendShouldCloseACK(bool proceed) = 0;
90
91  // If set, future loads will have |mime_type| set as the mime type.
92  // If not set, the mime type will default to "text/html".
93  virtual void SetContentsMimeType(const std::string& mime_type) = 0;
94
95  // Simulates the SwapOut_ACK that fires if you commit a cross-site
96  // navigation without making any network requests.
97  virtual void SimulateSwapOutACK() = 0;
98
99  // Makes the WasHidden/WasShown calls to the RenderWidget that
100  // tell it it has been hidden or restored from having been hidden.
101  virtual void SimulateWasHidden() = 0;
102  virtual void SimulateWasShown() = 0;
103};
104
105// You can instantiate only one class like this at a time.  During its
106// lifetime, RenderViewHost objects created may be used via
107// RenderViewHostTester.
108class RenderViewHostTestEnabler {
109 public:
110  RenderViewHostTestEnabler();
111  ~RenderViewHostTestEnabler();
112
113 private:
114  DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestEnabler);
115  friend class RenderViewHostTestHarness;
116
117  scoped_ptr<MockRenderProcessHostFactory> rph_factory_;
118  scoped_ptr<TestRenderViewHostFactory> rvh_factory_;
119};
120
121// RenderViewHostTestHarness ---------------------------------------------------
122class RenderViewHostTestHarness : public testing::Test {
123 public:
124  RenderViewHostTestHarness();
125  virtual ~RenderViewHostTestHarness();
126
127  NavigationController& controller();
128  WebContents* web_contents();
129  RenderViewHost* rvh();
130  RenderViewHost* pending_rvh();
131  RenderViewHost* active_rvh();
132  BrowserContext* browser_context();
133  MockRenderProcessHost* process();
134
135  // Frees the current WebContents for tests that want to test destruction.
136  void DeleteContents();
137
138  // Sets the current WebContents for tests that want to alter it. Takes
139  // ownership of the WebContents passed.
140  void SetContents(WebContents* contents);
141
142  // Creates a new test-enabled WebContents. Ownership passes to the
143  // caller.
144  WebContents* CreateTestWebContents();
145
146  // Cover for |contents()->NavigateAndCommit(url)|. See
147  // WebContentsTester::NavigateAndCommit for details.
148  void NavigateAndCommit(const GURL& url);
149
150  // Simulates a reload of the current page.
151  void Reload();
152  void FailedReload();
153
154 protected:
155  // testing::Test
156  virtual void SetUp() OVERRIDE;
157  virtual void TearDown() OVERRIDE;
158
159  // Derived classes should override this method to use a custom BrowserContext.
160  // It is invoked by SetUp after threads were started.
161  // RenderViewHostTestHarness will take ownership of the returned
162  // BrowserContext.
163  virtual BrowserContext* CreateBrowserContext();
164
165  // Configures which TestBrowserThreads inside |thread_bundle| are backed by
166  // real threads. Must be called before SetUp().
167  void SetThreadBundleOptions(int options) {
168    DCHECK(thread_bundle_.get() == NULL);
169    thread_bundle_options_ = options;
170  }
171
172  TestBrowserThreadBundle* thread_bundle() { return thread_bundle_.get(); }
173
174#if defined(USE_AURA)
175  aura::RootWindow* root_window() { return aura_test_helper_->root_window(); }
176#endif
177
178  // Replaces the RPH being used.
179  void SetRenderProcessHostFactory(RenderProcessHostFactory* factory);
180
181 private:
182  scoped_ptr<BrowserContext> browser_context_;
183
184  // It is important not to use this directly in the implementation as
185  // web_contents() and SetContents() are virtual and may be
186  // overridden by subclasses.
187  scoped_ptr<WebContents> contents_;
188#if defined(OS_WIN)
189  scoped_ptr<ui::ScopedOleInitializer> ole_initializer_;
190#endif
191#if defined(USE_AURA)
192  scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_;
193#endif
194  RenderViewHostTestEnabler rvh_test_enabler_;
195
196  int thread_bundle_options_;
197  scoped_ptr<TestBrowserThreadBundle> thread_bundle_;
198
199  DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestHarness);
200};
201
202}  // namespace content
203
204#endif  // CONTENT_PUBLIC_TEST_TEST_RENDERER_HOST_H_
205