Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / test / test_web_contents.cc
bloba8ffafb733244aa8db18d0434546a683a89b178c
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"
7 #include <utility>
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"
25 namespace content {
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 TestRenderFrameHost* TestWebContents::GetMainFrame() {
48 return static_cast<TestRenderFrameHost*>(WebContentsImpl::GetMainFrame());
51 TestRenderViewHost* TestWebContents::GetRenderViewHost() const {
52 return static_cast<TestRenderViewHost*>(
53 WebContentsImpl::GetRenderViewHost());
56 TestRenderFrameHost* TestWebContents::GetPendingMainFrame() const {
57 return static_cast<TestRenderFrameHost*>(
58 GetRenderManager()->pending_frame_host());
61 void TestWebContents::TestDidNavigate(RenderFrameHost* render_frame_host,
62 int page_id,
63 const GURL& url,
64 PageTransition transition) {
65 TestDidNavigateWithReferrer(render_frame_host,
66 page_id,
67 url,
68 Referrer(),
69 transition);
72 void TestWebContents::TestDidNavigateWithReferrer(
73 RenderFrameHost* render_frame_host,
74 int page_id,
75 const GURL& url,
76 const Referrer& referrer,
77 PageTransition transition) {
78 FrameHostMsg_DidCommitProvisionalLoad_Params params;
80 params.page_id = page_id;
81 params.url = url;
82 params.referrer = referrer;
83 params.transition = transition;
84 params.redirects = std::vector<GURL>();
85 params.should_update_history = false;
86 params.searchable_form_url = GURL();
87 params.searchable_form_encoding = std::string();
88 params.security_info = std::string();
89 params.gesture = NavigationGestureUser;
90 params.was_within_same_page = false;
91 params.is_post = false;
92 params.page_state = PageState::CreateFromURL(url);
94 RenderFrameHostImpl* rfhi =
95 static_cast<RenderFrameHostImpl*>(render_frame_host);
96 rfhi->frame_tree_node()->navigator()->DidNavigate(rfhi, params);
99 WebPreferences TestWebContents::TestComputeWebkitPrefs() {
100 return ComputeWebkitPrefs();
103 bool TestWebContents::CreateRenderViewForRenderManager(
104 RenderViewHost* render_view_host,
105 int opener_route_id,
106 int proxy_routing_id,
107 bool for_main_frame) {
108 UpdateMaxPageIDIfNecessary(render_view_host);
109 // This will go to a TestRenderViewHost.
110 static_cast<RenderViewHostImpl*>(
111 render_view_host)->CreateRenderView(base::string16(),
112 opener_route_id,
113 proxy_routing_id,
114 -1, false);
115 return true;
118 WebContents* TestWebContents::Clone() {
119 WebContentsImpl* contents =
120 Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
121 contents->GetController().CopyStateFrom(controller_);
122 return contents;
125 void TestWebContents::NavigateAndCommit(const GURL& url) {
126 GetController().LoadURL(
127 url, Referrer(), PAGE_TRANSITION_LINK, std::string());
128 GURL loaded_url(url);
129 bool reverse_on_redirect = false;
130 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
131 &loaded_url, GetBrowserContext(), &reverse_on_redirect);
133 // LoadURL created a navigation entry, now simulate the RenderView sending
134 // a notification that it actually navigated.
135 CommitPendingNavigation();
138 void TestWebContents::TestSetIsLoading(bool value) {
139 SetIsLoading(GetRenderViewHost(), value, true, NULL);
142 void TestWebContents::CommitPendingNavigation() {
143 // If we are doing a cross-site navigation, this simulates the current RVH
144 // notifying that it has unloaded so the pending RVH is resumed and can
145 // navigate.
146 ProceedWithCrossSiteNavigation();
147 TestRenderFrameHost* old_rfh = GetMainFrame();
148 TestRenderFrameHost* rfh = GetPendingMainFrame();
149 if (!rfh)
150 rfh = old_rfh;
152 const NavigationEntry* entry = GetController().GetPendingEntry();
153 DCHECK(entry);
154 int page_id = entry->GetPageID();
155 if (page_id == -1) {
156 // It's a new navigation, assign a never-seen page id to it.
157 page_id = GetMaxPageIDForSiteInstance(rfh->GetSiteInstance()) + 1;
160 rfh->SendNavigate(page_id, entry->GetURL());
161 // Simulate the SwapOut_ACK. This is needed when cross-site navigation happens
162 // (old_rfh != rfh).
163 if (old_rfh != rfh)
164 old_rfh->OnSwappedOut(false);
167 void TestWebContents::ProceedWithCrossSiteNavigation() {
168 if (!GetPendingMainFrame())
169 return;
170 GetMainFrame()->GetRenderViewHost()->SendBeforeUnloadACK(true);
173 RenderViewHostDelegateView* TestWebContents::GetDelegateView() {
174 if (delegate_view_override_)
175 return delegate_view_override_;
176 return WebContentsImpl::GetDelegateView();
179 void TestWebContents::SetOpener(TestWebContents* opener) {
180 // This is normally only set in the WebContents constructor, which also
181 // registers an observer for when the opener gets closed.
182 opener_ = opener;
183 AddDestructionObserver(opener_);
186 void TestWebContents::AddPendingContents(TestWebContents* contents) {
187 // This is normally only done in WebContentsImpl::CreateNewWindow.
188 pending_contents_[contents->GetRenderViewHost()->GetRoutingID()] = contents;
189 AddDestructionObserver(contents);
192 void TestWebContents::ExpectSetHistoryLengthAndPrune(
193 const SiteInstance* site_instance,
194 int history_length,
195 int32 min_page_id) {
196 expect_set_history_length_and_prune_ = true;
197 expect_set_history_length_and_prune_site_instance_ =
198 static_cast<const SiteInstanceImpl*>(site_instance);
199 expect_set_history_length_and_prune_history_length_ = history_length;
200 expect_set_history_length_and_prune_min_page_id_ = min_page_id;
203 void TestWebContents::SetHistoryLengthAndPrune(
204 const SiteInstance* site_instance, int history_length,
205 int32 min_page_id) {
206 EXPECT_TRUE(expect_set_history_length_and_prune_);
207 expect_set_history_length_and_prune_ = false;
208 EXPECT_EQ(expect_set_history_length_and_prune_site_instance_.get(),
209 site_instance);
210 EXPECT_EQ(expect_set_history_length_and_prune_history_length_,
211 history_length);
212 EXPECT_EQ(expect_set_history_length_and_prune_min_page_id_, min_page_id);
215 void TestWebContents::TestDidFinishLoad(const GURL& url) {
216 FrameHostMsg_DidFinishLoad msg(0, url);
217 frame_tree_.root()->current_frame_host()->OnMessageReceived(msg);
220 void TestWebContents::TestDidFailLoadWithError(
221 const GURL& url,
222 int error_code,
223 const base::string16& error_description) {
224 FrameHostMsg_DidFailLoadWithError msg(
225 0, url, error_code, error_description);
226 frame_tree_.root()->current_frame_host()->OnMessageReceived(msg);
229 void TestWebContents::CreateNewWindow(
230 int render_process_id,
231 int route_id,
232 int main_frame_route_id,
233 const ViewHostMsg_CreateWindow_Params& params,
234 SessionStorageNamespace* session_storage_namespace) {
237 void TestWebContents::CreateNewWidget(int render_process_id,
238 int route_id,
239 blink::WebPopupType popup_type) {
242 void TestWebContents::CreateNewFullscreenWidget(int render_process_id,
243 int route_id) {
246 void TestWebContents::ShowCreatedWindow(int route_id,
247 WindowOpenDisposition disposition,
248 const gfx::Rect& initial_pos,
249 bool user_gesture) {
252 void TestWebContents::ShowCreatedWidget(int route_id,
253 const gfx::Rect& initial_pos) {
256 void TestWebContents::ShowCreatedFullscreenWidget(int route_id) {
259 } // namespace content