Bug 462959 - Tests for HTMLMediaElement.played; r=cpearce+kinetik
☠☠ backed out by c8f939f67ab5 ☠ ☠
authorPaul ADENOT <paul@paul.cx>
Sun, 03 Jul 2011 11:53:39 +0200
changeset 73702 0040f406ea6d85fac4e773a2f44487175917b2d8
parent 73701 e0aab5011b705bc41e1307590c51320d5380cfe4
child 73703 272a5a897bb6b892306c4f9b9106ffae2c8784c6
push id2
push userbsmedberg@mozilla.com
push dateFri, 19 Aug 2011 14:38:13 +0000
reviewerscpearce
bugs462959
milestone8.0a1
Bug 462959 - Tests for HTMLMediaElement.played; r=cpearce+kinetik
content/media/test/Makefile.in
content/media/test/manifest.js
content/media/test/test_played.html
--- a/content/media/test/Makefile.in
+++ b/content/media/test/Makefile.in
@@ -121,16 +121,17 @@ include $(topsrcdir)/config/rules.mk
 		test_media_selection.html \
 		test_mozLoadFrom.html \
 		test_networkState.html \
 		test_new_audio.html \
 		test_paused.html \
 		test_paused_after_ended.html \
 		test_play_events.html \
 		test_play_events_2.html \
+		test_played.html \
 		test_playback.html \
 		test_playback_errors.html \
 		test_preload_actions.html \
 		test_preload_attribute.html \
 		test_progress.html \
 		test_reactivate.html \
 		test_readyState.html \
 		test_replay_metadata.html \
--- a/content/media/test/manifest.js
+++ b/content/media/test/manifest.js
@@ -18,16 +18,24 @@ var gProgressTests = [
   { name:"r11025_u8_c1.wav", type:"audio/x-wav", duration:1.0, size:11069 },
   { name:"big.wav", type:"audio/x-wav", duration:9.0, size:102444 },
   { name:"seek.ogv", type:"video/ogg", duration:3.966, size:285310 },
   { name:"320x240.ogv", type:"video/ogg", width:320, height:240, duration:0.233, size:28942 },
   { name:"seek.webm", type:"video/webm", duration:3.966, size:215529 },
   { name:"bogus.duh", type:"bogus/duh" }
 ];
 
+// Used by test_played
+var gPlayedTests = [
+  { name:"big.wav", type:"audio/x-wav", duration:9.0, size:102444 },
+  { name:"sound.ogg", type:"audio/ogg", duration:4.0, size:2603 },
+  { name:"seek.ogv", type:"video/ogg", duration:3.966, size:285310 },
+  { name:"seek.webm", type:"video/webm", duration:3.966 }
+];
+
 // Used by test_mozLoadFrom.  Need one test file per decoder backend, plus
 // anything for testing clone-specific bugs.
 var gCloneTests = gSmallTests.concat([
   // Actual duration is ~200ms, we have Content-Duration lie about it.
   { name:"bug520908.ogv", type:"video/ogg", duration:9000 },
 ]);
 
 // Used by test_play_twice.  Need one test file per decoder backend, plus
