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 #include "content/test/test_web_contents.h"
9 #include "base/command_line.h"
10 #include "content/browser/browser_url_handler_impl.h"
11 #include "content/browser/frame_host/cross_process_frame_connector.h"
12 #include "content/browser/frame_host/navigation_entry_impl.h"
13 #include "content/browser/frame_host/navigator.h"
14 #include "content/browser/frame_host/navigator_impl.h"
15 #include "content/browser/renderer_host/render_view_host_impl.h"
16 #include "content/browser/site_instance_impl.h"
17 #include "content/common/frame_messages.h"
18 #include "content/common/view_messages.h"
19 #include "content/public/browser/notification_registrar.h"
20 #include "content/public/browser/notification_source.h"
21 #include "content/public/browser/notification_types.h"
22 #include "content/public/common/content_switches.h"
23 #include "content/public/common/page_state.h"
24 #include "content/public/test/mock_render_process_host.h"
25 #include "content/test/test_render_view_host.h"
26 #include "ui/base/page_transition_types.h"
30 TestWebContents::TestWebContents(BrowserContext
* browser_context
)
31 : WebContentsImpl(browser_context
),
32 delegate_view_override_(NULL
),
33 expect_set_history_offset_and_length_(false),
34 expect_set_history_offset_and_length_history_length_(0) {
37 TestWebContents
* TestWebContents::Create(BrowserContext
* browser_context
,
38 SiteInstance
* instance
) {
39 TestWebContents
* test_web_contents
= new TestWebContents(browser_context
);
40 test_web_contents
->Init(WebContents::CreateParams(browser_context
, instance
));
41 return test_web_contents
;
44 TestWebContents::~TestWebContents() {
45 EXPECT_FALSE(expect_set_history_offset_and_length_
);
48 TestRenderFrameHost
* TestWebContents::GetMainFrame() {
49 return static_cast<TestRenderFrameHost
*>(WebContentsImpl::GetMainFrame());
52 TestRenderViewHost
* TestWebContents::GetRenderViewHost() const {
53 return static_cast<TestRenderViewHost
*>(
54 WebContentsImpl::GetRenderViewHost());
57 TestRenderFrameHost
* TestWebContents::GetPendingMainFrame() const {
58 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
59 switches::kEnableBrowserSideNavigation
)) {
60 return static_cast<TestRenderFrameHost
*>(
61 GetRenderManager()->speculative_render_frame_host_
.get());
63 return static_cast<TestRenderFrameHost
*>(
64 GetRenderManager()->pending_frame_host());
67 void TestWebContents::TestDidNavigate(RenderFrameHost
* render_frame_host
,
70 bool did_create_new_entry
,
72 ui::PageTransition transition
) {
73 TestDidNavigateWithReferrer(render_frame_host
,
82 void TestWebContents::TestDidNavigateWithReferrer(
83 RenderFrameHost
* render_frame_host
,
86 bool did_create_new_entry
,
88 const Referrer
& referrer
,
89 ui::PageTransition transition
) {
90 FrameHostMsg_DidCommitProvisionalLoad_Params params
;
92 params
.page_id
= page_id
;
93 params
.nav_entry_id
= nav_entry_id
;
95 params
.referrer
= referrer
;
96 params
.transition
= transition
;
97 params
.redirects
= std::vector
<GURL
>();
98 params
.should_update_history
= false;
99 params
.searchable_form_url
= GURL();
100 params
.searchable_form_encoding
= std::string();
101 params
.did_create_new_entry
= did_create_new_entry
;
102 params
.security_info
= std::string();
103 params
.gesture
= NavigationGestureUser
;
104 params
.was_within_same_page
= false;
105 params
.is_post
= false;
106 params
.page_state
= PageState::CreateFromURL(url
);
108 TestRenderFrameHost
* rfh
=
109 static_cast<TestRenderFrameHost
*>(render_frame_host
);
110 rfh
->InitializeRenderFrameIfNeeded();
111 rfh
->frame_tree_node()->navigator()->DidNavigate(rfh
, params
);
114 const std::string
& TestWebContents::GetSaveFrameHeaders() {
115 return save_frame_headers_
;
118 bool TestWebContents::CrossProcessNavigationPending() {
119 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
120 switches::kEnableBrowserSideNavigation
)) {
121 return GetRenderManager()->speculative_render_frame_host_
&&
122 static_cast<TestRenderFrameHost
*>(
123 GetRenderManager()->speculative_render_frame_host_
.get())
126 return GetRenderManager()->pending_frame_host() != nullptr;
129 bool TestWebContents::CreateRenderViewForRenderManager(
130 RenderViewHost
* render_view_host
,
132 int proxy_routing_id
,
133 const FrameReplicationState
& replicated_frame_state
,
134 bool for_main_frame
) {
135 UpdateMaxPageIDIfNecessary(render_view_host
);
136 // This will go to a TestRenderViewHost.
137 static_cast<RenderViewHostImpl
*>(
138 render_view_host
)->CreateRenderView(base::string16(),
142 replicated_frame_state
,
147 WebContents
* TestWebContents::Clone() {
148 WebContentsImpl
* contents
=
149 Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
150 contents
->GetController().CopyStateFrom(controller_
);
154 void TestWebContents::NavigateAndCommit(const GURL
& url
) {
155 GetController().LoadURL(
156 url
, Referrer(), ui::PAGE_TRANSITION_LINK
, std::string());
157 GURL
loaded_url(url
);
158 bool reverse_on_redirect
= false;
159 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
160 &loaded_url
, GetBrowserContext(), &reverse_on_redirect
);
162 // LoadURL created a navigation entry, now simulate the RenderView sending
163 // a notification that it actually navigated.
164 CommitPendingNavigation();
167 void TestWebContents::TestSetIsLoading(bool value
) {
168 SetIsLoading(value
, true, nullptr);
171 void TestWebContents::CommitPendingNavigation() {
172 const NavigationEntry
* entry
= GetController().GetPendingEntry();
175 // If we are doing a cross-site navigation, this simulates the current RFH
176 // notifying that it has unloaded so the pending RFH is resumed and can
178 // PlzNavigate: the pending RFH is not created before the navigation commit,
179 // so it is necessary to simulate the IO thread response here to commit in the
180 // proper renderer. It is necessary to call PrepareForCommit before getting
181 // the main and the pending frame because when we are trying to navigate to a
182 // webui from a new tab, a RenderFrameHost is created to display it that is
183 // committed immediately (since it is a new tab). Therefore the main frame is
184 // replaced without a pending frame being created, and we don't get the right
185 // values for the RFH to navigate: we try to use the old one that has been
186 // deleted in the meantime.
187 GetMainFrame()->PrepareForCommit();
189 TestRenderFrameHost
* old_rfh
= GetMainFrame();
190 TestRenderFrameHost
* rfh
= GetPendingMainFrame();
194 int page_id
= entry
->GetPageID();
196 // It's a new navigation, assign a never-seen page id to it.
197 page_id
= GetMaxPageIDForSiteInstance(rfh
->GetSiteInstance()) + 1;
200 rfh
->SendNavigate(page_id
, entry
->GetUniqueID(),
201 GetController().GetPendingEntryIndex() == -1,
203 // Simulate the SwapOut_ACK. This is needed when cross-site navigation
206 old_rfh
->OnSwappedOut();
209 void TestWebContents::ProceedWithCrossSiteNavigation() {
210 if (!GetPendingMainFrame())
212 GetMainFrame()->SendBeforeUnloadACK(true);
215 RenderViewHostDelegateView
* TestWebContents::GetDelegateView() {
216 if (delegate_view_override_
)
217 return delegate_view_override_
;
218 return WebContentsImpl::GetDelegateView();
221 void TestWebContents::SetOpener(TestWebContents
* opener
) {
222 frame_tree_
.root()->SetOpener(opener
->GetFrameTree()->root());
225 void TestWebContents::AddPendingContents(TestWebContents
* contents
) {
226 // This is normally only done in WebContentsImpl::CreateNewWindow.
227 pending_contents_
[contents
->GetRenderViewHost()->GetRoutingID()] = contents
;
228 AddDestructionObserver(contents
);
231 void TestWebContents::ExpectSetHistoryOffsetAndLength(int history_offset
,
232 int history_length
) {
233 expect_set_history_offset_and_length_
= true;
234 expect_set_history_offset_and_length_history_offset_
= history_offset
;
235 expect_set_history_offset_and_length_history_length_
= history_length
;
238 void TestWebContents::SetHistoryOffsetAndLength(int history_offset
,
239 int history_length
) {
240 EXPECT_TRUE(expect_set_history_offset_and_length_
);
241 expect_set_history_offset_and_length_
= false;
242 EXPECT_EQ(expect_set_history_offset_and_length_history_offset_
,
244 EXPECT_EQ(expect_set_history_offset_and_length_history_length_
,
248 void TestWebContents::TestDidFinishLoad(const GURL
& url
) {
249 FrameHostMsg_DidFinishLoad
msg(0, url
);
250 frame_tree_
.root()->current_frame_host()->OnMessageReceived(msg
);
253 void TestWebContents::TestDidFailLoadWithError(
256 const base::string16
& error_description
) {
257 FrameHostMsg_DidFailLoadWithError
msg(
258 0, url
, error_code
, error_description
);
259 frame_tree_
.root()->current_frame_host()->OnMessageReceived(msg
);
262 void TestWebContents::CreateNewWindow(
263 int render_process_id
,
265 int main_frame_route_id
,
266 const ViewHostMsg_CreateWindow_Params
& params
,
267 SessionStorageNamespace
* session_storage_namespace
) {
270 void TestWebContents::CreateNewWidget(int render_process_id
,
272 blink::WebPopupType popup_type
) {
275 void TestWebContents::CreateNewFullscreenWidget(int render_process_id
,
279 void TestWebContents::ShowCreatedWindow(int route_id
,
280 WindowOpenDisposition disposition
,
281 const gfx::Rect
& initial_rect
,
285 void TestWebContents::ShowCreatedWidget(int route_id
,
286 const gfx::Rect
& initial_rect
) {
289 void TestWebContents::ShowCreatedFullscreenWidget(int route_id
) {
292 void TestWebContents::SaveFrameWithHeaders(const GURL
& url
,
293 const Referrer
& referrer
,
294 const std::string
& headers
) {
295 save_frame_headers_
= headers
;
298 } // namespace content