OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/media/webrtc_browsertest_base.h" | 5 #include "chrome/browser/media/webrtc_browsertest_base.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/infobars/infobar_responder.h" |
11 #include "chrome/browser/infobars/infobar_service.h" | 11 #include "chrome/browser/infobars/infobar_service.h" |
12 #include "chrome/browser/media/media_stream_infobar_delegate.h" | |
13 #include "chrome/browser/media/webrtc_browsertest_common.h" | 12 #include "chrome/browser/media/webrtc_browsertest_common.h" |
14 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
15 #include "chrome/browser/ui/browser_tabstrip.h" | 14 #include "chrome/browser/ui/browser_tabstrip.h" |
16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
17 #include "chrome/browser/ui/website_settings/mock_permission_bubble_view.h" | |
18 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" | 16 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" |
19 #include "chrome/test/base/ui_test_utils.h" | 17 #include "chrome/test/base/ui_test_utils.h" |
20 #include "components/infobars/core/infobar.h" | 18 #include "components/infobars/core/infobar.h" |
21 #include "content/public/browser/notification_service.h" | |
22 #include "content/public/test/browser_test_utils.h" | 19 #include "content/public/test/browser_test_utils.h" |
23 #include "net/test/embedded_test_server/embedded_test_server.h" | 20 #include "net/test/embedded_test_server/embedded_test_server.h" |
24 | 21 |
25 #if defined(OS_WIN) | 22 #if defined(OS_WIN) |
26 // For fine-grained suppression. | 23 // For fine-grained suppression. |
27 #include "base/win/windows_version.h" | 24 #include "base/win/windows_version.h" |
28 #endif | 25 #endif |
29 | 26 |
30 const char WebRtcTestBase::kAudioVideoCallConstraints[] = | 27 const char WebRtcTestBase::kAudioVideoCallConstraints[] = |
31 "{audio: true, video: true}"; | 28 "{audio: true, video: true}"; |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 | 102 |
106 bool WebRtcTestBase::GetUserMediaAndAccept( | 103 bool WebRtcTestBase::GetUserMediaAndAccept( |
107 content::WebContents* tab_contents) const { | 104 content::WebContents* tab_contents) const { |
108 return GetUserMediaWithSpecificConstraintsAndAccept( | 105 return GetUserMediaWithSpecificConstraintsAndAccept( |
109 tab_contents, kAudioVideoCallConstraints); | 106 tab_contents, kAudioVideoCallConstraints); |
110 } | 107 } |
111 | 108 |
112 bool WebRtcTestBase::GetUserMediaWithSpecificConstraintsAndAccept( | 109 bool WebRtcTestBase::GetUserMediaWithSpecificConstraintsAndAccept( |
113 content::WebContents* tab_contents, | 110 content::WebContents* tab_contents, |
114 const std::string& constraints) const { | 111 const std::string& constraints) const { |
| 112 std::string result; |
115 if (PermissionBubbleManager::Enabled()) { | 113 if (PermissionBubbleManager::Enabled()) { |
116 scoped_ptr<MockPermissionBubbleView> mock_bubble_view( | |
117 new MockPermissionBubbleView()); | |
118 PermissionBubbleManager::FromWebContents(tab_contents) | 114 PermissionBubbleManager::FromWebContents(tab_contents) |
119 ->SetView(mock_bubble_view.get()); | 115 ->set_auto_response_for_test(PermissionBubbleManager::ACCEPT_ALL); |
120 mock_bubble_view->SetBrowserTest(true); | |
121 GetUserMedia(tab_contents, constraints); | 116 GetUserMedia(tab_contents, constraints); |
122 if (!mock_bubble_view->IsVisible()) | 117 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
123 content::RunMessageLoop(); | 118 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); |
124 mock_bubble_view->Accept(); | |
125 // Wait for WebRTC to call the success callback, then remove the view. | |
126 const bool result = test::PollingWaitUntil("obtainGetUserMediaResult()", | |
127 kOkGotStream, tab_contents); | |
128 PermissionBubbleManager::FromWebContents(tab_contents)->SetView(NULL); | |
129 return result; | |
130 } else { | 119 } else { |
131 infobars::InfoBar* infobar = | 120 InfoBarResponder infobar_accept_responder( |
132 GetUserMediaAndWaitForInfoBar(tab_contents, constraints); | 121 InfoBarService::FromWebContents(tab_contents), |
133 infobar->delegate()->AsConfirmInfoBarDelegate()->Accept(); | 122 InfoBarResponder::ACCEPT); |
134 CloseInfoBarInTab(tab_contents, infobar); | 123 GetUserMedia(tab_contents, constraints); |
135 // Wait for WebRTC to call the success callback. | 124 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
136 return test::PollingWaitUntil("obtainGetUserMediaResult()", kOkGotStream, | 125 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); |
137 tab_contents); | |
138 } | 126 } |
| 127 return kOkGotStream == result; |
139 } | 128 } |
140 | 129 |
141 void WebRtcTestBase::GetUserMediaAndDeny(content::WebContents* tab_contents) { | 130 void WebRtcTestBase::GetUserMediaAndDeny(content::WebContents* tab_contents) { |
142 return GetUserMediaWithSpecificConstraintsAndDeny(tab_contents, | 131 return GetUserMediaWithSpecificConstraintsAndDeny(tab_contents, |
143 kAudioVideoCallConstraints); | 132 kAudioVideoCallConstraints); |
144 } | 133 } |
145 | 134 |
146 void WebRtcTestBase::GetUserMediaWithSpecificConstraintsAndDeny( | 135 void WebRtcTestBase::GetUserMediaWithSpecificConstraintsAndDeny( |
147 content::WebContents* tab_contents, | 136 content::WebContents* tab_contents, |
148 const std::string& constraints) const { | 137 const std::string& constraints) const { |
| 138 std::string result; |
149 if (PermissionBubbleManager::Enabled()) { | 139 if (PermissionBubbleManager::Enabled()) { |
150 scoped_ptr<MockPermissionBubbleView> mock_bubble_view( | |
151 new MockPermissionBubbleView()); | |
152 PermissionBubbleManager::FromWebContents(tab_contents) | 140 PermissionBubbleManager::FromWebContents(tab_contents) |
153 ->SetView(mock_bubble_view.get()); | 141 ->set_auto_response_for_test(PermissionBubbleManager::DENY_ALL); |
154 mock_bubble_view->SetBrowserTest(true); | |
155 GetUserMedia(tab_contents, constraints); | 142 GetUserMedia(tab_contents, constraints); |
156 if (!mock_bubble_view->IsVisible()) | 143 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
157 content::RunMessageLoop(); | 144 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); |
158 mock_bubble_view->Deny(); | |
159 // Wait for WebRTC to call the fail callback, then remove the view. | |
160 EXPECT_TRUE(test::PollingWaitUntil("obtainGetUserMediaResult()", | |
161 kFailedWithPermissionDeniedError, | |
162 tab_contents)); | |
163 PermissionBubbleManager::FromWebContents(tab_contents)->SetView(NULL); | |
164 } else { | 145 } else { |
165 infobars::InfoBar* infobar = | 146 InfoBarResponder infobar_deny_responder( |
166 GetUserMediaAndWaitForInfoBar(tab_contents, constraints); | 147 InfoBarService::FromWebContents(tab_contents), InfoBarResponder::DENY); |
167 infobar->delegate()->AsConfirmInfoBarDelegate()->Cancel(); | 148 GetUserMedia(tab_contents, constraints); |
168 CloseInfoBarInTab(tab_contents, infobar); | 149 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
169 // Wait for WebRTC to call the fail callback. | 150 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); |
170 EXPECT_TRUE(test::PollingWaitUntil("obtainGetUserMediaResult()", | |
171 kFailedWithPermissionDeniedError, | |
172 tab_contents)); | |
173 } | 151 } |
| 152 EXPECT_EQ(kFailedWithPermissionDeniedError, result); |
174 } | 153 } |
175 | 154 |
176 void WebRtcTestBase::GetUserMediaAndDismiss( | 155 void WebRtcTestBase::GetUserMediaAndDismiss( |
177 content::WebContents* tab_contents) const { | 156 content::WebContents* tab_contents) const { |
| 157 std::string result; |
178 if (PermissionBubbleManager::Enabled()) { | 158 if (PermissionBubbleManager::Enabled()) { |
179 scoped_ptr<MockPermissionBubbleView> mock_bubble_view( | |
180 new MockPermissionBubbleView()); | |
181 PermissionBubbleManager::FromWebContents(tab_contents) | 159 PermissionBubbleManager::FromWebContents(tab_contents) |
182 ->SetView(mock_bubble_view.get()); | 160 ->set_auto_response_for_test(PermissionBubbleManager::DISMISS); |
183 mock_bubble_view->SetBrowserTest(true); | |
184 GetUserMedia(tab_contents, kAudioVideoCallConstraints); | 161 GetUserMedia(tab_contents, kAudioVideoCallConstraints); |
185 if (!mock_bubble_view->IsVisible()) | |
186 content::RunMessageLoop(); | |
187 mock_bubble_view->Close(); | |
188 // A dismiss should be treated like a deny. | 162 // A dismiss should be treated like a deny. |
189 EXPECT_TRUE(test::PollingWaitUntil("obtainGetUserMediaResult()", | 163 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
190 kFailedWithPermissionDismissedError, | 164 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); |
191 tab_contents)); | |
192 PermissionBubbleManager::FromWebContents(tab_contents)->SetView(NULL); | |
193 } else { | 165 } else { |
194 infobars::InfoBar* infobar = | 166 InfoBarResponder infobar_dismiss_responder( |
195 GetUserMediaAndWaitForInfoBar(tab_contents, kAudioVideoCallConstraints); | 167 InfoBarService::FromWebContents(tab_contents), |
196 infobar->delegate()->InfoBarDismissed(); | 168 InfoBarResponder::DISMISS); |
197 CloseInfoBarInTab(tab_contents, infobar); | 169 GetUserMedia(tab_contents, kAudioVideoCallConstraints); |
198 // A dismiss should be treated like a deny. | 170 // A dismiss should be treated like a deny. |
199 EXPECT_TRUE(test::PollingWaitUntil("obtainGetUserMediaResult()", | 171 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
200 kFailedWithPermissionDismissedError, | 172 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); |
201 tab_contents)); | |
202 } | 173 } |
| 174 EXPECT_EQ(kFailedWithPermissionDismissedError, result); |
203 } | 175 } |
204 | 176 |
205 void WebRtcTestBase::GetUserMedia(content::WebContents* tab_contents, | 177 void WebRtcTestBase::GetUserMedia(content::WebContents* tab_contents, |
206 const std::string& constraints) const { | 178 const std::string& constraints) const { |
207 // Request user media: this will launch the media stream info bar. | 179 // Request user media: this will launch the media stream info bar or bubble. |
208 std::string result; | 180 std::string result; |
209 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 181 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
210 tab_contents, "doGetUserMedia(" + constraints + ");", &result)); | 182 tab_contents, "doGetUserMedia(" + constraints + ");", &result)); |
211 EXPECT_EQ("ok-requested", result); | 183 EXPECT_TRUE(result == "request-callback-denied" || |
212 } | 184 result == "request-callback-granted"); |
213 | |
214 infobars::InfoBar* WebRtcTestBase::GetUserMediaAndWaitForInfoBar( | |
215 content::WebContents* tab_contents, | |
216 const std::string& constraints) const { | |
217 content::WindowedNotificationObserver infobar_added( | |
218 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, | |
219 content::NotificationService::AllSources()); | |
220 | |
221 // Request user media: this will launch the media stream info bar. | |
222 GetUserMedia(tab_contents, constraints); | |
223 | |
224 // Wait for the bar to pop up, then return it. | |
225 infobar_added.Wait(); | |
226 content::Details<infobars::InfoBar::AddedDetails> details( | |
227 infobar_added.details()); | |
228 EXPECT_TRUE(details->delegate()->AsMediaStreamInfoBarDelegate()); | |
229 return details.ptr(); | |
230 } | 185 } |
231 | 186 |
232 content::WebContents* WebRtcTestBase::OpenPageAndGetUserMediaInNewTab( | 187 content::WebContents* WebRtcTestBase::OpenPageAndGetUserMediaInNewTab( |
233 const GURL& url) const { | 188 const GURL& url) const { |
234 return OpenPageAndGetUserMediaInNewTabWithConstraints( | 189 return OpenPageAndGetUserMediaInNewTabWithConstraints( |
235 url, kAudioVideoCallConstraints); | 190 url, kAudioVideoCallConstraints); |
236 } | 191 } |
237 | 192 |
238 content::WebContents* | 193 content::WebContents* |
239 WebRtcTestBase::OpenPageAndGetUserMediaInNewTabWithConstraints( | 194 WebRtcTestBase::OpenPageAndGetUserMediaInNewTabWithConstraints( |
240 const GURL& url, | 195 const GURL& url, |
241 const std::string& constraints) const { | 196 const std::string& constraints) const { |
242 chrome::AddTabAt(browser(), GURL(), -1, true); | 197 chrome::AddTabAt(browser(), GURL(), -1, true); |
243 ui_test_utils::NavigateToURL(browser(), url); | 198 ui_test_utils::NavigateToURL(browser(), url); |
244 content::WebContents* new_tab = | 199 content::WebContents* new_tab = |
245 browser()->tab_strip_model()->GetActiveWebContents(); | 200 browser()->tab_strip_model()->GetActiveWebContents(); |
246 EXPECT_TRUE(GetUserMediaWithSpecificConstraintsAndAccept( | 201 EXPECT_TRUE(GetUserMediaWithSpecificConstraintsAndAccept( |
247 new_tab, constraints)); | 202 new_tab, constraints)); |
248 return new_tab; | 203 return new_tab; |
249 } | 204 } |
250 | 205 |
251 content::WebContents* WebRtcTestBase::OpenTestPageAndGetUserMediaInNewTab( | 206 content::WebContents* WebRtcTestBase::OpenTestPageAndGetUserMediaInNewTab( |
252 const std::string& test_page) const { | 207 const std::string& test_page) const { |
253 return OpenPageAndGetUserMediaInNewTab( | 208 return OpenPageAndGetUserMediaInNewTab( |
254 embedded_test_server()->GetURL(test_page)); | 209 embedded_test_server()->GetURL(test_page)); |
255 } | 210 } |
256 | 211 |
257 content::WebContents* WebRtcTestBase::OpenPageAndAcceptUserMedia( | |
258 const GURL& url) const { | |
259 content::WebContents* tab_contents = | |
260 browser()->tab_strip_model()->GetActiveWebContents(); | |
261 if (PermissionBubbleManager::Enabled()) { | |
262 scoped_ptr<MockPermissionBubbleView> mock_bubble_view( | |
263 new MockPermissionBubbleView()); | |
264 PermissionBubbleManager::FromWebContents(tab_contents) | |
265 ->SetView(mock_bubble_view.get()); | |
266 mock_bubble_view->SetBrowserTest(true); | |
267 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(browser(), url, | |
268 1); | |
269 if (!mock_bubble_view->IsVisible()) | |
270 content::RunMessageLoop(); | |
271 mock_bubble_view->Accept(); | |
272 // Wait to make sure the callback is fired before deleting the bubble view. | |
273 test::PollingWaitUntil("obtainGetUserMediaResult()", kOkGotStream, | |
274 tab_contents); | |
275 PermissionBubbleManager::FromWebContents(tab_contents)->SetView(NULL); | |
276 } else { | |
277 content::WindowedNotificationObserver nav( | |
278 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, | |
279 content::NotificationService::AllSources()); | |
280 ui_test_utils::NavigateToURL(browser(), url); | |
281 nav.Wait(); | |
282 | |
283 content::Details<infobars::InfoBar::AddedDetails> details(nav.details()); | |
284 infobars::InfoBar* infobar = details.ptr(); | |
285 EXPECT_TRUE(infobar); | |
286 infobar->delegate()->AsMediaStreamInfoBarDelegate()->Accept(); | |
287 | |
288 CloseInfoBarInTab(tab_contents, infobar); | |
289 } | |
290 return tab_contents; | |
291 } | |
292 | |
293 void WebRtcTestBase::CloseInfoBarInTab(content::WebContents* tab_contents, | |
294 infobars::InfoBar* infobar) const { | |
295 content::WindowedNotificationObserver infobar_removed( | |
296 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | |
297 content::NotificationService::AllSources()); | |
298 | |
299 InfoBarService* infobar_service = | |
300 InfoBarService::FromWebContents(tab_contents); | |
301 infobar_service->RemoveInfoBar(infobar); | |
302 | |
303 infobar_removed.Wait(); | |
304 } | |
305 | |
306 void WebRtcTestBase::CloseLastLocalStream( | 212 void WebRtcTestBase::CloseLastLocalStream( |
307 content::WebContents* tab_contents) const { | 213 content::WebContents* tab_contents) const { |
308 EXPECT_EQ("ok-stopped", | 214 EXPECT_EQ("ok-stopped", |
309 ExecuteJavascript("stopLocalStream();", tab_contents)); | 215 ExecuteJavascript("stopLocalStream();", tab_contents)); |
310 } | 216 } |
311 | 217 |
312 // Convenience method which executes the provided javascript in the context | 218 // Convenience method which executes the provided javascript in the context |
313 // of the provided web contents and returns what it evaluated to. | 219 // of the provided web contents and returns what it evaluated to. |
314 std::string WebRtcTestBase::ExecuteJavascript( | 220 std::string WebRtcTestBase::ExecuteJavascript( |
315 const std::string& javascript, | 221 const std::string& javascript, |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 #endif | 341 #endif |
436 } | 342 } |
437 | 343 |
438 bool WebRtcTestBase::OnWin8() const { | 344 bool WebRtcTestBase::OnWin8() const { |
439 #if defined(OS_WIN) | 345 #if defined(OS_WIN) |
440 return base::win::GetVersion() > base::win::VERSION_WIN7; | 346 return base::win::GetVersion() > base::win::VERSION_WIN7; |
441 #else | 347 #else |
442 return false; | 348 return false; |
443 #endif | 349 #endif |
444 } | 350 } |
OLD | NEW |