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.
4
5 #include "content/test/test_web_contents.h"
6
7 #include <utility>
8
9 #include "content/browser/browser_url_handler_impl.h"
10 #include "content/browser/frame_host/navigation_entry_impl.h"
11 #include "content/browser/renderer_host/render_view_host_impl.h"
12 #include "content/browser/site_instance_impl.h"
13 #include "content/common/view_messages.h"
14 #include "content/public/browser/notification_registrar.h"
15 #include "content/public/browser/notification_source.h"
16 #include "content/public/browser/notification_types.h"
17 #include "content/public/common/page_state.h"
18 #include "content/public/common/page_transition_types.h"
19 #include "content/public/test/mock_render_process_host.h"
20 #include "content/test/test_render_view_host.h"
21
22 namespace content {
23
TestWebContents(BrowserContext * browser_context)24 TestWebContents::TestWebContents(BrowserContext* browser_context)
25 : WebContentsImpl(browser_context, NULL),
26 transition_cross_site(false),
27 delegate_view_override_(NULL),
28 expect_set_history_length_and_prune_(false),
29 expect_set_history_length_and_prune_site_instance_(NULL),
30 expect_set_history_length_and_prune_history_length_(0),
31 expect_set_history_length_and_prune_min_page_id_(-1) {
32 }
33
Create(BrowserContext * browser_context,SiteInstance * instance)34 TestWebContents* TestWebContents::Create(BrowserContext* browser_context,
35 SiteInstance* instance) {
36 TestWebContents* test_web_contents = new TestWebContents(browser_context);
37 test_web_contents->Init(WebContents::CreateParams(browser_context, instance));
38 return test_web_contents;
39 }
40
~TestWebContents()41 TestWebContents::~TestWebContents() {
42 EXPECT_FALSE(expect_set_history_length_and_prune_);
43 }
44
GetPendingRenderViewHost() const45 RenderViewHost* TestWebContents::GetPendingRenderViewHost() const {
46 return GetRenderManager()->pending_render_view_host_;
47 }
48
pending_test_rvh() const49 TestRenderViewHost* TestWebContents::pending_test_rvh() const {
50 return static_cast<TestRenderViewHost*>(GetPendingRenderViewHost());
51 }
52
TestDidNavigate(RenderViewHost * render_view_host,int page_id,const GURL & url,PageTransition transition)53 void TestWebContents::TestDidNavigate(RenderViewHost* render_view_host,
54 int page_id,
55 const GURL& url,
56 PageTransition transition) {
57 TestDidNavigateWithReferrer(render_view_host,
58 page_id,
59 url,
60 Referrer(),
61 transition);
62 }
63
TestDidNavigateWithReferrer(RenderViewHost * render_view_host,int page_id,const GURL & url,const Referrer & referrer,PageTransition transition)64 void TestWebContents::TestDidNavigateWithReferrer(
65 RenderViewHost* render_view_host,
66 int page_id,
67 const GURL& url,
68 const Referrer& referrer,
69 PageTransition transition) {
70 ViewHostMsg_FrameNavigate_Params params;
71
72 params.page_id = page_id;
73 params.url = url;
74 params.referrer = referrer;
75 params.transition = transition;
76 params.redirects = std::vector<GURL>();
77 params.should_update_history = false;
78 params.searchable_form_url = GURL();
79 params.searchable_form_encoding = std::string();
80 params.security_info = std::string();
81 params.gesture = NavigationGestureUser;
82 params.was_within_same_page = false;
83 params.is_post = false;
84 params.page_state = PageState::CreateFromURL(url);
85
86 DidNavigate(render_view_host, params);
87 }
88
TestGetWebkitPrefs()89 WebPreferences TestWebContents::TestGetWebkitPrefs() {
90 return GetWebkitPrefs();
91 }
92
CreateRenderViewForRenderManager(RenderViewHost * render_view_host,int opener_route_id)93 bool TestWebContents::CreateRenderViewForRenderManager(
94 RenderViewHost* render_view_host, int opener_route_id) {
95 // This will go to a TestRenderViewHost.
96 static_cast<RenderViewHostImpl*>(
97 render_view_host)->CreateRenderView(base::string16(),
98 opener_route_id,
99 -1);
100 return true;
101 }
102
Clone()103 WebContents* TestWebContents::Clone() {
104 WebContentsImpl* contents =
105 Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
106 contents->GetController().CopyStateFrom(controller_);
107 return contents;
108 }
109
NavigateAndCommit(const GURL & url)110 void TestWebContents::NavigateAndCommit(const GURL& url) {
111 GetController().LoadURL(
112 url, Referrer(), PAGE_TRANSITION_LINK, std::string());
113 GURL loaded_url(url);
114 bool reverse_on_redirect = false;
115 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
116 &loaded_url, GetBrowserContext(), &reverse_on_redirect);
117
118 // LoadURL created a navigation entry, now simulate the RenderView sending
119 // a notification that it actually navigated.
120 CommitPendingNavigation();
121 }
122
TestSetIsLoading(bool value)123 void TestWebContents::TestSetIsLoading(bool value) {
124 SetIsLoading(GetRenderViewHost(), value, NULL);
125 }
126
CommitPendingNavigation()127 void TestWebContents::CommitPendingNavigation() {
128 // If we are doing a cross-site navigation, this simulates the current RVH
129 // notifying that it has unloaded so the pending RVH is resumed and can
130 // navigate.
131 ProceedWithCrossSiteNavigation();
132 RenderViewHost* old_rvh = GetRenderViewHost();
133 TestRenderViewHost* rvh =
134 static_cast<TestRenderViewHost*>(GetPendingRenderViewHost());
135 if (!rvh)
136 rvh = static_cast<TestRenderViewHost*>(old_rvh);
137
138 const NavigationEntry* entry = GetController().GetPendingEntry();
139 DCHECK(entry);
140 int page_id = entry->GetPageID();
141 if (page_id == -1) {
142 // It's a new navigation, assign a never-seen page id to it.
143 page_id = GetMaxPageIDForSiteInstance(rvh->GetSiteInstance()) + 1;
144 }
145
146 // Simulate the SwapOut_ACK that happens when we swap out the old
147 // RVH, before the navigation commits. This is needed when
148 // cross-site navigation happens (old_rvh != rvh).
149 if (old_rvh != rvh)
150 static_cast<RenderViewHostImpl*>(old_rvh)->OnSwappedOut(false);
151 rvh->SendNavigate(page_id, entry->GetURL());
152 }
153
ProceedWithCrossSiteNavigation()154 void TestWebContents::ProceedWithCrossSiteNavigation() {
155 if (!GetPendingRenderViewHost())
156 return;
157 TestRenderViewHost* rvh = static_cast<TestRenderViewHost*>(
158 GetRenderViewHost());
159 rvh->SendShouldCloseACK(true);
160 }
161
GetDelegateView()162 RenderViewHostDelegateView* TestWebContents::GetDelegateView() {
163 if (delegate_view_override_)
164 return delegate_view_override_;
165 return WebContentsImpl::GetDelegateView();
166 }
167
SetOpener(TestWebContents * opener)168 void TestWebContents::SetOpener(TestWebContents* opener) {
169 // This is normally only set in the WebContents constructor, which also
170 // registers an observer for when the opener gets closed.
171 opener_ = opener;
172 AddDestructionObserver(opener_);
173 }
174
AddPendingContents(TestWebContents * contents)175 void TestWebContents::AddPendingContents(TestWebContents* contents) {
176 // This is normally only done in WebContentsImpl::CreateNewWindow.
177 pending_contents_[contents->GetRenderViewHost()->GetRoutingID()] = contents;
178 AddDestructionObserver(contents);
179 }
180
ExpectSetHistoryLengthAndPrune(const SiteInstance * site_instance,int history_length,int32 min_page_id)181 void TestWebContents::ExpectSetHistoryLengthAndPrune(
182 const SiteInstance* site_instance,
183 int history_length,
184 int32 min_page_id) {
185 expect_set_history_length_and_prune_ = true;
186 expect_set_history_length_and_prune_site_instance_ =
187 static_cast<const SiteInstanceImpl*>(site_instance);
188 expect_set_history_length_and_prune_history_length_ = history_length;
189 expect_set_history_length_and_prune_min_page_id_ = min_page_id;
190 }
191
SetHistoryLengthAndPrune(const SiteInstance * site_instance,int history_length,int32 min_page_id)192 void TestWebContents::SetHistoryLengthAndPrune(
193 const SiteInstance* site_instance, int history_length,
194 int32 min_page_id) {
195 EXPECT_TRUE(expect_set_history_length_and_prune_);
196 expect_set_history_length_and_prune_ = false;
197 EXPECT_EQ(expect_set_history_length_and_prune_site_instance_, site_instance);
198 EXPECT_EQ(expect_set_history_length_and_prune_history_length_,
199 history_length);
200 EXPECT_EQ(expect_set_history_length_and_prune_min_page_id_, min_page_id);
201 }
202
TestDidFinishLoad(int64 frame_id,const GURL & url,bool is_main_frame)203 void TestWebContents::TestDidFinishLoad(int64 frame_id,
204 const GURL& url,
205 bool is_main_frame) {
206 ViewHostMsg_DidFinishLoad msg(0, frame_id, url, is_main_frame);
207 OnMessageReceived(GetRenderViewHost(), msg);
208 }
209
TestDidFailLoadWithError(int64 frame_id,const GURL & url,bool is_main_frame,int error_code,const base::string16 & error_description)210 void TestWebContents::TestDidFailLoadWithError(
211 int64 frame_id,
212 const GURL& url,
213 bool is_main_frame,
214 int error_code,
215 const base::string16& error_description) {
216 ViewHostMsg_DidFailLoadWithError msg(
217 0, frame_id, url, is_main_frame, error_code, error_description);
218 OnMessageReceived(GetRenderViewHost(), msg);
219 }
220
CreateNewWindow(int render_process_id,int route_id,int main_frame_route_id,const ViewHostMsg_CreateWindow_Params & params,SessionStorageNamespace * session_storage_namespace)221 void TestWebContents::CreateNewWindow(
222 int render_process_id,
223 int route_id,
224 int main_frame_route_id,
225 const ViewHostMsg_CreateWindow_Params& params,
226 SessionStorageNamespace* session_storage_namespace) {
227 }
228
CreateNewWidget(int render_process_id,int route_id,blink::WebPopupType popup_type)229 void TestWebContents::CreateNewWidget(int render_process_id,
230 int route_id,
231 blink::WebPopupType popup_type) {
232 }
233
CreateNewFullscreenWidget(int render_process_id,int route_id)234 void TestWebContents::CreateNewFullscreenWidget(int render_process_id,
235 int route_id) {
236 }
237
ShowCreatedWindow(int route_id,WindowOpenDisposition disposition,const gfx::Rect & initial_pos,bool user_gesture)238 void TestWebContents::ShowCreatedWindow(int route_id,
239 WindowOpenDisposition disposition,
240 const gfx::Rect& initial_pos,
241 bool user_gesture) {
242 }
243
ShowCreatedWidget(int route_id,const gfx::Rect & initial_pos)244 void TestWebContents::ShowCreatedWidget(int route_id,
245 const gfx::Rect& initial_pos) {
246 }
247
ShowCreatedFullscreenWidget(int route_id)248 void TestWebContents::ShowCreatedFullscreenWidget(int route_id) {
249 }
250
251 } // namespace content
252