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
3 // found in the LICENSE file.
5 #include "chrome/browser/media/webrtc_browsertest_base.h"
7 #include "base/lazy_instance.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/stringprintf.h"
10 #include "chrome/browser/chrome_notification_types.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"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_tabstrip.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/test/base/ui_test_utils.h"
18 #include "components/infobars/core/infobar.h"
19 #include "content/public/browser/notification_service.h"
20 #include "content/public/test/browser_test_utils.h"
21 #include "net/test/embedded_test_server/embedded_test_server.h"
23 const char WebRtcTestBase::kAudioVideoCallConstraints
[] =
24 "'{audio: true, video: true}'";
25 const char WebRtcTestBase::kAudioVideoCallConstraintsQVGA
[] =
26 "'{audio: true, video: {mandatory: {minWidth: 320, maxWidth: 320, "
27 " minHeight: 240, maxHeight: 240}}}'";
28 const char WebRtcTestBase::kAudioVideoCallConstraints360p
[] =
29 "'{audio: true, video: {mandatory: {minWidth: 640, maxWidth: 640, "
30 " minHeight: 360, maxHeight: 360}}}'";
31 const char WebRtcTestBase::kAudioVideoCallConstraintsVGA
[] =
32 "'{audio: true, video: {mandatory: {minWidth: 640, maxWidth: 640, "
33 " minHeight: 480, maxHeight: 480}}}'";
34 const char WebRtcTestBase::kAudioVideoCallConstraints720p
[] =
35 "'{audio: true, video: {mandatory: {minWidth: 1280, maxWidth: 1280, "
36 " minHeight: 720, maxHeight: 720}}}'";
37 const char WebRtcTestBase::kAudioVideoCallConstraints1080p
[] =
38 "'{audio: true, video: {mandatory: {minWidth: 1920, maxWidth: 1920, "
39 " minHeight: 1080, maxHeight: 1080}}}'";
40 const char WebRtcTestBase::kAudioOnlyCallConstraints
[] = "'{audio: true}'";
41 const char WebRtcTestBase::kVideoOnlyCallConstraints
[] = "'{video: true}'";
42 const char WebRtcTestBase::kFailedWithPermissionDeniedError
[] =
43 "failed-with-error-PermissionDeniedError";
44 const char WebRtcTestBase::kFailedWithPermissionDismissedError
[] =
45 "failed-with-error-PermissionDismissedError";
49 base::LazyInstance
<bool> hit_javascript_errors_
=
50 LAZY_INSTANCE_INITIALIZER
;
52 // Intercepts all log messages. We always attach this handler but only look at
53 // the results if the test requests so. Note that this will only work if the
54 // WebrtcTestBase-inheriting test cases do not run in parallel (if they did they
55 // would race to look at the log, which is global to all tests).
56 bool JavascriptErrorDetectingLogHandler(int severity
,
60 const std::string
& str
) {
61 if (file
== NULL
|| std::string("CONSOLE") != file
)
64 bool contains_uncaught
= str
.find("\"Uncaught ") != std::string::npos
;
65 if (severity
== logging::LOG_ERROR
||
66 (severity
== logging::LOG_INFO
&& contains_uncaught
)) {
67 hit_javascript_errors_
.Get() = true;
75 WebRtcTestBase::WebRtcTestBase(): detect_errors_in_javascript_(false) {
76 // The handler gets set for each test method, but that's fine since this
77 // set operation is idempotent.
78 logging::SetLogMessageHandler(&JavascriptErrorDetectingLogHandler
);
79 hit_javascript_errors_
.Get() = false;
84 WebRtcTestBase::~WebRtcTestBase() {
85 if (detect_errors_in_javascript_
) {
86 EXPECT_FALSE(hit_javascript_errors_
.Get())
87 << "Encountered javascript errors during test execution (Search "
88 << "for Uncaught or ERROR:CONSOLE in the test output).";
92 void WebRtcTestBase::GetUserMediaAndAccept(
93 content::WebContents
* tab_contents
) const {
94 GetUserMediaWithSpecificConstraintsAndAccept(tab_contents
,
95 kAudioVideoCallConstraints
);
98 void WebRtcTestBase::GetUserMediaWithSpecificConstraintsAndAccept(
99 content::WebContents
* tab_contents
,
100 const std::string
& constraints
) const {
101 infobars::InfoBar
* infobar
=
102 GetUserMediaAndWaitForInfoBar(tab_contents
, constraints
);
103 infobar
->delegate()->AsConfirmInfoBarDelegate()->Accept();
104 CloseInfoBarInTab(tab_contents
, infobar
);
106 // Wait for WebRTC to call the success callback.
107 const char kOkGotStream
[] = "ok-got-stream";
108 EXPECT_TRUE(test::PollingWaitUntil("obtainGetUserMediaResult()", kOkGotStream
,
112 void WebRtcTestBase::GetUserMediaAndDeny(content::WebContents
* tab_contents
) {
113 return GetUserMediaWithSpecificConstraintsAndDeny(tab_contents
,
114 kAudioVideoCallConstraints
);
117 void WebRtcTestBase::GetUserMediaWithSpecificConstraintsAndDeny(
118 content::WebContents
* tab_contents
,
119 const std::string
& constraints
) const {
120 infobars::InfoBar
* infobar
=
121 GetUserMediaAndWaitForInfoBar(tab_contents
, constraints
);
122 infobar
->delegate()->AsConfirmInfoBarDelegate()->Cancel();
123 CloseInfoBarInTab(tab_contents
, infobar
);
125 // Wait for WebRTC to call the fail callback.
126 EXPECT_TRUE(test::PollingWaitUntil("obtainGetUserMediaResult()",
127 kFailedWithPermissionDeniedError
,
131 void WebRtcTestBase::GetUserMediaAndDismiss(
132 content::WebContents
* tab_contents
) const {
133 infobars::InfoBar
* infobar
=
134 GetUserMediaAndWaitForInfoBar(tab_contents
, kAudioVideoCallConstraints
);
135 infobar
->delegate()->InfoBarDismissed();
136 CloseInfoBarInTab(tab_contents
, infobar
);
138 // A dismiss should be treated like a deny.
139 EXPECT_TRUE(test::PollingWaitUntil("obtainGetUserMediaResult()",
140 kFailedWithPermissionDismissedError
,
144 void WebRtcTestBase::GetUserMedia(content::WebContents
* tab_contents
,
145 const std::string
& constraints
) const {
146 // Request user media: this will launch the media stream info bar.
148 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
149 tab_contents
, "doGetUserMedia(" + constraints
+ ");", &result
));
150 EXPECT_EQ("ok-requested", result
);
153 infobars::InfoBar
* WebRtcTestBase::GetUserMediaAndWaitForInfoBar(
154 content::WebContents
* tab_contents
,
155 const std::string
& constraints
) const {
156 content::WindowedNotificationObserver
infobar_added(
157 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED
,
158 content::NotificationService::AllSources());
160 // Request user media: this will launch the media stream info bar.
161 GetUserMedia(tab_contents
, constraints
);
163 // Wait for the bar to pop up, then return it.
164 infobar_added
.Wait();
165 content::Details
<infobars::InfoBar::AddedDetails
> details(
166 infobar_added
.details());
167 EXPECT_TRUE(details
->delegate()->AsMediaStreamInfoBarDelegate());
168 return details
.ptr();
171 content::WebContents
* WebRtcTestBase::OpenPageAndGetUserMediaInNewTab(
172 const GURL
& url
) const {
173 return OpenPageAndGetUserMediaInNewTabWithConstraints(
174 url
, kAudioVideoCallConstraints
);
177 content::WebContents
*
178 WebRtcTestBase::OpenPageAndGetUserMediaInNewTabWithConstraints(
180 const std::string
& constraints
) const {
181 chrome::AddTabAt(browser(), GURL(), -1, true);
182 ui_test_utils::NavigateToURL(browser(), url
);
183 #if defined (OS_LINUX)
184 // Load the page again on Linux to work around crbug.com/281268.
185 ui_test_utils::NavigateToURL(browser(), url
);
187 content::WebContents
* new_tab
=
188 browser()->tab_strip_model()->GetActiveWebContents();
189 GetUserMediaWithSpecificConstraintsAndAccept(new_tab
, constraints
);
193 content::WebContents
* WebRtcTestBase::OpenTestPageAndGetUserMediaInNewTab(
194 const std::string
& test_page
) const {
195 return OpenPageAndGetUserMediaInNewTab(
196 embedded_test_server()->GetURL(test_page
));
199 content::WebContents
* WebRtcTestBase::OpenPageAndAcceptUserMedia(
200 const GURL
& url
) const {
201 content::WindowedNotificationObserver
infobar_added(
202 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED
,
203 content::NotificationService::AllSources());
205 ui_test_utils::NavigateToURL(browser(), url
);
207 infobar_added
.Wait();
209 content::WebContents
* tab_contents
=
210 browser()->tab_strip_model()->GetActiveWebContents();
211 content::Details
<infobars::InfoBar::AddedDetails
> details(
212 infobar_added
.details());
213 infobars::InfoBar
* infobar
= details
.ptr();
214 EXPECT_TRUE(infobar
);
215 infobar
->delegate()->AsMediaStreamInfoBarDelegate()->Accept();
217 CloseInfoBarInTab(tab_contents
, infobar
);
221 void WebRtcTestBase::CloseInfoBarInTab(content::WebContents
* tab_contents
,
222 infobars::InfoBar
* infobar
) const {
223 content::WindowedNotificationObserver
infobar_removed(
224 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED
,
225 content::NotificationService::AllSources());
227 InfoBarService
* infobar_service
=
228 InfoBarService::FromWebContents(tab_contents
);
229 infobar_service
->RemoveInfoBar(infobar
);
231 infobar_removed
.Wait();
234 void WebRtcTestBase::CloseLastLocalStream(
235 content::WebContents
* tab_contents
) const {
236 EXPECT_EQ("ok-stopped",
237 ExecuteJavascript("stopLocalStream();", tab_contents
));
240 // Convenience method which executes the provided javascript in the context
241 // of the provided web contents and returns what it evaluated to.
242 std::string
WebRtcTestBase::ExecuteJavascript(
243 const std::string
& javascript
,
244 content::WebContents
* tab_contents
) const {
246 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
247 tab_contents
, javascript
, &result
));
251 // The peer connection server lets our two tabs find each other and talk to
252 // each other (e.g. it is the application-specific "signaling solution").
253 void WebRtcTestBase::ConnectToPeerConnectionServer(
254 const std::string
& peer_name
,
255 content::WebContents
* tab_contents
) const {
256 std::string javascript
= base::StringPrintf(
257 "connect('http://localhost:%s', '%s');",
258 test::PeerConnectionServerRunner::kDefaultPort
, peer_name
.c_str());
259 EXPECT_EQ("ok-connected", ExecuteJavascript(javascript
, tab_contents
));
262 void WebRtcTestBase::EstablishCall(content::WebContents
* from_tab
,
263 content::WebContents
* to_tab
) const {
264 ConnectToPeerConnectionServer("peer 1", from_tab
);
265 ConnectToPeerConnectionServer("peer 2", to_tab
);
267 EXPECT_EQ("ok-peerconnection-created",
268 ExecuteJavascript("preparePeerConnection()", from_tab
));
269 EXPECT_EQ("ok-added", ExecuteJavascript("addLocalStream()", from_tab
));
270 EXPECT_EQ("ok-negotiating", ExecuteJavascript("negotiateCall()", from_tab
));
272 // Ensure the call gets up on both sides.
273 EXPECT_TRUE(test::PollingWaitUntil("getPeerConnectionReadyState()",
274 "active", from_tab
));
275 EXPECT_TRUE(test::PollingWaitUntil("getPeerConnectionReadyState()",
279 void WebRtcTestBase::HangUp(content::WebContents
* from_tab
) const {
280 EXPECT_EQ("ok-call-hung-up", ExecuteJavascript("hangUp()", from_tab
));
283 void WebRtcTestBase::WaitUntilHangupVerified(
284 content::WebContents
* tab_contents
) const {
285 EXPECT_TRUE(test::PollingWaitUntil("getPeerConnectionReadyState()",
286 "no-peer-connection", tab_contents
));
289 void WebRtcTestBase::DetectErrorsInJavaScript() {
290 detect_errors_in_javascript_
= true;
293 void WebRtcTestBase::StartDetectingVideo(
294 content::WebContents
* tab_contents
,
295 const std::string
& video_element
) const {
296 std::string javascript
= base::StringPrintf(
297 "startDetection('%s', 320, 240)", video_element
.c_str());
298 EXPECT_EQ("ok-started", ExecuteJavascript(javascript
, tab_contents
));
301 void WebRtcTestBase::WaitForVideoToPlay(
302 content::WebContents
* tab_contents
) const {
303 EXPECT_TRUE(test::PollingWaitUntil("isVideoPlaying()", "video-playing",
307 std::string
WebRtcTestBase::GetStreamSize(
308 content::WebContents
* tab_contents
,
309 const std::string
& video_element
) const {
310 std::string javascript
=
311 base::StringPrintf("getStreamSize('%s')", video_element
.c_str());
312 std::string result
= ExecuteJavascript(javascript
, tab_contents
);
313 EXPECT_TRUE(StartsWithASCII(result
, "ok-", true));
314 return result
.substr(3);
317 bool WebRtcTestBase::HasWebcamAvailableOnSystem(
318 content::WebContents
* tab_contents
) const {
320 ExecuteJavascript("HasVideoSourceOnSystem();", tab_contents
);
321 return result
== "has-video-source";