summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-scroll-anchoring/device-pixel-adjustment.html
blob: 4a135939fde657ab6819b167cfa72972373771b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE html>
<link rel="help" href="https://drafts.csswg.org/css-scroll-anchoring-1/">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<style>

body {
    height: 200vh;
}
#anchor {
    width: 100px;
    height: 100px;
    background-color: blue;
}

</style>
<div id="expander"></div>
<div id="anchor"></div>
<script>

// This tests that scroll anchor adjustments can happen by quantities smaller
// than a device pixel.
//
// Unfortunately, we can't test this by simply reading 'scrollTop', because
// 'scrollTop' may be rounded to the nearest CSS pixel. So, to test that
// subpixel adjustments can in fact happen, we repeatedly trigger a scroll
// adjustment in a way that would produce a different final .scrollTop value,
// depending on whether or not we rounded each adjustment as we apply it.

test(() => {
    let scroller = document.scrollingElement;
    let expander = document.querySelector("#expander");
    let anchor = document.querySelector("#anchor");
    const initialTop = 10;

    // Scroll 10px to activate scroll anchoring
    scroller.scrollTop = initialTop;

    // Helper to insert a div with specified height before the anchor node
    function addChild(height) {
        let child = document.createElement("div");
        child.style.height = `${height}px`;
        anchor.before(child);
    }

    // Calculate what fraction of a CSS pixel corresponds to one device pixel
    let devicePixel = 1.0 / window.devicePixelRatio;
    assert_true(devicePixel <= 1.0, "there should be more device pixels than CSS pixels");

    // The 0.5 is an arbitrary scale when creating the subpixel delta
    let delta = 0.5 * devicePixel;

    // To help us check for for premature rounding of adjustments, we'll
    // trigger "count" subpixel adjustments of size "delta", where "count" is
    // the first positive integer such that:
    //   round(count * delta) != count * round(delta)
    // As round(X) and count are integers, this happens when:
    //   count * delta = count * round(delta) +/- 1
    // Solving for count:
    //   count = 1 / abs(delta - round(delta))
    // Note that we don't need to worry about the denominator being zero, as:
    //   0 < devicePixel <= 1
    // And so halving devicePixel should never yield a whole number.
    let count = 1 / Math.abs(delta - Math.round(delta));

    for (let i = 0; i < count; i++) {
        addChild(delta);
        // Trigger an anchor adjustment by forcing a layout flush
        scroller.scrollTop;
    }

    let destination = Math.round(initialTop + delta * count);
    assert_equals(scroller.scrollTop, destination,
        `adjusting by ${delta}px, ${count} times, should be the same as adjusting by ${delta * count}px, once.`);
}, "Test that scroll anchor adjustments can happen by a sub device-pixel amount.");

</script>