Bug 975928 - Make test_buffered.html load media using xhr, so it's deterministic. r=kinetik
authorChris Pearce <cpearce@mozilla.com>
Wed, 26 Feb 2014 13:36:54 +1300
changeset 170934 59f6a07a619a71cce1f32442277a2c3b24f2fc34
parent 170933 e4c0e3d3a76857f6b92ef8df7d4d17bfdbd9ffe6
child 170935 0ad7988d53c994f7c4a31facfee80d1507959df4
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewerskinetik
bugs975928
milestone30.0a1
Bug 975928 - Make test_buffered.html load media using xhr, so it's deterministic. r=kinetik
content/media/test/test_buffered.html
--- a/content/media/test/test_buffered.html
+++ b/content/media/test/test_buffered.html
@@ -18,17 +18,17 @@ https://bugzilla.mozilla.org/show_bug.cg
 </div>
 <pre id="test">
 <script type="application/javascript">
 
 // Test for Bug 462957; HTMLMediaElement.buffered.
 
 var manager = new MediaTestManager;
 
-function ended(e) {
+function testBuffered(e) {
   var v = e.target;
   
   // The whole media should be buffered...
   var b = v.buffered;
   is(b.length, 1, v._name + ": Should be buffered in one range");
   is(b.start(0), 0, v._name + ": First range start should be media start");
   is(b.end(0), v.duration, v._name + ": First range end should be media end");
 
@@ -60,37 +60,60 @@ function ended(e) {
   caught = false;
   try {
     b.end(b.length);
   } catch (e) {
     caught = e.name == "IndexSizeError" && e.code == DOMException.INDEX_SIZE_ERR;
   }
   is(caught, true, v._name + ": Should throw INDEX_SIZE_ERR on over end bounds range");
 
+	manager.finished(v._token);
   v.src = "";
-  v.parentNode.removeChild(v);
-  manager.finished(v.token);
+  v.parentNode.removeChild(v);  
+}
+
+function fetch(url, fetched_callback) {
+  var xhr = new XMLHttpRequest();
+  xhr.open("GET", url, true);
+  xhr.responseType = "blob";
+
+  var loaded = function (event) {
+    if (xhr.status == 200 || xhr.status == 206) {
+      // Request fulfilled. Note sometimes we get 206... Presumably because either
+      // httpd.js or Necko cached the result.
+      fetched_callback(window.URL.createObjectURL(xhr.response));
+    } else {
+      ok(false, "Fetch failed headers=" + xhr.getAllResponseHeaders());
+    }
+  };
+
+  xhr.addEventListener("load", loaded, false);
+  xhr.send();
 }
 
 function startTest(test, token) {
-  var v = document.createElement('video');
-  v.preload = "metadata";
-  v.token = token;
-  manager.started(token);
+  // Fetch the media resource using XHR so we can be sure the entire
+  // resource is loaded before we test buffered ranges. This ensures
+  // we have deterministic behaviour.
+  var onfetched = function(uri) {
+    var v = document.createElement('video');
+    v.preload = "metadata";
+    v._token = token;
+    v.src = uri;
+    v._name = test.name;
+    v._test = test;
+    v.addEventListener("loadedmetadata", testBuffered, false);
+    document.body.appendChild(v);
+  };
 
-  v.src = test.name;
-  v._name = test.name;
-  v._test = test;
-  v._finished = false;
-  v.autoplay = true;
-  v.addEventListener("ended", ended, false);
-  document.body.appendChild(v);
+  manager.started(token);
+  fetch(test.name, onfetched);
 }
 
+// Note: No need to set media test prefs, since we're using XHR to fetch
+// media data.
 SimpleTest.waitForExplicitFinish();
-SpecialPowers.pushPrefEnv({"set": [["media.cache_size", 40000]]}, beginTest);
-function beginTest() {
-  manager.runTests(gSeekTests, startTest);
-}
+manager.runTests(gSeekTests, startTest);
+
 </script>
 </pre>
 </body>
 </html>