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/renderer_host/render_view_host_impl.h"
11 #include "content/browser/renderer_host/test_render_view_host.h"
12 #include "content/browser/site_instance_impl.h"
13 #include "content/browser/web_contents/navigation_entry_impl.h"
14 #include "content/common/view_messages.h"
15 #include "content/public/browser/notification_registrar.h"
16 #include "content/public/browser/notification_source.h"
17 #include "content/public/browser/notification_types.h"
18 #include "content/public/common/page_transition_types.h"
19 #include "content/public/common/password_form.h"
20 #include "content/public/test/mock_render_process_host.h"
21 #include "webkit/glue/glue_serialize.h"
25 TestWebContents::TestWebContents(BrowserContext
* browser_context
)
26 : WebContentsImpl(browser_context
, NULL
),
27 transition_cross_site(false),
28 delegate_view_override_(NULL
),
29 expect_set_history_length_and_prune_(false),
30 expect_set_history_length_and_prune_site_instance_(NULL
),
31 expect_set_history_length_and_prune_history_length_(0),
32 expect_set_history_length_and_prune_min_page_id_(-1) {
35 TestWebContents
* TestWebContents::Create(BrowserContext
* browser_context
,
36 SiteInstance
* instance
) {
37 TestWebContents
* test_web_contents
= new TestWebContents(browser_context
);
38 test_web_contents
->Init(WebContents::CreateParams(browser_context
, instance
));
39 return test_web_contents
;
42 TestWebContents::~TestWebContents() {
43 EXPECT_FALSE(expect_set_history_length_and_prune_
);
46 RenderViewHost
* TestWebContents::GetPendingRenderViewHost() const {
47 return render_manager_
.pending_render_view_host_
;
50 TestRenderViewHost
* TestWebContents::pending_test_rvh() const {
51 return static_cast<TestRenderViewHost
*>(GetPendingRenderViewHost());
54 void TestWebContents::TestDidNavigate(RenderViewHost
* render_view_host
,
57 PageTransition transition
) {
58 TestDidNavigateWithReferrer(render_view_host
,
65 void TestWebContents::TestDidNavigateWithReferrer(
66 RenderViewHost
* render_view_host
,
69 const Referrer
& referrer
,
70 PageTransition transition
) {
71 ViewHostMsg_FrameNavigate_Params params
;
73 params
.page_id
= page_id
;
75 params
.referrer
= referrer
;
76 params
.transition
= transition
;
77 params
.redirects
= std::vector
<GURL
>();
78 params
.should_update_history
= false;
79 params
.searchable_form_url
= GURL();
80 params
.searchable_form_encoding
= std::string();
81 params
.password_form
= PasswordForm();
82 params
.security_info
= std::string();
83 params
.gesture
= NavigationGestureUser
;
84 params
.was_within_same_page
= false;
85 params
.is_post
= false;
86 params
.content_state
= webkit_glue::CreateHistoryStateForURL(GURL(url
));
88 DidNavigate(render_view_host
, params
);
91 webkit_glue::WebPreferences
TestWebContents::TestGetWebkitPrefs() {
92 return GetWebkitPrefs();
95 bool TestWebContents::CreateRenderViewForRenderManager(
96 RenderViewHost
* render_view_host
, int opener_route_id
) {
97 // This will go to a TestRenderViewHost.
98 static_cast<RenderViewHostImpl
*>(
99 render_view_host
)->CreateRenderView(string16(),
105 WebContents
* TestWebContents::Clone() {
106 WebContentsImpl
* contents
=
107 Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
108 contents
->GetController().CopyStateFrom(controller_
);
112 void TestWebContents::NavigateAndCommit(const GURL
& url
) {
113 GetController().LoadURL(
114 url
, Referrer(), PAGE_TRANSITION_LINK
, std::string());
115 GURL
loaded_url(url
);
116 bool reverse_on_redirect
= false;
117 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
118 &loaded_url
, GetBrowserContext(), &reverse_on_redirect
);
120 // LoadURL created a navigation entry, now simulate the RenderView sending
121 // a notification that it actually navigated.
122 CommitPendingNavigation();
125 void TestWebContents::TestSetIsLoading(bool value
) {
126 SetIsLoading(value
, NULL
);
129 void TestWebContents::CommitPendingNavigation() {
130 // If we are doing a cross-site navigation, this simulates the current RVH
131 // notifying that it has unloaded so the pending RVH is resumed and can
133 ProceedWithCrossSiteNavigation();
134 RenderViewHost
* old_rvh
= render_manager_
.current_host();
135 TestRenderViewHost
* rvh
=
136 static_cast<TestRenderViewHost
*>(GetPendingRenderViewHost());
138 rvh
= static_cast<TestRenderViewHost
*>(old_rvh
);
140 const NavigationEntry
* entry
= GetController().GetPendingEntry();
142 int page_id
= entry
->GetPageID();
144 // It's a new navigation, assign a never-seen page id to it.
145 page_id
= GetMaxPageIDForSiteInstance(rvh
->GetSiteInstance()) + 1;
147 rvh
->SendNavigate(page_id
, entry
->GetURL());
149 // Simulate the SwapOut_ACK that fires if you commit a cross-site navigation
150 // without making any network requests.
152 static_cast<RenderViewHostImpl
*>(old_rvh
)->OnSwapOutACK(false);
155 void TestWebContents::ProceedWithCrossSiteNavigation() {
156 if (!GetPendingRenderViewHost())
158 TestRenderViewHost
* rvh
= static_cast<TestRenderViewHost
*>(
159 render_manager_
.current_host());
160 rvh
->SendShouldCloseACK(true);
163 RenderViewHostDelegateView
* TestWebContents::GetDelegateView() {
164 if (delegate_view_override_
)
165 return delegate_view_override_
;
166 return WebContentsImpl::GetDelegateView();
169 void TestWebContents::SetOpener(TestWebContents
* opener
) {
170 // This is normally only set in the WebContents constructor, which also
171 // registers an observer for when the opener gets closed.
173 registrar_
.Add(this, NOTIFICATION_WEB_CONTENTS_DESTROYED
,
174 Source
<WebContents
>(opener_
));
177 void TestWebContents::ExpectSetHistoryLengthAndPrune(
178 const SiteInstance
* site_instance
,
181 expect_set_history_length_and_prune_
= true;
182 expect_set_history_length_and_prune_site_instance_
=
183 static_cast<const SiteInstanceImpl
*>(site_instance
);
184 expect_set_history_length_and_prune_history_length_
= history_length
;
185 expect_set_history_length_and_prune_min_page_id_
= min_page_id
;
188 void TestWebContents::SetHistoryLengthAndPrune(
189 const SiteInstance
* site_instance
, int history_length
,
191 EXPECT_TRUE(expect_set_history_length_and_prune_
);
192 expect_set_history_length_and_prune_
= false;
193 EXPECT_EQ(expect_set_history_length_and_prune_site_instance_
, site_instance
);
194 EXPECT_EQ(expect_set_history_length_and_prune_history_length_
,
196 EXPECT_EQ(expect_set_history_length_and_prune_min_page_id_
, min_page_id
);
199 void TestWebContents::TestDidFinishLoad(int64 frame_id
,
201 bool is_main_frame
) {
202 ViewHostMsg_DidFinishLoad
msg(0, frame_id
, url
, is_main_frame
);
203 OnMessageReceived(render_manager_
.current_host(), msg
);
206 void TestWebContents::TestDidFailLoadWithError(
211 const string16
& error_description
) {
212 ViewHostMsg_DidFailLoadWithError
msg(
213 0, frame_id
, url
, is_main_frame
, error_code
, error_description
);
214 OnMessageReceived(render_manager_
.current_host(), msg
);
217 void TestWebContents::CreateNewWindow(
219 const ViewHostMsg_CreateWindow_Params
& params
,
220 SessionStorageNamespace
* session_storage_namespace
) {
223 void TestWebContents::CreateNewWidget(int route_id
,
224 WebKit::WebPopupType popup_type
) {
227 void TestWebContents::CreateNewFullscreenWidget(int route_id
) {
230 void TestWebContents::ShowCreatedWindow(int route_id
,
231 WindowOpenDisposition disposition
,
232 const gfx::Rect
& initial_pos
,
236 void TestWebContents::ShowCreatedWidget(int route_id
,
237 const gfx::Rect
& initial_pos
) {
240 void TestWebContents::ShowCreatedFullscreenWidget(int route_id
) {
243 } // namespace content