11cb0ef41Sopenharmony_ci<!DOCTYPE html>
21cb0ef41Sopenharmony_ci<script src="/resources/testharness.js"></script>
31cb0ef41Sopenharmony_ci<script src="/resources/testharnessreport.js"></script>
41cb0ef41Sopenharmony_ci<script type="text/javascript">
51cb0ef41Sopenharmony_ci'use strict';
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci// Computes greatest common divisor of a and b using Euclid's algorithm
81cb0ef41Sopenharmony_cifunction computeGCD(a, b) {
91cb0ef41Sopenharmony_ci  if (!Number.isInteger(a) || !Number.isInteger(b)) {
101cb0ef41Sopenharmony_ci    throw new Error('Parameters must be integer numbers');
111cb0ef41Sopenharmony_ci  }
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci  var r;
141cb0ef41Sopenharmony_ci  while (b != 0) {
151cb0ef41Sopenharmony_ci    r = a % b;
161cb0ef41Sopenharmony_ci    a = b;
171cb0ef41Sopenharmony_ci    b = r;
181cb0ef41Sopenharmony_ci  }
191cb0ef41Sopenharmony_ci  return (a < 0) ? -a : a;
201cb0ef41Sopenharmony_ci}
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci// Finds minimum resolution Δ given a set of samples which are known to be in the form of N*Δ.
231cb0ef41Sopenharmony_ci// We use GCD of all samples as a simple estimator.
241cb0ef41Sopenharmony_cifunction estimateMinimumResolution(samples) {
251cb0ef41Sopenharmony_ci  var gcd;
261cb0ef41Sopenharmony_ci  for (const sample of samples) {
271cb0ef41Sopenharmony_ci    gcd = gcd ? computeGCD(gcd, sample) : sample;
281cb0ef41Sopenharmony_ci  }
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  return gcd;
311cb0ef41Sopenharmony_ci}
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_citest(function() {
341cb0ef41Sopenharmony_ci  const samples = [];
351cb0ef41Sopenharmony_ci  for (var i = 0; i < 1e3; i++) {
361cb0ef41Sopenharmony_ci    var deltaInMicroSeconds = 0;
371cb0ef41Sopenharmony_ci    const e1 = new MouseEvent('test1');
381cb0ef41Sopenharmony_ci    do {
391cb0ef41Sopenharmony_ci      const e2 = new MouseEvent('test2');
401cb0ef41Sopenharmony_ci      deltaInMicroSeconds = Math.round((e2.timeStamp - e1.timeStamp) * 1000);
411cb0ef41Sopenharmony_ci    } while (deltaInMicroSeconds == 0) // only collect non-zero samples
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci    samples.push(deltaInMicroSeconds);
441cb0ef41Sopenharmony_ci  }
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  const minResolution = estimateMinimumResolution(samples);
471cb0ef41Sopenharmony_ci  assert_greater_than_equal(minResolution, 5);
481cb0ef41Sopenharmony_ci}, 'Event timestamp should not have a resolution better than 5 microseconds');
491cb0ef41Sopenharmony_ci</script>