Bug 1286810: [MSE] P3. Update mochitests to reflect new duration behavior. r=gerald.
authorJean-Yves Avenard <jyavenard@mozilla.com>
Fri, 15 Jul 2016 23:48:49 +1000
changeset 330699 66c06c6fb41bfe924a3ee8395831f3bac1ab6e0a
parent 330698 54291dfe3bcd5bff620c9c04a4c23581768cf799
child 330700 af10601ac03ffd7da6426faa56f7471f1fe63277
push id9858
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 14:37:10 +0000
treeherdermozilla-aurora@203106ef6cb6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgerald
bugs1286810
milestone50.0a1
Bug 1286810: [MSE] P3. Update mochitests to reflect new duration behavior. r=gerald. update/updateend is no longer fired when changing the duration. Also update the test to use promises, it makes things more readable MozReview-Commit-ID: AeuEolCR5yT
dom/media/mediasource/test/test_DurationUpdated.html
dom/media/mediasource/test/test_DurationUpdated_mp4.html
dom/media/mediasource/test/test_TimestampOffset_mp4.html
dom/media/mediasource/test/test_TruncatedDuration.html
dom/media/mediasource/test/test_TruncatedDuration_mp4.html
--- a/dom/media/mediasource/test/test_DurationUpdated.html
+++ b/dom/media/mediasource/test/test_DurationUpdated.html
@@ -7,52 +7,50 @@
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
 </head>
 <body>
 <pre id="test">
 <script class="testbody" type="text/javascript">
 
 SimpleTest.waitForExplicitFinish();
 
