CacheStorage: Remove unused interfaces from CacheStorageListener
[chromium-blink-merge.git] / content / test / test_web_contents.cc
blob9032fc4ee3463fd3bb4d8b19fc865a05124ab186
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 const GURL& url,
71 ui::PageTransition transition) {
72 TestDidNavigateWithReferrer(render_frame_host,
73 page_id,
74 url,
75 Referrer(),
76 transition);
79 void TestWebContents::TestDidNavigateWithReferrer(
80 RenderFrameHost* render_frame_host,
81 int page_id,
82 const GURL& url,
83 const Referrer& referrer,
84 ui::PageTransition transition) {
85 FrameHostMsg_DidCommitProvisionalLoad_Params params;
87 params.page_id = page_id;
88 params.url = url;
89 params.referrer = referrer;
90 params.transition = transition;
91 params.redirects = std::vector<GURL>();
92 params.should_update_history = false;
93 params.searchable_form_url = GURL();
94 params.searchable_form_encoding = std::string();
95 params.security_info = std::string();
96 params.gesture = NavigationGestureUser;
97 params.was_within_same_page = false;
98 params.is_post = false;
99 params.page_state = PageState::CreateFromURL(url);
101 RenderFrameHostImpl* rfhi =
102 static_cast<RenderFrameHostImpl*>(render_frame_host);
103 rfhi->frame_tree_node()->navigator()->DidNavigate(rfhi, params);
106 bool TestWebContents::CrossProcessNavigationPending() {
107 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
108 switches::kEnableBrowserSideNavigation)) {
109 return GetRenderManager()->speculative_render_frame_host_ &&
110 static_cast<TestRenderFrameHost*>(
111 GetRenderManager()->speculative_render_frame_host_.get())
112 ->pending_commit();
114 return GetRenderManager()->pending_frame_host() != nullptr;
117 bool TestWebContents::CreateRenderViewForRenderManager(
118 RenderViewHost* render_view_host,
119 int opener_route_id,
120 int proxy_routing_id,
121 bool for_main_frame) {
122 UpdateMaxPageIDIfNecessary(render_view_host);
123 // This will go to a TestRenderViewHost.
124 static_cast<RenderViewHostImpl*>(
125 render_view_host)->CreateRenderView(base::string16(),
126 opener_route_id,
127 proxy_routing_id,
128 -1, false);
129 return true;
132 WebContents* TestWebContents::Clone() {
133 WebContentsImpl* contents =
134 Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
135 contents->GetController().CopyStateFrom(controller_);
136 return contents;
139 void TestWebContents::NavigateAndCommit(const GURL& url) {
140 GetController().LoadURL(
141 url, Referrer(), ui::PAGE_TRANSITION_LINK, std::string());
142 GURL loaded_url(url);
143 bool reverse_on_redirect = false;
144 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
145 &loaded_url, GetBrowserContext(), &reverse_on_redirect);
147 // LoadURL created a navigation entry, now simulate the RenderView sending
148 // a notification that it actually navigated.
149 CommitPendingNavigation();
152 void TestWebContents::TestSetIsLoading(bool value) {
153 SetIsLoading(value, true, nullptr);
156 void TestWebContents::CommitPendingNavigation() {
157 const NavigationEntry* entry = GetController().GetPendingEntry();
158 DCHECK(entry);
160 // If we are doing a cross-site navigation, this simulates the current RFH
161 // notifying that it has unloaded so the pending RFH is resumed and can
162 // navigate.
163 // PlzNavigate: the pending RFH is not created before the navigation commit,
164 // so it is necessary to simulate the IO thread response here to commit in the
165 // proper renderer. It is necessary to call PrepareForCommit before getting
166 // the main and the pending frame because when we are trying to navigate to a
167 // webui from a new tab, a RenderFrameHost is created to display it that is
168 // committed immediately (since it is a new tab). Therefore the main frame is
169 // replaced without a pending frame being created, and we don't get the right
170 // values for the RFH to navigate: we try to use the old one that has been
171 // deleted in the meantime.
172 GetMainFrame()->PrepareForCommit();
174 TestRenderFrameHost* old_rfh = GetMainFrame();
175 TestRenderFrameHost* rfh = GetPendingMainFrame();
176 if (!rfh)
177 rfh = old_rfh;
179 int page_id = entry->GetPageID();
180 if (page_id == -1) {
181 // It's a new navigation, assign a never-seen page id to it.
182 page_id = GetMaxPageIDForSiteInstance(rfh->GetSiteInstance()) + 1;
185 rfh->SendNavigate(page_id, entry->GetURL());
186 // Simulate the SwapOut_ACK. This is needed when cross-site navigation
187 // happens.
188 if (old_rfh != rfh)
189 old_rfh->OnSwappedOut();
192 void TestWebContents::ProceedWithCrossSiteNavigation() {
193 if (!GetPendingMainFrame())
194 return;
195 GetMainFrame()->SendBeforeUnloadACK(true);
198 RenderViewHostDelegateView* TestWebContents::GetDelegateView() {
199 if (delegate_view_override_)
200 return delegate_view_override_;
201 return WebContentsImpl::GetDelegateView();
204 void TestWebContents::SetOpener(TestWebContents* opener) {
205 // This is normally only set in the WebContents constructor, which also
206 // registers an observer for when the opener gets closed.
207 opener_ = opener;
208 AddDestructionObserver(opener_);
211 void TestWebContents::AddPendingContents(TestWebContents* contents) {
212 // This is normally only done in WebContentsImpl::CreateNewWindow.
213 pending_contents_[contents->GetRenderViewHost()->GetRoutingID()] = contents;
214 AddDestructionObserver(contents);
217 void TestWebContents::ExpectSetHistoryOffsetAndLength(int history_offset,
218 int history_length) {
219 expect_set_history_offset_and_length_ = true;
220 expect_set_history_offset_and_length_history_offset_ = history_offset;
221 expect_set_history_offset_and_length_history_length_ = history_length;
224 void TestWebContents::SetHistoryOffsetAndLength(int history_offset,
225 int history_length) {
226 EXPECT_TRUE(expect_set_history_offset_and_length_);
227 expect_set_history_offset_and_length_ = false;
228 EXPECT_EQ(expect_set_history_offset_and_length_history_offset_,
229 history_offset);
230 EXPECT_EQ(expect_set_history_offset_and_length_history_length_,
231 history_length);
234 void TestWebContents::TestDidFinishLoad(const GURL& url) {
235 FrameHostMsg_DidFinishLoad msg(0, url);
236 frame_tree_.root()->current_frame_host()->OnMessageReceived(msg);
239 void TestWebContents::TestDidFailLoadWithError(
240 const GURL& url,
241 int error_code,
242 const base::string16& error_description) {
243 FrameHostMsg_DidFailLoadWithError msg(
244 0, url, error_code, error_description);
245 frame_tree_.root()->current_frame_host()->OnMessageReceived(msg);
248 void TestWebContents::CreateNewWindow(
249 int render_process_id,
250 int route_id,
251 int main_frame_route_id,
252 const ViewHostMsg_CreateWindow_Params& params,
253 SessionStorageNamespace* session_storage_namespace) {
256 void TestWebContents::CreateNewWidget(int render_process_id,
257 int route_id,
258 blink::WebPopupType popup_type) {
261 void TestWebContents::CreateNewFullscreenWidget(int render_process_id,
262 int route_id) {
265 void TestWebContents::ShowCreatedWindow(int route_id,
266 WindowOpenDisposition disposition,
267 const gfx::Rect& initial_rect,
268 bool user_gesture) {
271 void TestWebContents::ShowCreatedWidget(int route_id,
272 const gfx::Rect& initial_rect) {
275 void TestWebContents::ShowCreatedFullscreenWidget(int route_id) {
278 } // namespace content