tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

orientation-sensor-tests.js (3853B)


      1 'use strict';
      2 
      3 async function checkQuaternion(
      4    t, sensorType, testDriverName, permissionName, readings) {
      5  await test_driver.set_permission({name: permissionName}, 'granted');
      6  await test_driver.create_virtual_sensor(testDriverName);
      7  const sensor = new sensorType();
      8  t.add_cleanup(async () => {
      9    sensor.stop();
     10    await test_driver.remove_virtual_sensor(testDriverName);
     11  });
     12  const sensorWatcher =
     13      new EventWatcher(t, sensor, ['activate', 'reading', 'error']);
     14  sensor.start();
     15 
     16  await sensorWatcher.wait_for('activate');
     17  await Promise.all([
     18    test_driver.update_virtual_sensor(testDriverName, readings.next().value),
     19    sensorWatcher.wait_for('reading')
     20  ]);
     21  assert_equals(sensor.quaternion.length, 4, 'Quaternion length must be 4');
     22  assert_true(
     23      sensor.quaternion instanceof Array, 'Quaternion is must be array');
     24 };
     25 
     26 async function checkPopulateMatrix(
     27    t, sensorProvider, sensorType, testDriverName, permissionName, readings) {
     28  await test_driver.set_permission({name: permissionName}, 'granted');
     29  await test_driver.create_virtual_sensor(testDriverName);
     30  const sensor = new sensorType();
     31  t.add_cleanup(async () => {
     32    sensor.stop();
     33    await test_driver.remove_virtual_sensor(testDriverName);
     34  });
     35  const sensorWatcher =
     36      new EventWatcher(t, sensor, ['activate', 'reading', 'error']);
     37 
     38  // Throws with insufficient buffer space.
     39  assert_throws_js(
     40      TypeError, () => sensor.populateMatrix(new Float32Array(15)));
     41 
     42  // Throws if no orientation data available.
     43  assert_throws_dom(
     44      'NotReadableError', () => sensor.populateMatrix(new Float32Array(16)));
     45 
     46  // Throws if passed SharedArrayBuffer view.
     47  assert_throws_js(
     48      TypeError,
     49      // See https://github.com/whatwg/html/issues/5380 for why not `new
     50      // SharedArrayBuffer()` WebAssembly.Memory's size is in multiples of 64KiB
     51      () => sensor.populateMatrix(new Float32Array(
     52          new WebAssembly.Memory({shared: true, initial: 1, maximum: 1})
     53              .buffer)));
     54 
     55  sensor.start();
     56  await sensorWatcher.wait_for('activate');
     57 
     58  await Promise.all([
     59    test_driver.update_virtual_sensor(testDriverName, readings.next().value),
     60    sensorWatcher.wait_for('reading')
     61  ]);
     62 
     63  // Works for all supported types.
     64  const rotationMatrix32 = new Float32Array(16);
     65  sensor.populateMatrix(rotationMatrix32);
     66  assert_array_approx_equals(rotationMatrix32, kRotationMatrix, kEpsilon);
     67 
     68  let rotationMatrix64 = new Float64Array(16);
     69  sensor.populateMatrix(rotationMatrix64);
     70  assert_array_approx_equals(rotationMatrix64, kRotationMatrix, kEpsilon);
     71 
     72  let rotationDOMMatrix = new DOMMatrix();
     73  sensor.populateMatrix(rotationDOMMatrix);
     74  assert_array_approx_equals(
     75      rotationDOMMatrix.toFloat64Array(), kRotationMatrix, kEpsilon);
     76 
     77  // Sets every matrix element.
     78  rotationMatrix64.fill(123);
     79  sensor.populateMatrix(rotationMatrix64);
     80  assert_array_approx_equals(rotationMatrix64, kRotationMatrix, kEpsilon);
     81 }
     82 
     83 function runOrientationSensorTests(sensorData, readingData) {
     84  validate_sensor_data(sensorData);
     85  validate_reading_data(readingData);
     86 
     87  const {sensorName, permissionName, testDriverName} = sensorData;
     88  const sensorType = self[sensorName];
     89 
     90  const readings = new RingBuffer(readingData.readings);
     91 
     92  promise_test(async t => {
     93    assert_implements(sensorName in self, `${sensorName} is not supported.`);
     94    return checkQuaternion(
     95        t, sensorType, testDriverName, permissionName, readings);
     96  }, `${sensorName}.quaternion return a four-element FrozenArray.`);
     97 
     98  promise_test(async (t, sensorProvider) => {
     99    assert_implements(sensorName in self, `${sensorName} is not supported.`);
    100    return checkPopulateMatrix(
    101        t, sensorProvider, sensorType, testDriverName, permissionName,
    102        readings);
    103  }, `${sensorName}.populateMatrix() method works correctly.`);
    104 }