summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/orientation-sensor/orientation-sensor-tests.js
blob: d69fa3e54cdc70c60e10eb3c31ca426e3fee432b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'use strict';

async function checkQuaternion(
    t, sensorType, testDriverName, permissionName, readings) {
  await test_driver.set_permission({name: permissionName}, 'granted');
  await test_driver.create_virtual_sensor(testDriverName);
  const sensor = new sensorType();
  t.add_cleanup(async () => {
    sensor.stop();
    await test_driver.remove_virtual_sensor(testDriverName);
  });
  const sensorWatcher =
      new EventWatcher(t, sensor, ['activate', 'reading', 'error']);
  sensor.start();

  await sensorWatcher.wait_for('activate');
  await Promise.all([
    test_driver.update_virtual_sensor(testDriverName, readings.next().value),
    sensorWatcher.wait_for('reading')
  ]);
  assert_equals(sensor.quaternion.length, 4, 'Quaternion length must be 4');
  assert_true(
      sensor.quaternion instanceof Array, 'Quaternion is must be array');
};

async function checkPopulateMatrix(
    t, sensorProvider, sensorType, testDriverName, permissionName, readings) {
  await test_driver.set_permission({name: permissionName}, 'granted');
  await test_driver.create_virtual_sensor(testDriverName);
  const sensor = new sensorType();
  t.add_cleanup(async () => {
    sensor.stop();
    await test_driver.remove_virtual_sensor(testDriverName);
  });
  const sensorWatcher =
      new EventWatcher(t, sensor, ['activate', 'reading', 'error']);

  // Throws with insufficient buffer space.
  assert_throws_js(
      TypeError, () => sensor.populateMatrix(new Float32Array(15)));

  // Throws if no orientation data available.
  assert_throws_dom(
      'NotReadableError', () => sensor.populateMatrix(new Float32Array(16)));

  // Throws if passed SharedArrayBuffer view.
  assert_throws_js(
      TypeError,
      // See https://github.com/whatwg/html/issues/5380 for why not `new
      // SharedArrayBuffer()` WebAssembly.Memory's size is in multiples of 64KiB
      () => sensor.populateMatrix(new Float32Array(
          new WebAssembly.Memory({shared: true, initial: 1, maximum: 1})
              .buffer)));

  sensor.start();
  await sensorWatcher.wait_for('activate');

  await Promise.all([
    test_driver.update_virtual_sensor(testDriverName, readings.next().value),
    sensorWatcher.wait_for('reading')
  ]);

  // Works for all supported types.
  const rotationMatrix32 = new Float32Array(16);
  sensor.populateMatrix(rotationMatrix32);
  assert_array_approx_equals(rotationMatrix32, kRotationMatrix, kEpsilon);

  let rotationMatrix64 = new Float64Array(16);
  sensor.populateMatrix(rotationMatrix64);
  assert_array_approx_equals(rotationMatrix64, kRotationMatrix, kEpsilon);

  let rotationDOMMatrix = new DOMMatrix();
  sensor.populateMatrix(rotationDOMMatrix);
  assert_array_approx_equals(
      rotationDOMMatrix.toFloat64Array(), kRotationMatrix, kEpsilon);

  // Sets every matrix element.
  rotationMatrix64.fill(123);
  sensor.populateMatrix(rotationMatrix64);
  assert_array_approx_equals(rotationMatrix64, kRotationMatrix, kEpsilon);
}

function runOrientationSensorTests(sensorData, readingData) {
  validate_sensor_data(sensorData);
  validate_reading_data(readingData);

  const {sensorName, permissionName, testDriverName} = sensorData;
  const sensorType = self[sensorName];

  const readings = new RingBuffer(readingData.readings);

  promise_test(async t => {
    assert_implements(sensorName in self, `${sensorName} is not supported.`);
    return checkQuaternion(
        t, sensorType, testDriverName, permissionName, readings);
  }, `${sensorName}.quaternion return a four-element FrozenArray.`);

  promise_test(async (t, sensorProvider) => {
    assert_implements(sensorName in self, `${sensorName} is not supported.`);
    return checkPopulateMatrix(
        t, sensorProvider, sensorType, testDriverName, permissionName,
        readings);
  }, `${sensorName}.populateMatrix() method works correctly.`);
}