[WebRTC Browser Tests] Slight refactoring of the webrtc audio tests.

Lately, fixing some flakyness in the WebRTC Browser tests (crrev.com/c/1445936,
crrev.com/c/1447693) I noticed some inconsistency in the way the tests
are defined. This CL consolidates where the constraints that are used
throughout the tests are defined and should reduce the maintenance burden.

BUG=926830

Change-Id: I63f76e6e720f430f7b25a8326fe4dbb225a3a85a
Reviewed-on: https://chromium-review.googlesource.com/c/1451830
Reviewed-by: Guido Urdaneta <guidou@chromium.org>
Commit-Queue: Armando Miraglia <armax@chromium.org>
Cr-Commit-Position: refs/heads/master@{#629132}
diff --git a/content/browser/webrtc/webrtc_audio_browsertest.cc b/content/browser/webrtc/webrtc_audio_browsertest.cc
index 7411697..1b15aa6b 100644
--- a/content/browser/webrtc/webrtc_audio_browsertest.cc
+++ b/content/browser/webrtc/webrtc_audio_browsertest.cc
@@ -20,6 +20,9 @@
 #include "net/test/embedded_test_server/embedded_test_server.h"
 #include "testing/gtest/include/gtest/gtest-param-test.h"
 
+const char kAudioConstraints[] = "audio: {echoCancellation: {exact: false}}";
+const char kVideoConstraints[] = "video:true";
+
 namespace content {
 
 // This class tests the scenario when permission to access mic or camera is
@@ -69,66 +72,96 @@
     MakeTypicalCall(javascript, "/media/peerconnection-call-audio.html");
   }
 
+  std::string BuildConstraints(const char* audio, const char* video) {
+    DCHECK(audio);
+    DCHECK(video);
+
+    std::string audio_str(audio);
+    std::string video_str(video);
+    if (!audio_str.empty() && !video_str.empty())
+      return "{" + audio_str + "," + video_str + "}";
+    if (!audio_str.empty())
+      return "{" + audio_str + "}";
+
+    return "{" + video_str + "}";
+  }
+
  private:
   base::test::ScopedFeatureList audio_service_features_;
 };
 
 IN_PROC_BROWSER_TEST_P(WebRtcAudioBrowserTest,
                        CanMakeVideoCallAndThenRenegotiateToAudio) {
-  MakeAudioDetectingPeerConnectionCall(
-      "callAndRenegotiateToAudio({audio: {echoCancellation: {exact: false}}, "
-      "video:true}, {audio: {echoCancellation: {exact: false}}});");
+  std::string constraints =
+      BuildConstraints(kAudioConstraints, kVideoConstraints);
+  std::string audio_only_constraints = BuildConstraints(kAudioConstraints, "");
+  MakeAudioDetectingPeerConnectionCall("callAndRenegotiateToAudio(" +
+                                       constraints + ", " +
+                                       audio_only_constraints + ");");
 }
 
 IN_PROC_BROWSER_TEST_P(WebRtcAudioBrowserTest,
                        EstablishAudioVideoCallAndEnsureAudioIsPlaying) {
-  MakeAudioDetectingPeerConnectionCall(
-      "callAndEnsureAudioIsPlaying({audio: {echoCancellation: {exact: false}}, "
-      "video:true});");
+  std::string constraints =
+      BuildConstraints(kAudioConstraints, kVideoConstraints);
+  MakeAudioDetectingPeerConnectionCall("callAndEnsureAudioIsPlaying(" +
+                                       constraints + ");");
 }
 
 IN_PROC_BROWSER_TEST_P(WebRtcAudioBrowserTest,
                        EstablishAudioOnlyCallAndEnsureAudioIsPlaying) {
-  MakeAudioDetectingPeerConnectionCall(
-      "callAndEnsureAudioIsPlaying({audio:{echoCancellation: {exact: "
-      "false}}});");
+  std::string constraints =
+      BuildConstraints(kAudioConstraints, kVideoConstraints);
+  MakeAudioDetectingPeerConnectionCall("callAndEnsureAudioIsPlaying(" +
+                                       constraints + ");");
 }
 
 IN_PROC_BROWSER_TEST_P(WebRtcAudioBrowserTest,
                        EstablishIsac16KCallAndEnsureAudioIsPlaying) {
+  std::string constraints =
+      BuildConstraints(kAudioConstraints, kVideoConstraints);
   MakeAudioDetectingPeerConnectionCall(
-      "callWithIsac16KAndEnsureAudioIsPlaying({audio:{echoCancellation: "
-      "{exact: false}}});");
+      "callWithIsac16KAndEnsureAudioIsPlaying(" + constraints + ");");
 }
 
 IN_PROC_BROWSER_TEST_P(WebRtcAudioBrowserTest,
                        EstablishAudioVideoCallAndVerifyRemoteMutingWorks) {
+  std::string constraints =
+      BuildConstraints(kAudioConstraints, kVideoConstraints);
   MakeAudioDetectingPeerConnectionCall(
-      "callAndEnsureRemoteAudioTrackMutingWorks();");
+      "callAndEnsureRemoteAudioTrackMutingWorks(" + constraints + ");");
 }
 
 IN_PROC_BROWSER_TEST_P(WebRtcAudioBrowserTest,
                        EstablishAudioVideoCallAndVerifyLocalMutingWorks) {
+  std::string constraints =
+      BuildConstraints(kAudioConstraints, kVideoConstraints);
   MakeAudioDetectingPeerConnectionCall(
-      "callAndEnsureLocalAudioTrackMutingWorks();");
+      "callAndEnsureLocalAudioTrackMutingWorks(" + constraints + ");");
 }
 
 IN_PROC_BROWSER_TEST_P(WebRtcAudioBrowserTest,
                        EnsureLocalVideoMuteDoesntMuteAudio) {
+  std::string constraints =
+      BuildConstraints(kAudioConstraints, kVideoConstraints);
   MakeAudioDetectingPeerConnectionCall(
-      "callAndEnsureLocalVideoMutingDoesntMuteAudio();");
+      "callAndEnsureLocalVideoMutingDoesntMuteAudio(" + constraints + ");");
 }
 
 IN_PROC_BROWSER_TEST_P(WebRtcAudioBrowserTest,
                        EnsureRemoteVideoMuteDoesntMuteAudio) {
+  std::string constraints =
+      BuildConstraints(kAudioConstraints, kVideoConstraints);
   MakeAudioDetectingPeerConnectionCall(
-      "callAndEnsureRemoteVideoMutingDoesntMuteAudio();");
+      "callAndEnsureRemoteVideoMutingDoesntMuteAudio(" + constraints + ");");
 }
 
 IN_PROC_BROWSER_TEST_P(WebRtcAudioBrowserTest,
                        EstablishAudioVideoCallAndVerifyUnmutingWorks) {
-  MakeAudioDetectingPeerConnectionCall(
-      "callAndEnsureAudioTrackUnmutingWorks();");
+  std::string constraints =
+      BuildConstraints(kAudioConstraints, kVideoConstraints);
+  MakeAudioDetectingPeerConnectionCall("callAndEnsureAudioTrackUnmutingWorks(" +
+                                       constraints + ");");
 }
 
 // We run these tests with the audio service both in and out of the the browser
