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

[wdspec] browsingContext.print: fix rounding error in page.py test #40504

Merged
merged 5 commits into from
Jun 16, 2023

Conversation

thiagowfx
Copy link
Member

@thiagowfx thiagowfx commented Jun 12, 2023

pytest uses:

def cm_to_px(cm): return round(cm * 96 / 2.54)

js uses:

const viewport = page.getViewport({ scale: 96. / 72. });
...
canvas.height = viewport.height;
canvas.width = viewport.width;

This produces a rounding error, even though the dimension is correct:

>       assert cm_to_px(expected_dimensions["height"]) == height
E       assert 454 == 453
E         +454
E         -453

The inconsistency of rounding in both ends becomes clear when we eliminate "round" in the pytest side:

>       assert cm_to_px(expected_dimensions["height"]) == height
E       assert 453.54330708661416 == 453
E         +453.54330708661416
E         -453

There are multiple ways to fix this issue.

Option #1: Use "floor" instead of "round" in pytest. This approach fixes some tests, but others still fail. I guess in some cases "round" is correct.

Option #2: Use a range in the assertion comparison, allowing a difference of up to +-1.0. This is what this PR does.

The comparison is performed in
assert_pdf_dimensions.

The problematic part is .96 / .72 which evaluates to 4/3 = 1.333333....

@thiagowfx
Copy link
Member Author

image

Source.

@thiagowfx
Copy link
Member Author

I confirm this fixes all failing tests on our end:

image

@thiagowfx thiagowfx marked this pull request as ready for review June 12, 2023 16:18
@thiagowfx
Copy link
Member Author

@foolip rounding is hard, eh? ;)

[pytest](https://github.com/web-platform-tests/wpt/blob/7a087d54be8b6c0ca0181a86dc1ff0b28461c383/webdriver/tests/support/image.py)
uses:

    def cm_to_px(cm): return round(cm * 96 / 2.54)

[js](https://github.com/web-platform-tests/wpt/blob/7a087d54be8b6c0ca0181a86dc1ff0b28461c383/tools/wptrunner/wptrunner/print_pdf_runner.html)
uses:

    const viewport = page.getViewport({ scale: 96. / 72. });
    ...
    canvas.height = viewport.height;
    canvas.width = viewport.width;

This produces a rounding error, even though the dimension is correct:

    >       assert cm_to_px(expected_dimensions["height"]) == height
    E       assert 454 == 453
    E         +454
    E         -453

The inconsistency of rounding in both ends becomes clear when we
eliminate "round" in the pytest side:

    >       assert cm_to_px(expected_dimensions["height"]) == height
    E       assert 453.54330708661416 == 453
    E         +453.54330708661416
    E         -453

There are multiple ways to fix this issue.

Option #1: Use "floor" instead of "round" in pytest.

Option #2: Use a range in the assertion comparison, allowing a
difference of up to +-1.0. This is what this PR does.

The comparison is performed in
[`assert_pdf_dimensions`](https://github.com/web-platform-tests/wpt/blob/b6107cc1ac8b9c2800b4c8e58af719b8e4d9b8db/webdriver/tests/support/fixtures_bidi.py#L210).

The problematic part is .96 / .72 which evaluates to 4/3 = 1.333333....
@thiagowfx
Copy link
Member Author

thiagowfx commented Jun 13, 2023

Here's another way that works @whimboo:

assert height in [floor(cm_to_px(expected_dimensions["height"])), round(cm_to_px(expected_dimensions["height"]))]
assert width in [floor(cm_to_px(expected_dimensions["width"])), round(cm_to_px(expected_dimensions["width"]))]

In case you don't like the approach to compare to a +-1.0 range.

@thiagowfx thiagowfx enabled auto-merge (squash) June 14, 2023 13:19
@thiagowfx thiagowfx requested a review from whimboo June 15, 2023 11:05
@thiagowfx
Copy link
Member Author

I've discussed this topic with @jgraham and we think it will be fine to keep what you have right now. It shouldn't matter too much if there is a different of 2px or 1px by using floor.

@jgraham could you merge this?

Looking into the logs, it seems to me the failures are unrelated (but please double check).

Copy link
Contributor

@whimboo whimboo left a comment

Choose a reason for hiding this comment

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

Thanks!

@DanielRyanSmith DanielRyanSmith merged commit fb57353 into master Jun 16, 2023
29 of 36 checks passed
@DanielRyanSmith DanielRyanSmith deleted the thiagowfx/print-page-scale branch June 16, 2023 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
infra webdriver wg-testing_browser wptrunner The automated test runner, commonly called through ./wpt run
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants