summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/intersection-observer/nested-cross-origin-iframe.sub.html
blob: 090d2363998a9320b57ec772da741a8f8aa8e8e0 (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
<!DOCTYPE html>
<meta charset=utf-8>
<meta name="viewport" content="width=device-width,initial-scale=1">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="/css/cssom-view/support/scroll-behavior.js"></script>
<script src="./resources/intersection-observer-test-utils.js"></script>
<style>
.spacer {
  height: calc(100vh + 100px);
}
</style>
<div class="spacer"></div>
<iframe id="iframe"></iframe>
<script>

promise_test(async t => {
  iframe.src =      // non secure port.
    get_host_info().HTTP_NOTSAMESITE_ORIGIN + "/intersection-observer/resources/nested-cross-origin-child-iframe.sub.html";

  await new Promise(resolve => {
    window.addEventListener("message", event => {
      if (event.data == "ready") {
        resolve();
      }
    }, { once: true });
  });

  let isIntersecting = false;
  window.addEventListener("message", function listener(event) {
    if (event.origin == get_host_info().HTTPS_NOTSAMESITE_ORIGIN) {
      isIntersecting = event.data;
      window.removeEventListener("message", listener);
    }
  });

  await new Promise(resolve => waitForNotification(t, resolve));
  await new Promise(resolve => waitForNotification(t, resolve));

  assert_false(isIntersecting,
               "The target element is not intersecting in all ancestor viewports");

  // Scroll the iframe in this document into view, but still the target element
  // in the grand child document is out of the child iframe's viewport.
  iframe.scrollIntoView({ behavior: "instant" });

  await waitForScrollEnd(document.scrollingElement);

  assert_false(isIntersecting,
               "The target element is not intersecting in all ancestor viewports");

  // Now make the target element visible in the child iframe's viewport.
  frames[0].postMessage("scroll", "*");

  await new Promise(resolve => {
    window.addEventListener("message", function listener(event) {
      // It's possible that the message from the IntersectionObserver in the
      // grand child document (HTTPS_NORSAMESITE_ORIGIN) is delivered ealier
      // than scrollEnd message from the child document
      // (HTTP_NOTSAMESITE_ORIGIN), so we need to differentiate them.
      if (event.origin == get_host_info().HTTP_NOTSAMESITE_ORIGIN &&
          event.data == "scrollEnd" ) {
        window.removeEventListener("message", listener);
        resolve();
      }
    });
  });

  await new Promise(resolve => waitForNotification(t, resolve));

  assert_true(isIntersecting,
              "The target element is now intersecting in all ancestor viewports");
}, "IntersectionObserver with `implicit root` in a nested cross-origin iframe works");
</script>