base: Change DCHECK_IS_ON to a macro DCHECK_IS_ON().
[chromium-blink-merge.git] / content / test / test_web_contents.cc
blob6a2e05e2b9cc805bcd8109b20f72f186841f8c35
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 return test_web_contents;
44 TestWebContents::~TestWebContents() {
45 EXPECT_FALSE(expect_set_history_offset_and_length_);
48 TestRenderFrameHost* TestWebContents::GetMainFrame() {
49 return static_cast<TestRenderFrameHost*>(WebContentsImpl::GetMainFrame());
52 TestRenderViewHost* TestWebContents::GetRenderViewHost() const {
53 return static_cast<TestRenderViewHost*>(
54 WebContentsImpl::GetRenderViewHost());
57 TestRenderFrameHost* TestWebContents::GetPendingMainFrame() const {
58 return static_cast<TestRenderFrameHost*>(
59 GetRenderManager()->pending_frame_host());
62 void TestWebContents::TestDidNavigate(RenderFrameHost* render_frame_host,
63 int page_id,
64 const GURL& url,
65 ui::PageTransition transition) {
66 TestDidNavigateWithReferrer(render_frame_host,
67 page_id,
68 url,
69 Referrer(),
70 transition);
73 void TestWebContents::TestDidNavigateWithReferrer(
74 RenderFrameHost* render_frame_host,
75 int page_id,
76 const GURL& url,
77 const Referrer& referrer,
78 ui::PageTransition transition) {
79 FrameHostMsg_DidCommitProvisionalLoad_Params params;
81 params.page_id = page_id;
82 params.url = url;
83 params.referrer = referrer;
84 params.transition = transition;
85 params.redirects = std::vector<GURL>();
86 params.should_update_history = false;
87 params.searchable_form_url = GURL();
88 params.searchable_form_encoding = std::string();
89 params.security_info = std::string();
90 params.gesture = NavigationGestureUser;
91 params.was_within_same_page = false;
92 params.is_post = false;
93 params.page_state = PageState::CreateFromURL(url);
95 RenderFrameHostImpl* rfhi =
96 static_cast<RenderFrameHostImpl*>(render_frame_host);
97 rfhi->frame_tree_node()->navigator()->DidNavigate(rfhi, params);
100 WebPreferences TestWebContents::TestComputeWebkitPrefs() {
101 return ComputeWebkitPrefs();
104 bool TestWebContents::CreateRenderViewForRenderManager(
105 RenderViewHost* render_view_host,
106 int opener_route_id,
107 int proxy_routing_id,
108 bool for_main_frame) {
109 UpdateMaxPageIDIfNecessary(render_view_host);
110 // This will go to a TestRenderViewHost.
111 static_cast<RenderViewHostImpl*>(
112 render_view_host)->CreateRenderView(base::string16(),
113 opener_route_id,
114 proxy_routing_id,
115 -1, false);
116 return true;
119 WebContents* TestWebContents::Clone() {
120 WebContentsImpl* contents =
121 Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
122 contents->GetController().CopyStateFrom(controller_);
123 return contents;
126 void TestWebContents::NavigateAndCommit(const GURL& url) {
127 GetController().LoadURL(
128 url, Referrer(), ui::PAGE_TRANSITION_LINK, std::string());
129 GURL loaded_url(url);
130 bool reverse_on_redirect = false;
131 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
132 &loaded_url, GetBrowserContext(), &reverse_on_redirect);
134 // LoadURL created a navigation entry, now simulate the RenderView sending
135 // a notification that it actually navigated.
136 CommitPendingNavigation();
139 void TestWebContents::TestSetIsLoading(bool value) {
140 SetIsLoading(GetRenderViewHost(), value, true, NULL);
143 void TestWebContents::CommitPendingNavigation() {
144 // If we are doing a cross-site navigation, this simulates the current RFH
145 // notifying that it has unloaded so the pending RFH is resumed and can
146 // navigate.
147 TestRenderFrameHost* old_rfh = GetMainFrame();
148 const NavigationEntry* entry = GetController().GetPendingEntry();
149 DCHECK(entry);
151 // Simulate the BeforeUnload ACK if necessary.
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.
155 old_rfh->PrepareForCommit(entry->GetURL());
157 TestRenderFrameHost* rfh = GetPendingMainFrame();
158 if (!rfh)
159 rfh = old_rfh;
161 int page_id = entry->GetPageID();
162 if (page_id == -1) {
163 // It's a new navigation, assign a never-seen page id to it.
164 page_id = GetMaxPageIDForSiteInstance(rfh->GetSiteInstance()) + 1;
167 rfh->SendNavigate(page_id, entry->GetURL());
168 // Simulate the SwapOut_ACK. This is needed when cross-site navigation
169 // happens.
170 if (old_rfh != rfh)
171 old_rfh->OnSwappedOut();
174 void TestWebContents::ProceedWithCrossSiteNavigation() {
175 if (!GetPendingMainFrame())
176 return;
177 GetMainFrame()->SendBeforeUnloadACK(true);
180 RenderViewHostDelegateView* TestWebContents::GetDelegateView() {
181 if (delegate_view_override_)
182 return delegate_view_override_;
183 return WebContentsImpl::GetDelegateView();
186 void TestWebContents::SetOpener(TestWebContents* opener) {
187 // This is normally only set in the WebContents constructor, which also
188 // registers an observer for when the opener gets closed.
189 opener_ = opener;
190 AddDestructionObserver(opener_);
193 void TestWebContents::AddPendingContents(TestWebContents* contents) {
194 // This is normally only done in WebContentsImpl::CreateNewWindow.
195 pending_contents_[contents->GetRenderViewHost()->GetRoutingID()] = contents;
196 AddDestructionObserver(contents);
199 void TestWebContents::ExpectSetHistoryOffsetAndLength(int history_offset,
200 int history_length) {
201 expect_set_history_offset_and_length_ = true;
202 expect_set_history_offset_and_length_history_offset_ = history_offset;
203 expect_set_history_offset_and_length_history_length_ = history_length;
206 void TestWebContents::SetHistoryOffsetAndLength(int history_offset,
207 int history_length) {
208 EXPECT_TRUE(expect_set_history_offset_and_length_);
209 expect_set_history_offset_and_length_ = false;
210 EXPECT_EQ(expect_set_history_offset_and_length_history_offset_,
211 history_offset);
212 EXPECT_EQ(expect_set_history_offset_and_length_history_length_,
213 history_length);
216 void TestWebContents::TestDidFinishLoad(const GURL& url) {
217 FrameHostMsg_DidFinishLoad msg(0, url);
218 frame_tree_.root()->current_frame_host()->OnMessageReceived(msg);
221 void TestWebContents::TestDidFailLoadWithError(
222 const GURL& url,
223 int error_code,
224 const base::string16& error_description) {
225 FrameHostMsg_DidFailLoadWithError msg(
226 0, url, error_code, error_description);
227 frame_tree_.root()->current_frame_host()->OnMessageReceived(msg);
230 void TestWebContents::CreateNewWindow(
231 int render_process_id,
232 int route_id,
233 int main_frame_route_id,
234 const ViewHostMsg_CreateWindow_Params& params,
235 SessionStorageNamespace* session_storage_namespace) {
238 void TestWebContents::CreateNewWidget(int render_process_id,
239 int route_id,
240 blink::WebPopupType popup_type) {
243 void TestWebContents::CreateNewFullscreenWidget(int render_process_id,
244 int route_id) {
247 void TestWebContents::ShowCreatedWindow(int route_id,
248 WindowOpenDisposition disposition,
249 const gfx::Rect& initial_pos,
250 bool user_gesture) {
253 void TestWebContents::ShowCreatedWidget(int route_id,
254 const gfx::Rect& initial_pos) {
257 void TestWebContents::ShowCreatedFullscreenWidget(int route_id) {
260 } // namespace content