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 // Gives tests access to RenderFrameHostImpl::OnCreateChild. The returned
70 // RenderFrameHost is owned by the parent RenderFrameHost.
71 virtual RenderFrameHost
* AppendChild(const std::string
& frame_name
) = 0;
73 // Calls OnDidCommitProvisionalLoad on the RenderFrameHost with the given
74 // information. Sets the rest of the parameters in the message to the
75 // "typical" values. This is a helper function for simulating the most common
77 virtual void SendNavigate(int page_id
, const GURL
& url
) = 0;
78 virtual void SendFailedNavigate(int page_id
, const GURL
& url
) = 0;
80 // Calls OnDidCommitProvisionalLoad on the RenderFrameHost with the given
81 // information, including a custom PageTransition. Sets the rest of the
82 // parameters in the message to the "typical" values. This is a helper
83 // function for simulating the most common types of loads.
84 virtual void SendNavigateWithTransition(int page_id
,
86 ui::PageTransition transition
) = 0;
88 // If set, future loads will have |mime_type| set as the mime type.
89 // If not set, the mime type will default to "text/html".
90 virtual void SetContentsMimeType(const std::string
& mime_type
) = 0;
92 // Calls OnBeforeUnloadACK on this RenderFrameHost with the given parameter.
93 virtual void SendBeforeUnloadACK(bool proceed
) = 0;
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;
100 // An interface and utility for driving tests of RenderViewHost.
101 class RenderViewHostTester
{
103 // Retrieves the RenderViewHostTester that drives the specified
104 // RenderViewHost. The RenderViewHost must have been created while
105 // RenderViewHost testing was enabled; use a
106 // RenderViewHostTestEnabler instance (see below) to do this.
107 static RenderViewHostTester
* For(RenderViewHost
* host
);
109 // Calls the RenderViewHosts' private OnMessageReceived function with the
111 static bool TestOnMessageReceived(RenderViewHost
* rvh
,
112 const IPC::Message
& msg
);
114 // Returns whether the underlying web-page has any touch-event handlers.
115 static bool HasTouchEventHandler(RenderViewHost
* rvh
);
117 virtual ~RenderViewHostTester() {}
119 // Gives tests access to RenderViewHostImpl::CreateRenderView.
120 virtual bool CreateRenderView(const base::string16
& frame_name
,
122 int proxy_routing_id
,
124 bool created_with_opener
) = 0;
126 // Makes the WasHidden/WasShown calls to the RenderWidget that
127 // tell it it has been hidden or restored from having been hidden.
128 virtual void SimulateWasHidden() = 0;
129 virtual void SimulateWasShown() = 0;
131 // Promote ComputeWebkitPrefs to public.
132 virtual WebPreferences
TestComputeWebkitPrefs() = 0;
135 // You can instantiate only one class like this at a time. During its
136 // lifetime, RenderViewHost and RenderFrameHost objects created may be used via
137 // RenderViewHostTester and RenderFrameHostTester respectively.
138 class RenderViewHostTestEnabler
{
140 RenderViewHostTestEnabler();
141 ~RenderViewHostTestEnabler();
144 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestEnabler
);
145 friend class RenderViewHostTestHarness
;
147 scoped_ptr
<MockRenderProcessHostFactory
> rph_factory_
;
148 scoped_ptr
<TestRenderViewHostFactory
> rvh_factory_
;
149 scoped_ptr
<TestRenderFrameHostFactory
> rfh_factory_
;
152 // RenderViewHostTestHarness ---------------------------------------------------
153 class RenderViewHostTestHarness
: public testing::Test
{
155 RenderViewHostTestHarness();
156 ~RenderViewHostTestHarness() override
;
158 NavigationController
& controller();
160 // The contents under test.
161 WebContents
* web_contents();
163 // RVH/RFH getters are shorthand for oft-used bits of web_contents().
165 // rvh() is equivalent to either of:
166 // web_contents()->GetMainFrame()->GetRenderViewHost()
167 // web_contents()->GetRenderViewHost()
168 RenderViewHost
* rvh();
170 // pending_rvh() is equivalent to:
171 // WebContentsTester::For(web_contents())->GetPendingRenderViewHost()
172 RenderViewHost
* pending_rvh();
174 // active_rvh() is equivalent to pending_rvh() ? pending_rvh() : rvh()
175 RenderViewHost
* active_rvh();
177 // main_rfh() is equivalent to web_contents()->GetMainFrame()
178 RenderFrameHost
* main_rfh();
180 // pending_main_rfh() is equivalent to:
181 // WebContentsTester::For(web_contents())->GetPendingMainFrame()
182 RenderFrameHost
* pending_main_rfh();
184 BrowserContext
* browser_context();
185 MockRenderProcessHost
* process();
187 // Frees the current WebContents for tests that want to test destruction.
188 void DeleteContents();
190 // Sets the current WebContents for tests that want to alter it. Takes
191 // ownership of the WebContents passed.
192 void SetContents(WebContents
* contents
);
194 // Creates a new test-enabled WebContents. Ownership passes to the
196 WebContents
* CreateTestWebContents();
198 // Cover for |contents()->NavigateAndCommit(url)|. See
199 // WebContentsTester::NavigateAndCommit for details.
200 void NavigateAndCommit(const GURL
& url
);
202 // Simulates a reload of the current page.
208 void SetUp() override
;
209 void TearDown() override
;
211 // Derived classes should override this method to use a custom BrowserContext.
212 // It is invoked by SetUp after threads were started.
213 // RenderViewHostTestHarness will take ownership of the returned
215 virtual BrowserContext
* CreateBrowserContext();
217 // Configures which TestBrowserThreads inside |thread_bundle| are backed by
218 // real threads. Must be called before SetUp().
219 void SetThreadBundleOptions(int options
) {
220 DCHECK(thread_bundle_
.get() == NULL
);
221 thread_bundle_options_
= options
;
224 TestBrowserThreadBundle
* thread_bundle() { return thread_bundle_
.get(); }
226 #if defined(USE_AURA)
227 aura::Window
* root_window() { return aura_test_helper_
->root_window(); }
230 // Replaces the RPH being used.
231 void SetRenderProcessHostFactory(RenderProcessHostFactory
* factory
);
234 scoped_ptr
<ContentBrowserSanityChecker
> sanity_checker_
;
236 scoped_ptr
<BrowserContext
> browser_context_
;
238 scoped_ptr
<WebContents
> contents_
;
240 scoped_ptr
<ui::ScopedOleInitializer
> ole_initializer_
;
242 #if defined(USE_AURA)
243 scoped_ptr
<aura::test::AuraTestHelper
> aura_test_helper_
;
245 RenderViewHostTestEnabler rvh_test_enabler_
;
247 int thread_bundle_options_
;
248 scoped_ptr
<TestBrowserThreadBundle
> thread_bundle_
;
250 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestHarness
);
253 } // namespace content
255 #endif // CONTENT_PUBLIC_TEST_TEST_RENDERER_HOST_H_