Commit 7c2b0f20 authored by Robbie McElrath's avatar Robbie McElrath Committed by Commit Bot

Migrate one deviceorientation test to use mojom mocks

The rest of the tests will be migrated in upcoming CLs. This only does
one test to get feedback on the overall approach.

Bug: 802815
Change-Id: Icce6ac6a1055b699daca5a7e4c091d3e33754885
Reviewed-on: https://chromium-review.googlesource.com/1062777
Commit-Queue: Robbie McElrath <rmcelrath@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#561983}
parent cd32918f
...@@ -3,21 +3,19 @@ ...@@ -3,21 +3,19 @@
<body> <body>
<script src="../../resources/testharness.js"></script> <script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script> <script src="../../resources/testharnessreport.js"></script>
<script src="../../http/tests/resources/sensor-helpers.js"></script>
<script src="../resources/device-orientation-helpers.js"></script> <script src="../resources/device-orientation-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> <script>
'use strict'; 'use strict';
async_test(test => { sensor_test(async sensor => {
assertTestRunner(); const orientationData = generateOrientationData(1.1, 2.2, 3.3, false);
var orientationData = generateOrientationData(1.1, 2.2, 3.3, true);
var listener = test.step_func(event => { setMockOrientationData(sensor, orientationData);
checkOrientation(event, orientationData); let event = await createEventListenerPromise('deviceorientation');
test.done(); checkOrientation(event, orientationData);
});
setMockOrientation(orientationData);
window.addEventListener('deviceorientation', listener);
}, 'Tests basic operation of deviceorientation event using mock data.'); }, 'Tests basic operation of deviceorientation event using mock data.');
</script> </script>
</body> </body>
......
...@@ -64,6 +64,40 @@ function setMockOrientation(orientationData) { ...@@ -64,6 +64,40 @@ function setMockOrientation(orientationData) {
orientationData.absolute); orientationData.absolute);
} }
// Device[Orientation|Motion]EventPump treat NaN as a missing value.
let nullToNan = x => (x === null ? NaN : x);
function setMockMotionData(sensor, motionData) {
return Promise.all([
setMockSensorDataForType(sensor, device.mojom.SensorType.ACCELEROMETER, [
nullToNan(motionData.accelerationIncludingGravityX),
nullToNan(motionData.accelerationIncludingGravityY),
nullToNan(motionData.accelerationIncludingGravityZ),
]),
setMockSensorDataForType(sensor, device.mojom.SensorType.LINEAR_ACCELERATION, [
nullToNan(motionData.accelerationX),
nullToNan(motionData.accelerationY),
nullToNan(motionData.accelerationZ),
]),
setMockSensorDataForType(sensor, device.mojom.SensorType.GYROSCOPE, [
nullToNan(motionData.rotationRateAlpha),
nullToNan(motionData.rotationRateBeta),
nullToNan(motionData.rotationRateGamma),
]),
]);
}
function setMockOrientationData(sensor, orientationData) {
let sensorType = orientationData.absolute
? device.mojom.SensorType.ABSOLUTE_ORIENTATION_EULER_ANGLES
: device.mojom.SensorType.RELATIVE_ORIENTATION_EULER_ANGLES;
return setMockSensorDataForType(sensor, sensorType, [
nullToNan(orientationData.beta),
nullToNan(orientationData.gamma),
nullToNan(orientationData.alpha),
]);
}
function checkMotion(event, expectedMotionData) { function checkMotion(event, expectedMotionData) {
assert_equals(event.acceleration.x, expectedMotionData.accelerationX, "acceleration.x"); assert_equals(event.acceleration.x, expectedMotionData.accelerationX, "acceleration.x");
assert_equals(event.acceleration.y, expectedMotionData.accelerationY, "acceleration.y"); assert_equals(event.acceleration.y, expectedMotionData.accelerationY, "acceleration.y");
......
...@@ -28,6 +28,7 @@ function sensorMocks() { ...@@ -28,6 +28,7 @@ function sensorMocks() {
constructor(sensorRequest, handle, offset, size, reportingMode) { constructor(sensorRequest, handle, offset, size, reportingMode) {
this.client_ = null; this.client_ = null;
this.startShouldFail_ = false; this.startShouldFail_ = false;
this.notifyOnReadingChange_ = true;
this.reportingMode_ = reportingMode; this.reportingMode_ = reportingMode;
this.sensorReadingTimerId_ = null; this.sensorReadingTimerId_ = null;
this.updateReadingFunction_ = null; this.updateReadingFunction_ = null;
...@@ -113,6 +114,7 @@ function sensorMocks() { ...@@ -113,6 +114,7 @@ function sensorMocks() {
this.stopReading(); this.stopReading();
this.startShouldFail_ = false; this.startShouldFail_ = false;
this.notifyOnReadingChange_ = true;
this.updateReadingFunction_ = null; this.updateReadingFunction_ = null;
this.requestedFrequencies_ = []; this.requestedFrequencies_ = [];
this.suspendCalled_ = null; this.suspendCalled_ = null;
...@@ -142,6 +144,12 @@ function sensorMocks() { ...@@ -142,6 +144,12 @@ function sensorMocks() {
this.startShouldFail_ = shouldFail; this.startShouldFail_ = shouldFail;
} }
// Configures whether to report a reading change when in ON_CHANGE
// reporting mode.
configureReadingChangeNotifications(notifyOnReadingChange) {
this.notifyOnReadingChange_ = notifyOnReadingChange;
}
// Returns resolved promise if suspend() was called, rejected otherwise. // Returns resolved promise if suspend() was called, rejected otherwise.
suspendCalled() { suspendCalled() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
...@@ -182,7 +190,8 @@ function sensorMocks() { ...@@ -182,7 +190,8 @@ function sensorMocks() {
// increasing timestamp in seconds. // increasing timestamp in seconds.
this.buffer_[1] = window.performance.now() * 0.001; this.buffer_[1] = window.performance.now() * 0.001;
} }
if (this.reportingMode_ === device.mojom.ReportingMode.ON_CHANGE) { if (this.reportingMode_ === device.mojom.ReportingMode.ON_CHANGE &&
this.notifyOnReadingChange_) {
this.client_.sensorReadingChanged(); this.client_.sensorReadingChanged();
} }
}, timeout); }, timeout);
...@@ -381,6 +390,22 @@ function sensor_test(func, name, properties) { ...@@ -381,6 +390,22 @@ function sensor_test(func, name, properties) {
}, name, properties); }, name, properties);
} }
async function setMockSensorDataForType(sensor, sensorType, mockDataArray) {
let createdSensor = await sensor.mockSensorProvider.getCreatedSensor(sensorType);
return createdSensor.setUpdateSensorReadingFunction(buffer => {
buffer.set(mockDataArray, 2);
});
}
// Returns a promise that will be resolved with the next event fired of the
// given type. The event listener will be removed once the promise resolves.
function createEventListenerPromise(event, targetWindow = window) {
return new Promise((resolve, reject) => {
let wrapper = new CallbackWrapper(resolve, reject);
targetWindow.addEventListener(event, wrapper.callback, {once: true});
});
}
// TODO(Mikhail): Refactor further to remove code duplication // TODO(Mikhail): Refactor further to remove code duplication
// in <concrete sensor>.html files. // in <concrete sensor>.html files.
function verify_sensor_reading(pattern, values, timestamp, is_null) { function verify_sensor_reading(pattern, values, timestamp, is_null) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment