content/media/test/test_preload_actions.html
author Ryan VanderMeulen <ryanvm@gmail.com>
Tue, 21 May 2013 10:48:09 -0400
changeset 144876 0a830609f6197d2b786a97bbd99bd0412ff5c052
parent 144862 4cb1973f06b9abf494c1e2cb77f943a21ad2adce
child 147535 09fcb384c6a0f61db6fc6484bad1806832676edd
permissions -rw-r--r--
Backed out changeset 4cb1973f06b9 (bug 868439) for Android M2 failures. CLOSED TREE

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=548523
-->
<head>
  <title>Test for Bug 548523</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="text/javascript" src="manifest.js"></script>
  <script type="text/javascript" src="use_large_cache.js"></script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=548523">Mozilla Bug 548523</a>
<p id="display"></p>
<div id="content" style="display: none">
  
</div>
<!-- <button onClick="SimpleTest.finish();">Finish</button> -->
<div>Tests complete: <span id="log" style="font-size: small;"></span></div>
<pre id="test">
<script type="application/javascript">

/** Test for Bug 548523 **/

var manager = new MediaTestManager;

manager.onFinished = function() {
  is(gotLoadEvent, true, "Should not have delayed the load event indefinitely");
};

var test = getPlayableVideo(gSeekTests);
var baseName = test.name;
var gTest = test;
var bogusSrc = "bogus.duh";
var bogusType = "video/bogus";
var gotLoadEvent = false;
var finished = false;

addLoadEvent(function() {gotLoadEvent=true;});

function log(m) {
  var l = document.getElementById("log");
  l.innerHTML += m;
}

function maybeFinish(v, n) {
  if (v._finished) {
    return;
  }
  v._finished = true;
  log(n + ",");
  if (v.parentNode) {
    v.parentNode.removeChild(v);
  }
  manager.finished(v.token);
}

function filename(uri) {
  return uri.substr(uri.lastIndexOf("/")+1);
}

