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
;
43 // An interface and utility for driving tests of RenderFrameHost.
44 class RenderFrameHostTester
{
46 // Retrieves the RenderFrameHostTester that drives the specified
47 // RenderFrameHost. The RenderFrameHost must have been created while
48 // RenderFrameHost testing was enabled; use a
49 // RenderViewHostTestEnabler instance (see below) to do this.
50 static RenderFrameHostTester
* For(RenderFrameHost
* host
);
52 // If the given NavigationController has a pending main frame, returns it,
53 // otherwise NULL. This is an alternative to
54 // WebContentsTester::GetPendingMainFrame() when your WebContents was not
55 // created via a TestWebContents.
56 static RenderFrameHost
* GetPendingForController(
57 NavigationController
* controller
);
59 // This removes the need to expose
60 // RenderFrameHostImpl::is_swapped_out() outside of content.
62 // This is safe to call on any RenderFrameHost, not just ones
63 // constructed while a RenderViewHostTestEnabler is in play.
64 static bool IsRenderFrameHostSwappedOut(RenderFrameHost
* rfh
);
66 virtual ~RenderFrameHostTester() {}
68 // Gives tests access to RenderFrameHostImpl::OnCreateChild. The returned
69 // RenderFrameHost is owned by the parent RenderFrameHost.
70 virtual RenderFrameHost
* AppendChild(const std::string
& frame_name
) = 0;
72 // Calls OnDidCommitProvisionalLoad on the RenderFrameHost with the given
73 // information. Sets the rest of the parameters in the message to the
74 // "typical" values. This is a helper function for simulating the most common
76 virtual void SendNavigate(int page_id
, const GURL
& url
) = 0;
77 virtual void SendFailedNavigate(int page_id
, const GURL
& url
) = 0;
79 // Calls OnDidCommitProvisionalLoad on the RenderFrameHost with the given
80 // information, including a custom PageTransition. Sets the rest of the
81 // parameters in the message to the "typical" values. This is a helper
82 // function for simulating the most common types of loads.
83 virtual void SendNavigateWithTransition(int page_id
,
85 ui::PageTransition transition
) = 0;
87 // If set, future loads will have |mime_type| set as the mime type.
88 // If not set, the mime type will default to "text/html".
89 virtual void SetContentsMimeType(const std::string
& mime_type
) = 0;
91 // Calls OnBeforeUnloadACK on this RenderFrameHost with the given parameter.
92 virtual void SendBeforeUnloadACK(bool proceed
) = 0;
94 // Simulates the SwapOut_ACK that fires if you commit a cross-site
95 // navigation without making any network requests.
96 virtual void SimulateSwapOutACK() = 0;
99 // An interface and utility for driving tests of RenderViewHost.
100 class RenderViewHostTester
{
102 // Retrieves the RenderViewHostTester that drives the specified
103 // RenderViewHost. The RenderViewHost must have been created while
104 // RenderViewHost testing was enabled; use a
105 // RenderViewHostTestEnabler instance (see below) to do this.
106 static RenderViewHostTester
* For(RenderViewHost
* host
);
108 // Calls the RenderViewHosts' private OnMessageReceived function with the
110 static bool TestOnMessageReceived(RenderViewHost
* rvh
,
111 const IPC::Message
& msg
);
113 // Returns whether the underlying web-page has any touch-event handlers.
114 static bool HasTouchEventHandler(RenderViewHost
* rvh
);
116 virtual ~RenderViewHostTester() {}
118 // Gives tests access to RenderViewHostImpl::CreateRenderView.
119 virtual bool CreateRenderView(const base::string16
& frame_name
,
121 int proxy_routing_id
,
123 bool created_with_opener
) = 0;
125 // Makes the WasHidden/WasShown calls to the RenderWidget that
126 // tell it it has been hidden or restored from having been hidden.
127 virtual void SimulateWasHidden() = 0;
128 virtual void SimulateWasShown() = 0;
131 // You can instantiate only one class like this at a time. During its
132 // lifetime, RenderViewHost and RenderFrameHost objects created may be used via
133 // RenderViewHostTester and RenderFrameHostTester respectively.
134 class RenderViewHostTestEnabler
{
136 RenderViewHostTestEnabler();
137 ~RenderViewHostTestEnabler();
140 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestEnabler
);
141 friend class RenderViewHostTestHarness
;
143 scoped_ptr
<MockRenderProcessHostFactory
> rph_factory_
;
144 scoped_ptr
<TestRenderViewHostFactory
> rvh_factory_
;
145 scoped_ptr
<TestRenderFrameHostFactory
> rfh_factory_
;
148 // RenderViewHostTestHarness ---------------------------------------------------
149 class RenderViewHostTestHarness
: public testing::Test
{
151 RenderViewHostTestHarness();
152 ~RenderViewHostTestHarness() override
;
154 NavigationController
& controller();
156 // The contents under test.
157 WebContents
* web_contents();
159 // RVH/RFH getters are shorthand for oft-used bits of web_contents().
161 // rvh() is equivalent to either of:
162 // web_contents()->GetMainFrame()->GetRenderViewHost()
163 // web_contents()->GetRenderViewHost()
164 RenderViewHost
* rvh();
166 // pending_rvh() is equivalent to:
167 // WebContentsTester::For(web_contents())->GetPendingRenderViewHost()
168 RenderViewHost
* pending_rvh();
170 // active_rvh() is equivalent to pending_rvh() ? pending_rvh() : rvh()
171 RenderViewHost
* active_rvh();
173 // main_rfh() is equivalent to web_contents()->GetMainFrame()
174 RenderFrameHost
* main_rfh();
176 // pending_main_rfh() is equivalent to:
177 // WebContentsTester::For(web_contents())->GetPendingMainFrame()
178 RenderFrameHost
* pending_main_rfh();
180 BrowserContext
* browser_context();
181 MockRenderProcessHost
* process();
183 // Frees the current WebContents for tests that want to test destruction.
184 void DeleteContents();
186 // Sets the current WebContents for tests that want to alter it. Takes
187 // ownership of the WebContents passed.
188 void SetContents(WebContents
* contents
);
190 // Creates a new test-enabled WebContents. Ownership passes to the
192 WebContents
* CreateTestWebContents();
194 // Cover for |contents()->NavigateAndCommit(url)|. See
195 // WebContentsTester::NavigateAndCommit for details.
196 void NavigateAndCommit(const GURL
& url
);
198 // Simulates a reload of the current page.
204 void SetUp() override
;
205 void TearDown() override
;
207 // Derived classes should override this method to use a custom BrowserContext.
208 // It is invoked by SetUp after threads were started.
209 // RenderViewHostTestHarness will take ownership of the returned
211 virtual BrowserContext
* CreateBrowserContext();
213 // Configures which TestBrowserThreads inside |thread_bundle| are backed by
214 // real threads. Must be called before SetUp().
215 void SetThreadBundleOptions(int options
) {
216 DCHECK(thread_bundle_
.get() == NULL
);
217 thread_bundle_options_
= options
;
220 TestBrowserThreadBundle
* thread_bundle() { return thread_bundle_
.get(); }
222 #if defined(USE_AURA)
223 aura::Window
* root_window() { return aura_test_helper_
->root_window(); }
226 // Replaces the RPH being used.
227 void SetRenderProcessHostFactory(RenderProcessHostFactory
* factory
);
230 scoped_ptr
<ContentBrowserSanityChecker
> sanity_checker_
;
232 scoped_ptr
<BrowserContext
> browser_context_
;
234 scoped_ptr
<WebContents
> contents_
;
236 scoped_ptr
<ui::ScopedOleInitializer
> ole_initializer_
;
238 #if defined(USE_AURA)
239 scoped_ptr
<aura::test::AuraTestHelper
> aura_test_helper_
;
241 RenderViewHostTestEnabler rvh_test_enabler_
;
243 int thread_bundle_options_
;
244 scoped_ptr
<TestBrowserThreadBundle
> thread_bundle_
;
246 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestHarness
);
249 } // namespace content
251 #endif // CONTENT_PUBLIC_TEST_TEST_RENDERER_HOST_H_