GPU workaround to simulate Out of Memory errors with large textures
[chromium-blink-merge.git] / content / test / test_web_contents.cc
bloba3e164a60a72a8185f315f585833a564b6197a6c
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::CreateRenderViewForRenderManager(
107 RenderViewHost* render_view_host,
108 int opener_route_id,
109 int proxy_routing_id,
110 bool for_main_frame) {
111 UpdateMaxPageIDIfNecessary(render_view_host);
112 // This will go to a TestRenderViewHost.
113 static_cast<RenderViewHostImpl*>(
114 render_view_host)->CreateRenderView(base::string16(),
115 opener_route_id,
116 proxy_routing_id,
117 -1, false);
118 return true;
121 WebContents* TestWebContents::Clone() {
122 WebContentsImpl* contents =
123 Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
124 contents->GetController().CopyStateFrom(controller_);
125 return contents;
128 void TestWebContents::NavigateAndCommit(const GURL& url) {
129 GetController().LoadURL(
130 url, Referrer(), ui::PAGE_TRANSITION_LINK, std::string());
131 GURL loaded_url(url);
132 bool reverse_on_redirect = false;
133 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
134 &loaded_url, GetBrowserContext(), &reverse_on_redirect);
136 // LoadURL created a navigation entry, now simulate the RenderView sending
137 // a notification that it actually navigated.
138 CommitPendingNavigation();
141 void TestWebContents::TestSetIsLoading(bool value) {
142 SetIsLoading(value, true, nullptr);
145 void TestWebContents::CommitPendingNavigation() {
146 const NavigationEntry* entry = GetController().GetPendingEntry();
147 DCHECK(entry);
149 // If we are doing a cross-site navigation, this simulates the current RFH
150 // notifying that it has unloaded so the pending RFH is resumed and can
151 // navigate.
152 // PlzNavigate: the pending RFH is not created before the navigation commit,
153 // so it is necessary to simulate the IO thread response here to commit in the
154 // proper renderer. It is necessary to call PrepareForCommit before getting
155 // the main and the pending frame because when we are trying to navigate to a
156 // webui from a new tab, a RenderFrameHost is created to display it that is
157 // committed immediately (since it is a new tab). Therefore the main frame is
158 // replaced without a pending frame being created, and we don't get the right
159 // values for the RFH to navigate: we try to use the old one that has been
160 // deleted in the meantime.
161 GetMainFrame()->PrepareForCommit();
163 TestRenderFrameHost* old_rfh = GetMainFrame();
164 TestRenderFrameHost* rfh = GetPendingMainFrame();
165 if (!rfh)
166 rfh = old_rfh;
168 int page_id = entry->GetPageID();
169 if (page_id == -1) {
170 // It's a new navigation, assign a never-seen page id to it.
171 page_id = GetMaxPageIDForSiteInstance(rfh->GetSiteInstance()) + 1;
174 rfh->SendNavigate(page_id, entry->GetURL());
175 // Simulate the SwapOut_ACK. This is needed when cross-site navigation
176 // happens.
177 if (old_rfh != rfh)
178 old_rfh->OnSwappedOut();
181 void TestWebContents::ProceedWithCrossSiteNavigation() {
182 if (!GetPendingMainFrame())
183 return;
184 GetMainFrame()->SendBeforeUnloadACK(true);
187 RenderViewHostDelegateView* TestWebContents::GetDelegateView() {
188 if (delegate_view_override_)
189 return delegate_view_override_;
190 return WebContentsImpl::GetDelegateView();
193 void TestWebContents::SetOpener(TestWebContents* opener) {
194 // This is normally only set in the WebContents constructor, which also
195 // registers an observer for when the opener gets closed.
196 opener_ = opener;
197 AddDestructionObserver(opener_);
200 void TestWebContents::AddPendingContents(TestWebContents* contents) {
201 // This is normally only done in WebContentsImpl::CreateNewWindow.
202 pending_contents_[contents->GetRenderViewHost()->GetRoutingID()] = contents;
203 AddDestructionObserver(contents);
206 void TestWebContents::ExpectSetHistoryOffsetAndLength(int history_offset,
207 int history_length) {
208 expect_set_history_offset_and_length_ = true;
209 expect_set_history_offset_and_length_history_offset_ = history_offset;
210 expect_set_history_offset_and_length_history_length_ = history_length;
213 void TestWebContents::SetHistoryOffsetAndLength(int history_offset,
214 int history_length) {
215 EXPECT_TRUE(expect_set_history_offset_and_length_);
216 expect_set_history_offset_and_length_ = false;
217 EXPECT_EQ(expect_set_history_offset_and_length_history_offset_,
218 history_offset);
219 EXPECT_EQ(expect_set_history_offset_and_length_history_length_,
220 history_length);
223 void TestWebContents::TestDidFinishLoad(const GURL& url) {
224 FrameHostMsg_DidFinishLoad msg(0, url);
225 frame_tree_.root()->current_frame_host()->OnMessageReceived(msg);
228 void TestWebContents::TestDidFailLoadWithError(
229 const GURL& url,
230 int error_code,
231 const base::string16& error_description) {
232 FrameHostMsg_DidFailLoadWithError msg(
233 0, url, error_code, error_description);
234 frame_tree_.root()->current_frame_host()->OnMessageReceived(msg);
237 void TestWebContents::CreateNewWindow(
238 int render_process_id,
239 int route_id,
240 int main_frame_route_id,
241 const ViewHostMsg_CreateWindow_Params& params,
242 SessionStorageNamespace* session_storage_namespace) {
245 void TestWebContents::CreateNewWidget(int render_process_id,
246 int route_id,
247 blink::WebPopupType popup_type) {
250 void TestWebContents::CreateNewFullscreenWidget(int render_process_id,
251 int route_id) {
254 void TestWebContents::ShowCreatedWindow(int route_id,
255 WindowOpenDisposition disposition,
256 const gfx::Rect& initial_rect,
257 bool user_gesture) {
260 void TestWebContents::ShowCreatedWidget(int route_id,
261 const gfx::Rect& initial_rect) {
264 void TestWebContents::ShowCreatedFullscreenWidget(int route_id) {
267 } // namespace content