Bug 1493885 - Rewrite MediaStream-MediaElement-srcObject.https.html to use async/await. r=pehrsons
authorJan-Ivar Bruaroey <jib@mozilla.com>
Wed, 03 Oct 2018 21:44:42 +0000
changeset 495543 690cfe65a8c961ec5b2e56f8249498693e26edd0
parent 495542 690011b571305eb937172d9d68e5dd4260f6e33e
child 495544 ed24c0367ff3235166bd70e79dc6c39e3683d561
push id9984
push userffxbld-merge
push dateMon, 15 Oct 2018 21:07:35 +0000
treeherdermozilla-beta@183d27ea8570 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspehrsons
bugs1493885
milestone64.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1493885 - Rewrite MediaStream-MediaElement-srcObject.https.html to use async/await. r=pehrsons Differential Revision: https://phabricator.services.mozilla.com/D7568
testing/web-platform/tests/mediacapture-streams/MediaStream-MediaElement-srcObject.https.html
--- a/testing/web-platform/tests/mediacapture-streams/MediaStream-MediaElement-srcObject.https.html
+++ b/testing/web-platform/tests/mediacapture-streams/MediaStream-MediaElement-srcObject.https.html
@@ -13,49 +13,50 @@ the success callback in getUserMedia can
 via the <code>srcObject</code> attribute.</p>
 
 <video id="vid"></video>
 
 <div id='log'></div>
 <script src=/resources/testharness.js></script>
 <script src=/resources/testharnessreport.js></script>
 <script>
-var vid = document.getElementById("vid");
-var t = async_test("Tests that a MediaStream can be assigned to a video element with srcObject", {timeout: 10000});
-t.step(function() {
-  navigator.mediaDevices.getUserMedia({video: true})
-    .then(t.step_func(function (stream) {
-      const testOnceTimeUpdate = function() {
-         assert_equals(vid.played.length, 1, "A MediaStream's timeline always consists of a single range");
-         assert_equals(vid.played.start(0), 0, "A MediaStream's timeline always starts at zero");
-         assert_equals(vid.played.end(0), vid.currentTime, "A MediaStream's end MUST return the last known currentTime, says mediacapture-main");
-         assert_equals(vid.duration, Infinity, " A MediaStream does not have a pre-defined duration. ");
+'use strict';
+const vid = document.getElementById("vid");
+
+promise_test(async t => {
+  const wait = ms => new Promise(r => t.step_timeout(r, ms));
+  const timeout = (promise, time, msg) =>
+    Promise.race([promise, wait(time).then(() => Promise.reject(new Error(msg)))]);
 
-         var time = vid.currentTime;
-         vid.currentTime = 0;
-         assert_equals(vid.currentTime, time, "The UA MUST ignore attempts to set the currentTime attribute");
+  const stream = await timeout(navigator.mediaDevices.getUserMedia({video: true}), 10000, "getUserMedia timeout");
+  t.add_cleanup(() => stream.getTracks().forEach(track => track.stop()));
+  vid.srcObject = stream;
+  vid.play();
+  assert_true(!vid.seeking, "A MediaStream is not seekable");
+  assert_equals(vid.seekable.length, 0, "A MediaStream is not seekable");
+  assert_equals(vid.defaultPlaybackRate, 1, "playback rate is always 1");
+  assert_equals(vid.playbackRate, 1, "playback rate is always 1");
+  assert_equals(vid.buffered.length, 0, "A MediaStream cannot be preloaded.  Therefore, there is no buffered timeranges");
+  assert_equals(vid.readyState, vid.HAVE_NOTHING, "readyState is HAVE_NOTHING initially");
+  assert_equals(vid.duration, NaN, "A MediaStream does not have any duration initially.");
 
-         // TODO add test that duration must be set to currentTime
-         // when mediastream is destroyed
-      }
-      vid.addEventListener("timeupdate", t.step_func(testOnceTimeUpdate), {once: true});
+  const haveLoadedData = new Promise(r => vid.addEventListener("loadeddata", r, {once: true}));
 
-      const testOnceLoadedData = function() {
-         assert_equals(vid.readyState, vid.HAVE_ENOUGH_DATA, "Upon selecting a media stream, the UA sets readyState to HAVE_ENOUGH_DATA");
-         assert_equals(vid.duration, Infinity, "A MediaStream does not have a pre-defined duration.");
+  await new Promise(r => vid.addEventListener("timeupdate", r, {once: true}));
+  assert_equals(vid.played.length, 1, "A MediaStream's timeline always consists of a single range");
+  assert_equals(vid.played.start(0), 0, "A MediaStream's timeline always starts at zero");
+  assert_equals(vid.played.end(0), vid.currentTime, "A MediaStream's end MUST return the last known currentTime, says mediacapture-main");
+  assert_equals(vid.duration, Infinity, "A MediaStream does not have a pre-defined duration. ");
 
-         vid.addEventListener("timeupdate", () => t.step_timeout(t.done()), {once: true});
-      }
-      vid.addEventListener("loadeddata", t.step_func(testOnceLoadedData), {once: true});
-      vid.srcObject = stream;
-      vid.play();
-      assert_true(!vid.seeking, "A MediaStream is not seekable");
-      assert_equals(vid.seekable.length, 0, "A MediaStream is not seekable");
-      assert_equals(vid.defaultPlaybackRate, 1, "playback rate is always 1");
-      assert_equals(vid.playbackRate, 1, "playback rate is always 1");
-      assert_equals(vid.buffered.length, 0, "A MediaStream cannot be preloaded.  Therefore, there is no buffered timeranges");
-      assert_equals(vid.readyState, vid.HAVE_NOTHING, "readyState is HAVE_NOTHING initially");
-      assert_equals(vid.duration, NaN, "A MediaStream does not have any duration initially.");
-    }), function(error) {});
-});
+  const time = vid.currentTime;
+  vid.currentTime = 0;
+  assert_equals(vid.currentTime, time, "The UA MUST ignore attempts to set the currentTime attribute");
+
+  await haveLoadedData;
+  assert_equals(vid.readyState, vid.HAVE_ENOUGH_DATA, "Upon having loaded a media stream, the UA sets readyState to HAVE_ENOUGH_DATA");
+  assert_equals(vid.duration, Infinity, "A MediaStream does not have a pre-defined duration.");
+
+  // TODO add test that duration must be set to currentTime
+  // when mediastream is destroyed
+}, "Tests that a MediaStream can be assigned to a video element with srcObject");
 </script>
 </body>
 </html>