-var updateCount = 0;
 var durationChangeCount = 0;
 
 runWithMSE(function (ms, v) {
   ms.addEventListener("sourceopen", function () {
     var sb = ms.addSourceBuffer("video/webm");
 
+    v.addEventListener("durationchange", function () {
+      durationChangeCount++;
+    });
+
     fetchWithXHR("seek.webm", function (arrayBuffer) {
       sb.appendBuffer(new Uint8Array(arrayBuffer, 0, 318));
-      sb.addEventListener("updateend", function () {
-        updateCount++;
-        if (updateCount == 1) {
-          v.addEventListener("loadedmetadata", function () {
-            v.addEventListener("durationchange", function () {
-              durationChangeCount++;
-            });
-            // Set mediasource duration to 0, so future appendBuffer
-            // will update the mediasource duration
-            // setting ms.duration will fire updatestart/update/updateend
-            // event as per w3c spec followed by a durationchange
-            ms.duration = 0;
-          });
-        } else if (updateCount == 2) {
-          // will fire updatestart/update/updateend
-          // and a durationchange
-          sb.appendBuffer(new Uint8Array(arrayBuffer, 318));
-        } else if (updateCount == 3) {
+      // Adding the first init segment will fire a durationchange.
+      once(v, "loadedmetadata")
+      .then(function() {
+        ok(true, "got loadedmetadata");
+        // Set mediasource duration to 0, so future appendBuffer
+        // will update the mediasource duration.
+        // Changing the duration will fire a durationchange.
+        ms.duration = 0;
+        sb.appendBuffer(new Uint8Array(arrayBuffer, 318));
+        // Adding more data will fire durationchange.
+        once(sb, "updateend")
+        .then(function() {
+          ok(true, "got updateend");
           // this will not fire durationchange as new duration == old duration
           ms.endOfStream();
-        }
+        });
       });
     });
   });
   ms.addEventListener("sourceended", function () {
+    is(durationChangeCount, 3, "durationchange not fired as many times as expected");
     // XXX: Duration should be exactly 4.0, see bug 1065207.
-    is(durationChangeCount, 2, "durationchange not fired as many times as expected");
     ok(Math.abs(v.duration - 4) <= 0.002, "Video has correct duration");
     SimpleTest.finish();
   });
 });
 
 </script>
 </pre>
 </body>
--- a/dom/media/mediasource/test/test_DurationUpdated_mp4.html
+++ b/dom/media/mediasource/test/test_DurationUpdated_mp4.html
@@ -7,54 +7,50 @@
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
 </head>
 <body>
 <pre id="test">
 <script class="testbody" type="text/javascript">
 
 SimpleTest.waitForExplicitFinish();
 
-var updateCount = 0;
 var durationChangeCount = 0;
 
 runWithMSE(function (ms, v) {
   ms.addEventListener("sourceopen", function () {
     var sb = ms.addSourceBuffer("video/mp4");
 
+    v.addEventListener("durationchange", function () {
+      durationChangeCount++;
+    });
+
     fetchWithXHR("bipbop/bipbop2s.mp4", function (arrayBuffer) {
       sb.appendBuffer(new Uint8Array(arrayBuffer, 0, 1395));
-      sb.addEventListener("updateend", function () {
-        updateCount++;
-        if (updateCount == 1) {
-          v.addEventListener("loadedmetadata", function () {
-            v.addEventListener("durationchange", function () {
-              durationChangeCount++;
-            });
-            // Set mediasource duration to 0, so future appendBuffer
-            // will update the mediasource duration
-            // setting ms.duration will fire updatestart/update/updateend
-            // event as per w3c spec followed by a durationchange
-            ms.duration = 0;
-          });
-        } else if (updateCount == 2) {
-          // will fire updatestart/update/updateend
-          // and a durationchange
-          sb.appendBuffer(new Uint8Array(arrayBuffer, 1395));
-        } else if (updateCount == 3) {
+      // Adding the first init segment will fire a durationchange.
+      once(v, "loadedmetadata")
+      .then(function() {
+        ok(true, "got loadedmetadata");
+        // Set mediasource duration to 0, so future appendBuffer
+        // will update the mediasource duration.
+        // Changing the duration will fire a durationchange.
+        ms.duration = 0;
+        sb.appendBuffer(new Uint8Array(arrayBuffer, 1395));
+        // Adding more data will fire durationchange.
+        once(sb, "updateend")
+        .then(function() {
+          ok(true, "got updateend");
           // this will not fire durationchange as new duration == old duration
           ms.endOfStream();
-        }
+        });
       });
     });
   });
   ms.addEventListener("sourceended", function () {
-    is(durationChangeCount, 2, "durationchange not fired as many times as expected");
-    // The bipbop video doesn't start at 0. The old MSE code adjust the
-    // timestamps and ignore the audio track. The new one doesn't.
-    isfuzzy(v.duration, 1.696, 0.166, "Video has correct duration");
+    is(durationChangeCount, 3, "durationchange not fired as many times as expected");
+    is(v.duration, 1.696666, "Video has correct duration");
     SimpleTest.finish();
   });
 });
 
 </script>
 </pre>
 </body>
 </html>
--- a/dom/media/mediasource/test/test_TimestampOffset_mp4.html
+++ b/dom/media/mediasource/test/test_TimestampOffset_mp4.html
@@ -61,17 +61,18 @@ runWithMSE(function(ms, el) {
       audiosb.timestampOffset = 3;
     }).then(fetchAndLoad.bind(null, audiosb, 'bipbop/bipbop_audio', ['init'], '.mp4'))
     .then(fetchAndLoad.bind(null, audiosb, 'bipbop/bipbop_audio', range(1, 12), '.m4s'))
     .then(function() {
       is(audiosb.buffered.length, 1, "No audio discontinuity");
       isfuzzy(audiosb.buffered.start(0), 3, eps, "Audio starts at 3");
 
       // Trim the rest of the audio.
-      ms.duration = videosb.buffered.end(0);
+      audiosb.remove(videosb.buffered.end(0), Infinity);
+      videosb.remove(videosb.buffered.end(0), Infinity);
       return Promise.all([audiosb.updating ? once(audiosb, 'updateend') : Promise.resolve(),
                           videosb.updating ? once(videosb, 'updateend') : Promise.resolve()]);
     }).then(function() {
       ms.endOfStream();
       once(el, 'ended').then(SimpleTest.finish.bind(SimpleTest));
       el.play();
     });
   });
--- a/dom/media/mediasource/test/test_TruncatedDuration.html
+++ b/dom/media/mediasource/test/test_TruncatedDuration.html
@@ -29,25 +29,30 @@ function do_seeking(e) {
   v.removeEventListener("seeking", do_seeking, false);
   SimpleTest.finish();
 }
 
 function do_seeked(e) {
   var v = e.target;
   v.removeEventListener("seeked", do_seeked, false);
   var duration = round(v.duration / 3);
-  v._ms.duration = duration
-  v._sb.abort(); // this shouldn't abort updating the duration (bug 1130826).
-  ok(v.seeking, "seeking is true");
-  // test playback position was updated (bug 1130839).
-  is(v.currentTime, duration, "current time was updated");
-  is(v.duration, duration, "element duration was updated");
-  is(v._sb.buffered.length, 1, "One buffered range");
-  // Truncated mediasource duration will cause the video element to seek.
-  v.addEventListener("seeking", do_seeking, false);
+  is(v._sb.updating, false, "sourcebuffer isn't updating");
+  v._sb.remove(duration, Infinity);
+  once(v._sb, "updateend", function() {
+    v._ms.duration = duration
+    // frames aren't truncated, so duration may be slightly more.
+    isfuzzy(v.duration, duration, 1/30, "element duration was updated");
+    v._sb.abort(); // this shouldn't abort updating the duration (bug 1130826).
+    ok(v.seeking, "seeking is true");
+    // test playback position was updated (bug 1130839).
+    is(v.currentTime, v.duration, "current time was updated");
+    is(v._sb.buffered.length, 1, "One buffered range");
+    // Truncated mediasource duration will cause the video element to seek.
+    v.addEventListener("seeking", do_seeking, false);
+  });
 }
 
 function do_loaded(e) {
   var v = e.target;
   v.removeEventListener("loadeddata", do_loaded, false);
   v.currentTime = v.duration / 2;
   is(v.currentTime, v.duration / 2, "current time was updated");
   ok(v.seeking, "seeking is true");
--- a/dom/media/mediasource/test/test_TruncatedDuration_mp4.html
+++ b/dom/media/mediasource/test/test_TruncatedDuration_mp4.html
@@ -7,18 +7,17 @@
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
 </head>
 <body>
 <pre id="test">
 <script class="testbody" type="text/javascript">
 
 // This test append data to a mediasource and then seek to half the duration
 // of the video.
-// We then shorten the video to 1/3rd of its original size by modifying the
-// mediasource.duration attribute.
+// We then shorten the video to 1/3rd of its original size.
 // We ensure that the buffered range immediately reflect the truncation
 // and that we've seeked to the new end of the media as per W3C spec and
 // video.currentTime got updated.
 
 SimpleTest.waitForExplicitFinish();
 
 function round(n) {
   return Math.round(n * 1000) / 1000;
@@ -29,37 +28,45 @@ function do_seeking(e) {
   v.removeEventListener("seeking", do_seeking, false);
   SimpleTest.finish();
 }
 
 function do_seeked(e) {
   var v = e.target;
   v.removeEventListener("seeked", do_seeked, false);
   var duration = round(v.duration / 3);
-  v._ms.duration = duration
-  v._sb.abort(); // this shouldn't abort updating the duration (bug 1130826).
-  ok(v.seeking, "seeking is true");
-  // test playback position was updated (bug 1130839).
-  is(v.currentTime, duration, "current time was updated");
-  is(v.duration, duration, "element duration was updated");
-  is(v._sb.buffered.length, 1, "One buffered range");
-  // Truncated mediasource duration will cause the video element to seek.
-  v.addEventListener("seeking", do_seeking, false);
+  is(v._sb.updating, false, "sourcebuffer isn't updating");
+  v._sb.remove(duration, Infinity);
+  once(v._sb, "updateend", function() {
+    v._ms.duration = duration
+    // frames aren't truncated, so duration may be slightly more.
+    isfuzzy(v.duration, duration, 1/30, "element duration was updated");
+    v._sb.abort(); // this shouldn't abort updating the duration (bug 1130826).
+    ok(v.seeking, "seeking is true");
+    // test playback position was updated (bug 1130839).
+    is(v.currentTime, v.duration, "current time was updated");
+    is(v._sb.buffered.length, 1, "One buffered range");
+    // Truncated mediasource duration will cause the video element to seek.
+    v.addEventListener("seeking", do_seeking, false);
+  });
 }
 
 function do_loaded(e) {
   var v = e.target;
   v.removeEventListener("loadeddata", do_loaded, false);
   // mp4 metadata states 10s when we only have 1.6s worth of video.
-  v._ms.duration = v._sb.buffered.end(0);
-  is(v.duration, v._ms.duration, "current time updated with mediasource duration");
-  v.currentTime = v.duration / 2;
-  is(v.currentTime, v.duration / 2, "current time was updated");
-  ok(v.seeking, "seeking is true");
-  v.addEventListener("seeked", do_seeked, false);
+  v._sb.remove(v._sb.buffered.end(0), Infinity);
+  once(v._sb, "updateend", function() {
+    v._ms.duration = v._sb.buffered.end(0);
+    is(v.duration, v._ms.duration, "current time updated with mediasource duration");
+    v.currentTime = v.duration / 2;
+    is(v.currentTime, v.duration / 2, "current time was updated");
+    ok(v.seeking, "seeking is true");
+    v.addEventListener("seeked", do_seeked, false);
+  });
 }
 
 runWithMSE(function (ms, v) {
   ms.addEventListener("sourceopen", function () {
     var sb = ms.addSourceBuffer("video/mp4");
     v._sb = sb;
     v._ms = ms;