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.
5 #ifndef CONTENT_PUBLIC_TEST_TEST_RENDERER_HOST_H_
6 #define CONTENT_PUBLIC_TEST_TEST_RENDERER_HOST_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "content/public/browser/render_frame_host.h"
11 #include "content/public/browser/render_view_host.h"
12 #include "content/public/test/test_browser_thread_bundle.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/base/page_transition_types.h"
17 #include "ui/aura/test/aura_test_helper.h"
27 class ScopedOleInitializer
;
33 class ContentBrowserSanityChecker
;
34 class MockRenderProcessHost
;
35 class MockRenderProcessHostFactory
;
36 class NavigationController
;
37 class RenderProcessHostFactory
;
38 class RenderViewHostDelegate
;
39 class TestRenderFrameHostFactory
;
40 class TestRenderViewHostFactory
;
42 struct WebPreferences
;
44 // An interface and utility for driving tests of RenderFrameHost.
45 class RenderFrameHostTester
{
47 // Retrieves the RenderFrameHostTester that drives the specified
48 // RenderFrameHost. The RenderFrameHost must have been created while
49 // RenderFrameHost testing was enabled; use a
50 // RenderViewHostTestEnabler instance (see below) to do this.
51 static RenderFrameHostTester
* For(RenderFrameHost
* host
);
53 // If the given NavigationController has a pending main frame, returns it,
54 // otherwise NULL. This is an alternative to
55 // WebContentsTester::GetPendingMainFrame() when your WebContents was not
56 // created via a TestWebContents.
57 static RenderFrameHost
* GetPendingForController(
58 NavigationController
* controller
);
60 // This removes the need to expose
61 // RenderFrameHostImpl::is_swapped_out() outside of content.
63 // This is safe to call on any RenderFrameHost, not just ones
64 // constructed while a RenderViewHostTestEnabler is in play.
65 static bool IsRenderFrameHostSwappedOut(RenderFrameHost
* rfh
);
67 virtual ~RenderFrameHostTester() {}
69 // Simulates initialization of the RenderFrame object in the renderer process
70 // and ensures internal state of RenderFrameHost is ready for simulating
71 // RenderFrame originated IPCs.
72 virtual void InitializeRenderFrameIfNeeded() = 0;
74 // Gives tests access to RenderFrameHostImpl::OnCreateChild. The returned
75 // RenderFrameHost is owned by the parent RenderFrameHost.
76 virtual RenderFrameHost
* AppendChild(const std::string
& frame_name
) = 0;
78 // Simulates a renderer-initiated navigation to |url| starting in the
80 virtual void SimulateNavigationStart(const GURL
& url
) = 0;
82 // Simulates a redirect to |new_url| for the navigation in the
84 virtual void SimulateRedirect(const GURL
& new_url
) = 0;
86 // Simulates a navigation to |url| committing in the RenderFrameHost.
87 virtual void SimulateNavigationCommit(const GURL
& url
) = 0;
89 // Simulates a navigation to |url| failing with the error code |error_code|.
90 virtual void SimulateNavigationError(const GURL
& url
, int error_code
) = 0;
92 // Simulates the commit of an error page following a navigation failure.
93 virtual void SimulateNavigationErrorPageCommit() = 0;
95 // Simulates a navigation stopping in the RenderFrameHost.
96 virtual void SimulateNavigationStop() = 0;
98 // Calls OnDidCommitProvisionalLoad on the RenderFrameHost with the given
99 // information with various sets of parameters. These are helper functions for
100 // simulating the most common types of loads.
102 // Guidance for calling these:
103 // - nav_entry_id should be 0 if simulating a renderer-initiated navigation;
104 // if simulating a browser-initiated one, pass the GetUniqueID() value of
105 // the NavigationController's PendingEntry.
106 // - did_create_new_entry should be true if simulating a navigation that
107 // created a new navigation entry; false for history navigations, reloads,
108 // and other navigations that don't affect the history list.
109 virtual void SendNavigate(int page_id
,
111 bool did_create_new_entry
,
112 const GURL
& url
) = 0;
113 virtual void SendFailedNavigate(int page_id
,
115 bool did_create_new_entry
,
116 const GURL
& url
) = 0;
117 virtual void SendNavigateWithTransition(int page_id
,
119 bool did_create_new_entry
,
121 ui::PageTransition transition
) = 0;
123 // If set, future loads will have |mime_type| set as the mime type.
124 // If not set, the mime type will default to "text/html".
125 virtual void SetContentsMimeType(const std::string
& mime_type
) = 0;
127 // Calls OnBeforeUnloadACK on this RenderFrameHost with the given parameter.
128 virtual void SendBeforeUnloadACK(bool proceed
) = 0;
130 // Simulates the SwapOut_ACK that fires if you commit a cross-site
131 // navigation without making any network requests.
132 virtual void SimulateSwapOutACK() = 0;
135 // An interface and utility for driving tests of RenderViewHost.
136 class RenderViewHostTester
{
138 // Retrieves the RenderViewHostTester that drives the specified
139 // RenderViewHost. The RenderViewHost must have been created while
140 // RenderViewHost testing was enabled; use a
141 // RenderViewHostTestEnabler instance (see below) to do this.
142 static RenderViewHostTester
* For(RenderViewHost
* host
);
144 // Calls the RenderViewHosts' private OnMessageReceived function with the
146 static bool TestOnMessageReceived(RenderViewHost
* rvh
,
147 const IPC::Message
& msg
);
149 // Returns whether the underlying web-page has any touch-event handlers.
150 static bool HasTouchEventHandler(RenderViewHost
* rvh
);
152 virtual ~RenderViewHostTester() {}
154 // Gives tests access to RenderViewHostImpl::CreateRenderView.
155 virtual bool CreateTestRenderView(const base::string16
& frame_name
,
156 int opener_frame_route_id
,
157 int proxy_routing_id
,
159 bool created_with_opener
) = 0;
161 // Makes the WasHidden/WasShown calls to the RenderWidget that
162 // tell it it has been hidden or restored from having been hidden.
163 virtual void SimulateWasHidden() = 0;
164 virtual void SimulateWasShown() = 0;
166 // Promote ComputeWebkitPrefs to public.
167 virtual WebPreferences
TestComputeWebkitPrefs() = 0;
170 // You can instantiate only one class like this at a time. During its
171 // lifetime, RenderViewHost and RenderFrameHost objects created may be used via
172 // RenderViewHostTester and RenderFrameHostTester respectively.
173 class RenderViewHostTestEnabler
{
175 RenderViewHostTestEnabler();
176 ~RenderViewHostTestEnabler();
179 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestEnabler
);
180 friend class RenderViewHostTestHarness
;
182 scoped_ptr
<MockRenderProcessHostFactory
> rph_factory_
;
183 scoped_ptr
<TestRenderViewHostFactory
> rvh_factory_
;
184 scoped_ptr
<TestRenderFrameHostFactory
> rfh_factory_
;
187 // RenderViewHostTestHarness ---------------------------------------------------
188 class RenderViewHostTestHarness
: public testing::Test
{
190 RenderViewHostTestHarness();
191 ~RenderViewHostTestHarness() override
;
193 NavigationController
& controller();
195 // The contents under test.
196 WebContents
* web_contents();
198 // RVH/RFH getters are shorthand for oft-used bits of web_contents().
200 // rvh() is equivalent to either of:
201 // web_contents()->GetMainFrame()->GetRenderViewHost()
202 // web_contents()->GetRenderViewHost()
203 RenderViewHost
* rvh();
205 // pending_rvh() is equivalent to:
206 // WebContentsTester::For(web_contents())->GetPendingRenderViewHost()
207 RenderViewHost
* pending_rvh();
209 // active_rvh() is equivalent to pending_rvh() ? pending_rvh() : rvh()
210 RenderViewHost
* active_rvh();
212 // main_rfh() is equivalent to web_contents()->GetMainFrame()
213 RenderFrameHost
* main_rfh();
215 // pending_main_rfh() is equivalent to:
216 // WebContentsTester::For(web_contents())->GetPendingMainFrame()
217 RenderFrameHost
* pending_main_rfh();
219 BrowserContext
* browser_context();
220 MockRenderProcessHost
* process();
222 // Frees the current WebContents for tests that want to test destruction.
223 void DeleteContents();
225 // Sets the current WebContents for tests that want to alter it. Takes
226 // ownership of the WebContents passed.
227 void SetContents(WebContents
* contents
);
229 // Creates a new test-enabled WebContents. Ownership passes to the
231 WebContents
* CreateTestWebContents();
233 // Cover for |contents()->NavigateAndCommit(url)|. See
234 // WebContentsTester::NavigateAndCommit for details.
235 void NavigateAndCommit(const GURL
& url
);
237 // Simulates a reload of the current page.
243 void SetUp() override
;
244 void TearDown() override
;
246 // Derived classes should override this method to use a custom BrowserContext.
247 // It is invoked by SetUp after threads were started.
248 // RenderViewHostTestHarness will take ownership of the returned
250 virtual BrowserContext
* CreateBrowserContext();
252 // Configures which TestBrowserThreads inside |thread_bundle| are backed by
253 // real threads. Must be called before SetUp().
254 void SetThreadBundleOptions(int options
) {
255 DCHECK(thread_bundle_
.get() == NULL
);
256 thread_bundle_options_
= options
;
259 TestBrowserThreadBundle
* thread_bundle() { return thread_bundle_
.get(); }
261 #if defined(USE_AURA)
262 aura::Window
* root_window() { return aura_test_helper_
->root_window(); }
265 // Replaces the RPH being used.
266 void SetRenderProcessHostFactory(RenderProcessHostFactory
* factory
);
269 int thread_bundle_options_
;
270 scoped_ptr
<TestBrowserThreadBundle
> thread_bundle_
;
272 scoped_ptr
<ContentBrowserSanityChecker
> sanity_checker_
;
274 scoped_ptr
<BrowserContext
> browser_context_
;
276 scoped_ptr
<WebContents
> contents_
;
278 scoped_ptr
<ui::ScopedOleInitializer
> ole_initializer_
;
280 #if defined(USE_AURA)
281 scoped_ptr
<aura::test::AuraTestHelper
> aura_test_helper_
;
283 RenderViewHostTestEnabler rvh_test_enabler_
;
285 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestHarness
);
288 } // namespace content
290 #endif // CONTENT_PUBLIC_TEST_TEST_RENDERER_HOST_H_