Move browser-to-renderer opener plumbing to frame routing IDs.
[chromium-blink-merge.git] / content / public / test / test_renderer_host.h
blobc429876fc5b3e182b9655612a5ad173e6d60191b
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"
16 #if defined(USE_AURA)
17 #include "ui/aura/test/aura_test_helper.h"
18 #endif
20 namespace aura {
21 namespace test {
22 class AuraTestHelper;
26 namespace ui {
27 class ScopedOleInitializer;
30 namespace content {
32 class BrowserContext;
33 class ContentBrowserSanityChecker;
34 class MockRenderProcessHost;
35 class MockRenderProcessHostFactory;
36 class NavigationController;
37 class RenderProcessHostFactory;
38 class RenderViewHostDelegate;
39 class TestRenderFrameHostFactory;
40 class TestRenderViewHostFactory;
41 class WebContents;
42 struct WebPreferences;
44 // An interface and utility for driving tests of RenderFrameHost.
45 class RenderFrameHostTester {
46 public:
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,
90 int nav_entry_id,
91 bool did_create_new_entry,
92 const GURL& url) = 0;
93 virtual void SendFailedNavigate(int page_id,
94 int nav_entry_id,
95 bool did_create_new_entry,
96 const GURL& url) = 0;
97 virtual void SendNavigateWithTransition(int page_id,
98 int nav_entry_id,
99 bool did_create_new_entry,
100 const GURL& url,
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 {
117 public:
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
125 // given message.
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(const base::string16& frame_name,
136 int opener_frame_route_id,
137 int proxy_routing_id,
138 int32 max_page_id,
139 bool created_with_opener) = 0;
141 // Makes the WasHidden/WasShown calls to the RenderWidget that
142 // tell it it has been hidden or restored from having been hidden.
143 virtual void SimulateWasHidden() = 0;
144 virtual void SimulateWasShown() = 0;
146 // Promote ComputeWebkitPrefs to public.
147 virtual WebPreferences TestComputeWebkitPrefs() = 0;
150 // You can instantiate only one class like this at a time. During its
151 // lifetime, RenderViewHost and RenderFrameHost objects created may be used via
152 // RenderViewHostTester and RenderFrameHostTester respectively.
153 class RenderViewHostTestEnabler {
154 public:
155 RenderViewHostTestEnabler();
156 ~RenderViewHostTestEnabler();
158 private:
159 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestEnabler);
160 friend class RenderViewHostTestHarness;
162 scoped_ptr<MockRenderProcessHostFactory> rph_factory_;
163 scoped_ptr<TestRenderViewHostFactory> rvh_factory_;
164 scoped_ptr<TestRenderFrameHostFactory> rfh_factory_;
167 // RenderViewHostTestHarness ---------------------------------------------------
168 class RenderViewHostTestHarness : public testing::Test {
169 public:
170 RenderViewHostTestHarness();
171 ~RenderViewHostTestHarness() override;
173 NavigationController& controller();
175 // The contents under test.
176 WebContents* web_contents();
178 // RVH/RFH getters are shorthand for oft-used bits of web_contents().
180 // rvh() is equivalent to either of:
181 // web_contents()->GetMainFrame()->GetRenderViewHost()
182 // web_contents()->GetRenderViewHost()
183 RenderViewHost* rvh();
185 // pending_rvh() is equivalent to:
186 // WebContentsTester::For(web_contents())->GetPendingRenderViewHost()
187 RenderViewHost* pending_rvh();
189 // active_rvh() is equivalent to pending_rvh() ? pending_rvh() : rvh()
190 RenderViewHost* active_rvh();
192 // main_rfh() is equivalent to web_contents()->GetMainFrame()
193 RenderFrameHost* main_rfh();
195 // pending_main_rfh() is equivalent to:
196 // WebContentsTester::For(web_contents())->GetPendingMainFrame()
197 RenderFrameHost* pending_main_rfh();
199 BrowserContext* browser_context();
200 MockRenderProcessHost* process();
202 // Frees the current WebContents for tests that want to test destruction.
203 void DeleteContents();
205 // Sets the current WebContents for tests that want to alter it. Takes
206 // ownership of the WebContents passed.
207 void SetContents(WebContents* contents);
209 // Creates a new test-enabled WebContents. Ownership passes to the
210 // caller.
211 WebContents* CreateTestWebContents();
213 // Cover for |contents()->NavigateAndCommit(url)|. See
214 // WebContentsTester::NavigateAndCommit for details.
215 void NavigateAndCommit(const GURL& url);
217 // Simulates a reload of the current page.
218 void Reload();
219 void FailedReload();
221 protected:
222 // testing::Test
223 void SetUp() override;
224 void TearDown() override;
226 // Derived classes should override this method to use a custom BrowserContext.
227 // It is invoked by SetUp after threads were started.
228 // RenderViewHostTestHarness will take ownership of the returned
229 // BrowserContext.
230 virtual BrowserContext* CreateBrowserContext();
232 // Configures which TestBrowserThreads inside |thread_bundle| are backed by
233 // real threads. Must be called before SetUp().
234 void SetThreadBundleOptions(int options) {
235 DCHECK(thread_bundle_.get() == NULL);
236 thread_bundle_options_ = options;
239 TestBrowserThreadBundle* thread_bundle() { return thread_bundle_.get(); }
241 #if defined(USE_AURA)
242 aura::Window* root_window() { return aura_test_helper_->root_window(); }
243 #endif
245 // Replaces the RPH being used.
246 void SetRenderProcessHostFactory(RenderProcessHostFactory* factory);
248 private:
249 int thread_bundle_options_;
250 scoped_ptr<TestBrowserThreadBundle> thread_bundle_;
252 scoped_ptr<ContentBrowserSanityChecker> sanity_checker_;
254 scoped_ptr<BrowserContext> browser_context_;
256 scoped_ptr<WebContents> contents_;
257 #if defined(OS_WIN)
258 scoped_ptr<ui::ScopedOleInitializer> ole_initializer_;
259 #endif
260 #if defined(USE_AURA)
261 scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_;
262 #endif
263 RenderViewHostTestEnabler rvh_test_enabler_;
265 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestHarness);
268 } // namespace content
270 #endif // CONTENT_PUBLIC_TEST_TEST_RENDERER_HOST_H_