| <!DOCTYPE html> |
| <script src="../resources/testharness.js"></script> |
| <script src="../resources/testharnessreport.js"></script> |
| <script src="../http/tests/resources/sensor-helpers.js"></script> |
| <script src="file:///gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script> |
| <script src="file:///gen/services/device/public/mojom/sensor_provider.mojom.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 = [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 kRotationDOMMatrix = new DOMMatrix(kRotationMatrix); |
| |
| // For 'orientation.angle == 270', which is set for tests at |
| // at SensorProxy::GetScreenOrientationAngle(). |
| const kMappedQuaternion = [-0.707107, 0.707107, 0, 0]; |
| |
| function update_sensor_reading(buffer) { |
| buffer.set(kQuaternion, 2); |
| } |
| |
| async function checkPopulateMatrix(sensorProvider, sensorType, mojomSensorType) { |
| let sensorObject = new sensorType(); |
| |
| // Throws with insufficient buffer space. |
| assert_throws({ name: 'TypeError' }, () => sensorObject.populateMatrix(new Float32Array(15))); |
| |
| // Throws if no orientation data available. |
| assert_throws({ name: 'NotReadableError' }, () => sensorObject.populateMatrix(new Float32Array(16))); |
| |
| if (window.SharedArrayBuffer) { |
| // Throws if passed SharedArrayBuffer view. |
| assert_throws({ name: 'TypeError' }, () => sensorObject.populateMatrix(new Float32Array(new SharedArrayBuffer(16)))); |
| } |
| |
| sensorObject.start(); |
| |
| let mockSensor = await sensorProvider.getCreatedSensor(mojomSensorType); |
| await mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); |
| await 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.toFloat64Array(), |
| kRotationMatrix); |
| |
| // Sets every matrix element. |
| rotationMatrix64.fill(123); |
| sensorObject.populateMatrix(rotationMatrix64); |
| assert_array_equals(rotationMatrix64, kRotationMatrix); |
| |
| sensorObject.stop(); |
| resolve(mockSensor); |
| }, reject); |
| |
| sensorObject.onreading = wrapper.callback; |
| sensorObject.onerror = reject; |
| }); |
| return mockSensor.removeConfigurationCalled(); |
| } |
| |
| runGenericSensorTests( |
| AbsoluteOrientationSensor, |
| device.mojom.SensorType.ABSOLUTE_ORIENTATION_QUATERNION, |
| update_sensor_reading, |
| verify_quat_sensor_reading.bind(null, kQuaternion), |
| verify_quat_sensor_reading.bind(null, kMappedQuaternion), |
| ['accelerometer', 'gyroscope', 'magnetometer']); |
| |
| sensor_test(sensorProvider => { |
| return checkPopulateMatrix( |
| sensorProvider, |
| AbsoluteOrientationSensor, |
| device.mojom.SensorType.ABSOLUTE_ORIENTATION_QUATERNION); |
| }, 'Test AbsoluteOrientationSensor.populateMatrix() method works correctly.'); |
| |
| runGenericSensorTests( |
| RelativeOrientationSensor, |
| device.mojom.SensorType.RELATIVE_ORIENTATION_QUATERNION, |
| update_sensor_reading, |
| verify_quat_sensor_reading.bind(null, kQuaternion), |
| verify_quat_sensor_reading.bind(null, kMappedQuaternion), |
| ['accelerometer', 'gyroscope']); |
| |
| sensor_test(sensorProvider => { |
| return checkPopulateMatrix( |
| sensorProvider, |
| RelativeOrientationSensor, |
| device.mojom.SensorType.RELATIVE_ORIENTATION_QUATERNION); |
| }, 'Test RelativeOrientationSensor.populateMatrix() method works correctly.'); |
| </script> |