Skip to content

Commit

Permalink
sensors: Convert existing Generic Sensor web tests to test_driver (we…
Browse files Browse the repository at this point in the history
…b-platform-tests#41410)

All Generic Sensor web tests are now using virtual sensors and their
implementation in WPT's testdriver.js and ChromeDriver/content_shell.
These tests no longer depend on generic_sensor_mocks.js (or Mojo mocks
in general), which has been kept only for the Device Orientation tests
at this point, and should be fully interoperable now. This also
exercises the code in //content and //services.

Compared to the JS mocks-based implementation, one of the biggest
changes is that there is no equivalent to a MockSensor that takes an
array of readings and periodically rotates and reports them to
consumers. This was an implementation detail that was kind of hard to
justify in spec terms, and the interaction between the JS timer and the
C++ code in services could lead to races between shared buffer values
and expected values (bug 1073865 is probably related, for example).

We now follow the new Automation section in the Generic Sensor spec and
explicitly require reading updates to be done via calls to
test_driver.update_virtual_sensor(). These calls might not result in a
change in readings if the sensor is not active or if the
threshold/rounding checks fail, so the tests are careful to check that a
reading has gone through when necessary. Furthermore, given how the code
in //services is implemented it is not possible to assert that a call to
update_virtual_sensor() will resolve _before_ a "reading" event is
emitted, so some Promise.all() calls are required.

The iframe tests have been essentially rewritten for clarity and to test
the right things:
* iframe_sensor_handler.html is used only by the cross-origin test. The
  same-origin one simply creates the sensor in the test file directly.
* send_message_to_iframe() does not take a |reply| argument anymore.
  Instead, it propagate the replies to the callers so they can do
  whatever they need and we do not need to come up with values like
  "success" in replies.
* The cross-origin and same-origin tests follow a workflow that is
  easier to follow and mostly relies on timestamps that are part of the
  public API to determine whether readings are being provided or not
  depending on frame focus.

Now that the //content code is being exercised, the Ambient Light Sensor
and Magnetometer web tests need the GenericSensorExtraClasses feature to
be enabled, which is done by the newly-created
generic-sensor-extra-classes virtual test suite.

Finally, the referenceFrame test in generic-sensor-tests.js has been
disabled and a copy was added to wpt_internal, as there is no
cross-platform way to set an orientation angle and in wpt_internal it
can be set via Internals' setMockScreenOrientation().

Bug: 1278377, 1471996
Change-Id: Ie556c6d7bcfc0b2b84abc5f0770f6b3120ec81a2

Co-authored-by: Raphael Kubo da Costa <[email protected]>
  • Loading branch information
chromium-wpt-export-bot and Raphael Kubo da Costa authored Oct 26, 2023
1 parent 873c5ae commit ea2a5d6
Show file tree
Hide file tree
Showing 27 changed files with 1,233 additions and 803 deletions.
9 changes: 6 additions & 3 deletions accelerometer/Accelerometer-iframe-access.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
<link rel="help" href="https://www.w3.org/TR/accelerometer/">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/generic-sensor/resources/generic-sensor-helpers.js"></script>
<script src="/generic-sensor/generic-sensor-iframe-tests.sub.js"></script>
<script src="/generic-sensor/generic-sensor-feature-policy-test.sub.js"></script>
<script src="resources/sensor-data.js"></script>
<div id="log"></div>
<script>
run_generic_sensor_iframe_tests('Accelerometer');
run_generic_sensor_iframe_tests('LinearAccelerationSensor');
run_generic_sensor_iframe_tests('GravitySensor');
run_generic_sensor_iframe_tests(kAccelerometerSensorData, kAccelerometerReadings);
run_generic_sensor_iframe_tests(kLinearAccelerationSensorData, kAccelerometerReadings);
run_generic_sensor_iframe_tests(kGravitySensorData, kAccelerometerReadings);
</script>
26 changes: 5 additions & 21 deletions accelerometer/Accelerometer.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,12 @@
<link rel="help" href="https://www.w3.org/TR/accelerometer/">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/page-visibility/resources/window_state_context.js"></script>
<script src="/generic-sensor/resources/generic-sensor-helpers.js"></script>
<script src="/generic-sensor/generic-sensor-tests.js"></script>
<script src="resources/sensor-data.js"></script>
<script>

'use strict';

const kReadings = {
readings: [
[1.12345, 2.12345, 3.12345]
],
expectedReadings: [
[1.12345, 2.12345, 3.12345]
],
expectedRemappedReadings: [
[-2.12345, 1.12345, 3.12345]
]
};

runGenericSensorTests(
'Accelerometer',
kReadings,
verifyXyzSensorReading,
['accelerometer']);

runGenericSensorTests(kAccelerometerSensorData, kAccelerometerReadings);
</script>
22 changes: 5 additions & 17 deletions accelerometer/GravitySensor.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,16 @@
<link rel="help" href="https://www.w3.org/TR/accelerometer/">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/page-visibility/resources/window_state_context.js"></script>
<script src="/generic-sensor/resources/generic-sensor-helpers.js"></script>
<script src="/generic-sensor/generic-sensor-tests.js"></script>
<script src="resources/sensor-data.js"></script>
<script>

'use strict';

const kReadings = {
readings: [
[1.12345, 2.12345, 3.12345]
],
expectedReadings: [
[1.12345, 2.12345, 3.12345]
],
expectedRemappedReadings: [
[-2.12345, 1.12345, 3.12345]
]
};

runGenericSensorTests(
'GravitySensor',
kReadings,
verifyXyzSensorReading,
['accelerometer']);
runGenericSensorTests(kGravitySensorData, kAccelerometerReadings);

</script>
22 changes: 5 additions & 17 deletions accelerometer/LinearAccelerationSensor.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,16 @@
<link rel="help" href="https://www.w3.org/TR/accelerometer/">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/page-visibility/resources/window_state_context.js"></script>
<script src="/generic-sensor/resources/generic-sensor-helpers.js"></script>
<script src="/generic-sensor/generic-sensor-tests.js"></script>
<script src="resources/sensor-data.js"></script>
<script>

'use strict';

const kReadings = {
readings: [
[1.12345, 2.12345, 3.12345]
],
expectedReadings: [
[1.12345, 2.12345, 3.12345]
],
expectedRemappedReadings: [
[-2.12345, 1.12345, 3.12345]
]
};

runGenericSensorTests(
'LinearAccelerationSensor',
kReadings,
verifyXyzSensorReading,
['accelerometer']);
runGenericSensorTests(kLinearAccelerationSensorData, kAccelerometerReadings);

</script>
34 changes: 34 additions & 0 deletions accelerometer/resources/sensor-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

const kAccelerometerSensorData = {
sensorName: 'Accelerometer',
permissionName: 'accelerometer',
testDriverName: 'accelerometer',
featurePolicyNames: ['accelerometer']
};

const kGravitySensorData = {
sensorName: 'GravitySensor',
permissionName: 'accelerometer',
testDriverName: 'gravity',
featurePolicyNames: ['accelerometer']
};

const kLinearAccelerationSensorData = {
sensorName: 'LinearAccelerationSensor',
permissionName: 'accelerometer',
testDriverName: 'linear-acceleration',
featurePolicyNames: ['accelerometer']
};

const kAccelerometerReadings = {
readings: [
{ x: 1.12345, y: 2.12345, z: 3.12345 }
],
expectedReadings: [
{ x: 1.1, y: 2.1, z: 3.1 }
],
expectedRemappedReadings: [
{ x: -2.1, y: 1.1, z: 3.1 }
]
};
5 changes: 4 additions & 1 deletion ambient-light/AmbientLightSensor-iframe-access.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
<link rel="help" href="https://w3c.github.io/ambient-light/">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/generic-sensor/resources/generic-sensor-helpers.js"></script>
<script src="/generic-sensor/generic-sensor-iframe-tests.sub.js"></script>
<script src="/generic-sensor/generic-sensor-feature-policy-test.sub.js"></script>
<script src="resources/sensor-data.js"></script>
<div id="log"></div>
<script>
run_generic_sensor_iframe_tests('AmbientLightSensor');
run_generic_sensor_iframe_tests(kSensorData, kReadings);
</script>
56 changes: 5 additions & 51 deletions ambient-light/AmbientLightSensor.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,12 @@
<link rel="help" href="https://www.w3.org/TR/ambient-light/">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/page-visibility/resources/window_state_context.js"></script>
<script src="/generic-sensor/resources/generic-sensor-helpers.js"></script>
<script src="/generic-sensor/generic-sensor-tests.js"></script>
<script src="resources/sensor-data.js"></script>
<script>

'use strict';

const kReadings = {
readings: [
// Readings are selected so that illuminance significance check causes
// the following to happen:
// 1. First two values test situation when two values would be rounded
// to same value. As the second value would be rounded to same value
// as first it won't trigger reading event.
// 2. New value is set to 24. And test checks it is correctly rounded to
// 0.
// 3. New reading is attempted to set to 35.
// 4. Value is read from sensor and compared new reading. But as new
// reading was not significantly different compared to initial, for
// privacy reasons, service returns the initial value.
// 5. New value is set to 49. And test checks it is correctly rounded to
// 50. New value is allowed as it is significantly different compared
// to old value (24).
// 6. New reading is attempted to set to 35.
// 7. Value is read from sensor and compared new reading. But as new
// reading was not significantly different compared to initial, for
// privacy reasons, service returns the initial value.
// 8. New value is set to 23. And test checks it is correctly rounded to
// 0. New value is allowed as it is significantly different compared
// to old value (49).
//
// Note: Readings and expectedReadings wraps around correctly as next
// value would be 150 (output from 127).
[127],
[165],
[24],
[35],
[49],
[35],
[23]
],
expectedReadings: [
[150], // output from 127
[0], // output from 24
[50], // output from 49
[0] // output from 23
]
};

runGenericSensorTests(
'AmbientLightSensor',
kReadings,
verifyAlsSensorReading,
['ambient-light-sensor']);

runGenericSensorTests(kSensorData, kReadings);
</script>
47 changes: 47 additions & 0 deletions ambient-light/resources/sensor-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const kSensorData = {
sensorName: 'AmbientLightSensor',
permissionName: 'ambient-light-sensor',
testDriverName: 'ambient-light',
featurePolicyNames: ['ambient-light-sensor']
};

const kReadings = {
readings: [
// Readings are selected so that illuminance significance check causes
// the following to happen:
// 1. First two values test situation when two values would be rounded
// to same value. As the second value would be rounded to same value
// as first it won't trigger reading event.
// 2. New value is set to 24. And test checks it is correctly rounded to
// 0.
// 3. New reading is attempted to set to 35.
// 4. Value is read from sensor and compared new reading. But as new
// reading was not significantly different compared to initial, for
// privacy reasons, service returns the initial value.
// 5. New value is set to 49. And test checks it is correctly rounded to
// 50. New value is allowed as it is significantly different compared
// to old value (24).
// 6. New reading is attempted to set to 35.
// 7. Value is read from sensor and compared new reading. But as new
// reading was not significantly different compared to initial, for
// privacy reasons, service returns the initial value.
// 8. New value is set to 23. And test checks it is correctly rounded to
// 0. New value is allowed as it is significantly different compared
// to old value (49).
//
// Note: Readings and expectedReadings wraps around correctly as next
// value would be 150 (output from 127).
{ illuminance: 127 }, { illuminance: 165 }, { illuminance: 24 }, {
illuminance:
35
}, { illuminance: 49 }, { illuminance: 35 }, { illuminance: 23 }
],
expectedReadings: [
{ illuminance: 150 }, // output from 127
{ illuminance: 0 }, // output from 24
{ illuminance: 50 }, // output from 49
{ illuminance: 0 } // output from 23
]
};
Loading

0 comments on commit ea2a5d6

Please sign in to comment.