content/media/test/test_played.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/media/test/test_played.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,227 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<head>
     1.7 +<title>Test played member for media elements</title>
     1.8 +<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
     1.9 +<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.10 +<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.11 +<script type="text/javascript" src="manifest.js"></script>
    1.12 +</head>
    1.13 +<body>
    1.14 +<pre id='test'>
    1.15 +<script class="testbody" type='application/javascript;version=1.8'>
    1.16 +
    1.17 +let manager = new MediaTestManager;
    1.18 +
    1.19 +SimpleTest.expectAssertions(0, 2);
    1.20 +
    1.21 +function finish_test(element) {
    1.22 +  if (element.parentNode)
    1.23 +    element.parentNode.removeChild(element);
    1.24 +  element.src="";
    1.25 +  manager.finished(element.token);
    1.26 +}
    1.27 +
    1.28 +// Check that a file has been played in its entirety.
    1.29 +function check_full_file_played(element) {
    1.30 +  element.addEventListener('ended', (function(e) {
    1.31 +    let interval_count = e.target.played.length;
    1.32 +    is(interval_count, 1, "normal play : a.played.length must be 1");
    1.33 +    is(element.played.start(0), 0, "start time shall be 0");
    1.34 +    is(element.played.end(0), e.target.duration, "end time shall be duration");
    1.35 +    finish_test(e.target);
    1.36 +  }), false);
    1.37 +}
    1.38 +
    1.39 +var tests = [
    1.40 +// Without playing, check that player.played.length == 0.
    1.41 +{
    1.42 +  setup : function(element) {
    1.43 +    element.addEventListener("loadedmetadata", function() {
    1.44 +      is(element.played.length, 0, "audio : initial played.length equals zero");
    1.45 +      finish_test(element);
    1.46 +    });
    1.47 +  }
    1.48 +},
    1.49 +// Play the file, test the range we have.
    1.50 +{
    1.51 +  setup : function(element) {
    1.52 +    check_full_file_played(element);
    1.53 +    element.play();
    1.54 +  }
    1.55 +},
    1.56 +
    1.57 +// Play the second half of the file, pause, play
    1.58 +// an check we have only one range.
    1.59 +{
    1.60 +  setup : function (element) {
    1.61 +    element.addEventListener("ended", function (e) {
    1.62 +      var t = e.target;
    1.63 +      check_full_file_played(t);
    1.64 +      t.pause();
    1.65 +      t.currentTime = 0;
    1.66 +      t.play();
    1.67 +    }, false);
    1.68 +    element.addEventListener("loadedmetadata", function() {
    1.69 +      element.currentTime = element.duration / 2;
    1.70 +      element.play();
    1.71 +    }, false);
    1.72 +  }
    1.73 +},
    1.74 +
    1.75 +// Play the first half of the file, seek back, while
    1.76 +// continuing to play. We shall have only one range.
    1.77 +{
    1.78 +  setup : function (element) {
    1.79 +    let onTimeUpdate = function() {
    1.80 +      if (element.currentTime > element.duration/2) {
    1.81 +        element.removeEventListener("timeupdate", onTimeUpdate, false);
    1.82 +        element.pause();
    1.83 +        var oldEndRange = element.played.end(0);
    1.84 +        element.currentTime = element.duration / 4;
    1.85 +        is(element.played.end(0), oldEndRange,
    1.86 +            "When seeking back, |played| should not be changed");
    1.87 +        element.play();
    1.88 +      }
    1.89 +    }
    1.90 +    element.addEventListener("timeupdate", onTimeUpdate, false);
    1.91 +    check_full_file_played(element);
    1.92 +    element.play();
    1.93 +  }
    1.94 +},
    1.95 +
    1.96 +// Play and seek to have two ranges, and check that, as well a
    1.97 +// boundaries.
    1.98 +{
    1.99 +  setup : function (element) {
   1.100 +    let onTimeUpdate = function() {
   1.101 +      if (element.currentTime > element.duration / 2) {
   1.102 +        element.removeEventListener("timeupdate", onTimeUpdate, false);
   1.103 +        element.pause();
   1.104 +        element.currentTime += element.duration/10;
   1.105 +        element.play();
   1.106 +      }
   1.107 +    }
   1.108 +
   1.109 +    element.addEventListener("loadedmetadata", function() {
   1.110 +      element.addEventListener("timeupdate", onTimeUpdate, false);
   1.111 +    }, false);
   1.112 +
   1.113 +
   1.114 +    element.addEventListener("ended", (function() {
   1.115 +      if(element.played.length > 1) {
   1.116 +        is(element.played.length, 2, "element.played.length == 2");
   1.117 +        var guess = element.played.end(0) + element.duration/10.0;
   1.118 +        ok(rangeCheck(element.played.start(1), guess), "we should have seeked forward by one tenth of the duration");
   1.119 +        is(element.played.end(1), element.duration, "end of second range shall be the total duration");
   1.120 +      }
   1.121 +      is(element.played.start(0), 0, "start of first range shall be 0");
   1.122 +      finish_test(element);
   1.123 +    }), false);
   1.124 +
   1.125 +    element.play();
   1.126 +  }
   1.127 +},
   1.128 +
   1.129 +// Play to create two ranges, in the reverse order. check that they are sorted.
   1.130 +{
   1.131 +  setup : function (element) {
   1.132 +    function end() {
   1.133 +      element.pause();
   1.134 +      let p = element.played;
   1.135 +      ok(p.length >= 1, "There should be at least one range");
   1.136 +      is(p.start(0), element.duration/6, "Start of first range should be the sixth of the duration");
   1.137 +      ok(p.end(p.length - 1) > 5*element.duration/6, "End of last range should be greater that five times the sixth of the duration");
   1.138 +      finish_test(element);
   1.139 +    }
   1.140 +
   1.141 +    function pauseseekrestart() {
   1.142 +      element.pause();
   1.143 +      element.currentTime = element.duration/6;
   1.144 +      element.play();
   1.145 +    }
   1.146 +
   1.147 +    function onTimeUpdate_pauseseekrestart() {
   1.148 +      if (element.currentTime > 5*element.duration/6) {
   1.149 +        element.removeEventListener("timeupdate", onTimeUpdate_pauseseekrestart, false);
   1.150 +        pauseseekrestart();
   1.151 +        element.addEventListener("timeupdate", onTimeUpdate_end, false);
   1.152 +      }
   1.153 +    }
   1.154 +
   1.155 +    function onTimeUpdate_end() {
   1.156 +      if (element.currentTime > 3 * element.duration/6) {
   1.157 +        element.removeEventListener("timeupdate", onTimeUpdate_end, false);
   1.158 +        end();
   1.159 +      }
   1.160 +    }
   1.161 +
   1.162 +    element.addEventListener("timeupdate", onTimeUpdate_pauseseekrestart, false);
   1.163 +
   1.164 +    element.addEventListener('loadedmetadata', function() {
   1.165 +      element.currentTime = 4 * element.duration/6;
   1.166 +      element.play();
   1.167 +    }, false);
   1.168 +  }
   1.169 +},
   1.170 +// Seek repeatedly without playing. No range should appear.
   1.171 +{
   1.172 +  setup : function(element) {
   1.173 +    let index = 1;
   1.174 +
   1.175 +    element.addEventListener('seeked', function() {
   1.176 +      index++;
   1.177 +      element.currentTime = index * element.duration / 5;
   1.178 +      is(element.played.length, 0, "element.played.length should be 0");
   1.179 +      if (index == 5) {
   1.180 +        finish_test(element);
   1.181 +      }
   1.182 +    }, false);
   1.183 +
   1.184 +    element.addEventListener('loadedmetadata', function() {
   1.185 +      element.currentTime = element.duration / 5;
   1.186 +    }, false);
   1.187 +  }
   1.188 +}
   1.189 +];
   1.190 +
   1.191 +function rangeCheck(n1, n2) {
   1.192 +  var THRESHOLD = 0.35;
   1.193 +  var diff = Math.abs(n1 - n2);
   1.194 +  if (diff < THRESHOLD) {
   1.195 +    return true;
   1.196 +  }
   1.197 +  return false;
   1.198 +}
   1.199 +
   1.200 +function createTestArray() {
   1.201 +  var A = [];
   1.202 +  for (var i=0; i<tests.length; i++) {
   1.203 +    for (var k=0; k<gPlayedTests.length; k++) {
   1.204 +      var t = new Object();
   1.205 +      t.setup = tests[i].setup;
   1.206 +      t.name = gPlayedTests[k].name;
   1.207 +      t.type = gPlayedTests[k].type;
   1.208 +      A.push(t);
   1.209 +    }
   1.210 +  }
   1.211 +  return A;
   1.212 +}
   1.213 +
   1.214 +function startTest(test, token) {
   1.215 +  var elemType = getMajorMimeType(test.type);
   1.216 +  var element = document.createElement(elemType);
   1.217 +  element.src = test.name;
   1.218 +  element.token = token;
   1.219 +  element.volume = 0;
   1.220 +  test.setup(element);
   1.221 +  manager.started(token);
   1.222 +}
   1.223 +
   1.224 +
   1.225 +manager.runTests(createTestArray(), startTest);
   1.226 +
   1.227 +</script>
   1.228 +</pre>
   1.229 +</body>
   1.230 +</html>

mercurial