Bug 1331839 - Harden test_mediarecorder_bitrate.html. r=jwwang a=test-only
authorAndreas Pehrson <pehrsons@gmail.com>
Tue, 24 Jan 2017 15:07:43 +0100
changeset 375727 55c6b21a6ff2df8e18dffc8dbd9e1bea4ca097fa
parent 375726 d859e69475796f76214f07c6d133c3a1f325156c
child 375728 1f158e88b8669d86b20fd2e41fbbcbd325678930
push id6996
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 20:48:21 +0000
treeherdermozilla-beta@d89512dab048 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjwwang, test-only
bugs1331839
milestone53.0a2
Bug 1331839 - Harden test_mediarecorder_bitrate.html. r=jwwang a=test-only MozReview-Commit-ID: BFNAhS7B7C6
dom/media/test/test_mediarecorder_bitrate.html
--- a/dom/media/test/test_mediarecorder_bitrate.html
+++ b/dom/media/test/test_mediarecorder_bitrate.html
@@ -24,108 +24,105 @@ function startTest(test, token) {
 
 function runTest(test, token, bitrate) {
   var element = document.createElement('video');
   var expectedMimeType = test.type.substring(0, test.type.indexOf(';'));
 
   element.token = token;
 
   element.src = test.name;
-  element.test = test;
-  element.stream = element.mozCaptureStreamUntilEnded();
-
-  var mediaRecorder = new MediaRecorder(element.stream , {videoBitsPerSecond: bitrate});
-  var onStopFired = false;
-  var onDataAvailableFired = false;
-  var encoded_size = 0;
-
-  mediaRecorder.onerror = function () {
-    ok(false, 'Unexpected onerror callback fired');
-  };
-
-  mediaRecorder.onwarning = function () {
-    ok(false, 'Unexpected onwarning callback fired');
-  };
-
-  // This handler verifies that only a single onstop event handler is fired.
-  mediaRecorder.onstop = function () {
-    if (onStopFired) {
-      ok(false, 'onstop unexpectedly fired more than once');
-    } else {
-      onStopFired = true;
+  element.preload = "metadata";
+  element.onloadedmetadata = function () {
+    info("loadedmetadata");
+    const stream = element.mozCaptureStreamUntilEnded();
+    element.onloadedmetadata = null;
+    element.play();
 
-      // ondataavailable should always fire before onstop
-      if (onDataAvailableFired) {
-        ok(true, 'onstop fired after ondataavailable');
-        info("test " + test.name + " encoded@" + bitrate + "=" + encoded_size);
-        if (results[test.name]) {
-          var big, small, temp;
-          big = {};
-	  big.bitrate = bitrate;
-	  big.size = encoded_size;
-	  small = results[test.name];
-	  // Don't assume the order that these will finish in
-	  if (results[test.name].bitrate > bitrate) {
-	    temp = big;
-	    big = small;
-	    small = temp;
-	  }
-	  // Ensure there is a big enough difference in the encoded
-	  // sizes
-          ok(small.size*1.25 < big.size,
-	     test.name + ' encoded@' + big.bitrate + '=' + big.size +
-	     ' > encoded@' + small.bitrate + '=' + small.size);
-          manager.finished(token);
-        } else {
-	  results[test.name] = {};
-	  results[test.name].bitrate = bitrate;
-          results[test.name].size = encoded_size;
-        }
-      } else {
-        ok(false, 'onstop fired without an ondataavailable event first');
-      }
-    }
-  };
-
-  // This handler verifies that only a single ondataavailable event handler
-  // is fired with the blob generated having greater than zero size
-  // and a correct mime type.
-  mediaRecorder.ondataavailable = function (evt) {
-    if (onDataAvailableFired) {
-      ok(false, 'ondataavailable unexpectedly fired more than once');
-    } else {
-      onDataAvailableFired = true;
-
-      ok(evt instanceof BlobEvent,
-         'Events fired from ondataavailable should be BlobEvent');
-      is(evt.type, 'dataavailable',
-         'Event type should dataavailable');
-      ok(evt.data.size > 0,
-         'Blob data received should be greater than zero');
-      encoded_size = evt.data.size;
-
-      // onstop should not have fired before ondataavailable
-      if (onStopFired) {
-        ok(false, 'ondataavailable unexpectedly fired later than onstop');
-        manager.finished(token);
-      }
-    }
-  };
-
-  element.preload = "metadata";
-
-  element.onloadedmetadata = function () {
-    element.onloadedmetadata = null;
+    const mediaRecorder = new MediaRecorder(stream, {videoBitsPerSecond: bitrate});
     mediaRecorder.start();
-    is(mediaRecorder.state, 'recording',
-     'Media recorder should be recording');
-    is(mediaRecorder.stream, element.stream,
+    is(mediaRecorder.state, 'recording', 'Media recorder should be recording');
+    is(mediaRecorder.stream, stream,
      'Media recorder stream = element stream at the start of recording');
 
-    element.play();
-  }
+    var onStopFired = false;
+    var onDataAvailableFired = false;
+    var encoded_size = 0;
+
+    mediaRecorder.onerror = function () {
+      ok(false, 'Unexpected onerror callback fired');
+    };
+
+    mediaRecorder.onwarning = function () {
+      ok(false, 'Unexpected onwarning callback fired');
+    };
+
+    // This handler verifies that only a single onstop event handler is fired.
+    mediaRecorder.onstop = function () {
+      if (onStopFired) {
+        ok(false, 'onstop unexpectedly fired more than once');
+      } else {
+        onStopFired = true;
+
+        // ondataavailable should always fire before onstop
+        if (onDataAvailableFired) {
+          ok(true, 'onstop fired after ondataavailable');
+          info("test " + test.name + " encoded@" + bitrate + "=" + encoded_size);
+          if (results[test.name]) {
+            var big, small, temp;
+            big = {};
+            big.bitrate = bitrate;
+            big.size = encoded_size;
+            small = results[test.name];
+            // Don't assume the order that these will finish in
+            if (results[test.name].bitrate > bitrate) {
+              temp = big;
+              big = small;
+              small = temp;
+            }
+            // Ensure there is a big enough difference in the encoded
+            // sizes
+            ok(small.size*1.25 < big.size,
+               test.name + ' encoded@' + big.bitrate + '=' + big.size +
+               ' > encoded@' + small.bitrate + '=' + small.size);
+            manager.finished(token);
+          } else {
+            results[test.name] = {};
+            results[test.name].bitrate = bitrate;
+            results[test.name].size = encoded_size;
+          }
+        } else {
+          ok(false, 'onstop fired without an ondataavailable event first');
+        }
+      }
+    };
+
+    // This handler verifies that only a single ondataavailable event handler
+    // is fired with the blob generated having greater than zero size
+    // and a correct mime type.
+    mediaRecorder.ondataavailable = function (evt) {
+      if (onDataAvailableFired) {
+        ok(false, 'ondataavailable unexpectedly fired more than once');
+      } else {
+        onDataAvailableFired = true;
+
+        ok(evt instanceof BlobEvent,
+           'Events fired from ondataavailable should be BlobEvent');
+        is(evt.type, 'dataavailable',
+           'Event type should dataavailable');
+        ok(evt.data.size > 0,
+           'Blob data received should be greater than zero');
+        encoded_size = evt.data.size;
+
+        // onstop should not have fired before ondataavailable
+        if (onStopFired) {
+          ok(false, 'ondataavailable unexpectedly fired later than onstop');
+          manager.finished(token);
+        }
+      }
+    };
+  };
 }
 
 manager.runTests(gMediaRecorderVideoTests, startTest);
 </script>
 </pre>
 </body>
 </html>