diff --git a/content/test/data/media/peerconnection-call-audio.html b/content/test/data/media/peerconnection-call-audio.html
index b93e4c2..ee824a27 100644
--- a/content/test/data/media/peerconnection-call-audio.html
+++ b/content/test/data/media/peerconnection-call-audio.html
@@ -93,9 +93,8 @@
     localStream.getAudioTracks()[0].enabled = enabled;
   }
 
-  function callAndEnsureRemoteAudioTrackMutingWorks() {
-    setupCallAndPromiseAudioPlaying(
-        {audio: {echoCancellation: {exact: false}}, video: true}).then(() => {
+  function callAndEnsureRemoteAudioTrackMutingWorks(constraints) {
+    setupCallAndPromiseAudioPlaying(constraints).then(() => {
       // Call is up, now mute the remote track and check we stop playing out
       // audio (after a small delay, we don't expect it to happen instantly).
       enableRemoteAudio(gSecondConnection, false);
@@ -105,9 +104,8 @@
     .catch(failTest);
   }
 
-  function callAndEnsureLocalAudioTrackMutingWorks() {
-    setupCallAndPromiseAudioPlaying(
-        {audio: {echoCancellation: {exact: false}}, video: true}).then(() => {
+  function callAndEnsureLocalAudioTrackMutingWorks(constraints) {
+    setupCallAndPromiseAudioPlaying(constraints).then(() => {
       // Call is up, now mute the local track of the sending side and ensure
       // the receiving side stops receiving audio.
       enableLocalAudio(gFirstConnection, false);
@@ -117,9 +115,8 @@
     .catch(failTest);
   }
 
-  function callAndEnsureAudioTrackUnmutingWorks() {
-    setupCallAndPromiseAudioPlaying(
-        {audio: {echoCancellation: {exact: false}}, video: true}).then(() => {
+  function callAndEnsureAudioTrackUnmutingWorks(constraints) {
+    setupCallAndPromiseAudioPlaying(constraints).then(() => {
       // Mute, wait a while, unmute, verify audio gets back up.
       // (Also, ensure video muting doesn't affect audio).
       enableRemoteAudio(gSecondConnection, false);
@@ -137,18 +134,16 @@
     }).catch(failTest);
   }
 
-  function callAndEnsureLocalVideoMutingDoesntMuteAudio() {
-    setupCallAndPromiseAudioPlaying(
-        {audio: {echoCancellation: {exact: false}}, video: true}).then(() => {
+  function callAndEnsureLocalVideoMutingDoesntMuteAudio(constraints) {
+    setupCallAndPromiseAudioPlaying(constraints).then(() => {
       enableLocalVideo(gFirstConnection, false);
       return ensureAudioPlaying(gSecondConnection)
           .then(reportTestSuccess);
     });
   }
 
-  function callAndEnsureRemoteVideoMutingDoesntMuteAudio() {
-    setupCallAndPromiseAudioPlaying(
-        {audio: {echoCancellation: {exact: false}}, video: true}).then(() => {
+  function callAndEnsureRemoteVideoMutingDoesntMuteAudio(constraints) {
+    setupCallAndPromiseAudioPlaying(constraints).then(() => {
       enableRemoteVideo(gSecondConnection, false);
       return ensureAudioPlaying(gSecondConnection)
           .then(reportTestSuccess);