Disable tab_switching.tough_energy_cases on Linux
[chromium-blink-merge.git] / content / test / test_web_contents.cc
blob28c39801e8b8156197ccb2e036b1ee0a4bdf8aa9
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 "base/command_line.h"
10 #include "content/browser/browser_url_handler_impl.h"
11 #include "content/browser/frame_host/cross_process_frame_connector.h"
12 #include "content/browser/frame_host/navigation_entry_impl.h"
13 #include "content/browser/frame_host/navigator.h"
14 #include "content/browser/frame_host/navigator_impl.h"
15 #include "content/browser/renderer_host/render_view_host_impl.h"
16 #include "content/browser/site_instance_impl.h"
17 #include "content/common/frame_messages.h"
18 #include "content/common/view_messages.h"
19 #include "content/public/browser/notification_registrar.h"
20 #include "content/public/browser/notification_source.h"
21 #include "content/public/browser/notification_types.h"
22 #include "content/public/common/content_switches.h"
23 #include "content/public/common/page_state.h"
24 #include "content/public/test/mock_render_process_host.h"
25 #include "content/test/test_render_view_host.h"
26 #include "ui/base/page_transition_types.h"
28 namespace content {
30 TestWebContents::TestWebContents(BrowserContext* browser_context)
31 : WebContentsImpl(browser_context, NULL),
32 delegate_view_override_(NULL),
33 expect_set_history_offset_and_length_(false),
34 expect_set_history_offset_and_length_history_length_(0) {
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 test_web_contents->RenderFrameCreated(test_web_contents->GetMainFrame());
42 return test_web_contents;
45 TestWebContents::~TestWebContents() {
46 EXPECT_FALSE(expect_set_history_offset_and_length_);
49 TestRenderFrameHost* TestWebContents::GetMainFrame() {
50 return static_cast<TestRenderFrameHost*>(WebContentsImpl::GetMainFrame());
53 TestRenderViewHost* TestWebContents::GetRenderViewHost() const {
54 return static_cast<TestRenderViewHost*>(
55 WebContentsImpl::GetRenderViewHost());
58 TestRenderFrameHost* TestWebContents::GetPendingMainFrame() const {
59 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
60 switches::kEnableBrowserSideNavigation)) {
61 return static_cast<TestRenderFrameHost*>(
62 GetRenderManager()->speculative_render_frame_host_.get());
64 return static_cast<TestRenderFrameHost*>(
65 GetRenderManager()->pending_frame_host());
68 void TestWebContents::TestDidNavigate(RenderFrameHost* render_frame_host,
69 int page_id,
70 int nav_entry_id,
71 bool did_create_new_entry,
72 const GURL& url,
73 ui::PageTransition transition) {
74 TestDidNavigateWithReferrer(render_frame_host,
75 page_id,
76 nav_entry_id,
77 did_create_new_entry,
78 url,
79 Referrer(),
80 transition);
83 void TestWebContents::TestDidNavigateWithReferrer(
84 RenderFrameHost* render_frame_host,
85 int page_id,
86 int nav_entry_id,
87 bool did_create_new_entry,
88 const GURL& url,
89 const Referrer& referrer,
90 ui::PageTransition transition) {
91 FrameHostMsg_DidCommitProvisionalLoad_Params params;
93 params.page_id = page_id;
94 params.nav_entry_id = nav_entry_id;
95 params.url = url;
96 params.referrer = referrer;
97 params.transition = transition;
98 params.redirects = std::vector<GURL>();
99 params.should_update_history = false;
100 params.searchable_form_url = GURL();
101 params.searchable_form_encoding = std::string();
102 params.did_create_new_entry = did_create_new_entry;
103 params.security_info = std::string();
104 params.gesture = NavigationGestureUser;
105 params.was_within_same_page = false;
106 params.is_post = false;
107 params.page_state = PageState::CreateFromURL(url);
109 RenderFrameHostImpl* rfhi =
110 static_cast<RenderFrameHostImpl*>(render_frame_host);
111 rfhi->frame_tree_node()->navigator()->DidNavigate(rfhi, params);
114 bool TestWebContents::CrossProcessNavigationPending() {
115 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
116 switches::kEnableBrowserSideNavigation)) {
117 return GetRenderManager()->speculative_render_frame_host_ &&
118 static_cast<TestRenderFrameHost*>(
119 GetRenderManager()->speculative_render_frame_host_.get())
120 ->pending_commit();
122 return GetRenderManager()->pending_frame_host() != nullptr;
125 bool TestWebContents::CreateRenderViewForRenderManager(
126 RenderViewHost* render_view_host,
127 int opener_route_id,
128 int proxy_routing_id,
129 bool for_main_frame) {
130 UpdateMaxPageIDIfNecessary(render_view_host);
131 // This will go to a TestRenderViewHost.
132 static_cast<RenderViewHostImpl*>(
133 render_view_host)->CreateRenderView(base::string16(),
134 opener_route_id,
135 proxy_routing_id,
136 -1, false);
137 return true;
140 WebContents* TestWebContents::Clone() {
141 WebContentsImpl* contents =
142 Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
143 contents->GetController().CopyStateFrom(controller_);
144 return contents;
147 void TestWebContents::NavigateAndCommit(const GURL& url) {
148 GetController().LoadURL(
149 url, Referrer(), ui::PAGE_TRANSITION_LINK, std::string());
150 GURL loaded_url(url);
151 bool reverse_on_redirect = false;
152 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
153 &loaded_url, GetBrowserContext(), &reverse_on_redirect);
155 // LoadURL created a navigation entry, now simulate the RenderView sending
156 // a notification that it actually navigated.
157 CommitPendingNavigation();
160 void TestWebContents::TestSetIsLoading(bool value) {
161 SetIsLoading(value, true, nullptr);
164 void TestWebContents::CommitPendingNavigation() {
165 const NavigationEntry* entry = GetController().GetPendingEntry();
166 DCHECK(entry);
168 // If we are doing a cross-site navigation, this simulates the current RFH
169 // notifying that it has unloaded so the pending RFH is resumed and can
170 // navigate.
171 // PlzNavigate: the pending RFH is not created before the navigation commit,
172 // so it is necessary to simulate the IO thread response here to commit in the
173 // proper renderer. It is necessary to call PrepareForCommit before getting
174 // the main and the pending frame because when we are trying to navigate to a
175 // webui from a new tab, a RenderFrameHost is created to display it that is
176 // committed immediately (since it is a new tab). Therefore the main frame is
177 // replaced without a pending frame being created, and we don't get the right
178 // values for the RFH to navigate: we try to use the old one that has been
179 // deleted in the meantime.
180 GetMainFrame()->PrepareForCommit();
182 TestRenderFrameHost* old_rfh = GetMainFrame();
183 TestRenderFrameHost* rfh = GetPendingMainFrame();
184 if (!rfh)
185 rfh = old_rfh;
187 int page_id = entry->GetPageID();
188 if (page_id == -1) {
189 // It's a new navigation, assign a never-seen page id to it.
190 page_id = GetMaxPageIDForSiteInstance(rfh->GetSiteInstance()) + 1;
193 rfh->SendNavigate(page_id, entry->GetUniqueID(),
194 GetController().GetPendingEntryIndex() == -1,
195 entry->GetURL());
196 // Simulate the SwapOut_ACK. This is needed when cross-site navigation
197 // happens.
198 if (old_rfh != rfh)
199 old_rfh->OnSwappedOut();
202 void TestWebContents::ProceedWithCrossSiteNavigation() {
203 if (!GetPendingMainFrame())
204 return;
205 GetMainFrame()->SendBeforeUnloadACK(true);
208 RenderViewHostDelegateView* TestWebContents::GetDelegateView() {
209 if (delegate_view_override_)
210 return delegate_view_override_;
211 return WebContentsImpl::GetDelegateView();
214 void TestWebContents::SetOpener(TestWebContents* opener) {
215 // This is normally only set in the WebContents constructor, which also
216 // registers an observer for when the opener gets closed.
217 opener_ = opener;
218 AddDestructionObserver(opener_);
221 void TestWebContents::AddPendingContents(TestWebContents* contents) {
222 // This is normally only done in WebContentsImpl::CreateNewWindow.
223 pending_contents_[contents->GetRenderViewHost()->GetRoutingID()] = contents;
224 AddDestructionObserver(contents);
227 void TestWebContents::ExpectSetHistoryOffsetAndLength(int history_offset,
228 int history_length) {
229 expect_set_history_offset_and_length_ = true;
230 expect_set_history_offset_and_length_history_offset_ = history_offset;
231 expect_set_history_offset_and_length_history_length_ = history_length;
234 void TestWebContents::SetHistoryOffsetAndLength(int history_offset,
235 int history_length) {
236 EXPECT_TRUE(expect_set_history_offset_and_length_);
237 expect_set_history_offset_and_length_ = false;
238 EXPECT_EQ(expect_set_history_offset_and_length_history_offset_,
239 history_offset);
240 EXPECT_EQ(expect_set_history_offset_and_length_history_length_,
241 history_length);
244 void TestWebContents::TestDidFinishLoad(const GURL& url) {
245 FrameHostMsg_DidFinishLoad msg(0, url);
246 frame_tree_.root()->current_frame_host()->OnMessageReceived(msg);
249 void TestWebContents::TestDidFailLoadWithError(
250 const GURL& url,
251 int error_code,
252 const base::string16& error_description) {
253 FrameHostMsg_DidFailLoadWithError msg(
254 0, url, error_code, error_description);
255 frame_tree_.root()->current_frame_host()->OnMessageReceived(msg);
258 void TestWebContents::CreateNewWindow(
259 int render_process_id,
260 int route_id,
261 int main_frame_route_id,
262 const ViewHostMsg_CreateWindow_Params& params,
263 SessionStorageNamespace* session_storage_namespace) {
266 void TestWebContents::CreateNewWidget(int render_process_id,
267 int route_id,
268 blink::WebPopupType popup_type) {
271 void TestWebContents::CreateNewFullscreenWidget(int render_process_id,
272 int route_id) {
275 void TestWebContents::ShowCreatedWindow(int route_id,
276 WindowOpenDisposition disposition,
277 const gfx::Rect& initial_rect,
278 bool user_gesture) {
281 void TestWebContents::ShowCreatedWidget(int route_id,
282 const gfx::Rect& initial_rect) {
285 void TestWebContents::ShowCreatedFullscreenWidget(int route_id) {
288 } // namespace content