// Every test must have a setup(v) function, and must call maybeFinish() when test is complete.
var tests = [
  {
    // 1. Add preload:none video with src to document. Load should halt at NETWORK_IDLE and HAVE_NOTHING,
    // after receiving a suspend event. Should not receive loaded events until after we call load().
    // Note the suspend event is explictly sent by our "stop the load" code, but other tests can't rely
    // on it for the preload:metadata case, as there can be multiple suspend events when loading metadata.
    suspend:
    function(e) {
      var v = e.target;
      is(v._gotLoadStart, true, "(1) Must get loadstart.");
      is(v._gotLoadedMetaData, false, "(1) Must not get loadedmetadata.");
      is(v.readyState, v.HAVE_NOTHING, "(1) ReadyState must be HAVE_NOTHING");
      is(v.networkState, v.NETWORK_IDLE, "(1) NetworkState must be NETWORK_IDLE");
      maybeFinish(v, 1);
    },
    
    setup:
    function(v) {
      v._gotLoadStart = false;
      v._gotLoadedMetaData = false;
      v.preload = "none";
      v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
      v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
      v.addEventListener("suspend", this.suspend, false);
      v.src = test.name;
      document.body.appendChild(v); // Causes implicit load, which will be halted due to preload:none.
    },
  },
  {
    // 2. Add preload:metadata video with src to document. Should halt with NETWORK_IDLE, HAVE_CURRENT_DATA
    // after suspend event and after loadedmetadata.
    loadeddata:
    function(e) {
      var v = e.target;
      is(v._gotLoadStart, true, "(2) Must get loadstart.");
      is(v._gotLoadedMetaData, true, "(2) Must get loadedmetadata.");
      ok(v.readyState >= v.HAVE_CURRENT_DATA, "(2) ReadyState must be >= HAVE_CURRENT_DATA");
      is(v.networkState, v.NETWORK_IDLE, "(2) NetworkState must be NETWORK_IDLE");
      maybeFinish(v, 2);
    },
    
    setup:
    function(v) {
      v._gotLoadStart = false;
      v._gotLoadedMetaData = false;
      v.preload = "metadata";
      v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
      v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
      v.addEventListener("loadeddata", this.loadeddata, false);
      v.src = test.name;
      document.body.appendChild(v); // Causes implicit load, which will be halted after
                                     // metadata due to preload:metadata.
    },
  },
  {
    // 3. Add preload:auto to document. Should receive canplaythrough eventually.
    canplaythrough:
    function(e) {
      var v = e.target;
      is(v._gotLoadStart, true, "(3) Must get loadstart.");
      is(v._gotLoadedMetaData, true, "(3) Must get loadedmetadata.");
      maybeFinish(v, 3);
    },
    
    setup:
    function(v) {
      v._gotLoadStart = false;
      v._gotLoadedMetaData = false;
      v.preload = "auto";
      v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
      v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
      v.addEventListener("canplaythrough", this.canplaythrough, false);
      v.src = test.name; // Causes implicit load.
      document.body.appendChild(v);
    },
  },
  {
    // 4. Add preload:none video to document. Call play(), should load then play through.
    suspend:
    function(e) {
      var v = e.target;
      if (v._gotSuspend) {
        return; // We can receive multiple suspend events, like the one after download completes.
      }
      v._gotSuspend = true;
      is(v._gotLoadStart, true, "(4) Must get loadstart.");
      is(v._gotLoadedMetaData, false, "(4) Must not get loadedmetadata.");
      is(v.readyState, v.HAVE_NOTHING, "(4) ReadyState must be HAVE_NOTHING");
      is(v.networkState, v.NETWORK_IDLE, "(4) NetworkState must be NETWORK_IDLE");
      v.play(); // Should load and play through.
    },
    
    ended:
    function(e) {
      ok(true, "(4) Got playback ended");
      maybeFinish(e.target, 4);
    },
      
    setup:
    function(v) {
      v._gotLoadStart = false;
      v._gotLoadedMetaData = false;
      v._gotSuspend = false;
      v.preload = "none";
      v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
      v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
      v.addEventListener("suspend", this.suspend, false);
      v.addEventListener("ended", this.ended, false);
      v.src = test.name;
      document.body.appendChild(v);
    },
  },
  {
    // 5. preload:none video without resource, add to document, will implicitly start a
    // preload:none load. Add a src, it shouldn't load.
    suspend:
    function(e) {
      var v = e.target;
      is(v._gotLoadStart, true, "(5) Must get loadstart.");
      is(v._gotLoadedMetaData, false, "(5) Must not get loadedmetadata.");
      is(v.readyState, v.HAVE_NOTHING, "(5) ReadyState must be HAVE_NOTHING");
      is(v.networkState, v.NETWORK_IDLE, "(5) NetworkState must be NETWORK_IDLE");
      maybeFinish(v, 5);
    },
      
    setup:
    function(v) {
      v._gotLoadStart = false;
      v._gotLoadedMetaData = false;
      v.preload = "none";
      v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
      v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
      v.addEventListener("suspend", this.suspend, false);
      document.body.appendChild(v); // Causes implicit load, which will be halted due to no resource.
      v.src = test.name; // Load should start, and halt at preload:none.
    },
  },
  {
    // 6. preload:none video without resource, add to document, will implicitly start a
    // preload:none load. Add a source, it shouldn't load.
    suspend:
    function(e) {
      var v = e.target;
      is(v._gotLoadStart, true, "(6) Must get loadstart.");
      is(v._gotLoadedMetaData, false, "(6) Must not get loadedmetadata.");
      is(v.readyState, v.HAVE_NOTHING, "(6) ReadyState must be HAVE_NOTHING");
      is(v.networkState, v.NETWORK_IDLE, "(6) NetworkState must be NETWORK_IDLE");
      maybeFinish(v, 6);
    },
      
    setup:
    function(v) {
      v._gotLoadStart = false;
      v._gotLoadedMetaData = false;
      v.preload = "none";
      v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
      v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
      v.addEventListener("suspend", this.suspend, false);
      document.body.appendChild(v); // Causes implicit load, which will be halted due to no resource.
      var s = document.createElement("source");
      s.src = test.name;
      s.type = test.type;
      v.appendChild(s); // Load should start, and halt at preload:none.
    },
  },
  {
    // 7. create a preload:none document with multiple sources, the first of which is invalid.
    // Add to document, then play. It should load and play through the second source.
    suspend:
    function(e) {
      var v = e.target;
      if (v._gotSuspend) 
        return; // We can receive multiple suspend events, like the one after download completes.
      v._gotSuspend = true;
      is(v._gotLoadStart, true, "(7) Must get loadstart.");
      is(v._gotLoadedMetaData, false, "(7) Must not get loadedmetadata.");
      is(v.readyState, v.HAVE_NOTHING, "(7) ReadyState must be HAVE_NOTHING");
      is(v.networkState, v.NETWORK_IDLE, "(7) NetworkState must be NETWORK_IDLE");
      v.play(); // Should load and play through.
    },

    ended:
    function(e) {
      ok(true, "(7) Got playback ended");
      var v = e.target;
      is(v._gotErrorEvent, true, "(7) Should get error event from first source load failure");      
      maybeFinish(v, 7);
    },
      
    setup:
    function(v) {
      v._gotLoadStart = false;
      v._gotLoadedMetaData = false;
      v.preload = "none";
      v._gotErrorEvent = false;
      v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
      v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
      v.addEventListener("suspend", this.suspend, false);
      v.addEventListener("ended", this.ended, false);
      var s1 = document.createElement("source");
      s1.src = "not-a-real-file.404"
      s1.type = test.type;
      s1.addEventListener("error", function(e){v._gotErrorEvent = true;}, false);
      v.appendChild(s1);
      var s2 = document.createElement("source");
      s2.src = test.name;
      s2.type = test.type;
      v.appendChild(s2);
      document.body.appendChild(v); // Causes implicit load, which will be halt at preload:none on the second resource.
    },
  },
  {
    // 8. Change preload value from none to metadata should cause metadata to be loaded.
    loadeddata:
    function(e) {
      var v = e.target;
      is(v._gotLoadedMetaData, true, "(8) Must get loadedmetadata.");
      ok(v.readyState >= v.HAVE_CURRENT_DATA, "(8) ReadyState must be >= HAVE_CURRENT_DATA on suspend.");
      is(v.networkState, v.NETWORK_IDLE, "(8) NetworkState must be NETWORK_IDLE when load is halted");
      maybeFinish(v, 8);
    },
    
    setup:
    function(v) {
      v._gotLoadedMetaData = false;
      v.preload = "none";
      v.addEventListener("loadstart", function(e){v.preload = "metadata";}, false);
      v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
      v.addEventListener("loadeddata", this.loadeddata, false);
      v.src = test.name; // Causes implicit load.
      document.body.appendChild(v);
    },
  },
  /*{
    // 9. Change preload value from metadata to auto should cause entire media to be loaded.
    // For some reason we don't always receive the canplaythrough event, particuarly on this test.
    // We've disabled this test until bug 568402 is fixed.
    canplaythrough:
    function(e) {
      var v = e.target;
      is(v._gotLoadStart, true, "(9) Must get loadstart.");
      is(v._gotLoadedMetaData, true, "(9) Must get loadedmetadata.");
      maybeFinish(v, 9);
    },
    
    setup:
    function(v) {
      v._gotLoadStart = false;
      v._gotLoadedMetaData = false;
      v.preload = "metadata";
      v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
      v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
      v.addEventListener("loadeddata", function(){v.preload = "auto"}, false);
      v.addEventListener("canplaythrough", this.canplaythrough, false);
      v.src = test.name; // Causes implicit load.
      document.body.appendChild(v);
    },
  },*/
  {
    // 10. Change preload value from none to auto should cause entire media to be loaded.
    canplaythrough:
    function(e) {
      var v = e.target;
      is(v._gotLoadedMetaData, true, "(10) Must get loadedmetadata.");
      maybeFinish(v, 10);
    },
    
    setup:
    function(v) {
      v._gotLoadedMetaData = false;
      v.preload = "none";
      v.addEventListener("loadstart", function(e){v.preload = "auto";}, false);
      v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
      v.addEventListener("canplaythrough", this.canplaythrough, false);
      v.src = test.name; // Causes implicit load.
      document.body.appendChild(v);
    },
  },
  {
    // 11. Change preload value from none to metadata should cause metadata to load.
    loadeddata:
    function(e) {
      var v = e.target;
      is(v._gotLoadedMetaData, true, "(11) Must get loadedmetadata.");
      ok(v.readyState >= v.HAVE_CURRENT_DATA, "(11) ReadyState must be >= HAVE_CURRENT_DATA.");
      is(v.networkState, v.NETWORK_IDLE, "(11) NetworkState must be NETWORK_IDLE.");
      maybeFinish(v, 11);
    },

    setup:
    function(v) {
      v._gotLoadedMetaData = false;
      v.preload = "none";
      v.addEventListener("loadstart", function(e){v.preload = "metadata";}, false);
      v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
      v.addEventListener("loadeddata", this.loadeddata, false);
      v.src = test.name; // Causes implicit load.
      document.body.appendChild(v);
    },
  },
  {
    // 12. Change preload value from auto to metadata after load started,
    // should still do full load, should not halt after metadata only.
    canplaythrough:
    function(e) {
      var v = e.target;
      is(v._gotLoadedMetaData, true, "(12) Must get loadedmetadata.");
      is(v._gotLoadStart, true, "(12) Must get loadstart.");
      maybeFinish(v, 12);
    },

    setup:
    function(v) {
      v._gotLoadStart = false;
      v._gotLoadedMetaData = false;
      v.preload = "auto";
      v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
      v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
      v.addEventListener("canplaythrough", this.canplaythrough, false);
      v.src = test.name; // Causes implicit load.
      document.body.appendChild(v);
      v.preload = "metadata";
    },
  },
  {
    // 13. Change preload value from auto to none after specifying a src
    // should load according to preload none, no buffering should have taken place
    suspend:
    function(e) {
      var v = e.target;
      is(v._gotLoadStart, true, "(13) Must get loadstart.");
      is(v._gotLoadedMetaData, false, "(13) Must not get loadedmetadata.");
      is(v.readyState, v.HAVE_NOTHING, "(13) ReadyState must be HAVE_NOTHING");
      is(v.networkState, v.NETWORK_IDLE, "(13) NetworkState must be NETWORK_IDLE");
      maybeFinish(v, 13);
    },

    setup:
    function(v) {
      v._gotLoadStart = false;
      v._gotLoadedMetaData = false;
      v.preload = "auto";
      v.src = test.name;
      v.preload = "none";
      v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
      v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
      v.addEventListener("suspend", this.suspend, false);
      document.body.appendChild(v); // Causes implicit load, should load according to preload none
      var s = document.createElement("source");
    }
  },
  {
    // 14. Add preload:metadata video with src to document. Play(), should play through.
    loadeddata:
    function(e) {
      var v = e.target;
      is(v._gotLoadStart, true, "(14) Must get loadstart.");
      is(v._gotLoadedMetaData, true, "(14) Must get loadedmetadata.");
      ok(v.readyState >= v.HAVE_CURRENT_DATA, "(14) ReadyState must be >= HAVE_CURRENT_DATA");
      is(v.networkState, v.NETWORK_IDLE, "(14) NetworkState must be NETWORK_IDLE");
      v.play();
    },
    
    ended:
    function(e) {
      ok(true, "(14) Got playback ended");
      var v = e.target;
      maybeFinish(v, 14);
    },

    setup:
    function(v) {
      v._gotLoadStart = false;
      v._gotLoadedMetaData = false;
      v.preload = "metadata";
      v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
      v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
      v.addEventListener("ended", this.ended, false);
      v.addEventListener("loadeddata", this.loadeddata, false);
      v.src = test.name;
      document.body.appendChild(v); // Causes implicit load, which will be halted after
                                     // metadata due to preload:metadata.
    },
  },
  {
    // 15. Autoplay should override preload:none.
    ended:
    function(e) {
      ok(true, "(15) Got playback ended.");
      var v = e.target;
      maybeFinish(v, 15);
    },
    
    setup:
    function(v) {
      v._gotLoadStart = false;
      v._gotLoadedMetaData = false;
      v.preload = "none";
      v.autoplay = true;
      v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
      v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
      v.addEventListener("ended", this.ended, false);
      v.src = test.name; // Causes implicit load.
      document.body.appendChild(v);
    },
  },
  {
    // 16. Autoplay should override preload:metadata.
    ended:
    function(e) {
      ok(true, "(16) Got playback ended.");
      var v = e.target;
      maybeFinish(v, 16);
    },
    
    setup:
    function(v) {
      v.preload = "metadata";
      v.autoplay = true;
      v.addEventListener("ended", this.ended, false);
      v.src = test.name; // Causes implicit load.
      document.body.appendChild(v);
    },
  },
  {
    // 17. On a preload:none video, adding autoplay should disable preload none, i.e. don't break autoplay!
    ended:
    function(e) {
      ok(true, "(17) Got playback ended.");
      var v = e.target;
      maybeFinish(v, 17);
    },
    
    setup:
    function(v) {
      v.addEventListener("ended", this.ended, false);
      v.preload = "none";
      document.body.appendChild(v); // Causes implicit load, which will be halted due to preload:none.
      v.autoplay = true;
      v.src = test.name;
    },    
  },
  {
    // 18. On a preload='none' video, call play() before load algorithms's sync
    // has run, the play() call should override preload='none'.
    ended:
    function(e) {
      ok(true, "(18) Got playback ended.");
      var v = e.target;
      maybeFinish(v, 18);
    },
    
    setup:
    function(v) {
      v.addEventListener("ended", this.ended, false);
      v.preload = "none";
      v.src = test.name; // Schedules async section to continue load algorithm.
      document.body.appendChild(v);
      v.play(); // Should cause preload:none to be overridden.
    },  
  },
  {
    // 19. Set preload='auto' on first video source then switching preload='none' and swapping the video source to another.
    // The second video should not start playing as it's preload state has been changed to 'none' from 'auto'
    loadedmetadata: function(e) {
      var v = e.target;
      is(v.preload === "auto", true, "(19) preload is initially auto");
      setTimeout(function() {
        // set preload state to none and switch video sources
        v.preload="none";
        v.src = test.name + "?asdf";
        setTimeout(function() {
          is(v.readyState === 0, true, "(19) no buffering has taken place");
          maybeFinish(v, 19);
        }, 2000);
      }, 2000);

    },

    setup:
    function(v) {
      var that = this;
      v.preload = "auto";
      v.src = test.name;
      // add a listener for when the video has loaded, so we know preload auto has worked
      v.addEventListener( "loadedmetadata", this.loadedmetadata, false);
      document.body.appendChild(v);
    }
  }
];

var iterationCount = 0;
function startTest(test, token) {
  if (test == tests[0]) {
    ++iterationCount;
  }
  if (iterationCount == 2) {
    // Do this series of tests on logically different resources
    test.name = baseName + "?" + Math.floor(Math.random()*100000);
  }
  var v = document.createElement("video");
  v.token = token;
  test.setup(v);
  manager.started(token);
}

var twiceTests = tests.concat(tests);
manager.runTests(twiceTests, startTest);

</script>
</pre>
</body>
</html>