Commit 005ddc87 authored by Majid Valipour's avatar Majid Valipour Committed by Commit Bot

Make event timestamp minimum resolution more robust.

Old test was too specific to Blink implementation and assumed a resolution is a
multiple of 5 microseconds.

The new test takes multiple samples and uses a GCD estimator for min
resolution.

See relevant discussion here: https://bugzilla.mozilla.org/show_bug.cgi?id=1381492#c10

Bug: 
Change-Id: Id67f76511ec55c6e94c4dcfab07b95777b10425d
Reviewed-on: https://chromium-review.googlesource.com/677981Reviewed-by: default avatarDave Tapuska <dtapuska@chromium.org>
Commit-Queue: Majid Valipour <majidvp@chromium.org>
Cr-Commit-Position: refs/heads/master@{#505262}
parent 079829bb
<!DOCTYPE html> <!DOCTYPE html>
<script src="/resources/testharness.js"></script> <script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script> <script src="/resources/testharnessreport.js"></script>
<script type="text/javascript"> <script type="text/javascript">
'use strict'; 'use strict';
// Computes greatest common divisor of a and b using Euclid's algorithm
function computeGCD(a, b) {
if (!Number.isInteger(a) || !Number.isInteger(b)) {
throw new Error('Parameters must be integer numbers');
}
var r;
while (b != 0) {
r = a % b;
a = b;
b = r;
}
return (a < 0) ? -a : a;
}
// Finds minimum resolution Δ given a set of samples which are known to be in the form of N*Δ.
// We use GCD of all samples as a simple estimator.
function estimateMinimumResolution(samples) {
var gcd;
for (const sample of samples) {
gcd = gcd ? computeGCD(gcd, sample) : sample;
}
return gcd;
}
test(function() { test(function() {
let e1 = new MouseEvent('test1'); const samples = [];
let e2 = new MouseEvent('test2'); for (var i = 0; i < 1e3; i++) {
var deltaInMicroSeconds = 0;
const e1 = new MouseEvent('test1');
do {
const e2 = new MouseEvent('test2');
deltaInMicroSeconds = Math.round((e2.timeStamp - e1.timeStamp) * 1000);
} while (deltaInMicroSeconds == 0) // only collect non-zero samples
while (e1.timeStamp == e2.timeStamp) samples.push(deltaInMicroSeconds);
e2 = new MouseEvent('test2'); }
let expectedResolutionMilliseconds = 0.005; const minResolution = estimateMinimumResolution(samples);
let integerMultipleOfResolution = (e2.timeStamp - e1.timeStamp) / expectedResolutionMilliseconds; assert_greater_than_equal(minResolution, 5);
let shouldBeNearZeroOrOne = integerMultipleOfResolution % 1;
assert_true(shouldBeNearZeroOrOne < 1e-10 || Math.abs(shouldBeNearZeroOrOne - 1) < 1e-10);
}, 'Event timestamp should not have a resolution better that 5 microseconds'); }, 'Event timestamp should not have a resolution better that 5 microseconds');
</script> </script>
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment