Bug 1270572 - write tests for the umprompted gUM request; r=florian draft
authorMunro Mengjue Chiang <mchiang@mozilla.com>
Tue, 20 Dec 2016 14:51:36 +0800
changeset 451430 6eb5ec1e80bbedbf8f7617cfaea2f32bb8963fb5
parent 451429 fd743505cf6a989c5ea381f792bd142ba19be223
child 540021 2b97a64495df2c6336762ebff68f31dea6f5d912
push id39172
push usermchiang@mozilla.com
push dateTue, 20 Dec 2016 09:29:33 +0000
reviewersflorian
bugs1270572
milestone53.0a1
Bug 1270572 - write tests for the umprompted gUM request; r=florian MozReview-Commit-ID: LYWTNnfli6K
browser/base/content/test/webrtc/browser_devices_get_user_media.js
browser/base/content/test/webrtc/get_user_media.html
browser/base/content/test/webrtc/head.js
--- a/browser/base/content/test/webrtc/browser_devices_get_user_media.js
+++ b/browser/base/content/test/webrtc/browser_devices_get_user_media.js
@@ -39,16 +39,53 @@ var gTests = [
 
     yield indicator;
     yield checkSharingUI({audio: true, video: true});
     yield closeStream();
   }
 },
 
 {
+  desc: "getUserMedia audio+video while page already has live tracks connected to the same devices",
+  run: function* checkAudioVideoWhileLiveTracksExist() {
+    let promise = promisePopupNotificationShown("webRTC-shareDevices");
+    yield promiseRequestDevice(true, true, 0, 0, false);
+    yield promise;
+    yield expectObserverCalled("getUserMedia:request");
+
+    let indicator = promiseIndicatorWindow();
+    yield promiseMessage("ok", () => {
+      PopupNotifications.panel.firstChild.button.click();
+    });
+    yield expectObserverCalled("getUserMedia:response:allow");
+    yield expectObserverCalled("recording-device-events");
+    Assert.deepEqual((yield getMediaCaptureState()), {audio: true, video: true},
+                     "expected camera and microphone to be shared");
+
+    yield indicator;
+    yield checkSharingUI({audio: true, video: true});
+
+    promise = promiseMessage("ok");
+    yield promiseRequestDevice(true, true, 0, 0, true);
+    yield promise;
+
+    yield expectObserverCalled("getUserMedia:request");
+    yield promiseNoPopupNotification("webRTC-shareDevices");
+    yield expectObserverCalled("getUserMedia:response:allow");
+    yield expectObserverCalled("recording-device-events");
+    Assert.deepEqual((yield getMediaCaptureState()), {audio: true, video: true},
+                     "expected camera and microphone to be shared");
+
+    yield checkSharingUI({audio: true, video: true});
+    yield closeStream(false, 0, true);
+    yield closeStream(false, 0, false);
+  }
+},
+
+{
   desc: "getUserMedia audio only",
   run: function* checkAudioOnly() {
     let promise = promisePopupNotificationShown("webRTC-shareDevices");
     yield promiseRequestDevice(true);
     yield promise;
     yield expectObserverCalled("getUserMedia:request");
 
     is(PopupNotifications.getNotification("webRTC-shareDevices").anchorID,
--- a/browser/base/content/test/webrtc/get_user_media.html
+++ b/browser/base/content/test/webrtc/get_user_media.html
@@ -18,38 +18,55 @@ try {
 }
 
 function message(m) {
   document.getElementById("message").innerHTML = m;
   window.parent.postMessage(m, "*");
 }
 
 var gStream;
+var gSecondStream;
 
-function requestDevice(aAudio, aVideo, aShare) {
+function requestDevice(aAudio, aVideo, aShare, aSecondStream) {
   var opts = {video: aVideo, audio: aAudio};
   if (aShare) {
     opts.video = {
       mozMediaSource: aShare,
       mediaSource: aShare
     }
   } else if (useFakeStreams) {
     opts.fake = true;
   }
 
-  window.navigator.mediaDevices.getUserMedia(opts)
-    .then(stream => {
-      gStream = stream;
-      message("ok");
-    }, err => message("error: " + err));
+  if (!aSecondStream) {
+    window.navigator.mediaDevices.getUserMedia(opts)
+      .then(stream => {
+        gStream = stream;
+        message("ok");
+      }, err => message("error: " + err));
+  } else {
+    window.navigator.mediaDevices.getUserMedia(opts)
+      .then(stream => {
+        gSecondStream = stream;
+        message("ok");
+      }, err => message("error: " + err));
+  }
 }
 message("pending");
 
-function closeStream() {
-  if (!gStream)
-    return;
-  gStream.getTracks().forEach(t => t.stop());
-  gStream = null;
-  message("closed");
+function closeStream(aSecondStream) {
+  if (!aSecondStream) {
+    if (!gStream)
+      return;
+    gStream.getTracks().forEach(t => t.stop());
+    gStream = null;
+    message("closed");
+  } else {
+    if (!gSecondStream)
+      return;
+    gSecondStream.getTracks().forEach(t => t.stop());
+    gSecondStream = null;
+    message("closed");
+  }
 }
 </script>
 </body>
 </html>
--- a/browser/base/content/test/webrtc/head.js
+++ b/browser/base/content/test/webrtc/head.js
@@ -365,49 +365,56 @@ function* stopSharing(aType = "camera", 
     yield expectObserverCalled("recording-window-ended");
 
   yield expectNoObserverCalled(aExpectDoubleRecordingEvent);
 
   if (!aShouldKeepSharing)
     yield* checkNotSharing();
 }
 
-function promiseRequestDevice(aRequestAudio, aRequestVideo, aFrameId, aType) {
+function promiseRequestDevice(aRequestAudio, aRequestVideo, aFrameId, aType, aSecondStream) {
   info("requesting devices");
   return ContentTask.spawn(gBrowser.selectedBrowser,
-                           {aRequestAudio, aRequestVideo, aFrameId, aType},
+                           {aRequestAudio, aRequestVideo, aFrameId, aType, aSecondStream},
                            function*(args) {
     let global = content.wrappedJSObject;
     if (args.aFrameId)
       global = global.document.getElementById(args.aFrameId).contentWindow;
-    global.requestDevice(args.aRequestAudio, args.aRequestVideo, args.aType);
+    global.requestDevice(args.aRequestAudio, args.aRequestVideo, args.aType, args.aSecondStream);
   });
 }
 
-function* closeStream(aAlreadyClosed, aFrameId) {
+function* closeStream(aAlreadyClosed, aFrameId, aSecondStream) {
   yield expectNoObserverCalled();
 
   let promises;
   if (!aAlreadyClosed) {
-    promises = [promiseObserverCalled("recording-device-events"),
-                promiseObserverCalled("recording-window-ended")];
+    if (aSecondStream) {
+      promises = [promiseObserverCalled("recording-device-events")];
+    } else {
+      promises = [promiseObserverCalled("recording-device-events"),
+                  promiseObserverCalled("recording-window-ended")];
+    }
   }
 
   info("closing the stream");
-  yield ContentTask.spawn(gBrowser.selectedBrowser, aFrameId, function*(contentFrameId) {
+  yield ContentTask.spawn(gBrowser.selectedBrowser,
+                          {aFrameId, aSecondStream},
+                          function*(args) {
     let global = content.wrappedJSObject;
-    if (contentFrameId)
-      global = global.document.getElementById(contentFrameId).contentWindow;
-    global.closeStream();
+    if (args.aFrameId)
+      global = global.document.getElementById(args.aFrameId).contentWindow;
+    global.closeStream(args.aSecondStream);
   });
 
   if (promises)
     yield Promise.all(promises);
 
-  yield* assertWebRTCIndicatorStatus(null);
+  if (!aSecondStream)
+    yield* assertWebRTCIndicatorStatus(null);
 }
 
 function* reloadAndAssertClosedStreams() {
   info("reloading the web page");
   let promise = promiseObserverCalled("recording-device-events");
   yield ContentTask.spawn(gBrowser.selectedBrowser, null,
                           "() => content.location.reload()");
   yield promise;