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
, NULL
),
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 test_web_contents
->RenderFrameCreated(test_web_contents
->GetMainFrame());
42 return test_web_contents
;
45 TestWebContents::~TestWebContents() {
46 EXPECT_FALSE(expect_set_history_offset_and_length_
);
49 TestRenderFrameHost
* TestWebContents::GetMainFrame() {
50 return static_cast<TestRenderFrameHost
*>(WebContentsImpl::GetMainFrame());
53 TestRenderViewHost
* TestWebContents::GetRenderViewHost() const {
54 return static_cast<TestRenderViewHost
*>(
55 WebContentsImpl::GetRenderViewHost());
58 TestRenderFrameHost
* TestWebContents::GetPendingMainFrame() const {
59 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
60 switches::kEnableBrowserSideNavigation
)) {
61 return static_cast<TestRenderFrameHost
*>(
62 GetRenderManager()->speculative_render_frame_host_
.get());
64 return static_cast<TestRenderFrameHost
*>(
65 GetRenderManager()->pending_frame_host());
68 void TestWebContents::TestDidNavigate(RenderFrameHost
* render_frame_host
,
71 bool did_create_new_entry
,
73 ui::PageTransition transition
) {
74 TestDidNavigateWithReferrer(render_frame_host
,
83 void TestWebContents::TestDidNavigateWithReferrer(
84 RenderFrameHost
* render_frame_host
,
87 bool did_create_new_entry
,
89 const Referrer
& referrer
,
90 ui::PageTransition transition
) {
91 FrameHostMsg_DidCommitProvisionalLoad_Params params
;
93 params
.page_id
= page_id
;
94 params
.nav_entry_id
= nav_entry_id
;
96 params
.referrer
= referrer
;
97 params
.transition
= transition
;
98 params
.redirects
= std::vector
<GURL
>();
99 params
.should_update_history
= false;
100 params
.searchable_form_url
= GURL();
101 params
.searchable_form_encoding
= std::string();
102 params
.did_create_new_entry
= did_create_new_entry
;
103 params
.security_info
= std::string();
104 params
.gesture
= NavigationGestureUser
;
105 params
.was_within_same_page
= false;
106 params
.is_post
= false;
107 params
.page_state
= PageState::CreateFromURL(url
);
109 RenderFrameHostImpl
* rfhi
=
110 static_cast<RenderFrameHostImpl
*>(render_frame_host
);
111 rfhi
->frame_tree_node()->navigator()->DidNavigate(rfhi
, params
);
114 bool TestWebContents::CrossProcessNavigationPending() {
115 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
116 switches::kEnableBrowserSideNavigation
)) {
117 return GetRenderManager()->speculative_render_frame_host_
&&
118 static_cast<TestRenderFrameHost
*>(
119 GetRenderManager()->speculative_render_frame_host_
.get())
122 return GetRenderManager()->pending_frame_host() != nullptr;
125 bool TestWebContents::CreateRenderViewForRenderManager(
126 RenderViewHost
* render_view_host
,
128 int proxy_routing_id
,
129 bool for_main_frame
) {
130 UpdateMaxPageIDIfNecessary(render_view_host
);
131 // This will go to a TestRenderViewHost.
132 static_cast<RenderViewHostImpl
*>(
133 render_view_host
)->CreateRenderView(base::string16(),
140 WebContents
* TestWebContents::Clone() {
141 WebContentsImpl
* contents
=
142 Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
143 contents
->GetController().CopyStateFrom(controller_
);
147 void TestWebContents::NavigateAndCommit(const GURL
& url
) {
148 GetController().LoadURL(
149 url
, Referrer(), ui::PAGE_TRANSITION_LINK
, std::string());
150 GURL
loaded_url(url
);
151 bool reverse_on_redirect
= false;
152 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
153 &loaded_url
, GetBrowserContext(), &reverse_on_redirect
);
155 // LoadURL created a navigation entry, now simulate the RenderView sending
156 // a notification that it actually navigated.
157 CommitPendingNavigation();
160 void TestWebContents::TestSetIsLoading(bool value
) {
161 SetIsLoading(value
, true, nullptr);
164 void TestWebContents::CommitPendingNavigation() {
165 const NavigationEntry
* entry
= GetController().GetPendingEntry();
168 // If we are doing a cross-site navigation, this simulates the current RFH
169 // notifying that it has unloaded so the pending RFH is resumed and can
171 // PlzNavigate: the pending RFH is not created before the navigation commit,
172 // so it is necessary to simulate the IO thread response here to commit in the
173 // proper renderer. It is necessary to call PrepareForCommit before getting
174 // the main and the pending frame because when we are trying to navigate to a
175 // webui from a new tab, a RenderFrameHost is created to display it that is
176 // committed immediately (since it is a new tab). Therefore the main frame is
177 // replaced without a pending frame being created, and we don't get the right
178 // values for the RFH to navigate: we try to use the old one that has been
179 // deleted in the meantime.
180 GetMainFrame()->PrepareForCommit();
182 TestRenderFrameHost
* old_rfh
= GetMainFrame();
183 TestRenderFrameHost
* rfh
= GetPendingMainFrame();
187 int page_id
= entry
->GetPageID();
189 // It's a new navigation, assign a never-seen page id to it.
190 page_id
= GetMaxPageIDForSiteInstance(rfh
->GetSiteInstance()) + 1;
193 rfh
->SendNavigate(page_id
, entry
->GetUniqueID(),
194 GetController().GetPendingEntryIndex() == -1,
196 // Simulate the SwapOut_ACK. This is needed when cross-site navigation
199 old_rfh
->OnSwappedOut();
202 void TestWebContents::ProceedWithCrossSiteNavigation() {
203 if (!GetPendingMainFrame())
205 GetMainFrame()->SendBeforeUnloadACK(true);
208 RenderViewHostDelegateView
* TestWebContents::GetDelegateView() {
209 if (delegate_view_override_
)
210 return delegate_view_override_
;
211 return WebContentsImpl::GetDelegateView();
214 void TestWebContents::SetOpener(TestWebContents
* opener
) {
215 // This is normally only set in the WebContents constructor, which also
216 // registers an observer for when the opener gets closed.
218 AddDestructionObserver(opener_
);
221 void TestWebContents::AddPendingContents(TestWebContents
* contents
) {
222 // This is normally only done in WebContentsImpl::CreateNewWindow.
223 pending_contents_
[contents
->GetRenderViewHost()->GetRoutingID()] = contents
;
224 AddDestructionObserver(contents
);
227 void TestWebContents::ExpectSetHistoryOffsetAndLength(int history_offset
,
228 int history_length
) {
229 expect_set_history_offset_and_length_
= true;
230 expect_set_history_offset_and_length_history_offset_
= history_offset
;
231 expect_set_history_offset_and_length_history_length_
= history_length
;
234 void TestWebContents::SetHistoryOffsetAndLength(int history_offset
,
235 int history_length
) {
236 EXPECT_TRUE(expect_set_history_offset_and_length_
);
237 expect_set_history_offset_and_length_
= false;
238 EXPECT_EQ(expect_set_history_offset_and_length_history_offset_
,
240 EXPECT_EQ(expect_set_history_offset_and_length_history_length_
,
244 void TestWebContents::TestDidFinishLoad(const GURL
& url
) {
245 FrameHostMsg_DidFinishLoad
msg(0, url
);
246 frame_tree_
.root()->current_frame_host()->OnMessageReceived(msg
);
249 void TestWebContents::TestDidFailLoadWithError(
252 const base::string16
& error_description
) {
253 FrameHostMsg_DidFailLoadWithError
msg(
254 0, url
, error_code
, error_description
);
255 frame_tree_
.root()->current_frame_host()->OnMessageReceived(msg
);
258 void TestWebContents::CreateNewWindow(
259 int render_process_id
,
261 int main_frame_route_id
,
262 const ViewHostMsg_CreateWindow_Params
& params
,
263 SessionStorageNamespace
* session_storage_namespace
) {
266 void TestWebContents::CreateNewWidget(int render_process_id
,
268 blink::WebPopupType popup_type
) {
271 void TestWebContents::CreateNewFullscreenWidget(int render_process_id
,
275 void TestWebContents::ShowCreatedWindow(int route_id
,
276 WindowOpenDisposition disposition
,
277 const gfx::Rect
& initial_rect
,
281 void TestWebContents::ShowCreatedWidget(int route_id
,
282 const gfx::Rect
& initial_rect
) {
285 void TestWebContents::ShowCreatedFullscreenWidget(int route_id
) {
288 } // namespace content