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 transition_cross_site(false),
30 delegate_view_override_(NULL),
31 expect_set_history_length_and_prune_(false),
32 expect_set_history_length_and_prune_site_instance_(NULL),
33 expect_set_history_length_and_prune_history_length_(0),
34 expect_set_history_length_and_prune_min_page_id_(-1) {
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_length_and_prune_);
48 RenderViewHost* TestWebContents::GetPendingRenderViewHost() const {
49 return GetRenderManager()->pending_render_view_host();
52 TestRenderViewHost* TestWebContents::pending_test_rvh() const {
53 return static_cast<TestRenderViewHost*>(GetPendingRenderViewHost());
56 void TestWebContents::TestDidNavigate(RenderViewHost* render_view_host,
59 PageTransition transition) {
60 TestDidNavigateWithReferrer(render_view_host,
67 void TestWebContents::TestDidNavigateWithReferrer(
68 RenderViewHost* render_view_host,
71 const Referrer& referrer,
72 PageTransition transition) {
73 FrameHostMsg_DidCommitProvisionalLoad_Params params;
75 params.page_id = page_id;
77 params.referrer = referrer;
78 params.transition = transition;
79 params.redirects = std::vector<GURL>();
80 params.should_update_history = false;
81 params.searchable_form_url = GURL();
82 params.searchable_form_encoding = std::string();
83 params.security_info = std::string();
84 params.gesture = NavigationGestureUser;
85 params.was_within_same_page = false;
86 params.is_post = false;
87 params.page_state = PageState::CreateFromURL(url);
89 RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>(render_view_host);
90 RenderFrameHostImpl* rfh = RenderFrameHostImpl::FromID(
91 rvh->GetProcess()->GetID(), rvh->main_frame_routing_id());
92 frame_tree_.root()->navigator()->DidNavigate(rfh, params);
95 WebPreferences TestWebContents::TestGetWebkitPrefs() {
96 return GetWebkitPrefs();
99 bool TestWebContents::CreateRenderViewForRenderManager(
100 RenderViewHost* render_view_host,
102 CrossProcessFrameConnector* frame_connector) {
103 UpdateMaxPageIDIfNecessary(render_view_host);
104 // This will go to a TestRenderViewHost.
105 static_cast<RenderViewHostImpl*>(
106 render_view_host)->CreateRenderView(base::string16(),
112 WebContents* TestWebContents::Clone() {
113 WebContentsImpl* contents =
114 Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
115 contents->GetController().CopyStateFrom(controller_);
119 void TestWebContents::NavigateAndCommit(const GURL& url) {
120 GetController().LoadURL(
121 url, Referrer(), PAGE_TRANSITION_LINK, std::string());
122 GURL loaded_url(url);
123 bool reverse_on_redirect = false;
124 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
125 &loaded_url, GetBrowserContext(), &reverse_on_redirect);
127 // LoadURL created a navigation entry, now simulate the RenderView sending
128 // a notification that it actually navigated.
129 CommitPendingNavigation();
132 void TestWebContents::TestSetIsLoading(bool value) {
133 SetIsLoading(GetRenderViewHost(), value, true, NULL);
136 void TestWebContents::CommitPendingNavigation() {
137 // If we are doing a cross-site navigation, this simulates the current RVH
138 // notifying that it has unloaded so the pending RVH is resumed and can
140 ProceedWithCrossSiteNavigation();
141 RenderViewHost* old_rvh = GetRenderViewHost();
142 TestRenderViewHost* rvh =
143 static_cast<TestRenderViewHost*>(GetPendingRenderViewHost());
145 rvh = static_cast<TestRenderViewHost*>(old_rvh);
147 const NavigationEntry* entry = GetController().GetPendingEntry();
149 int page_id = entry->GetPageID();
151 // It's a new navigation, assign a never-seen page id to it.
152 page_id = GetMaxPageIDForSiteInstance(rvh->GetSiteInstance()) + 1;
155 rvh->SendNavigate(page_id, entry->GetURL());
156 // Simulate the SwapOut_ACK. This is needed when cross-site navigation happens
159 static_cast<RenderViewHostImpl*>(old_rvh)->OnSwappedOut(false);
162 void TestWebContents::ProceedWithCrossSiteNavigation() {
163 if (!GetPendingRenderViewHost())
165 TestRenderViewHost* rvh = static_cast<TestRenderViewHost*>(
166 GetRenderViewHost());
167 rvh->SendBeforeUnloadACK(true);
170 RenderViewHostDelegateView* TestWebContents::GetDelegateView() {
171 if (delegate_view_override_)
172 return delegate_view_override_;
173 return WebContentsImpl::GetDelegateView();
176 void TestWebContents::SetOpener(TestWebContents* opener) {
177 // This is normally only set in the WebContents constructor, which also
178 // registers an observer for when the opener gets closed.
180 AddDestructionObserver(opener_);
183 void TestWebContents::AddPendingContents(TestWebContents* contents) {
184 // This is normally only done in WebContentsImpl::CreateNewWindow.
185 pending_contents_[contents->GetRenderViewHost()->GetRoutingID()] = contents;
186 AddDestructionObserver(contents);
189 void TestWebContents::ExpectSetHistoryLengthAndPrune(
190 const SiteInstance* site_instance,
193 expect_set_history_length_and_prune_ = true;
194 expect_set_history_length_and_prune_site_instance_ =
195 static_cast<const SiteInstanceImpl*>(site_instance);
196 expect_set_history_length_and_prune_history_length_ = history_length;
197 expect_set_history_length_and_prune_min_page_id_ = min_page_id;
200 void TestWebContents::SetHistoryLengthAndPrune(
201 const SiteInstance* site_instance, int history_length,
203 EXPECT_TRUE(expect_set_history_length_and_prune_);
204 expect_set_history_length_and_prune_ = false;
205 EXPECT_EQ(expect_set_history_length_and_prune_site_instance_, site_instance);
206 EXPECT_EQ(expect_set_history_length_and_prune_history_length_,
208 EXPECT_EQ(expect_set_history_length_and_prune_min_page_id_, min_page_id);
211 void TestWebContents::TestDidFinishLoad(const GURL& url) {
212 FrameHostMsg_DidFinishLoad msg(0, url);
213 frame_tree_.root()->current_frame_host()->OnMessageReceived(msg);
216 void TestWebContents::TestDidFailLoadWithError(
219 const base::string16& error_description) {
220 FrameHostMsg_DidFailLoadWithError msg(
221 0, url, error_code, error_description);
222 frame_tree_.root()->current_frame_host()->OnMessageReceived(msg);
225 void TestWebContents::CreateNewWindow(
226 int render_process_id,
228 int main_frame_route_id,
229 const ViewHostMsg_CreateWindow_Params& params,
230 SessionStorageNamespace* session_storage_namespace) {
233 void TestWebContents::CreateNewWidget(int render_process_id,
235 blink::WebPopupType popup_type) {
238 void TestWebContents::CreateNewFullscreenWidget(int render_process_id,
242 void TestWebContents::ShowCreatedWindow(int route_id,
243 WindowOpenDisposition disposition,
244 const gfx::Rect& initial_pos,
248 void TestWebContents::ShowCreatedWidget(int route_id,
249 const gfx::Rect& initial_pos) {
252 void TestWebContents::ShowCreatedFullscreenWidget(int route_id) {
255 } // namespace content