content/media/test/test_playback_rate.html

Fri, 16 Jan 2015 04:50:19 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 04:50:19 +0100
branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
permissions
-rw-r--r--

Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32

     1 <!DOCTYPE HTML>
     2 <html>
     3 <head>
     4   <title>Test for the playbackRate property </title>
     5   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     6   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
     7   <script type="text/javascript" src="manifest.js"></script>
     8 </head>
     9 <body>
    10 <pre id="test">
    11 <script class="testbody" type='application/javascript;version=1.8'>
    13 SimpleTest.expectAssertions(0, 2);
    15 if (navigator.platform.startsWith("Win")) {
    16   SimpleTest.expectAssertions(0, 1);
    17 } else if (navigator.platform.startsWith("Mac")) {
    18   SimpleTest.expectAssertions(0, 2);
    19 }
    21 let manager = new MediaTestManager;
    23 function rangeCheck(lhs, rhs, threshold) {
    24   var diff = Math.abs(lhs - rhs);
    25   if (diff < threshold) {
    26     return true;
    27   }
    28   return false;
    29 }
    31 function checkPlaybackRate(wallclock, media, expected, threshold) {
    32   if (rangeCheck(media / wallclock, expected, threshold)) {
    33     return true;
    34   }
    35   return false;
    36 }
    38 // Those value are expected to match those at the top of HTMLMediaElement.cpp.
    39 let VERY_SLOW_RATE = 0.1,
    40     SLOW_RATE = 0.25,
    41     FAST_RATE = 5,
    42     VERY_FAST_RATE = 20,
    43     NULL_RATE = 0.0;
    45 function ontimeupdate(e) {
    46   var t = e.target;
    47   if (t.gotEnded) {
    48     return;
    49   }
    50   t.testedForSlowdown = true;
    51   if (t.currentTime > t.duration / 2) {
    52     t.oldCurrentTime = t.currentTime;
    53     t.timestamp = Date.now();
    54     var delta = t.oldCurrentTime,
    55         delta_wallclock = (t.timestamp - t.startTimestamp - t.bufferingTime) / 1000;
    57     t.mozPreservesPitch = false;
    58     is(t.mozPreservesPitch, false, "If we disable the pitch preservation, it should appear as such.");
    60     t.bufferingTime = 0;
    62     is(t.playbackRate, SLOW_RATE,
    63         "The playback rate shoud be "+SLOW_RATE+"." + t.token);
    64     ok(checkPlaybackRate(delta_wallclock, delta, SLOW_RATE, 0.25), "We are effectively slowing down playback. (" + delta_wallclock + ", " + delta + ") for " + t.token);
    65     t.removeEventListener("timeupdate", ontimeupdate);
    66     t.addEventListener("pause", onpaused);
    67     t.playbackRate = NULL_RATE;
    68     t.oldCurrentTime = t.currentTime;
    69     setTimeout(function() {
    70       afterNullPlaybackRate(e);
    71     }, 100);
    72   }
    73 }
    75 function onpaused(e) {
    76   var t = e.target;
    77   t.pausedReceived = true;
    78 }
    80 function afterNullPlaybackRate(e) {
    81   var t = e.target;
    83   t.testedForNull = true;
    85   if (t.gotEnded) {
    86     return;
    87   }
    88   // The currentTime won't be perfectly equal because of the latency
    89   // compensation. We expect no more that 300ms difference.
    90   let THRESHOLD = 0.3;
    91   ok(rangeCheck(t.currentTime, t.oldCurrentTime, THRESHOLD), "Current time should not change when playbackRate is null (" + t.currentTime + " " + t.oldCurrentTime + ").");
    92   ok(!t.paused, "The element should not be in paused state.");
    93   t.removeEventListener("paused", onpaused);
    94   is(t.pausedReceived, undefined, "Paused event should not have been received.");
    95   t.timestamp = Date.now();
    96   t.oldCurrentTime = t.currentTime;
    97   t.playbackRate = VERY_FAST_RATE;
    98   is(t.playbackRate, FAST_RATE, "Playback rate should be clamped to " + FAST_RATE + ".");
    99 }
   101 function onended(e) {
   102   var t = e.target,
   103       playtime = (Date.now() - t.timestamp - t.bufferingTime) / 1000,
   104       mediaTime = t.duration - t.oldCurrentTime;
   106   if (!(t.testedForSlowdown && t.testedForNull)) {
   107     t.gotEnded = true;
   108     t.skippedFastPart = true;
   109   }
   111   t.bufferingTime = 0;
   112   // We got "ended" too early, skip these tests.
   113   if (t.testedForSlowdown && t.testedForNull) {
   114     is(t.playbackRate, FAST_RATE, "The playback rate should still be "+FAST_RATE+".");
   115     ok(!t.muted, "The audio should be muted when playing at high speed, but should not appear as such.");
   116     is(t.currentTime, t.duration, "Current time should be equal to the duration (not change by playback rate).");
   117   }
   118   finish_test(t);
   119 }
   121 function onratechange(e) {
   122   if (!e.target.ratechangecount) {
   123     e.target.ratechangecount = 0;
   124   }
   125   e.target.ratechangecount++;
   126 }
   128 function finish_test(element) {
   129   if (element.parentNode)
   130     element.parentNode.removeChild(element);
   131   element.src="";
   132   manager.finished(element.token);
   133 }
   135 // These two functions handle the case when the playback pauses for buffering. It
   136 // adjusts the timestamps to be accurate. Despite the fact that the web servers
   137 // is supposed to be on the same machine, buffering pauses can occur (rarely,
   138 // but still).
   139 function onplaying(e) {
   140   var t = e.target;
   141   if (t.bufferingTimestamp != undefined) {
   142     t.bufferingTime += (Date.now() - t.bufferingTimestamp);
   143     t.bufferingTimestamp = undefined;
   144   }
   145 }
   147 function onwaiting(e) {
   148   var t = e.target;
   149   t.bufferingTimestamp = Date.now();
   150 }
   152 function onvolumechange(e) {
   153   ok(false, "We should not receive a volumechange event when changing the playback rate.");
   154 }
   156 function startTest(test, token) {
   157   let elemType = /^audio/.test(test.type) ? "audio" : "video";
   158   let element = document.createElement(elemType);
   159   element.src = test.name;
   160   element.preload = "metadata";
   161   element.token = token;
   162   element.controls = true;
   163   element.bufferingTime = 0;
   164   document.body.appendChild(element);
   165   element.addEventListener("ratechange", onratechange);
   166   element.addEventListener("timeupdate", ontimeupdate);
   167   element.addEventListener("ended", onended);
   168   element.addEventListener("waiting", onwaiting);
   169   element.addEventListener("playing", onplaying);
   170   element.addEventListener("volumechange", onvolumechange);
   171   manager.started(token);
   172   element.startTimestamp = Date.now();
   173   is(element.mozPreservesPitch, true, "Pitch preservation should be enabled by default.");
   174   element.addEventListener("loadedmetadata", function() {
   175     is(element.playbackRate, 1.0, "playbackRate should be initially 1.0");
   176     is(element.defaultPlaybackRate, 1.0, "defaultPlaybackRate should be initially 1.0");
   177     element.playbackRate = VERY_SLOW_RATE;
   178     is(element.playbackRate, SLOW_RATE, "PlaybackRate should be clamped to " + SLOW_RATE + ".");
   179     element.play();
   180     element.playbackRate = SLOW_RATE;
   181   });
   182 }
   184 manager.runTests(gPlayedTests, startTest);
   186 </script>
   187 </pre>
   188 <div id="elements">
   189 </div>
   190 </body>
   191 </html>

mercurial