Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

orientation-event: Add Permissions Policy integration tests #45428

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

rakuco
Copy link
Member

@rakuco rakuco commented Mar 29, 2024

Add tests for deviceorientationabsolute, deviceorientation and devicemotion
as described in
https://w3c.github.io/deviceorientation/#permissions-policy-integration:

  • The deviceorientation event requires the "accelerometer" and "gyroscope"
    features.
  • The deviceorientationabsolute event requires the "accelerometer",
    "gyroscope" and "magnetometer" features.
  • The devicemotion event requires the "accelerometer" and "gyroscope"
    features.

(WebKit currently requires the "magnetometer" feature for deviceorientation
events, see https://bugs.webkit.org/show_bug.cgi?id=271891).

The tests being added follow permissions-policy/README.md's recommendations:

  • Tests with different Permissions-Policy header values and how they
    influence usage in the top-level frame, same-origin iframes and
    cross-origin iframes.
  • Different values for the "allow" attribute and how they influece
    same-origin and cross-origin iframes.
  • Usage of the "allow" attribute with redirection.

This is the minimum amount of coverage we need. These tests can be expanded
to include:

  • Calls to run_all_fp_tests_allow_self() to test the behavior of the
    default policy for each feature (it requires making the function work with
    more than one feature).
  • The relative orientation to absolute orientation fallback behavior that
    depends on whether the "magnetometer" feature is enabled.
  • Not enabling all features (it may cause an explosion in the number of
    tests though).

Add tests for deviceorientationabsolute, deviceorientation and devicemotion
as described in
https://w3c.github.io/deviceorientation/#permissions-policy-integration:

- The deviceorientation event requires the "accelerometer" and "gyroscope"
  features.
- The deviceorientationabsolute event requires the "accelerometer",
  "gyroscope" and "magnetometer" features.
- The devicemotion event requires the "accelerometer" and "gyroscope"
  features.

(WebKit currently requires the "magnetometer" feature for deviceorientation
events, see https://bugs.webkit.org/show_bug.cgi?id=271891).

The tests being added follow permissions-policy/README.md's recommendations:
- Tests with different Permissions-Policy header values and how they
  influence usage in the top-level frame, same-origin iframes and
  cross-origin iframes.
- Different values for the "allow" attribute and how they influece
  same-origin and cross-origin iframes.
- Usage of the "allow" attribute with redirection.

This is the minimum amount of coverage we need. These tests can be expanded
to include:
- Calls to `run_all_fp_tests_allow_self()` to test the behavior of the
  default policy for each feature (it requires making the function work with
  more than one feature).
- The relative orientation to absolute orientation fallback behavior that
  depends on whether the "magnetometer" feature is enabled.
- Not enabling all features (it may cause an explosion in the number of
  tests though).
Copy link
Contributor

@reillyeon reillyeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with nits

promise_test(async t => {
const helper = new SensorTestHelper(t, 'devicemotion');
await helper.grantSensorsPermissions();
await helper.initializeSensors();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This puts a dependency on create_virtual_sensor :( Are we sure there isn't some other way to test this?

With other permission policy tests, we usually just have a simple iframe that tries to use the API.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This puts a dependency on create_virtual_sensor :( Are we sure there isn't some other way to test this?

I don't think so. This API does not have any mechanisms to throw an exception when permissions policy checks fail like other APIs do, so we need a way to verify that the checks have failed, in which case we wait for readings not to be delivered. We could try to reach consensus on w3c/deviceorientation#118, but it wouldn't help with the case in which we need to verify that the checks do pass, as without virtual sensors we'd be entirely dependent on the presence of specific hardware for them to work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, yeah... I was trying to think of something too... and the only thing was trying to create the events in JS, which is silly.

Raphael Kubo da Costa and others added 2 commits April 1, 2024 15:06
Co-authored-by: Reilly Grant <[email protected]>
Co-authored-by: Reilly Grant <[email protected]>
Raphael Kubo da Costa and others added 4 commits April 1, 2024 15:07
Co-authored-by: Reilly Grant <[email protected]>
Co-authored-by: Reilly Grant <[email protected]>
Co-authored-by: Reilly Grant <[email protected]>
Co-authored-by: Reilly Grant <[email protected]>

async function waitForOrientationEvent(eventName) {
if (eventName !== 'devicemotion' && eventName !== 'deviceorientation' && eventName !== 'deviceorientationabsolute') {
return 'ERROR';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should throw real errors here. It might make things easier to debug if things go wrong.

// policy checks fail, so what we do here is wait for the timeout and the
// devicemotion event handler to race each other.
value = await new Promise((resolve, reject) => {
const timeoutId = window.setTimeout(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't fly... it's gonna be super racy on testing infrastructure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have anything else in mind? This API doesn't throw any errors or fire any events when a permissions/permissions policy check fails, so the only other option would be adding a ReportingObserver and waiting for a permissions-policy-violation report. Would that be acceptable?

value = await new Promise((resolve, reject) => {
const timeoutId = window.setTimeout(() => {
window.removeEventListener(eventName, handler);
reject('NO-EVENT');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reject with real error if we do this... otherwise we lose the error stack.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is not having an error stack really a problem in this case? test_feature_availability_with_post_message_result() expects a string, so throwing an Error here would essentially require some plumbing that would ultimately result in a string comparison anyway.

Raphael Kubo da Costa added 2 commits April 5, 2024 14:20
The Promise.resolve() idea was copy-pasted from another file, but adding a
load event handler makes it more clear and in line with other files in
permissions-policy/resources.

While here, use Promise.then() instead of async/await to match other files
as well.
@marcoscaceres
Copy link
Contributor

Hey, promise to take a look soon! At a conference this week, but will try to sneak these in!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants