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 "content/browser/browser_url_handler_impl.h"
10 #include "content/browser/frame_host/cross_process_frame_connector.h"
11 #include "content/browser/frame_host/navigation_entry_impl.h"
12 #include "content/browser/frame_host/navigator.h"
13 #include "content/browser/renderer_host/render_view_host_impl.h"
14 #include "content/browser/site_instance_impl.h"
15 #include "content/common/frame_messages.h"
16 #include "content/common/view_messages.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "content/public/browser/notification_source.h"
19 #include "content/public/browser/notification_types.h"
20 #include "content/public/common/page_state.h"
21 #include "content/public/common/page_transition_types.h"
22 #include "content/public/test/mock_render_process_host.h"
23 #include "content/test/test_render_view_host.h"
27 TestWebContents::TestWebContents(BrowserContext
* browser_context
)
28 : WebContentsImpl(browser_context
, NULL
),
29 delegate_view_override_(NULL
),
30 expect_set_history_length_and_prune_(false),
31 expect_set_history_length_and_prune_site_instance_(NULL
),
32 expect_set_history_length_and_prune_history_length_(0),
33 expect_set_history_length_and_prune_min_page_id_(-1) {
36 TestWebContents
* TestWebContents::Create(BrowserContext
* browser_context
,
37 SiteInstance
* instance
) {
38 TestWebContents
* test_web_contents
= new TestWebContents(browser_context
);
39 test_web_contents
->Init(WebContents::CreateParams(browser_context
, instance
));
40 return test_web_contents
;
43 TestWebContents::~TestWebContents() {
44 EXPECT_FALSE(expect_set_history_length_and_prune_
);
47 RenderViewHost
* TestWebContents::GetPendingRenderViewHost() const {
48 return GetRenderManager()->pending_render_view_host();
51 TestRenderViewHost
* TestWebContents::pending_test_rvh() const {
52 return static_cast<TestRenderViewHost
*>(GetPendingRenderViewHost());
55 void TestWebContents::TestDidNavigate(RenderViewHost
* render_view_host
,
58 PageTransition transition
) {
59 TestDidNavigateWithReferrer(render_view_host
,
66 void TestWebContents::TestDidNavigateWithReferrer(
67 RenderViewHost
* render_view_host
,
70 const Referrer
& referrer
,
71 PageTransition transition
) {
72 FrameHostMsg_DidCommitProvisionalLoad_Params params
;
74 params
.page_id
= page_id
;
76 params
.referrer
= referrer
;
77 params
.transition
= transition
;
78 params
.redirects
= std::vector
<GURL
>();
79 params
.should_update_history
= false;
80 params
.searchable_form_url
= GURL();
81 params
.searchable_form_encoding
= std::string();
82 params
.security_info
= std::string();
83 params
.gesture
= NavigationGestureUser
;
84 params
.was_within_same_page
= false;
85 params
.is_post
= false;
86 params
.page_state
= PageState::CreateFromURL(url
);
88 RenderViewHostImpl
* rvh
= static_cast<RenderViewHostImpl
*>(render_view_host
);
89 RenderFrameHostImpl
* rfh
= RenderFrameHostImpl::FromID(
90 rvh
->GetProcess()->GetID(), rvh
->main_frame_routing_id());
91 frame_tree_
.root()->navigator()->DidNavigate(rfh
, params
);
94 WebPreferences
TestWebContents::TestGetWebkitPrefs() {
95 return GetWebkitPrefs();
98 bool TestWebContents::CreateRenderViewForRenderManager(
99 RenderViewHost
* render_view_host
,
101 int proxy_routing_id
,
102 bool for_main_frame
) {
103 UpdateMaxPageIDIfNecessary(render_view_host
);
104 // This will go to a TestRenderViewHost.
105 static_cast<RenderViewHostImpl
*>(
106 render_view_host
)->CreateRenderView(base::string16(),
113 WebContents
* TestWebContents::Clone() {
114 WebContentsImpl
* contents
=
115 Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
116 contents
->GetController().CopyStateFrom(controller_
);
120 void TestWebContents::NavigateAndCommit(const GURL
& url
) {
121 GetController().LoadURL(
122 url
, Referrer(), PAGE_TRANSITION_LINK
, std::string());
123 GURL
loaded_url(url
);
124 bool reverse_on_redirect
= false;
125 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
126 &loaded_url
, GetBrowserContext(), &reverse_on_redirect
);
128 // LoadURL created a navigation entry, now simulate the RenderView sending
129 // a notification that it actually navigated.
130 CommitPendingNavigation();
133 void TestWebContents::TestSetIsLoading(bool value
) {
134 SetIsLoading(GetRenderViewHost(), value
, true, NULL
);
137 void TestWebContents::CommitPendingNavigation() {
138 // If we are doing a cross-site navigation, this simulates the current RVH
139 // notifying that it has unloaded so the pending RVH is resumed and can
141 ProceedWithCrossSiteNavigation();
142 RenderViewHost
* old_rvh
= GetRenderViewHost();
143 TestRenderViewHost
* rvh
=
144 static_cast<TestRenderViewHost
*>(GetPendingRenderViewHost());
146 rvh
= static_cast<TestRenderViewHost
*>(old_rvh
);
148 const NavigationEntry
* entry
= GetController().GetPendingEntry();
150 int page_id
= entry
->GetPageID();
152 // It's a new navigation, assign a never-seen page id to it.
153 page_id
= GetMaxPageIDForSiteInstance(rvh
->GetSiteInstance()) + 1;
156 rvh
->SendNavigate(page_id
, entry
->GetURL());
157 // Simulate the SwapOut_ACK. This is needed when cross-site navigation happens
160 static_cast<RenderViewHostImpl
*>(old_rvh
)->OnSwappedOut(false);
163 void TestWebContents::ProceedWithCrossSiteNavigation() {
164 if (!GetPendingRenderViewHost())
166 TestRenderViewHost
* rvh
= static_cast<TestRenderViewHost
*>(
167 GetRenderViewHost());
168 rvh
->SendBeforeUnloadACK(true);
171 RenderViewHostDelegateView
* TestWebContents::GetDelegateView() {
172 if (delegate_view_override_
)
173 return delegate_view_override_
;
174 return WebContentsImpl::GetDelegateView();
177 void TestWebContents::SetOpener(TestWebContents
* opener
) {
178 // This is normally only set in the WebContents constructor, which also
179 // registers an observer for when the opener gets closed.
181 AddDestructionObserver(opener_
);
184 void TestWebContents::AddPendingContents(TestWebContents
* contents
) {
185 // This is normally only done in WebContentsImpl::CreateNewWindow.
186 pending_contents_
[contents
->GetRenderViewHost()->GetRoutingID()] = contents
;
187 AddDestructionObserver(contents
);
190 void TestWebContents::ExpectSetHistoryLengthAndPrune(
191 const SiteInstance
* site_instance
,
194 expect_set_history_length_and_prune_
= true;
195 expect_set_history_length_and_prune_site_instance_
=
196 static_cast<const SiteInstanceImpl
*>(site_instance
);
197 expect_set_history_length_and_prune_history_length_
= history_length
;
198 expect_set_history_length_and_prune_min_page_id_
= min_page_id
;
201 void TestWebContents::SetHistoryLengthAndPrune(
202 const SiteInstance
* site_instance
, int history_length
,
204 EXPECT_TRUE(expect_set_history_length_and_prune_
);
205 expect_set_history_length_and_prune_
= false;
206 EXPECT_EQ(expect_set_history_length_and_prune_site_instance_
, site_instance
);
207 EXPECT_EQ(expect_set_history_length_and_prune_history_length_
,
209 EXPECT_EQ(expect_set_history_length_and_prune_min_page_id_
, min_page_id
);
212 void TestWebContents::TestDidFinishLoad(const GURL
& url
) {
213 FrameHostMsg_DidFinishLoad
msg(0, url
);
214 frame_tree_
.root()->current_frame_host()->OnMessageReceived(msg
);
217 void TestWebContents::TestDidFailLoadWithError(
220 const base::string16
& error_description
) {
221 FrameHostMsg_DidFailLoadWithError
msg(
222 0, url
, error_code
, error_description
);
223 frame_tree_
.root()->current_frame_host()->OnMessageReceived(msg
);
226 void TestWebContents::CreateNewWindow(
227 int render_process_id
,
229 int main_frame_route_id
,
230 const ViewHostMsg_CreateWindow_Params
& params
,
231 SessionStorageNamespace
* session_storage_namespace
) {
234 void TestWebContents::CreateNewWidget(int render_process_id
,
236 blink::WebPopupType popup_type
) {
239 void TestWebContents::CreateNewFullscreenWidget(int render_process_id
,
243 void TestWebContents::ShowCreatedWindow(int route_id
,
244 WindowOpenDisposition disposition
,
245 const gfx::Rect
& initial_pos
,
249 void TestWebContents::ShowCreatedWidget(int route_id
,
250 const gfx::Rect
& initial_pos
) {
253 void TestWebContents::ShowCreatedFullscreenWidget(int route_id
) {
256 } // namespace content