summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/intersection-observer/intersection-ratio-ib-split.html
blob: ba5370e335e102dd4aa4b86c4985be4465bf31b0 (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
<!DOCTYPE html>
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="author" href="mailto:emilio@crisal.io" title="Emilio Cobos Álvarez">
<link rel="author" href="https://mozilla.org" title="Mozilla">
<link rel="help" href="https://w3c.github.io/IntersectionObserver/#dom-intersectionobserverentry-intersectionratio">
<link rel="help" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1581876">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<style>
  block {
    display: block;
    width: 50vw;
    height: 50vh;
    background: green;
  }
</style>
<inline>
  <block></block>
</inline>
<script>

// Account for rounding differences when viewport sizes can't cleanly divide.
const epsilon = 1;

promise_test(async function() {
  for (let element of document.querySelectorAll("inline, block")) {
    let entries = await new Promise(resolve => {
      new IntersectionObserver(resolve).observe(element);
    });
    assert_equals(entries.length, 1, element.nodeName + ": Should get an entry");
    assert_true(entries[0].isIntersecting, element.nodeName + ": Should be intersecting");
    assert_equals(entries[0].intersectionRatio, 1, element.nodeName + ": Should be fully intersecting");

    function assert_rects_equal(r1, r2, label) {
      assert_approx_equals(r1.top, r2.top, epsilon, label + ": top should be equal");
      assert_approx_equals(r1.right, r2.right, epsilon, label + ": right should be equal");
      assert_approx_equals(r1.bottom, r2.bottom, epsilon, label + ": bottom should be equal");
      assert_approx_equals(r1.left, r2.left, epsilon, label + ": left should be equal");
    }

    assert_rects_equal(entries[0].boundingClientRect, element.getBoundingClientRect(), element.nodeName + ": boundingClientRect should match");
    assert_rects_equal(entries[0].intersectionRect, entries[0].boundingClientRect, element.nodeName + ": intersectionRect should match entry.boundingClientRect");
  }
}, "IntersectionObserver on an IB split gets the right intersection ratio");
</script>