Allow RTCDTMFToneChangeEvents to fire up to 1ms early.

Firefox's timer implementation allows events to fire 250μs early. Instead of
using a non-fuzzy timer in Firefox (may cause extra wakeups) or an artificial
extra delay on every dtmf event, relax the test case a bit.

Differential Revision: https://phabricator.services.mozilla.com/D201821

bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=1785418
gecko-commit: 8175af8c6fb443418b4fb401b5a2ab282ed73846
gecko-reviewers: bwc
diff --git a/webrtc/RTCDTMFSender-helper.js b/webrtc/RTCDTMFSender-helper.js
index 4316c38..2346560 100644
--- a/webrtc/RTCDTMFSender-helper.js
+++ b/webrtc/RTCDTMFSender-helper.js
@@ -117,10 +117,12 @@
         assert_equals(dtmfSender.toneBuffer, expectedToneBuffer,
           `Expect dtmfSender.toneBuffer to be updated to ${expectedToneBuffer}`);
 
-        // We check that the cumulative delay is at least the expected one, but
-        // system load may cause random delays, so we do not put any
-        // realistic upper bound on the timing of the events.
-        assert_between_inclusive(Date.now() - start, expectedTime,
+        // We check that the cumulative delay is at least the expected one.
+        // Note that as a UA optimization events can fire a bit (<1ms) early,
+        // and system load may cause random delays. We therefore allow events
+        // to be 1ms early and do not put any realistic expectation on the upper
+        // bound of their timing.
+        assert_between_inclusive(Date.now() - start, Math.max(0, expectedTime - 1),
                                  expectedTime + 4000,
           `Expect tonechange event for "${tone}" to be fired approximately after ${expectedTime} milliseconds`);
         if (cumulativeToneChanges.length === 0) {