content/media/test/test_played.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 played member for media elements</title>
     5 <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
     6 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
     8 <script type="text/javascript" src="manifest.js"></script>
     9 </head>
    10 <body>
    11 <pre id='test'>
    12 <script class="testbody" type='application/javascript;version=1.8'>
    14 let manager = new MediaTestManager;
    16 SimpleTest.expectAssertions(0, 2);
    18 function finish_test(element) {
    19   if (element.parentNode)
    20     element.parentNode.removeChild(element);
    21   element.src="";
    22   manager.finished(element.token);
    23 }
    25 // Check that a file has been played in its entirety.
    26 function check_full_file_played(element) {
    27   element.addEventListener('ended', (function(e) {
    28     let interval_count = e.target.played.length;
    29     is(interval_count, 1, "normal play : a.played.length must be 1");
    30     is(element.played.start(0), 0, "start time shall be 0");
    31     is(element.played.end(0), e.target.duration, "end time shall be duration");
    32     finish_test(e.target);
    33   }), false);
    34 }
    36 var tests = [
    37 // Without playing, check that player.played.length == 0.
    38 {
    39   setup : function(element) {
    40     element.addEventListener("loadedmetadata", function() {
    41       is(element.played.length, 0, "audio : initial played.length equals zero");
    42       finish_test(element);
    43     });
    44   }
    45 },
    46 // Play the file, test the range we have.
    47 {
    48   setup : function(element) {
    49     check_full_file_played(element);
    50     element.play();
    51   }
    52 },
    54 // Play the second half of the file, pause, play
    55 // an check we have only one range.
    56 {
    57   setup : function (element) {
    58     element.addEventListener("ended", function (e) {
    59       var t = e.target;
    60       check_full_file_played(t);
    61       t.pause();
    62       t.currentTime = 0;
    63       t.play();
    64     }, false);
    65     element.addEventListener("loadedmetadata", function() {
    66       element.currentTime = element.duration / 2;
    67       element.play();
    68     }, false);
    69   }
    70 },
    72 // Play the first half of the file, seek back, while
    73 // continuing to play. We shall have only one range.
    74 {
    75   setup : function (element) {
    76     let onTimeUpdate = function() {
    77       if (element.currentTime > element.duration/2) {
    78         element.removeEventListener("timeupdate", onTimeUpdate, false);
    79         element.pause();
    80         var oldEndRange = element.played.end(0);
    81         element.currentTime = element.duration / 4;
    82         is(element.played.end(0), oldEndRange,
    83             "When seeking back, |played| should not be changed");
    84         element.play();
    85       }
    86     }
    87     element.addEventListener("timeupdate", onTimeUpdate, false);
    88     check_full_file_played(element);
    89     element.play();
    90   }
    91 },
    93 // Play and seek to have two ranges, and check that, as well a
    94 // boundaries.
    95 {
    96   setup : function (element) {
    97     let onTimeUpdate = function() {
    98       if (element.currentTime > element.duration / 2) {
    99         element.removeEventListener("timeupdate", onTimeUpdate, false);
   100         element.pause();
   101         element.currentTime += element.duration/10;
   102         element.play();
   103       }
   104     }
   106     element.addEventListener("loadedmetadata", function() {
   107       element.addEventListener("timeupdate", onTimeUpdate, false);
   108     }, false);
   111     element.addEventListener("ended", (function() {
   112       if(element.played.length > 1) {
   113         is(element.played.length, 2, "element.played.length == 2");
   114         var guess = element.played.end(0) + element.duration/10.0;
   115         ok(rangeCheck(element.played.start(1), guess), "we should have seeked forward by one tenth of the duration");
   116         is(element.played.end(1), element.duration, "end of second range shall be the total duration");
   117       }
   118       is(element.played.start(0), 0, "start of first range shall be 0");
   119       finish_test(element);
   120     }), false);
   122     element.play();
   123   }
   124 },
   126 // Play to create two ranges, in the reverse order. check that they are sorted.
   127 {
   128   setup : function (element) {
   129     function end() {
   130       element.pause();
   131       let p = element.played;
   132       ok(p.length >= 1, "There should be at least one range");
   133       is(p.start(0), element.duration/6, "Start of first range should be the sixth of the duration");
   134       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");
   135       finish_test(element);
   136     }
   138     function pauseseekrestart() {
   139       element.pause();
   140       element.currentTime = element.duration/6;
   141       element.play();
   142     }
   144     function onTimeUpdate_pauseseekrestart() {
   145       if (element.currentTime > 5*element.duration/6) {
   146         element.removeEventListener("timeupdate", onTimeUpdate_pauseseekrestart, false);
   147         pauseseekrestart();
   148         element.addEventListener("timeupdate", onTimeUpdate_end, false);
   149       }
   150     }
   152     function onTimeUpdate_end() {
   153       if (element.currentTime > 3 * element.duration/6) {
   154         element.removeEventListener("timeupdate", onTimeUpdate_end, false);
   155         end();
   156       }
   157     }
   159     element.addEventListener("timeupdate", onTimeUpdate_pauseseekrestart, false);
   161     element.addEventListener('loadedmetadata', function() {
   162       element.currentTime = 4 * element.duration/6;
   163       element.play();
   164     }, false);
   165   }
   166 },
   167 // Seek repeatedly without playing. No range should appear.
   168 {
   169   setup : function(element) {
   170     let index = 1;
   172     element.addEventListener('seeked', function() {
   173       index++;
   174       element.currentTime = index * element.duration / 5;
   175       is(element.played.length, 0, "element.played.length should be 0");
   176       if (index == 5) {
   177         finish_test(element);
   178       }
   179     }, false);
   181     element.addEventListener('loadedmetadata', function() {
   182       element.currentTime = element.duration / 5;
   183     }, false);
   184   }
   185 }
   186 ];
   188 function rangeCheck(n1, n2) {
   189   var THRESHOLD = 0.35;
   190   var diff = Math.abs(n1 - n2);
   191   if (diff < THRESHOLD) {
   192     return true;
   193   }
   194   return false;
   195 }
   197 function createTestArray() {
   198   var A = [];
   199   for (var i=0; i<tests.length; i++) {
   200     for (var k=0; k<gPlayedTests.length; k++) {
   201       var t = new Object();
   202       t.setup = tests[i].setup;
   203       t.name = gPlayedTests[k].name;
   204       t.type = gPlayedTests[k].type;
   205       A.push(t);
   206     }
   207   }
   208   return A;
   209 }
   211 function startTest(test, token) {
   212   var elemType = getMajorMimeType(test.type);
   213   var element = document.createElement(elemType);
   214   element.src = test.name;
   215   element.token = token;
   216   element.volume = 0;
   217   test.setup(element);
   218   manager.started(token);
   219 }
   222 manager.runTests(createTestArray(), startTest);
   224 </script>
   225 </pre>
   226 </body>
   227 </html>

mercurial