summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/orientation-sensor/orientation-sensor-tests.js
blob: 88fd432d470d025b3dd9679e216221e5fc431b3d (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
'use strict';

const kDefaultReading = [
    [ 1, 0, 0, 0 ]  // 180 degrees around X axis.
];
const kRotationMatrix = [1,  0,  0,  0,
                         0, -1,  0,  0,
                         0,  0, -1,  0,
                         0,  0,  0,  1];
const kReadings = {
    readings: kDefaultReading,
    expectedReadings: kDefaultReading,
    expectedRemappedReadings: [
        // For 'orientation.angle == 270', which is set for tests at
        // at SensorProxy::GetScreenOrientationAngle().
        [-0.707107, 0.707107, 0, 0]
    ]
};

async function checkQuaternion(t, sensorType) {
  const sensor = new sensorType();
  const eventWatcher = new EventWatcher(t, sensor, ["reading", "error"]);
  sensor.start();

  await eventWatcher.wait_for("reading");
  assert_equals(sensor.quaternion.length, 4);
  assert_true(sensor.quaternion instanceof Array);
  sensor.stop();
};

async function checkPopulateMatrix(t, sensorProvider, sensorType) {
  const sensor = new sensorType();
  const eventWatcher = new EventWatcher(t, sensor, ["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 64 KiB
      () => sensor.populateMatrix(new Float32Array(new WebAssembly.Memory({ shared:true, initial:1, maximum:1 }).buffer)));

  sensor.start();

  const mockSensor = await sensorProvider.getCreatedSensor(sensorType.name);
  await mockSensor.setSensorReading(kDefaultReading);

  await eventWatcher.wait_for("reading");

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

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

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

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

  sensor.stop();
}

function runOrientationSensorTests(sensorName) {
  const sensorType = self[sensorName];

  sensor_test(async t => {
    assert_true(sensorName in self);
    return checkQuaternion(t, sensorType);
  }, `${sensorName}.quaternion return a four-element FrozenArray.`);

  sensor_test(async (t, sensorProvider) => {
    assert_true(sensorName in self);
    return checkPopulateMatrix(t, sensorProvider, sensorType);
  }, `${sensorName}.populateMatrix() method works correctly.`);
}