Bug 1296531 - Let waitForAnalysisSuccess take a cancelPromise. r?jib
MozReview-Commit-ID: 3UHUXbg2laL
--- a/dom/media/tests/mochitest/head.js
+++ b/dom/media/tests/mochitest/head.js
@@ -114,37 +114,39 @@ AudioStreamAnalyser.prototype = {
this.sourceNodes.forEach(n => n.disconnect());
this.sourceNodes = [];
this.stream.removeEventListener("addtrack", this.onaddtrack);
},
/**
* Return a Promise, that will be resolved when the function passed as
* argument, when called, returns true (meaning the analysis was a
- * success).
+ * success). The promise is rejected if the cancel promise resolves first.
*
* @param {function} analysisFunction
- * A fonction that performs an analysis, and returns true if the
+ * A function that performs an analysis, and resolves with true if the
* analysis was a success (i.e. it found what it was looking for)
+ * @param {promise} cancel
+ * A promise that on resolving will reject the promise we returned.
*/
- waitForAnalysisSuccess: function(analysisFunction) {
- var self = this;
- return new Promise((resolve, reject) => {
- function analysisLoop() {
- var success = analysisFunction(self.getByteFrequencyData());
- if (success) {
- resolve();
- return;
- }
- // else, we need more time
- requestAnimationFrame(analysisLoop);
+ waitForAnalysisSuccess: async function(analysisFunction,
+ cancel = wait(60000, new Error("Audio analysis timed out"))) {
+ let aborted = false;
+ cancel.then(() => aborted = true);
+
+ // We need to give the Analyser some time to start gathering data.
+ await wait(200);
+
+ do {
+ await new Promise(resolve => requestAnimationFrame(resolve));
+ if (aborted) {
+ throw error;
}
- // We need to give the Analyser some time to start gathering data.
- wait(200).then(analysisLoop);
- });
+ }
+ while (!analysisFunction(this.getByteFrequencyData()));
},
/**
* Return the FFT bin index for a given frequency.
*
* @param {double} frequency
* The frequency for whicht to return the bin number.
* @returns {integer} the index of the bin in the FFT array.
--- a/dom/media/tests/mochitest/test_getUserMedia_audioCapture.html
+++ b/dom/media/tests/mochitest/test_getUserMedia_audioCapture.html
@@ -92,17 +92,17 @@ scriptsReady
// energy in between, not just a flat white noise.
return (array[analyser.binIndexForFrequency(50)] < 50 &&
array[analyser.binIndexForFrequency(1000)] > 200 &&
array[analyser.binIndexForFrequency(2500)] < 50 &&
array[analyser.binIndexForFrequency(5000)] > 200 &&
array[analyser.binIndexForFrequency(7500)] < 50 &&
array[analyser.binIndexForFrequency(10000)] > 200);
}).then(finish);
- }).catch(finish);
+ });
});
});
</script>
</pre>
</body>