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/navigation_request.h"
14 #include "content/browser/frame_host/navigator.h"
15 #include "content/browser/frame_host/navigator_impl.h"
16 #include "content/browser/renderer_host/render_view_host_impl.h"
17 #include "content/browser/site_instance_impl.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/browser/stream_handle.h"
23 #include "content/public/common/content_switches.h"
24 #include "content/public/common/page_state.h"
25 #include "content/public/test/mock_render_process_host.h"
26 #include "content/test/browser_side_navigation_test_utils.h"
27 #include "content/test/test_navigation_url_loader.h"
28 #include "content/test/test_render_view_host.h"
29 #include "ui/base/page_transition_types.h"
33 TestWebContents::TestWebContents(BrowserContext
* browser_context
)
34 : WebContentsImpl(browser_context
, NULL
),
35 delegate_view_override_(NULL
),
36 expect_set_history_offset_and_length_(false),
37 expect_set_history_offset_and_length_history_length_(0) {
40 TestWebContents
* TestWebContents::Create(BrowserContext
* browser_context
,
41 SiteInstance
* instance
) {
42 TestWebContents
* test_web_contents
= new TestWebContents(browser_context
);
43 test_web_contents
->Init(WebContents::CreateParams(browser_context
, instance
));
44 return test_web_contents
;
47 TestWebContents::~TestWebContents() {
48 EXPECT_FALSE(expect_set_history_offset_and_length_
);
51 TestRenderFrameHost
* TestWebContents::GetMainFrame() {
52 return static_cast<TestRenderFrameHost
*>(WebContentsImpl::GetMainFrame());
55 TestRenderViewHost
* TestWebContents::GetRenderViewHost() const {
56 return static_cast<TestRenderViewHost
*>(
57 WebContentsImpl::GetRenderViewHost());
60 TestRenderFrameHost
* TestWebContents::GetPendingMainFrame() const {
61 return static_cast<TestRenderFrameHost
*>(
62 GetRenderManager()->pending_frame_host());
65 void TestWebContents::TestDidNavigate(RenderFrameHost
* render_frame_host
,
68 ui::PageTransition transition
) {
69 TestDidNavigateWithReferrer(render_frame_host
,
76 void TestWebContents::TestDidNavigateWithReferrer(
77 RenderFrameHost
* render_frame_host
,
80 const Referrer
& referrer
,
81 ui::PageTransition transition
) {
82 FrameHostMsg_DidCommitProvisionalLoad_Params params
;
84 params
.page_id
= page_id
;
86 params
.referrer
= referrer
;
87 params
.transition
= transition
;
88 params
.redirects
= std::vector
<GURL
>();
89 params
.should_update_history
= false;
90 params
.searchable_form_url
= GURL();
91 params
.searchable_form_encoding
= std::string();
92 params
.security_info
= std::string();
93 params
.gesture
= NavigationGestureUser
;
94 params
.was_within_same_page
= false;
95 params
.is_post
= false;
96 params
.page_state
= PageState::CreateFromURL(url
);
98 RenderFrameHostImpl
* rfhi
=
99 static_cast<RenderFrameHostImpl
*>(render_frame_host
);
100 rfhi
->frame_tree_node()->navigator()->DidNavigate(rfhi
, params
);
103 WebPreferences
TestWebContents::TestComputeWebkitPrefs() {
104 return ComputeWebkitPrefs();
107 bool TestWebContents::CreateRenderViewForRenderManager(
108 RenderViewHost
* render_view_host
,
110 int proxy_routing_id
,
111 bool for_main_frame
) {
112 UpdateMaxPageIDIfNecessary(render_view_host
);
113 // This will go to a TestRenderViewHost.
114 static_cast<RenderViewHostImpl
*>(
115 render_view_host
)->CreateRenderView(base::string16(),
122 WebContents
* TestWebContents::Clone() {
123 WebContentsImpl
* contents
=
124 Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
125 contents
->GetController().CopyStateFrom(controller_
);
129 void TestWebContents::NavigateAndCommit(const GURL
& url
) {
130 bool has_live_renderer
= GetMainFrame()->IsRenderFrameLive();
131 GetController().LoadURL(
132 url
, Referrer(), ui::PAGE_TRANSITION_LINK
, std::string());
133 GURL
loaded_url(url
);
134 bool reverse_on_redirect
= false;
135 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
136 &loaded_url
, GetBrowserContext(), &reverse_on_redirect
);
139 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
140 switches::kEnableBrowserSideNavigation
)) {
141 // Simulate the renderer response if there was a live renderer when the
142 // navigation started. Otherwise, it will have been sent directly to the
144 if (has_live_renderer
)
145 GetMainFrame()->SendBeginNavigationWithURL(url
);
147 // Now simulate the network stack commit without any redirects. This will
148 // cause the navigation to commit at the same url.
149 FrameTreeNode
* frame_tree_node
= GetMainFrame()->frame_tree_node();
150 NavigationRequest
* request
=
151 static_cast<NavigatorImpl
*>(frame_tree_node
->navigator())
152 ->GetNavigationRequestForNodeForTesting(frame_tree_node
);
153 TestNavigationURLLoader
* url_loader
=
154 static_cast<TestNavigationURLLoader
*>(request
->loader_for_testing());
155 scoped_refptr
<ResourceResponse
> response(new ResourceResponse
);
156 url_loader
->CallOnResponseStarted(response
, MakeEmptyStream());
159 // LoadURL created a navigation entry, now simulate the RenderView sending
160 // a notification that it actually navigated.
161 CommitPendingNavigation();
164 void TestWebContents::TestSetIsLoading(bool value
) {
165 SetIsLoading(GetRenderViewHost(), value
, true, NULL
);
168 void TestWebContents::CommitPendingNavigation() {
169 // If we are doing a cross-site navigation, this simulates the current RVH
170 // notifying that it has unloaded so the pending RVH is resumed and can
172 ProceedWithCrossSiteNavigation();
173 TestRenderFrameHost
* old_rfh
= GetMainFrame();
174 TestRenderFrameHost
* rfh
= GetPendingMainFrame();
178 const NavigationEntry
* entry
= GetController().GetPendingEntry();
180 int page_id
= entry
->GetPageID();
182 // It's a new navigation, assign a never-seen page id to it.
183 page_id
= GetMaxPageIDForSiteInstance(rfh
->GetSiteInstance()) + 1;
186 rfh
->SendNavigate(page_id
, entry
->GetURL());
187 // Simulate the SwapOut_ACK. This is needed when cross-site navigation happens
190 old_rfh
->OnSwappedOut();
193 void TestWebContents::ProceedWithCrossSiteNavigation() {
194 if (!GetPendingMainFrame())
196 GetMainFrame()->SendBeforeUnloadACK(true);
199 RenderViewHostDelegateView
* TestWebContents::GetDelegateView() {
200 if (delegate_view_override_
)
201 return delegate_view_override_
;
202 return WebContentsImpl::GetDelegateView();
205 void TestWebContents::SetOpener(TestWebContents
* opener
) {
206 // This is normally only set in the WebContents constructor, which also
207 // registers an observer for when the opener gets closed.
209 AddDestructionObserver(opener_
);
212 void TestWebContents::AddPendingContents(TestWebContents
* contents
) {
213 // This is normally only done in WebContentsImpl::CreateNewWindow.
214 pending_contents_
[contents
->GetRenderViewHost()->GetRoutingID()] = contents
;
215 AddDestructionObserver(contents
);
218 void TestWebContents::ExpectSetHistoryOffsetAndLength(int history_offset
,
219 int history_length
) {
220 expect_set_history_offset_and_length_
= true;
221 expect_set_history_offset_and_length_history_offset_
= history_offset
;
222 expect_set_history_offset_and_length_history_length_
= history_length
;
225 void TestWebContents::SetHistoryOffsetAndLength(int history_offset
,
226 int history_length
) {
227 EXPECT_TRUE(expect_set_history_offset_and_length_
);
228 expect_set_history_offset_and_length_
= false;
229 EXPECT_EQ(expect_set_history_offset_and_length_history_offset_
,
231 EXPECT_EQ(expect_set_history_offset_and_length_history_length_
,
235 void TestWebContents::TestDidFinishLoad(const GURL
& url
) {
236 FrameHostMsg_DidFinishLoad
msg(0, url
);
237 frame_tree_
.root()->current_frame_host()->OnMessageReceived(msg
);
240 void TestWebContents::TestDidFailLoadWithError(
243 const base::string16
& error_description
) {
244 FrameHostMsg_DidFailLoadWithError
msg(
245 0, url
, error_code
, error_description
);
246 frame_tree_
.root()->current_frame_host()->OnMessageReceived(msg
);
249 void TestWebContents::CreateNewWindow(
250 int render_process_id
,
252 int main_frame_route_id
,
253 const ViewHostMsg_CreateWindow_Params
& params
,
254 SessionStorageNamespace
* session_storage_namespace
) {
257 void TestWebContents::CreateNewWidget(int render_process_id
,
259 blink::WebPopupType popup_type
) {
262 void TestWebContents::CreateNewFullscreenWidget(int render_process_id
,
266 void TestWebContents::ShowCreatedWindow(int route_id
,
267 WindowOpenDisposition disposition
,
268 const gfx::Rect
& initial_pos
,
272 void TestWebContents::ShowCreatedWidget(int route_id
,
273 const gfx::Rect
& initial_pos
) {
276 void TestWebContents::ShowCreatedFullscreenWidget(int route_id
) {
279 } // namespace content