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 // Calls OnDidCommitProvisionalLoad on the RenderFrameHost with the given
79 // information with various sets of parameters. These are helper functions for
80 // simulating the most common types of loads.
82 // Guidance for calling these:
83 // - nav_entry_id should be 0 if simulating a renderer-initiated navigation;
84 // if simulating a browser-initiated one, pass the GetUniqueID() value of
85 // the NavigationController's PendingEntry.
86 // - did_create_new_entry should be true if simulating a navigation that
87 // created a new navigation entry; false for history navigations, reloads,
88 // and other navigations that don't affect the history list.
89 virtual void SendNavigate(int page_id
,
91 bool did_create_new_entry
,
93 virtual void SendFailedNavigate(int page_id
,
95 bool did_create_new_entry
,
97 virtual void SendNavigateWithTransition(int page_id
,
99 bool did_create_new_entry
,
101 ui::PageTransition transition
) = 0;
103 // If set, future loads will have |mime_type| set as the mime type.
104 // If not set, the mime type will default to "text/html".
105 virtual void SetContentsMimeType(const std::string
& mime_type
) = 0;
107 // Calls OnBeforeUnloadACK on this RenderFrameHost with the given parameter.
108 virtual void SendBeforeUnloadACK(bool proceed
) = 0;
110 // Simulates the SwapOut_ACK that fires if you commit a cross-site
111 // navigation without making any network requests.
112 virtual void SimulateSwapOutACK() = 0;
115 // An interface and utility for driving tests of RenderViewHost.
116 class RenderViewHostTester
{
118 // Retrieves the RenderViewHostTester that drives the specified
119 // RenderViewHost. The RenderViewHost must have been created while
120 // RenderViewHost testing was enabled; use a
121 // RenderViewHostTestEnabler instance (see below) to do this.
122 static RenderViewHostTester
* For(RenderViewHost
* host
);
124 // Calls the RenderViewHosts' private OnMessageReceived function with the
126 static bool TestOnMessageReceived(RenderViewHost
* rvh
,
127 const IPC::Message
& msg
);
129 // Returns whether the underlying web-page has any touch-event handlers.
130 static bool HasTouchEventHandler(RenderViewHost
* rvh
);
132 virtual ~RenderViewHostTester() {}
134 // Gives tests access to RenderViewHostImpl::CreateRenderView.
135 virtual bool CreateTestRenderView(
136 const base::string16
& frame_name
,
138 int proxy_routing_id
,
140 bool created_with_opener
) = 0;
142 // Makes the WasHidden/WasShown calls to the RenderWidget that
143 // tell it it has been hidden or restored from having been hidden.
144 virtual void SimulateWasHidden() = 0;
145 virtual void SimulateWasShown() = 0;
147 // Promote ComputeWebkitPrefs to public.
148 virtual WebPreferences
TestComputeWebkitPrefs() = 0;
151 // You can instantiate only one class like this at a time. During its
152 // lifetime, RenderViewHost and RenderFrameHost objects created may be used via
153 // RenderViewHostTester and RenderFrameHostTester respectively.
154 class RenderViewHostTestEnabler
{
156 RenderViewHostTestEnabler();
157 ~RenderViewHostTestEnabler();
160 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestEnabler
);
161 friend class RenderViewHostTestHarness
;
163 scoped_ptr
<MockRenderProcessHostFactory
> rph_factory_
;
164 scoped_ptr
<TestRenderViewHostFactory
> rvh_factory_
;
165 scoped_ptr
<TestRenderFrameHostFactory
> rfh_factory_
;
168 // RenderViewHostTestHarness ---------------------------------------------------
169 class RenderViewHostTestHarness
: public testing::Test
{
171 RenderViewHostTestHarness();
172 ~RenderViewHostTestHarness() override
;
174 NavigationController
& controller();
176 // The contents under test.
177 WebContents
* web_contents();
179 // RVH/RFH getters are shorthand for oft-used bits of web_contents().
181 // rvh() is equivalent to either of:
182 // web_contents()->GetMainFrame()->GetRenderViewHost()
183 // web_contents()->GetRenderViewHost()
184 RenderViewHost
* rvh();
186 // pending_rvh() is equivalent to:
187 // WebContentsTester::For(web_contents())->GetPendingRenderViewHost()
188 RenderViewHost
* pending_rvh();
190 // active_rvh() is equivalent to pending_rvh() ? pending_rvh() : rvh()
191 RenderViewHost
* active_rvh();
193 // main_rfh() is equivalent to web_contents()->GetMainFrame()
194 RenderFrameHost
* main_rfh();
196 // pending_main_rfh() is equivalent to:
197 // WebContentsTester::For(web_contents())->GetPendingMainFrame()
198 RenderFrameHost
* pending_main_rfh();
200 BrowserContext
* browser_context();
201 MockRenderProcessHost
* process();
203 // Frees the current WebContents for tests that want to test destruction.
204 void DeleteContents();
206 // Sets the current WebContents for tests that want to alter it. Takes
207 // ownership of the WebContents passed.
208 void SetContents(WebContents
* contents
);
210 // Creates a new test-enabled WebContents. Ownership passes to the
212 WebContents
* CreateTestWebContents();
214 // Cover for |contents()->NavigateAndCommit(url)|. See
215 // WebContentsTester::NavigateAndCommit for details.
216 void NavigateAndCommit(const GURL
& url
);
218 // Simulates a reload of the current page.
224 void SetUp() override
;
225 void TearDown() override
;
227 // Derived classes should override this method to use a custom BrowserContext.
228 // It is invoked by SetUp after threads were started.
229 // RenderViewHostTestHarness will take ownership of the returned
231 virtual BrowserContext
* CreateBrowserContext();
233 // Configures which TestBrowserThreads inside |thread_bundle| are backed by
234 // real threads. Must be called before SetUp().
235 void SetThreadBundleOptions(int options
) {
236 DCHECK(thread_bundle_
.get() == NULL
);
237 thread_bundle_options_
= options
;
240 TestBrowserThreadBundle
* thread_bundle() { return thread_bundle_
.get(); }
242 #if defined(USE_AURA)
243 aura::Window
* root_window() { return aura_test_helper_
->root_window(); }
246 // Replaces the RPH being used.
247 void SetRenderProcessHostFactory(RenderProcessHostFactory
* factory
);
250 int thread_bundle_options_
;
251 scoped_ptr
<TestBrowserThreadBundle
> thread_bundle_
;
253 scoped_ptr
<ContentBrowserSanityChecker
> sanity_checker_
;
255 scoped_ptr
<BrowserContext
> browser_context_
;
257 scoped_ptr
<WebContents
> contents_
;
259 scoped_ptr
<ui::ScopedOleInitializer
> ole_initializer_
;
261 #if defined(USE_AURA)
262 scoped_ptr
<aura::test::AuraTestHelper
> aura_test_helper_
;
264 RenderViewHostTestEnabler rvh_test_enabler_
;
266 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestHarness
);
269 } // namespace content
271 #endif // CONTENT_PUBLIC_TEST_TEST_RENDERER_HOST_H_