blob: e5235207164dbf39cad92775e8d1bc278162d820 [file] [log] [blame]
<!DOCTYPE html>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="../resources/mojo-helpers.js"></script>
<script src="resources/sensor-helpers.js"></script>
<script src="resources/generic-sensor-tests.js"></script>
<script>
'use strict';
if (!window.testRunner)
debug('This test cannot be run without the TestRunner');
const kQuaternion = [0, 1, 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 kRotationDOMMatrix = new DOMMatrix(kRotationMatrix);
function update_sensor_reading(buffer) {
buffer[1] = window.performance.now();
buffer[2] = 1;
buffer[3] = 0;
buffer[4] = 0;
buffer[5] = 0;
}
function verify_sensor_reading(sensor, should_be_null) {
if (should_be_null)
return sensor.quaternion == null && sensor.timestamp == null;
if (sensor.timestamp == null ||
sensor.quaternion.toString() != kQuaternion.toString())
return false;
return true;
}
runGenericSensorTests(AbsoluteOrientationSensor, update_sensor_reading, verify_sensor_reading);
sensor_test(sensor => {
let sensorObject = new AbsoluteOrientationSensor();
// Throws with insufficient buffer space.
assert_throws({ name: 'TypeError' }, () => sensorObject.populateMatrix(new Float32Array(15)));
// Throws at wrong sensor state.
assert_throws({ name: 'InvalidStateError' }, () => sensorObject.populateMatrix(new Float32Array(16)));
sensorObject.start();
return sensor.mockSensorProvider.getCreatedSensor()
.then(mockSensor => {
return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading);
})
.then(mockSensor => {
return new Promise((resolve, reject) => {
let wrapper = new CallbackWrapper(() => {
// Works for all supported types.
let rotationMatrix32 = new Float32Array(16);
sensorObject.populateMatrix(rotationMatrix32);
assert_array_equals(rotationMatrix32, kRotationMatrix);
let rotationMatrix64 = new Float64Array(16);
sensorObject.populateMatrix(rotationMatrix64);
assert_array_equals(rotationMatrix64, kRotationMatrix);
let rotationDOMMatrix = new DOMMatrix();
sensorObject.populateMatrix(rotationDOMMatrix);
assert_array_equals(rotationDOMMatrix, kRotationDOMMatrix);
// Sets every matrix element.
rotationMatrix64.fill(123);
sensorObject.populateMatrix(rotationMatrix64);
assert_array_equals(rotationMatrix64, kRotationMatrix);
sensorObject.stop();
resolve(mockSensor);
}, reject);
sensorObject.onchange = wrapper.callback;
sensorObject.onerror = reject;
});
})
.then(mockSensor => { return mockSensor.removeConfigurationCalled(); });
}, 'Test AbsoluteOrientationSensor.populateMatrix() method works correctly.');
</script>