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_state.h"
19 #include "content/public/common/page_transition_types.h"
20 #include "content/public/test/mock_render_process_host.h"
24 TestWebContents::TestWebContents(BrowserContext
* browser_context
)
25 : WebContentsImpl(browser_context
, NULL
),
26 transition_cross_site(false),
27 delegate_view_override_(NULL
),
28 expect_set_history_length_and_prune_(false),
29 expect_set_history_length_and_prune_site_instance_(NULL
),
30 expect_set_history_length_and_prune_history_length_(0),
31 expect_set_history_length_and_prune_min_page_id_(-1) {
34 TestWebContents
* TestWebContents::Create(BrowserContext
* browser_context
,
35 SiteInstance
* instance
) {
36 TestWebContents
* test_web_contents
= new TestWebContents(browser_context
);
37 test_web_contents
->Init(WebContents::CreateParams(browser_context
, instance
));
38 return test_web_contents
;
41 TestWebContents::~TestWebContents() {
42 EXPECT_FALSE(expect_set_history_length_and_prune_
);
45 RenderViewHost
* TestWebContents::GetPendingRenderViewHost() const {
46 return render_manager_
.pending_render_view_host_
;
49 TestRenderViewHost
* TestWebContents::pending_test_rvh() const {
50 return static_cast<TestRenderViewHost
*>(GetPendingRenderViewHost());
53 void TestWebContents::TestDidNavigate(RenderViewHost
* render_view_host
,
56 PageTransition transition
) {
57 TestDidNavigateWithReferrer(render_view_host
,
64 void TestWebContents::TestDidNavigateWithReferrer(
65 RenderViewHost
* render_view_host
,
68 const Referrer
& referrer
,
69 PageTransition transition
) {
70 ViewHostMsg_FrameNavigate_Params params
;
72 params
.page_id
= page_id
;
74 params
.referrer
= referrer
;
75 params
.transition
= transition
;
76 params
.redirects
= std::vector
<GURL
>();
77 params
.should_update_history
= false;
78 params
.searchable_form_url
= GURL();
79 params
.searchable_form_encoding
= std::string();
80 params
.security_info
= std::string();
81 params
.gesture
= NavigationGestureUser
;
82 params
.was_within_same_page
= false;
83 params
.is_post
= false;
84 params
.page_state
= PageState::CreateFromURL(url
);
86 DidNavigate(render_view_host
, params
);
89 WebPreferences
TestWebContents::TestGetWebkitPrefs() {
90 return GetWebkitPrefs();
93 bool TestWebContents::CreateRenderViewForRenderManager(
94 RenderViewHost
* render_view_host
, int opener_route_id
) {
95 // This will go to a TestRenderViewHost.
96 static_cast<RenderViewHostImpl
*>(
97 render_view_host
)->CreateRenderView(string16(),
103 WebContents
* TestWebContents::Clone() {
104 WebContentsImpl
* contents
=
105 Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
106 contents
->GetController().CopyStateFrom(controller_
);
110 void TestWebContents::NavigateAndCommit(const GURL
& url
) {
111 GetController().LoadURL(
112 url
, Referrer(), PAGE_TRANSITION_LINK
, std::string());
113 GURL
loaded_url(url
);
114 bool reverse_on_redirect
= false;
115 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
116 &loaded_url
, GetBrowserContext(), &reverse_on_redirect
);
118 // LoadURL created a navigation entry, now simulate the RenderView sending
119 // a notification that it actually navigated.
120 CommitPendingNavigation();
123 void TestWebContents::TestSetIsLoading(bool value
) {
124 SetIsLoading(GetRenderViewHost(), value
, NULL
);
127 void TestWebContents::CommitPendingNavigation() {
128 // If we are doing a cross-site navigation, this simulates the current RVH
129 // notifying that it has unloaded so the pending RVH is resumed and can
131 ProceedWithCrossSiteNavigation();
132 RenderViewHost
* old_rvh
= GetRenderViewHost();
133 TestRenderViewHost
* rvh
=
134 static_cast<TestRenderViewHost
*>(GetPendingRenderViewHost());
136 rvh
= static_cast<TestRenderViewHost
*>(old_rvh
);
138 const NavigationEntry
* entry
= GetController().GetPendingEntry();
140 int page_id
= entry
->GetPageID();
142 // It's a new navigation, assign a never-seen page id to it.
143 page_id
= GetMaxPageIDForSiteInstance(rvh
->GetSiteInstance()) + 1;
146 // Simulate the SwapOut_ACK that happens when we swap out the old
147 // RVH, before the navigation commits. This is needed when
148 // cross-site navigation happens (old_rvh != rvh).
150 static_cast<RenderViewHostImpl
*>(old_rvh
)->OnSwappedOut(false);
151 rvh
->SendNavigate(page_id
, entry
->GetURL());
154 void TestWebContents::ProceedWithCrossSiteNavigation() {
155 if (!GetPendingRenderViewHost())
157 TestRenderViewHost
* rvh
= static_cast<TestRenderViewHost
*>(
158 GetRenderViewHost());
159 rvh
->SendShouldCloseACK(true);
162 RenderViewHostDelegateView
* TestWebContents::GetDelegateView() {
163 if (delegate_view_override_
)
164 return delegate_view_override_
;
165 return WebContentsImpl::GetDelegateView();
168 void TestWebContents::SetOpener(TestWebContents
* opener
) {
169 // This is normally only set in the WebContents constructor, which also
170 // registers an observer for when the opener gets closed.
172 AddDestructionObserver(opener_
);
175 void TestWebContents::AddPendingContents(TestWebContents
* contents
) {
176 // This is normally only done in WebContentsImpl::CreateNewWindow.
177 pending_contents_
[contents
->GetRenderViewHost()->GetRoutingID()] = contents
;
178 AddDestructionObserver(contents
);
181 void TestWebContents::ExpectSetHistoryLengthAndPrune(
182 const SiteInstance
* site_instance
,
185 expect_set_history_length_and_prune_
= true;
186 expect_set_history_length_and_prune_site_instance_
=
187 static_cast<const SiteInstanceImpl
*>(site_instance
);
188 expect_set_history_length_and_prune_history_length_
= history_length
;
189 expect_set_history_length_and_prune_min_page_id_
= min_page_id
;
192 void TestWebContents::SetHistoryLengthAndPrune(
193 const SiteInstance
* site_instance
, int history_length
,
195 EXPECT_TRUE(expect_set_history_length_and_prune_
);
196 expect_set_history_length_and_prune_
= false;
197 EXPECT_EQ(expect_set_history_length_and_prune_site_instance_
, site_instance
);
198 EXPECT_EQ(expect_set_history_length_and_prune_history_length_
,
200 EXPECT_EQ(expect_set_history_length_and_prune_min_page_id_
, min_page_id
);
203 void TestWebContents::TestDidFinishLoad(int64 frame_id
,
205 bool is_main_frame
) {
206 ViewHostMsg_DidFinishLoad
msg(0, frame_id
, url
, is_main_frame
);
207 OnMessageReceived(GetRenderViewHost(), msg
);
210 void TestWebContents::TestDidFailLoadWithError(
215 const string16
& error_description
) {
216 ViewHostMsg_DidFailLoadWithError
msg(
217 0, frame_id
, url
, is_main_frame
, error_code
, error_description
);
218 OnMessageReceived(GetRenderViewHost(), msg
);
221 void TestWebContents::CreateNewWindow(
223 int main_frame_route_id
,
224 const ViewHostMsg_CreateWindow_Params
& params
,
225 SessionStorageNamespace
* session_storage_namespace
) {
228 void TestWebContents::CreateNewWidget(int route_id
,
229 WebKit::WebPopupType popup_type
) {
232 void TestWebContents::CreateNewFullscreenWidget(int route_id
) {
235 void TestWebContents::ShowCreatedWindow(int route_id
,
236 WindowOpenDisposition disposition
,
237 const gfx::Rect
& initial_pos
,
241 void TestWebContents::ShowCreatedWidget(int route_id
,
242 const gfx::Rect
& initial_pos
) {
245 void TestWebContents::ShowCreatedFullscreenWidget(int route_id
) {
248 } // namespace content