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 ui::PageTransition transition
) {
72 TestDidNavigateWithReferrer(render_frame_host
,
79 void TestWebContents::TestDidNavigateWithReferrer(
80 RenderFrameHost
* render_frame_host
,
83 const Referrer
& referrer
,
84 ui::PageTransition transition
) {
85 FrameHostMsg_DidCommitProvisionalLoad_Params params
;
87 params
.page_id
= page_id
;
89 params
.referrer
= referrer
;
90 params
.transition
= transition
;
91 params
.redirects
= std::vector
<GURL
>();
92 params
.should_update_history
= false;
93 params
.searchable_form_url
= GURL();
94 params
.searchable_form_encoding
= std::string();
95 params
.security_info
= std::string();
96 params
.gesture
= NavigationGestureUser
;
97 params
.was_within_same_page
= false;
98 params
.is_post
= false;
99 params
.page_state
= PageState::CreateFromURL(url
);
101 RenderFrameHostImpl
* rfhi
=
102 static_cast<RenderFrameHostImpl
*>(render_frame_host
);
103 rfhi
->frame_tree_node()->navigator()->DidNavigate(rfhi
, params
);
106 bool TestWebContents::CreateRenderViewForRenderManager(
107 RenderViewHost
* render_view_host
,
109 int proxy_routing_id
,
110 bool for_main_frame
) {
111 UpdateMaxPageIDIfNecessary(render_view_host
);
112 // This will go to a TestRenderViewHost.
113 static_cast<RenderViewHostImpl
*>(
114 render_view_host
)->CreateRenderView(base::string16(),
121 WebContents
* TestWebContents::Clone() {
122 WebContentsImpl
* contents
=
123 Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
124 contents
->GetController().CopyStateFrom(controller_
);
128 void TestWebContents::NavigateAndCommit(const GURL
& url
) {
129 GetController().LoadURL(
130 url
, Referrer(), ui::PAGE_TRANSITION_LINK
, std::string());
131 GURL
loaded_url(url
);
132 bool reverse_on_redirect
= false;
133 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
134 &loaded_url
, GetBrowserContext(), &reverse_on_redirect
);
136 // LoadURL created a navigation entry, now simulate the RenderView sending
137 // a notification that it actually navigated.
138 CommitPendingNavigation();
141 void TestWebContents::TestSetIsLoading(bool value
) {
142 SetIsLoading(value
, true, nullptr);
145 void TestWebContents::CommitPendingNavigation() {
146 const NavigationEntry
* entry
= GetController().GetPendingEntry();
149 // If we are doing a cross-site navigation, this simulates the current RFH
150 // notifying that it has unloaded so the pending RFH is resumed and can
152 // PlzNavigate: the pending RFH is not created before the navigation commit,
153 // so it is necessary to simulate the IO thread response here to commit in the
154 // proper renderer. It is necessary to call PrepareForCommit before getting
155 // the main and the pending frame because when we are trying to navigate to a
156 // webui from a new tab, a RenderFrameHost is created to display it that is
157 // committed immediately (since it is a new tab). Therefore the main frame is
158 // replaced without a pending frame being created, and we don't get the right
159 // values for the RFH to navigate: we try to use the old one that has been
160 // deleted in the meantime.
161 GetMainFrame()->PrepareForCommit();
163 TestRenderFrameHost
* old_rfh
= GetMainFrame();
164 TestRenderFrameHost
* rfh
= GetPendingMainFrame();
168 int page_id
= entry
->GetPageID();
170 // It's a new navigation, assign a never-seen page id to it.
171 page_id
= GetMaxPageIDForSiteInstance(rfh
->GetSiteInstance()) + 1;
174 rfh
->SendNavigate(page_id
, entry
->GetURL());
175 // Simulate the SwapOut_ACK. This is needed when cross-site navigation
178 old_rfh
->OnSwappedOut();
181 void TestWebContents::ProceedWithCrossSiteNavigation() {
182 if (!GetPendingMainFrame())
184 GetMainFrame()->SendBeforeUnloadACK(true);
187 RenderViewHostDelegateView
* TestWebContents::GetDelegateView() {
188 if (delegate_view_override_
)
189 return delegate_view_override_
;
190 return WebContentsImpl::GetDelegateView();
193 void TestWebContents::SetOpener(TestWebContents
* opener
) {
194 // This is normally only set in the WebContents constructor, which also
195 // registers an observer for when the opener gets closed.
197 AddDestructionObserver(opener_
);
200 void TestWebContents::AddPendingContents(TestWebContents
* contents
) {
201 // This is normally only done in WebContentsImpl::CreateNewWindow.
202 pending_contents_
[contents
->GetRenderViewHost()->GetRoutingID()] = contents
;
203 AddDestructionObserver(contents
);
206 void TestWebContents::ExpectSetHistoryOffsetAndLength(int history_offset
,
207 int history_length
) {
208 expect_set_history_offset_and_length_
= true;
209 expect_set_history_offset_and_length_history_offset_
= history_offset
;
210 expect_set_history_offset_and_length_history_length_
= history_length
;
213 void TestWebContents::SetHistoryOffsetAndLength(int history_offset
,
214 int history_length
) {
215 EXPECT_TRUE(expect_set_history_offset_and_length_
);
216 expect_set_history_offset_and_length_
= false;
217 EXPECT_EQ(expect_set_history_offset_and_length_history_offset_
,
219 EXPECT_EQ(expect_set_history_offset_and_length_history_length_
,
223 void TestWebContents::TestDidFinishLoad(const GURL
& url
) {
224 FrameHostMsg_DidFinishLoad
msg(0, url
);
225 frame_tree_
.root()->current_frame_host()->OnMessageReceived(msg
);
228 void TestWebContents::TestDidFailLoadWithError(
231 const base::string16
& error_description
) {
232 FrameHostMsg_DidFailLoadWithError
msg(
233 0, url
, error_code
, error_description
);
234 frame_tree_
.root()->current_frame_host()->OnMessageReceived(msg
);
237 void TestWebContents::CreateNewWindow(
238 int render_process_id
,
240 int main_frame_route_id
,
241 const ViewHostMsg_CreateWindow_Params
& params
,
242 SessionStorageNamespace
* session_storage_namespace
) {
245 void TestWebContents::CreateNewWidget(int render_process_id
,
247 blink::WebPopupType popup_type
) {
250 void TestWebContents::CreateNewFullscreenWidget(int render_process_id
,
254 void TestWebContents::ShowCreatedWindow(int route_id
,
255 WindowOpenDisposition disposition
,
256 const gfx::Rect
& initial_rect
,
260 void TestWebContents::ShowCreatedWidget(int route_id
,
261 const gfx::Rect
& initial_rect
) {
264 void TestWebContents::ShowCreatedFullscreenWidget(int route_id
) {
267 } // namespace content