Removed unused VideoCaptureCapability parameters.
[chromium-blink-merge.git] / content / test / test_web_contents.cc
bloba5e3a819d60fad7c7089a806498b14fcc81c053c
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 "content/browser/browser_url_handler_impl.h"
10 #include "content/browser/renderer_host/render_view_host_impl.h"
11 #include "content/browser/renderer_host/test_render_view_host.h"
12 #include "content/browser/site_instance_impl.h"
13 #include "content/browser/web_contents/navigation_entry_impl.h"
14 #include "content/common/view_messages.h"
15 #include "content/public/browser/notification_registrar.h"
16 #include "content/public/browser/notification_source.h"
17 #include "content/public/browser/notification_types.h"
18 #include "content/public/common/page_state.h"
19 #include "content/public/common/page_transition_types.h"
20 #include "content/public/test/mock_render_process_host.h"
22 namespace content {
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) {
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;
41 TestWebContents::~TestWebContents() {
42 EXPECT_FALSE(expect_set_history_length_and_prune_);
45 RenderViewHost* TestWebContents::GetPendingRenderViewHost() const {
46 return render_manager_.pending_render_view_host_;
49 TestRenderViewHost* TestWebContents::pending_test_rvh() const {
50 return static_cast<TestRenderViewHost*>(GetPendingRenderViewHost());
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);
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;
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);
86 DidNavigate(render_view_host, params);
89 WebPreferences TestWebContents::TestGetWebkitPrefs() {
90 return GetWebkitPrefs();
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(string16(),
98 opener_route_id,
99 -1);
100 return true;
103 WebContents* TestWebContents::Clone() {
104 WebContentsImpl* contents =
105 Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
106 contents->GetController().CopyStateFrom(controller_);
107 return contents;
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);
118 // LoadURL created a navigation entry, now simulate the RenderView sending
119 // a notification that it actually navigated.
120 CommitPendingNavigation();
123 void TestWebContents::TestSetIsLoading(bool value) {
124 SetIsLoading(GetRenderViewHost(), value, NULL);
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);
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;
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());
154 void TestWebContents::ProceedWithCrossSiteNavigation() {
155 if (!GetPendingRenderViewHost())
156 return;
157 TestRenderViewHost* rvh = static_cast<TestRenderViewHost*>(
158 GetRenderViewHost());
159 rvh->SendShouldCloseACK(true);
162 RenderViewHostDelegateView* TestWebContents::GetDelegateView() {
163 if (delegate_view_override_)
164 return delegate_view_override_;
165 return WebContentsImpl::GetDelegateView();
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_);
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);
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;
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);
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);
210 void TestWebContents::TestDidFailLoadWithError(
211 int64 frame_id,
212 const GURL& url,
213 bool is_main_frame,
214 int error_code,
215 const string16& error_description) {
216 ViewHostMsg_DidFailLoadWithError msg(
217 0, frame_id, url, is_main_frame, error_code, error_description);
218 OnMessageReceived(GetRenderViewHost(), msg);
221 void TestWebContents::CreateNewWindow(
222 int route_id,
223 int main_frame_route_id,
224 const ViewHostMsg_CreateWindow_Params& params,
225 SessionStorageNamespace* session_storage_namespace) {
228 void TestWebContents::CreateNewWidget(int route_id,
229 WebKit::WebPopupType popup_type) {
232 void TestWebContents::CreateNewFullscreenWidget(int route_id) {
235 void TestWebContents::ShowCreatedWindow(int route_id,
236 WindowOpenDisposition disposition,
237 const gfx::Rect& initial_pos,
238 bool user_gesture) {
241 void TestWebContents::ShowCreatedWidget(int route_id,
242 const gfx::Rect& initial_pos) {
245 void TestWebContents::ShowCreatedFullscreenWidget(int route_id) {
248 } // namespace content