Bug 1319356 - clean up media elements when tests are done. r=cpearce
authorJW Wang <jwwang@mozilla.com>
Mon, 21 Nov 2016 13:54:40 +0800
changeset 324027 fec45c20351f688a29bd4e635098fd91832002ca
parent 324026 a8dabadba0abc4467a03fa3778b93ea1dbe291d7
child 324028 a7389b4dca6380b2eac4020079a36a7dae317222
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewerscpearce
bugs1319356
milestone53.0a1
Bug 1319356 - clean up media elements when tests are done. r=cpearce MozReview-Commit-ID: CYJDwPJ5E2P
dom/media/test/eme.js
dom/media/test/test_eme_canvas_blocked.html
dom/media/test/test_eme_stream_capture_blocked_case1.html
dom/media/test/test_eme_stream_capture_blocked_case2.html
dom/media/test/test_eme_stream_capture_blocked_case3.html
--- a/dom/media/test/eme.js
+++ b/dom/media/test/eme.js
@@ -259,29 +259,34 @@ function LoadTest(test, elem, token, loa
         resolve();
       }).catch(function() {
         Log(token, "error while loading tracks");
       });
     })
   });
 }
 
-// Same as LoadTest, but manage a token+"_load" start&finished.
-// Also finish main token if loading fails.
-function LoadTestWithManagedLoadToken(test, elem, manager, token, loadParams)
-{
-  manager.started(token + "_load");
-  return LoadTest(test, elem, token, loadParams)
-  .catch(function (reason) {
+function EMEPromise() {
+  var self = this;
+  self.promise = new Promise(function(resolve, reject) {
+    self.resolve = resolve;
+    self.reject = reject;
+  });
+}
+
+// Finish |token| when all promises are resolved or any one promise is
+// rejected. It also clean up the media element to release resources.
+function EMEPromiseAll(v, token, promises) {
+  Promise.all(promises).then(values => {
+    removeNodeAndSource(v);
+    manager.finished(token);
+  }, reason => {
     ok(false, TimeStamp(token) + " - Error during load: " + reason);
-    manager.finished(token + "_load");
+    removeNodeAndSource(v);
     manager.finished(token);
-  })
-  .then(function () {
-    manager.finished(token + "_load");
   });
 }
 
 function SetupEME(test, token, params)
 {
   var v = document.createElement("video");
   v.crossOrigin = test.crossOrigin || false;
   v.sessions = [];
--- a/dom/media/test/test_eme_canvas_blocked.html
+++ b/dom/media/test/test_eme_canvas_blocked.html
@@ -16,35 +16,36 @@ function startTest(test, token)
 {
   manager.started(token);
 
   var sessions = [];
 
   var v = SetupEME(test, token);
   v.preload = "auto"; // Required due to "canplay" not firing for MSE unless we do this.
 
+  var p1 = new EMEPromise;
   v.addEventListener("loadeddata", function(ev) {
     var video = ev.target;
     var canvas = document.createElement("canvas");
     canvas.width = video.videoWidth;
     canvas.height = video.videoHeight;
     document.body.appendChild(canvas);
     var ctx = canvas.getContext("2d");
     var threwError = false;
     try {
       ctx.drawImage(video, 0, 0);
     } catch (ex) {
       threwError = true;
     }
     ok(threwError, TimeStamp(token) + " - Should throw an error when trying to draw EME video to canvas.");
-    manager.finished(token);
+    p1.resolve();
   });
 
-  LoadTestWithManagedLoadToken(test, v, manager, token,
-                               { onlyLoadFirstFragments:2, noEndOfStream:false });
+  var p2 = LoadTest(test, v, token, { onlyLoadFirstFragments:2, noEndOfStream:false });
+  EMEPromiseAll(v, token, [p1.promise, p2]);
 }
 
 function beginTest() {
   manager.runTests(gEMETests, startTest);
 }
 
 if (!IsMacOSSnowLeopardOrEarlier()) {
   SimpleTest.waitForExplicitFinish();
--- a/dom/media/test/test_eme_stream_capture_blocked_case1.html
+++ b/dom/media/test/test_eme_stream_capture_blocked_case1.html
@@ -15,30 +15,31 @@ var manager = new MediaTestManager;
 function startTest(test, token)
 {
   // Three cases:
   // 1. setting MediaKeys on an element captured by MediaElementSource should fail, and
   // 2. creating a MediaElementSource on a media element with a MediaKeys should fail, and
   // 3. capturing a media element with mozCaptureStream that has a MediaKeys should fail.
 
   // Case 1. setting MediaKeys on an element captured by MediaElementSource should fail.
+  var p1 = new EMEPromise;
   var case1token = token + "_case1";
   var setKeysFailed = function() {
     ok(true, TimeStamp(case1token) + " setMediaKeys failed as expected.");
-    manager.finished(case1token);
+    p1.resolve();
   };
   var v1 = SetupEME(test, case1token,  { onSetKeysFail: setKeysFailed });
   var context = new AudioContext();
   var node = context.createMediaElementSource(v1);
   v1.addEventListener("loadeddata", function(ev) {
     ok(false, TimeStamp(case1token) + " should never reach loadeddata, as setMediaKeys should fail");
   });
   manager.started(case1token);
-  LoadTestWithManagedLoadToken(test, v1, manager, case1token,
-                               { onlyLoadFirstFragments:2, noEndOfStream:false });
+  var p2 = LoadTest(test, v1, case1token, { onlyLoadFirstFragments:2, noEndOfStream:false });
+  EMEPromiseAll(v1, case1token, [p1.promise, p2]);
 }
 
 function beginTest() {
   manager.runTests(gEMETests, startTest);
 }
 
 if (!IsMacOSSnowLeopardOrEarlier()) {
   SimpleTest.waitForExplicitFinish();
--- a/dom/media/test/test_eme_stream_capture_blocked_case2.html
+++ b/dom/media/test/test_eme_stream_capture_blocked_case2.html
@@ -15,33 +15,34 @@ var manager = new MediaTestManager;
 function startTest(test, token)
 {
   // Three cases:
   // 1. setting MediaKeys on an element captured by MediaElementSource should fail, and
   // 2. creating a MediaElementSource on a media element with a MediaKeys should fail, and
   // 3. capturing a media element with mozCaptureStream that has a MediaKeys should fail.
 
   // Case 2. creating a MediaElementSource on a media element with a MediaKeys should fail.
+  var p1 = new EMEPromise;
   var case2token = token + "_case2";
   var v2 = SetupEME(test, case2token);
   v2.addEventListener("loadeddata", function(ev) {
     ok(true, case2token + " should reach loadeddata");
     var threw = false;
     try {
       var context = new AudioContext();
       var node = context.createMediaElementSource(v2);
     } catch (e) {
       threw = true;
     }
     ok(threw, "Should throw an error creating a MediaElementSource on an EME video.");
-    manager.finished(case2token);
+    p1.resolve();
   });
   manager.started(case2token);
-  LoadTestWithManagedLoadToken(test, v2, manager, case2token,
-                               { onlyLoadFirstFragments:2, noEndOfStream:false });
+  var p2 = LoadTest(test, v2, case2token, { onlyLoadFirstFragments:2, noEndOfStream:false });
+  EMEPromiseAll(v2, case2token, [p1.promise, p2]);
 }
 
 function beginTest() {
   manager.runTests(gEMETests, startTest);
 }
 
 if (!IsMacOSSnowLeopardOrEarlier()) {
   SimpleTest.waitForExplicitFinish();
--- a/dom/media/test/test_eme_stream_capture_blocked_case3.html
+++ b/dom/media/test/test_eme_stream_capture_blocked_case3.html
@@ -15,32 +15,33 @@ var manager = new MediaTestManager;
 function startTest(test, token)
 {
   // Three cases:
   // 1. setting MediaKeys on an element captured by MediaElementSource should fail, and
   // 2. creating a MediaElementSource on a media element with a MediaKeys should fail, and
   // 3. capturing a media element with mozCaptureStream that has a MediaKeys should fail.
 
   // Case 3. capturing a media element with mozCaptureStream that has a MediaKeys should fail.
+  var p1 = new EMEPromise;
   var case3token = token + "_case3";
   var v3 = SetupEME(test, case3token);
   v3.addEventListener("loadeddata", function(ev) {
     ok(true, TimeStamp(case3token) + " should reach loadeddata");
     var threw = false;
     try {
       var stream = v3.mozCaptureStreamUntilEnded();
     } catch (e) {
       threw = true;
     }
     ok(threw, TimeStamp(case3token) + " Should throw an error calling mozCaptureStreamUntilEnded an EME video.");
-    manager.finished(case3token);
+    p1.resolve();
   });
   manager.started(case3token);
-  LoadTestWithManagedLoadToken(test, v3, manager, case3token,
-                               { onlyLoadFirstFragments:2, noEndOfStream:false });
+  var p2 = LoadTest(test, v3, case3token, { onlyLoadFirstFragments:2, noEndOfStream:false });
+  EMEPromiseAll(v3, case3token, [p1.promise, p2]);
 }
 
 function beginTest() {
   manager.runTests(gEMETests, startTest);
 }
 
 if (!IsMacOSSnowLeopardOrEarlier()) {
   SimpleTest.waitForExplicitFinish();