Make sure webrtc::VideoSource is released when WebRtcVideoTrackAdapter is destroyed.
[chromium-blink-merge.git] / content / test / test_web_contents.cc
bloba9c239e2d34648251328e16373b324a34d377ec6
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/frame_host/cross_process_frame_connector.h"
11 #include "content/browser/frame_host/navigation_entry_impl.h"
12 #include "content/browser/frame_host/navigator.h"
13 #include "content/browser/renderer_host/render_view_host_impl.h"
14 #include "content/browser/site_instance_impl.h"
15 #include "content/common/frame_messages.h"
16 #include "content/common/view_messages.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "content/public/browser/notification_source.h"
19 #include "content/public/browser/notification_types.h"
20 #include "content/public/common/page_state.h"
21 #include "content/public/common/page_transition_types.h"
22 #include "content/public/test/mock_render_process_host.h"
23 #include "content/test/test_render_view_host.h"
25 namespace content {
27 TestWebContents::TestWebContents(BrowserContext* browser_context)
28 : WebContentsImpl(browser_context, NULL),
29 transition_cross_site(false),
30 delegate_view_override_(NULL),
31 expect_set_history_length_and_prune_(false),
32 expect_set_history_length_and_prune_site_instance_(NULL),
33 expect_set_history_length_and_prune_history_length_(0),
34 expect_set_history_length_and_prune_min_page_id_(-1) {
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_length_and_prune_);
48 RenderViewHost* TestWebContents::GetPendingRenderViewHost() const {
49 return GetRenderManager()->pending_render_view_host();
52 TestRenderViewHost* TestWebContents::pending_test_rvh() const {
53 return static_cast<TestRenderViewHost*>(GetPendingRenderViewHost());
56 void TestWebContents::TestDidNavigate(RenderViewHost* render_view_host,
57 int page_id,
58 const GURL& url,
59 PageTransition transition) {
60 TestDidNavigateWithReferrer(render_view_host,
61 page_id,
62 url,
63 Referrer(),
64 transition);
67 void TestWebContents::TestDidNavigateWithReferrer(
68 RenderViewHost* render_view_host,
69 int page_id,
70 const GURL& url,
71 const Referrer& referrer,
72 PageTransition transition) {
73 FrameHostMsg_DidCommitProvisionalLoad_Params params;
75 params.page_id = page_id;
76 params.url = url;
77 params.referrer = referrer;
78 params.transition = transition;
79 params.redirects = std::vector<GURL>();
80 params.should_update_history = false;
81 params.searchable_form_url = GURL();
82 params.searchable_form_encoding = std::string();
83 params.security_info = std::string();
84 params.gesture = NavigationGestureUser;
85 params.was_within_same_page = false;
86 params.is_post = false;
87 params.page_state = PageState::CreateFromURL(url);
89 RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>(render_view_host);
90 RenderFrameHostImpl* rfh = RenderFrameHostImpl::FromID(
91 rvh->GetProcess()->GetID(), rvh->main_frame_routing_id());
92 frame_tree_.root()->navigator()->DidNavigate(rfh, params);
95 WebPreferences TestWebContents::TestGetWebkitPrefs() {
96 return GetWebkitPrefs();
99 bool TestWebContents::CreateRenderViewForRenderManager(
100 RenderViewHost* render_view_host,
101 int opener_route_id,
102 int proxy_routing_id,
103 bool for_main_frame) {
104 UpdateMaxPageIDIfNecessary(render_view_host);
105 // This will go to a TestRenderViewHost.
106 static_cast<RenderViewHostImpl*>(
107 render_view_host)->CreateRenderView(base::string16(),
108 opener_route_id,
109 proxy_routing_id,
110 -1, false);
111 return true;
114 WebContents* TestWebContents::Clone() {
115 WebContentsImpl* contents =
116 Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
117 contents->GetController().CopyStateFrom(controller_);
118 return contents;
121 void TestWebContents::NavigateAndCommit(const GURL& url) {
122 GetController().LoadURL(
123 url, Referrer(), PAGE_TRANSITION_LINK, std::string());
124 GURL loaded_url(url);
125 bool reverse_on_redirect = false;
126 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
127 &loaded_url, GetBrowserContext(), &reverse_on_redirect);
129 // LoadURL created a navigation entry, now simulate the RenderView sending
130 // a notification that it actually navigated.
131 CommitPendingNavigation();
134 void TestWebContents::TestSetIsLoading(bool value) {
135 SetIsLoading(GetRenderViewHost(), value, true, NULL);
138 void TestWebContents::CommitPendingNavigation() {
139 // If we are doing a cross-site navigation, this simulates the current RVH
140 // notifying that it has unloaded so the pending RVH is resumed and can
141 // navigate.
142 ProceedWithCrossSiteNavigation();
143 RenderViewHost* old_rvh = GetRenderViewHost();
144 TestRenderViewHost* rvh =
145 static_cast<TestRenderViewHost*>(GetPendingRenderViewHost());
146 if (!rvh)
147 rvh = static_cast<TestRenderViewHost*>(old_rvh);
149 const NavigationEntry* entry = GetController().GetPendingEntry();
150 DCHECK(entry);
151 int page_id = entry->GetPageID();
152 if (page_id == -1) {
153 // It's a new navigation, assign a never-seen page id to it.
154 page_id = GetMaxPageIDForSiteInstance(rvh->GetSiteInstance()) + 1;
157 rvh->SendNavigate(page_id, entry->GetURL());
158 // Simulate the SwapOut_ACK. This is needed when cross-site navigation happens
159 // (old_rvh != rvh).
160 if (old_rvh != rvh)
161 static_cast<RenderViewHostImpl*>(old_rvh)->OnSwappedOut(false);
164 void TestWebContents::ProceedWithCrossSiteNavigation() {
165 if (!GetPendingRenderViewHost())
166 return;
167 TestRenderViewHost* rvh = static_cast<TestRenderViewHost*>(
168 GetRenderViewHost());
169 rvh->SendBeforeUnloadACK(true);
172 RenderViewHostDelegateView* TestWebContents::GetDelegateView() {
173 if (delegate_view_override_)
174 return delegate_view_override_;
175 return WebContentsImpl::GetDelegateView();
178 void TestWebContents::SetOpener(TestWebContents* opener) {
179 // This is normally only set in the WebContents constructor, which also
180 // registers an observer for when the opener gets closed.
181 opener_ = opener;
182 AddDestructionObserver(opener_);
185 void TestWebContents::AddPendingContents(TestWebContents* contents) {
186 // This is normally only done in WebContentsImpl::CreateNewWindow.
187 pending_contents_[contents->GetRenderViewHost()->GetRoutingID()] = contents;
188 AddDestructionObserver(contents);
191 void TestWebContents::ExpectSetHistoryLengthAndPrune(
192 const SiteInstance* site_instance,
193 int history_length,
194 int32 min_page_id) {
195 expect_set_history_length_and_prune_ = true;
196 expect_set_history_length_and_prune_site_instance_ =
197 static_cast<const SiteInstanceImpl*>(site_instance);
198 expect_set_history_length_and_prune_history_length_ = history_length;
199 expect_set_history_length_and_prune_min_page_id_ = min_page_id;
202 void TestWebContents::SetHistoryLengthAndPrune(
203 const SiteInstance* site_instance, int history_length,
204 int32 min_page_id) {
205 EXPECT_TRUE(expect_set_history_length_and_prune_);
206 expect_set_history_length_and_prune_ = false;
207 EXPECT_EQ(expect_set_history_length_and_prune_site_instance_, site_instance);
208 EXPECT_EQ(expect_set_history_length_and_prune_history_length_,
209 history_length);
210 EXPECT_EQ(expect_set_history_length_and_prune_min_page_id_, min_page_id);
213 void TestWebContents::TestDidFinishLoad(const GURL& url) {
214 FrameHostMsg_DidFinishLoad msg(0, url);
215 frame_tree_.root()->current_frame_host()->OnMessageReceived(msg);
218 void TestWebContents::TestDidFailLoadWithError(
219 const GURL& url,
220 int error_code,
221 const base::string16& error_description) {
222 FrameHostMsg_DidFailLoadWithError msg(
223 0, url, error_code, error_description);
224 frame_tree_.root()->current_frame_host()->OnMessageReceived(msg);
227 void TestWebContents::CreateNewWindow(
228 int render_process_id,
229 int route_id,
230 int main_frame_route_id,
231 const ViewHostMsg_CreateWindow_Params& params,
232 SessionStorageNamespace* session_storage_namespace) {
235 void TestWebContents::CreateNewWidget(int render_process_id,
236 int route_id,
237 blink::WebPopupType popup_type) {
240 void TestWebContents::CreateNewFullscreenWidget(int render_process_id,
241 int route_id) {
244 void TestWebContents::ShowCreatedWindow(int route_id,
245 WindowOpenDisposition disposition,
246 const gfx::Rect& initial_pos,
247 bool user_gesture) {
250 void TestWebContents::ShowCreatedWidget(int route_id,
251 const gfx::Rect& initial_pos) {
254 void TestWebContents::ShowCreatedFullscreenWidget(int route_id) {
257 } // namespace content