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 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 ui::PageTransition transition
) {
71 TestDidNavigateWithReferrer(render_frame_host
,
78 void TestWebContents::TestDidNavigateWithReferrer(
79 RenderFrameHost
* render_frame_host
,
82 const Referrer
& referrer
,
83 ui::PageTransition transition
) {
84 FrameHostMsg_DidCommitProvisionalLoad_Params params
;
86 params
.page_id
= page_id
;
88 params
.referrer
= referrer
;
89 params
.transition
= transition
;
90 params
.redirects
= std::vector
<GURL
>();
91 params
.should_update_history
= false;
92 params
.searchable_form_url
= GURL();
93 params
.searchable_form_encoding
= std::string();
94 params
.security_info
= std::string();
95 params
.gesture
= NavigationGestureUser
;
96 params
.was_within_same_page
= false;
97 params
.is_post
= false;
98 params
.page_state
= PageState::CreateFromURL(url
);
100 RenderFrameHostImpl
* rfhi
=
101 static_cast<RenderFrameHostImpl
*>(render_frame_host
);
102 rfhi
->frame_tree_node()->navigator()->DidNavigate(rfhi
, params
);
105 WebPreferences
TestWebContents::TestComputeWebkitPrefs() {
106 return ComputeWebkitPrefs();
109 bool TestWebContents::CreateRenderViewForRenderManager(
110 RenderViewHost
* render_view_host
,
112 int proxy_routing_id
,
113 bool for_main_frame
) {
114 UpdateMaxPageIDIfNecessary(render_view_host
);
115 // This will go to a TestRenderViewHost.
116 static_cast<RenderViewHostImpl
*>(
117 render_view_host
)->CreateRenderView(base::string16(),
124 WebContents
* TestWebContents::Clone() {
125 WebContentsImpl
* contents
=
126 Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
127 contents
->GetController().CopyStateFrom(controller_
);
131 void TestWebContents::NavigateAndCommit(const GURL
& url
) {
132 GetController().LoadURL(
133 url
, Referrer(), ui::PAGE_TRANSITION_LINK
, std::string());
134 GURL
loaded_url(url
);
135 bool reverse_on_redirect
= false;
136 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
137 &loaded_url
, GetBrowserContext(), &reverse_on_redirect
);
139 // LoadURL created a navigation entry, now simulate the RenderView sending
140 // a notification that it actually navigated.
141 CommitPendingNavigation();
144 void TestWebContents::TestSetIsLoading(bool value
) {
145 SetIsLoading(GetRenderViewHost(), value
, true, NULL
);
148 void TestWebContents::CommitPendingNavigation() {
149 const NavigationEntry
* entry
= GetController().GetPendingEntry();
152 // If we are doing a cross-site navigation, this simulates the current RFH
153 // notifying that it has unloaded so the pending RFH is resumed and can
155 // PlzNavigate: the pending RFH is not created before the navigation commit,
156 // so it is necessary to simulate the IO thread response here to commit in the
157 // proper renderer. It is necessary to call PrepareForCommit before getting
158 // the main and the pending frame because when we are trying to navigate to a
159 // webui from a new tab, a RenderFrameHost is created to display it that is
160 // committed immediately (since it is a new tab). Therefore the main frame is
161 // replaced without a pending frame being created, and we don't get the right
162 // values for the RFH to navigate: we try to use the old one that has been
163 // deleted in the meantime.
164 GetMainFrame()->PrepareForCommit(entry
->GetURL());
166 TestRenderFrameHost
* old_rfh
= GetMainFrame();
167 TestRenderFrameHost
* rfh
= GetPendingMainFrame();
171 int page_id
= entry
->GetPageID();
173 // It's a new navigation, assign a never-seen page id to it.
174 page_id
= GetMaxPageIDForSiteInstance(rfh
->GetSiteInstance()) + 1;
177 rfh
->SendNavigate(page_id
, entry
->GetURL());
178 // Simulate the SwapOut_ACK. This is needed when cross-site navigation
181 old_rfh
->OnSwappedOut();
184 void TestWebContents::ProceedWithCrossSiteNavigation() {
185 if (!GetPendingMainFrame())
187 GetMainFrame()->SendBeforeUnloadACK(true);
190 RenderViewHostDelegateView
* TestWebContents::GetDelegateView() {
191 if (delegate_view_override_
)
192 return delegate_view_override_
;
193 return WebContentsImpl::GetDelegateView();
196 void TestWebContents::SetOpener(TestWebContents
* opener
) {
197 // This is normally only set in the WebContents constructor, which also
198 // registers an observer for when the opener gets closed.
200 AddDestructionObserver(opener_
);
203 void TestWebContents::AddPendingContents(TestWebContents
* contents
) {
204 // This is normally only done in WebContentsImpl::CreateNewWindow.
205 pending_contents_
[contents
->GetRenderViewHost()->GetRoutingID()] = contents
;
206 AddDestructionObserver(contents
);
209 void TestWebContents::ExpectSetHistoryOffsetAndLength(int history_offset
,
210 int history_length
) {
211 expect_set_history_offset_and_length_
= true;
212 expect_set_history_offset_and_length_history_offset_
= history_offset
;
213 expect_set_history_offset_and_length_history_length_
= history_length
;
216 void TestWebContents::SetHistoryOffsetAndLength(int history_offset
,
217 int history_length
) {
218 EXPECT_TRUE(expect_set_history_offset_and_length_
);
219 expect_set_history_offset_and_length_
= false;
220 EXPECT_EQ(expect_set_history_offset_and_length_history_offset_
,
222 EXPECT_EQ(expect_set_history_offset_and_length_history_length_
,
226 void TestWebContents::TestDidFinishLoad(const GURL
& url
) {
227 FrameHostMsg_DidFinishLoad
msg(0, url
);
228 frame_tree_
.root()->current_frame_host()->OnMessageReceived(msg
);
231 void TestWebContents::TestDidFailLoadWithError(
234 const base::string16
& error_description
) {
235 FrameHostMsg_DidFailLoadWithError
msg(
236 0, url
, error_code
, error_description
);
237 frame_tree_
.root()->current_frame_host()->OnMessageReceived(msg
);
240 void TestWebContents::CreateNewWindow(
241 int render_process_id
,
243 int main_frame_route_id
,
244 const ViewHostMsg_CreateWindow_Params
& params
,
245 SessionStorageNamespace
* session_storage_namespace
) {
248 void TestWebContents::CreateNewWidget(int render_process_id
,
250 blink::WebPopupType popup_type
) {
253 void TestWebContents::CreateNewFullscreenWidget(int render_process_id
,
257 void TestWebContents::ShowCreatedWindow(int route_id
,
258 WindowOpenDisposition disposition
,
259 const gfx::Rect
& initial_rect
,
263 void TestWebContents::ShowCreatedWidget(int route_id
,
264 const gfx::Rect
& initial_rect
) {
267 void TestWebContents::ShowCreatedFullscreenWidget(int route_id
) {
270 } // namespace content