new file mode 100644
--- /dev/null
+++ b/content/media/test/test_played.html
@@ -0,0 +1,233 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+<title>Test played member for media elements</title>
+<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
+<script type="text/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>
+</head>
+<body>
+<pre id='test'>
+<script class="testbody" type='application/javascript;version=1.8'>
+
+let manager = new MediaTestManager;
+
+function finish_test(element) {
+  if (element.parentNode)
+    element.parentNode.removeChild(element);
+  element.src = "";
+  manager.finished(element.token);
+}
+
+// Check that a file has been played in its entirety
+function check_full_file_played(element) {
+  element.addEventListener('ended', function() {
+    let interval_count = element.played.length;
+    is(interval_count, 1, "normal play: a.played.length must be 1");
+    is(element.played.start(0), 0, "start time shall be 0");
+    is(element.played.end(0), element.duration, "end time shall be duration");
+    finish_test(element);
+  }, false);
+}
+
+var tests = [
+// Without playing, check that player.played.length == 0
+{
+  setup : function(element) {
+    element.addEventListener("loadedmetadata", function() {
+      is(element.played.length, 0, "audio: initial played.length equals zero");
+      finish_test(element);
+    });
+  }
+},
+// Play the file, test the range we have
+{
+  setup : function(element) {
+    check_full_file_played(element);
+    element.play();
+  }
+},
+
+// Play the first half of the file, pause, play
+// and check we have only one range
+{
+  setup : function (element) {
+    let onEnded = function() {
+      element.pause();
+      element.currentTime = 0;
+      element.play();
+    }
+    element.addEventListener("ended", onEnded, false);
+    check_full_file_played(element);
+    element.play();
+  }
+},
+
+// Play the first half of the file, seek back, while
+// continuing to play. We shall have only one range
+{
+  setup : function (element) {
+    let onTimeUpdate = function() {
+      if (element.currentTime > element.duration / 2) {
+        element.removeEventListener("timeupdate", onTimeUpdate, false);
+        element.currentTime = element.duration / 4;
+      }
+    }
+    element.addEventListener("timeupdate", onTimeUpdate, false);
+    check_full_file_played(element);
+    element.play();
+  }
+},
+
+// Play and seek to have two ranges, and check that, as well a
+// boundaries
+{
+  setup : function (element) {
+    let onTimeUpdate = function() {
+      if (element.currentTime > element.duration / 2) {
+        element.removeEventListener("timeupdate", onTimeUpdate, false);
+        element.pause();
+        element.currentTime += element.duration / 10;
+        element.play();
+      }
+    }
+
+    element.addEventListener("loadedmetadata", function() {
+      element.addEventListener("timeupdate", onTimeUpdate, false);
+    }, false);
+
+
+    element.addEventListener("ended", function() {
+      if (element.played.length > 1) {
+        is(element.played.length, 2, "element.played.length == 2");
+        var guess = element.played.end(0) + element.duration / 10.0;
+        ok(rangeCheck(element.played.start(1), guess), "we should have seeked forward by one tenth of the duration");
+        is(element.played.end(1), element.duration, "end of second range shall be the total duration");
+      }
+      is(element.played.start(0), 0, "start of first range shall be 0");
+      finish_test(element);
+    }, false);
+
+    element.play();
+  }
+},
+
+// Play and seek to have to overlapping ranges. They should be merged, to a
+// range spanning all the test audio file.
+{
+  setup : function (element) {
+    let onTimeUpdate = function() {
+      if (element.currentTime > element.duration / 2) {
+        element.removeEventListener("timeupdate", onTimeUpdate, false);
+        element.currentTime = element.duration / 3;
+      }
+    };
+    element.addEventListener("loadedmetadata", function() {
+      element.addEventListener("timeupdate", onTimeUpdate, false);
+      check_full_file_played(element);
+      element.play();
+    }, false);
+  }
+},
+
+// Play to create two ranges, in the reverse order. check that they are sorted.
+{
+  setup : function (element) {
+    function end() {
+      element.pause();
+      let p = element.played;
+      ok(p.length >= 1, "There should be at least one range");
+      is(p.start(0), element.duration / 6,
+         "Start of first range should be the sixth of the duration");
+      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");
+      finish_test(element);
+    }
+
+    function pauseseekrestart() {
+      element.pause();
+      element.currentTime = element.duration / 6;
+      element.play();
+    }
+
+    function onTimeUpdate_pauseseekrestart() {
+      if (element.currentTime > 5 * element.duration / 6) {
+        element.removeEventListener("timeupdate", onTimeUpdate_pauseseekrestart, false);
+        pauseseekrestart();
+        element.addEventListener("timeupdate", onTimeUpdate_end, false);
+      }
+    }
+
+    function onTimeUpdate_end() {
+      if (element.currentTime > 3 * element.duration / 6) {
+        element.removeEventListener("timeupdate", onTimeUpdate_end, false);
+        end();
+      }
+    }
+
+    element.addEventListener("timeupdate", onTimeUpdate_pauseseekrestart, false);
+
+    element.addEventListener('loadedmetadata', function() {
+      element.currentTime = 4 * element.duration / 6;
+      element.play();
+    }, false);
+  }
+},
+// Seek repeatedly without playing. No range should appear.
+{
+  setup : function(element) {
+    let index = 1;
+
+    element.addEventListener('ended', function() {
+      is(element.played.length, 0, "element.played.length should be 0");
+      finish_test(element);
+    }, false);
+
+    element.addEventListener('seeked', function() {
+      index++;
+      element.currentTime = index * element.duration / 5;
+    }, false);
+
+    element.addEventListener('loadedmetadata', function() {
+      element.currentTime = element.duration / 5;
+    }, false);
+  }
+}
+];
+
+function rangeCheck(n1, n2) {
+  var THRESHOLD = 0.35;
+  var diff = Math.abs(n1 - n2);
+  return diff < THRESHOLD;
+}
+
+function createTestArray(tests) {
+  var A = [];
+  for (var i = 0; i < tests.length; i++) {
+    for (var k = 0; k < gPlayedTests.length; k++) {
+      A.push({
+        setup : tests[i].setup,
+        name : gPlayedTests[k].name,
+        type : gPlayedTests[k].type
+      });
+    }
+  }
+  return A;
+}
+
+function startTest(test, token) {
+  var elemType = /^audio/.test(test.type) ? "audio" : "video";
+  var element = document.createElement(elemType);
+  element.src = test.name;
+  element.token = token;
+  test.setup(element);
+  manager.started(token);
+}
+
+manager.runTests(createTestArray(tests), startTest);
+
+</script>
+</pre>
+</body>
+</html>