Bug 1523563 - Modernize test_mr_record_audionode.html. r=jib
authorAndreas Pehrson <apehrson@mozilla.com>
Wed, 19 Jun 2019 16:09:17 +0000
changeset 481086 e431e395e20e3028689d4bf4e8529414e16b9fdd
parent 481085 a618b9ef5080f01fd243a77c10dc534c6c894ddc
child 481087 944da9f033e8c2da0610742f7dcbf8a05c4a775b
push id89078
push userpehrsons@gmail.com
push dateWed, 03 Jul 2019 09:31:19 +0000
treeherderautoland@d9f2bc3dd281 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjib
bugs1523563
milestone69.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 1523563 - Modernize test_mr_record_audionode.html. r=jib Differential Revision: https://phabricator.services.mozilla.com/D35169
dom/media/test/test_mediarecorder_record_audionode.html
--- a/dom/media/test/test_mediarecorder_record_audionode.html
+++ b/dom/media/test/test_mediarecorder_record_audionode.html
@@ -10,117 +10,109 @@
 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=968109">Mozilla Bug 968109</a>
 
 <script class="testbody" type="text/javascript">
 
 SimpleTest.waitForExplicitFinish();
 
 function setUpSource(contextType, nodeType) {
   // Use contextType to choose offline or real-time context.
-  var context = contextType === 'offline'?
+  const context = contextType == "offline"?
     new OfflineAudioContext(2 , 80920, 44100) : new AudioContext();
-  var buffer = context.createBuffer(2, 80920, context.sampleRate);
-  for (var i = 0; i < 80920; ++i) {
+  const buffer = context.createBuffer(2, 80920, context.sampleRate);
+  for (let i = 0; i < 80920; ++i) {
     buffer.getChannelData(0)[i] = Math.sin(1000 * 2 * Math.PI * i / context.sampleRate);
     buffer.getChannelData(1)[i] = Math.sin(1000 * 2 * Math.PI * i / context.sampleRate);
   }
 
-  var source = context.createBufferSource();
+  const source = context.createBufferSource();
   source.buffer = buffer;
   source.loop = true;
 
   source.start(0);
 
   // nodeType decides which node in graph should be the source of recording.
-  var node;
-  if (nodeType === 'source') {
+  let node;
+  if (nodeType == "source") {
     node = source;
-  } else if (nodeType === 'splitter') {
-    var splitter = context.createChannelSplitter();
+  } else if (nodeType == "splitter") {
+    const splitter = context.createChannelSplitter();
     source.connect(splitter);
     node = splitter;
-  } else if (nodeType == 'destination') {
+  } else if (nodeType == "destination") {
     source.connect(context.destination);
     node = context.destination;
   }
   // Explicitly start offline context.
-  if (contextType === 'offline') {
+  if (contextType == "offline") {
     context.startRendering();
   }
 
   return node;
 }
 
-function testRecord(source, done) {
-  ok(source, 'source node should be ok');
-  var recorder;
-  var didThrow = false;
+async function testRecord(source) {
+  const isOffline = source.context instanceof OfflineAudioContext;
+  const recorder = new MediaRecorder(source);
+
+  recorder.onwarning = () => ok(false, "should not fire onwarning");
+  recorder.onerror = () => ok(false, "should not fire onerror");
+  if (!isOffline) {
+    recorder.onstop = () => ok(false, "should not fire stop yet");
+  }
+
+  recorder.start(1000);
+  is("recording", recorder.state, "state should become recording after calling start()");
+
+  const chunks = [];
+  let {data} = await new Promise(r => recorder.ondataavailable = r);
+  is(data.type, "audio/ogg", "Blob has expected mimetype");
+  isnot(data.size, 0, "should get data and its length should be > 0");
+  chunks.push(data);
+
+  if (isOffline) {
+    await new Promise(r => recorder.onstop = r);
+    is(recorder.state, "inactive", "Offline context should end by itself");
+  } else {
+    is(recorder.state, "recording", "Expected to still be recording");
+    recorder.stop();
+    ({data} = await new Promise(r => recorder.ondataavailable = r));
+    is(recorder.state, "inactive", "Expected to be inactive after last blob");
+    isnot(data.size, 0, "should get data and its length should be > 0");
+    chunks.push(data);
+
+    await new Promise(r => recorder.onstop = r);
+    is(recorder.state, "inactive", "state should remain inactive after stop event");
+  }
+  return new Blob(chunks, {type: chunks[0].type});
+}
+
+addLoadEvent(async function() {
+  const src = setUpSource();
+  let didThrow = false;
   try {
-    recorder = new MediaRecorder(source);
+    new MediaRecorder(src);
   } catch (e) {
     didThrow = true;
   }
-  ok(!didThrow, 'MediaRecorder(AudioNode) should be visible after pref turned on');
+  ok(didThrow, "MediaRecorder(AudioNode) should be hidden behind a pref");
 
-  recorder.onwarning = function() {
-    ok(false, 'should not fire onwarning');
-  };
-
-  recorder.onerror = function() {
-    ok(false, 'should not fire onerror');
-  };
+  await SpecialPowers.pushPrefEnv({"set": [
+      ["media.recorder.audio_node.enabled", true],
+    ]});
 
-  recorder.onstop = function() {
-    is(recorder.state, 'inactive', 'state should become "inactive" after calling stop()');
-    done();
-  };
-
-  recorder.ondataavailable = function (e) {
-    if (recorder.state == 'recording') {
-      is('audio/ogg', recorder.mimeType, 'mimetype should be audio/ogg, not ' + recorder.mimeType);
-      ok(e.data && e.data.size > 0, 'should get data and its length should be > 0');
-      recorder.stop();
-    }
+  // Test with various context and source node types.
+  for (const {context, node} of [
+    {context: "", node: "source"},
+    {context: "", node: "splitter"},
+    {context: "offline", node: "destination"},
+  ]) {
+    info(`Testing recording ${context == "" ? "regular" : context} context and ${node} node`);
+    await testRecord(setUpSource(context, node));
   };
 
-  try {
-    recorder.start(1000);
-    is('recording', recorder.state, 'state should become "recording" after calling start()');
-  } catch (e) {
-    didThrow = true;
-  }
-  ok(!didThrow, 'start() should succeed');
-}
-
-addLoadEvent(function() {
-  var src = setUpSource();
-  var recorder;
-  var didThrow = false;
-  try {
-    recorder = new MediaRecorder(src);
-  } catch (e) {
-    didThrow = true;
-  }
-  ok(didThrow, 'MediaRecorder(AudioNode) should be hidden behind a pref');
-
-  SpecialPowers.pushPrefEnv({"set": [[ 'media.recorder.audio_node.enabled', true ]]},
-    function () {
-      // Test with various context and source node types.
-      var done1 = new Promise(function (resolve, reject) {
-        testRecord(setUpSource('', 'source'), resolve);
-      });
-      var done2 = new Promise(function (resolve, reject) {
-        testRecord(setUpSource('', 'splitter'), resolve);
-      });
-      var done3 = new Promise(function (resolve, reject) {
-        testRecord(setUpSource('offline', 'destination'), resolve);
-      });
-      // Finish test after all is done.
-      Promise.all([done1, done2, done3]).then(
-        function () { SimpleTest.finish(); }
-      );
-    });
+  SimpleTest.finish();
 });
 
 </script>
 </pre>
 </body>
 </html>