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 int TestWebContents::DownloadImage(const GURL
& url
,
69 uint32_t max_bitmap_size
,
71 const ImageDownloadCallback
& callback
) {
72 static int g_next_image_download_id
= 0;
73 return ++g_next_image_download_id
;
76 void TestWebContents::TestDidNavigate(RenderFrameHost
* render_frame_host
,
79 bool did_create_new_entry
,
81 ui::PageTransition transition
) {
82 TestDidNavigateWithReferrer(render_frame_host
,
91 void TestWebContents::TestDidNavigateWithReferrer(
92 RenderFrameHost
* render_frame_host
,
95 bool did_create_new_entry
,
97 const Referrer
& referrer
,
98 ui::PageTransition transition
) {
99 FrameHostMsg_DidCommitProvisionalLoad_Params params
;
101 params
.page_id
= page_id
;
102 params
.nav_entry_id
= nav_entry_id
;
104 params
.referrer
= referrer
;
105 params
.transition
= transition
;
106 params
.redirects
= std::vector
<GURL
>();
107 params
.should_update_history
= false;
108 params
.searchable_form_url
= GURL();
109 params
.searchable_form_encoding
= std::string();
110 params
.did_create_new_entry
= did_create_new_entry
;
111 params
.security_info
= std::string();
112 params
.gesture
= NavigationGestureUser
;
113 params
.was_within_same_page
= false;
114 params
.is_post
= false;
115 params
.page_state
= PageState::CreateFromURL(url
);
117 TestRenderFrameHost
* rfh
=
118 static_cast<TestRenderFrameHost
*>(render_frame_host
);
119 rfh
->InitializeRenderFrameIfNeeded();
120 rfh
->frame_tree_node()->navigator()->DidNavigate(rfh
, params
);
123 const std::string
& TestWebContents::GetSaveFrameHeaders() {
124 return save_frame_headers_
;
127 bool TestWebContents::CrossProcessNavigationPending() {
128 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
129 switches::kEnableBrowserSideNavigation
)) {
130 return GetRenderManager()->speculative_render_frame_host_
&&
131 static_cast<TestRenderFrameHost
*>(
132 GetRenderManager()->speculative_render_frame_host_
.get())
135 return GetRenderManager()->pending_frame_host() != nullptr;
138 bool TestWebContents::CreateRenderViewForRenderManager(
139 RenderViewHost
* render_view_host
,
140 int opener_frame_routing_id
,
141 int proxy_routing_id
,
142 const FrameReplicationState
& replicated_frame_state
,
143 bool for_main_frame
) {
144 UpdateMaxPageIDIfNecessary(render_view_host
);
145 // This will go to a TestRenderViewHost.
146 static_cast<RenderViewHostImpl
*>(render_view_host
)
147 ->CreateRenderView(opener_frame_routing_id
, proxy_routing_id
, -1,
148 replicated_frame_state
, false);
152 WebContents
* TestWebContents::Clone() {
153 WebContentsImpl
* contents
=
154 Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
155 contents
->GetController().CopyStateFrom(controller_
);
159 void TestWebContents::NavigateAndCommit(const GURL
& url
) {
160 GetController().LoadURL(
161 url
, Referrer(), ui::PAGE_TRANSITION_LINK
, std::string());
162 GURL
loaded_url(url
);
163 bool reverse_on_redirect
= false;
164 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
165 &loaded_url
, GetBrowserContext(), &reverse_on_redirect
);
167 // LoadURL created a navigation entry, now simulate the RenderView sending
168 // a notification that it actually navigated.
169 CommitPendingNavigation();
172 void TestWebContents::TestSetIsLoading(bool value
) {
173 SetIsLoading(value
, true, nullptr);
176 void TestWebContents::CommitPendingNavigation() {
177 const NavigationEntry
* entry
= GetController().GetPendingEntry();
180 // If we are doing a cross-site navigation, this simulates the current RFH
181 // notifying that it has unloaded so the pending RFH is resumed and can
183 // PlzNavigate: the pending RFH is not created before the navigation commit,
184 // so it is necessary to simulate the IO thread response here to commit in the
185 // proper renderer. It is necessary to call PrepareForCommit before getting
186 // the main and the pending frame because when we are trying to navigate to a
187 // webui from a new tab, a RenderFrameHost is created to display it that is
188 // committed immediately (since it is a new tab). Therefore the main frame is
189 // replaced without a pending frame being created, and we don't get the right
190 // values for the RFH to navigate: we try to use the old one that has been
191 // deleted in the meantime.
192 // Note that for some synchronous navigations (about:blank, javascript
193 // urls, etc.) there will be no NavigationRequest, and no simulation of the
194 // network stack is required.
195 bool browser_side_navigation
=
196 base::CommandLine::ForCurrentProcess()->HasSwitch(
197 switches::kEnableBrowserSideNavigation
);
198 if (!browser_side_navigation
||
199 GetMainFrame()->frame_tree_node()->navigation_request()) {
200 GetMainFrame()->PrepareForCommit();
203 TestRenderFrameHost
* old_rfh
= GetMainFrame();
204 TestRenderFrameHost
* rfh
= GetPendingMainFrame();
207 CHECK_IMPLIES(browser_side_navigation
, rfh
->is_loading());
208 CHECK_IMPLIES(browser_side_navigation
,
209 !rfh
->frame_tree_node()->navigation_request());
211 int page_id
= entry
->GetPageID();
213 // It's a new navigation, assign a never-seen page id to it.
214 page_id
= GetMaxPageIDForSiteInstance(rfh
->GetSiteInstance()) + 1;
217 rfh
->SendNavigate(page_id
, entry
->GetUniqueID(),
218 GetController().GetPendingEntryIndex() == -1,
220 // Simulate the SwapOut_ACK. This is needed when cross-site navigation
223 old_rfh
->OnSwappedOut();
226 void TestWebContents::ProceedWithCrossSiteNavigation() {
227 if (!GetPendingMainFrame())
229 GetMainFrame()->SendBeforeUnloadACK(true);
232 RenderViewHostDelegateView
* TestWebContents::GetDelegateView() {
233 if (delegate_view_override_
)
234 return delegate_view_override_
;
235 return WebContentsImpl::GetDelegateView();
238 void TestWebContents::SetOpener(TestWebContents
* opener
) {
239 frame_tree_
.root()->SetOpener(opener
->GetFrameTree()->root());
242 void TestWebContents::AddPendingContents(TestWebContents
* contents
) {
243 // This is normally only done in WebContentsImpl::CreateNewWindow.
244 pending_contents_
[contents
->GetRenderViewHost()->GetRoutingID()] = contents
;
245 AddDestructionObserver(contents
);
248 void TestWebContents::ExpectSetHistoryOffsetAndLength(int history_offset
,
249 int history_length
) {
250 expect_set_history_offset_and_length_
= true;
251 expect_set_history_offset_and_length_history_offset_
= history_offset
;
252 expect_set_history_offset_and_length_history_length_
= history_length
;
255 void TestWebContents::SetHistoryOffsetAndLength(int history_offset
,
256 int history_length
) {
257 EXPECT_TRUE(expect_set_history_offset_and_length_
);
258 expect_set_history_offset_and_length_
= false;
259 EXPECT_EQ(expect_set_history_offset_and_length_history_offset_
,
261 EXPECT_EQ(expect_set_history_offset_and_length_history_length_
,
265 void TestWebContents::TestDidFinishLoad(const GURL
& url
) {
266 FrameHostMsg_DidFinishLoad
msg(0, url
);
267 frame_tree_
.root()->current_frame_host()->OnMessageReceived(msg
);
270 void TestWebContents::TestDidFailLoadWithError(
273 const base::string16
& error_description
,
274 bool was_ignored_by_handler
) {
275 FrameHostMsg_DidFailLoadWithError
msg(
276 0, url
, error_code
, error_description
, was_ignored_by_handler
);
277 frame_tree_
.root()->current_frame_host()->OnMessageReceived(msg
);
280 void TestWebContents::CreateNewWindow(
281 int render_process_id
,
283 int main_frame_route_id
,
284 const ViewHostMsg_CreateWindow_Params
& params
,
285 SessionStorageNamespace
* session_storage_namespace
) {
288 void TestWebContents::CreateNewWidget(int render_process_id
,
290 blink::WebPopupType popup_type
) {
293 void TestWebContents::CreateNewFullscreenWidget(int render_process_id
,
297 void TestWebContents::ShowCreatedWindow(int route_id
,
298 WindowOpenDisposition disposition
,
299 const gfx::Rect
& initial_rect
,
303 void TestWebContents::ShowCreatedWidget(int route_id
,
304 const gfx::Rect
& initial_rect
) {
307 void TestWebContents::ShowCreatedFullscreenWidget(int route_id
) {
310 void TestWebContents::SaveFrameWithHeaders(const GURL
& url
,
311 const Referrer
& referrer
,
312 const std::string
& headers
) {
313 save_frame_headers_
= headers
;
316 } // namespace content