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 MockRenderProcessHost;
34 class MockRenderProcessHostFactory;
35 class NavigationController;
36 class RenderProcessHostFactory;
37 class RenderViewHostDelegate;
38 class TestRenderFrameHostFactory;
39 class TestRenderViewHostFactory;
42 // An interface and utility for driving tests of RenderFrameHost.
43 class RenderFrameHostTester {
45 // Retrieves the RenderFrameHostTester that drives the specified
46 // RenderFrameHost. The RenderFrameHost must have been created while
47 // RenderFrameHost testing was enabled; use a
48 // RenderViewHostTestEnabler instance (see below) to do this.
49 static RenderFrameHostTester* For(RenderFrameHost* host);
51 // If the given NavigationController has a pending main frame, returns it,
52 // otherwise NULL. This is an alternative to
53 // WebContentsTester::GetPendingMainFrame() when your WebContents was not
54 // created via a TestWebContents.
55 static RenderFrameHost* GetPendingForController(
56 NavigationController* controller);
58 // This removes the need to expose
59 // RenderFrameHostImpl::is_swapped_out() outside of content.
61 // This is safe to call on any RenderFrameHost, not just ones
62 // constructed while a RenderViewHostTestEnabler is in play.
63 static bool IsRenderFrameHostSwappedOut(RenderFrameHost* rfh);
65 virtual ~RenderFrameHostTester() {}
67 // Gives tests access to RenderFrameHostImpl::OnCreateChild. The returned
68 // RenderFrameHost is owned by the parent RenderFrameHost.
69 virtual RenderFrameHost* AppendChild(const std::string& frame_name) = 0;
71 // Calls OnDidCommitProvisionalLoad on the RenderFrameHost with the given
72 // information. Sets the rest of the parameters in the message to the
73 // "typical" values. This is a helper function for simulating the most common
75 virtual void SendNavigate(int page_id, const GURL& url) = 0;
76 virtual void SendFailedNavigate(int page_id, const GURL& url) = 0;
78 // Calls OnDidCommitProvisionalLoad on the RenderFrameHost with the given
79 // information, including a custom PageTransition. Sets the rest of the
80 // parameters in the message to the "typical" values. This is a helper
81 // function for simulating the most common types of loads.
82 virtual void SendNavigateWithTransition(int page_id,
84 ui::PageTransition transition) = 0;
86 // If set, future loads will have |mime_type| set as the mime type.
87 // If not set, the mime type will default to "text/html".
88 virtual void SetContentsMimeType(const std::string& mime_type) = 0;
90 // Calls OnBeforeUnloadACK on this RenderFrameHost with the given parameter.
91 virtual void SendBeforeUnloadACK(bool proceed) = 0;
93 // Simulates the SwapOut_ACK that fires if you commit a cross-site
94 // navigation without making any network requests.
95 virtual void SimulateSwapOutACK() = 0;
98 // An interface and utility for driving tests of RenderViewHost.
99 class RenderViewHostTester {
101 // Retrieves the RenderViewHostTester that drives the specified
102 // RenderViewHost. The RenderViewHost must have been created while
103 // RenderViewHost testing was enabled; use a
104 // RenderViewHostTestEnabler instance (see below) to do this.
105 static RenderViewHostTester* For(RenderViewHost* host);
107 // Calls the RenderViewHosts' private OnMessageReceived function with the
109 static bool TestOnMessageReceived(RenderViewHost* rvh,
110 const IPC::Message& msg);
112 // Returns whether the underlying web-page has any touch-event handlers.
113 static bool HasTouchEventHandler(RenderViewHost* rvh);
115 virtual ~RenderViewHostTester() {}
117 // Gives tests access to RenderViewHostImpl::CreateRenderView.
118 virtual bool CreateRenderView(const base::string16& frame_name,
120 int proxy_routing_id,
122 bool created_with_opener) = 0;
124 // Makes the WasHidden/WasShown calls to the RenderWidget that
125 // tell it it has been hidden or restored from having been hidden.
126 virtual void SimulateWasHidden() = 0;
127 virtual void SimulateWasShown() = 0;
130 // You can instantiate only one class like this at a time. During its
131 // lifetime, RenderViewHost and RenderFrameHost objects created may be used via
132 // RenderViewHostTester and RenderFrameHostTester respectively.
133 class RenderViewHostTestEnabler {
135 RenderViewHostTestEnabler();
136 ~RenderViewHostTestEnabler();
139 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestEnabler);
140 friend class RenderViewHostTestHarness;
142 scoped_ptr<MockRenderProcessHostFactory> rph_factory_;
143 scoped_ptr<TestRenderViewHostFactory> rvh_factory_;
144 scoped_ptr<TestRenderFrameHostFactory> rfh_factory_;
147 // RenderViewHostTestHarness ---------------------------------------------------
148 class RenderViewHostTestHarness : public testing::Test {
150 RenderViewHostTestHarness();
151 ~RenderViewHostTestHarness() override;
153 NavigationController& controller();
155 // The contents under test.
156 WebContents* web_contents();
158 // RVH/RFH getters are shorthand for oft-used bits of web_contents().
160 // rvh() is equivalent to either of:
161 // web_contents()->GetMainFrame()->GetRenderViewHost()
162 // web_contents()->GetRenderViewHost()
163 RenderViewHost* rvh();
165 // pending_rvh() is equivalent to:
166 // WebContentsTester::For(web_contents())->GetPendingRenderViewHost()
167 RenderViewHost* pending_rvh();
169 // active_rvh() is equivalent to pending_rvh() ? pending_rvh() : rvh()
170 RenderViewHost* active_rvh();
172 // main_rfh() is equivalent to web_contents()->GetMainFrame()
173 RenderFrameHost* main_rfh();
175 // pending_main_rfh() is equivalent to:
176 // WebContentsTester::For(web_contents())->GetPendingMainFrame()
177 RenderFrameHost* pending_main_rfh();
179 BrowserContext* browser_context();
180 MockRenderProcessHost* process();
182 // Frees the current WebContents for tests that want to test destruction.
183 void DeleteContents();
185 // Sets the current WebContents for tests that want to alter it. Takes
186 // ownership of the WebContents passed.
187 void SetContents(WebContents* contents);
189 // Creates a new test-enabled WebContents. Ownership passes to the
191 WebContents* CreateTestWebContents();
193 // Cover for |contents()->NavigateAndCommit(url)|. See
194 // WebContentsTester::NavigateAndCommit for details.
195 void NavigateAndCommit(const GURL& url);
197 // Simulates a reload of the current page.
203 void SetUp() override;
204 void TearDown() override;
206 // Derived classes should override this method to use a custom BrowserContext.
207 // It is invoked by SetUp after threads were started.
208 // RenderViewHostTestHarness will take ownership of the returned
210 virtual BrowserContext* CreateBrowserContext();
212 // Configures which TestBrowserThreads inside |thread_bundle| are backed by
213 // real threads. Must be called before SetUp().
214 void SetThreadBundleOptions(int options) {
215 DCHECK(thread_bundle_.get() == NULL);
216 thread_bundle_options_ = options;
219 TestBrowserThreadBundle* thread_bundle() { return thread_bundle_.get(); }
221 #if defined(USE_AURA)
222 aura::Window* root_window() { return aura_test_helper_->root_window(); }
225 // Replaces the RPH being used.
226 void SetRenderProcessHostFactory(RenderProcessHostFactory* factory);
229 scoped_ptr<BrowserContext> browser_context_;
231 scoped_ptr<WebContents> contents_;
233 scoped_ptr<ui::ScopedOleInitializer> ole_initializer_;
235 #if defined(USE_AURA)
236 scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_;
238 RenderViewHostTestEnabler rvh_test_enabler_;
240 int thread_bundle_options_;
241 scoped_ptr<TestBrowserThreadBundle> thread_bundle_;
243 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestHarness);
246 } // namespace content
248 #endif // CONTENT_PUBLIC_TEST_TEST_RENDERER_HOST_H_