summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/browsers/browsing-the-web/history-traversal/persisted-user-state-restoration/scroll-restoration-fragment-scrolling-cross-origin.html
blob: fec801e94babfcb3b2072842394547f1f3a8c5f9 (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
<!DOCTYPE html>
<meta name=timeout content=long>
<title>Precedence of scroll restoration mode over fragment scrolling in cross-origin history traversal</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<style>
  iframe {
    height: 300px;
    width: 300px;
  }
</style>
<div id="log"></div>
<script>
  'use strict';

  var next;
  function frameOnload() {
    if (next) {
      next();
    } else {
      // The test does the following navigation steps for iframe
      // 1. load page-with-fragment.html#fragment
      // 2. load blank1
      // 3. go back to page-with-fragment.html
      async_test(function(t) {
        var iframe = document.querySelector('iframe');
        var hostInfo = get_host_info();
        var basePath = location.pathname.substring(0, location.pathname.lastIndexOf('/'));
        var localURL = hostInfo.HTTP_ORIGIN + basePath + '/resources/page-with-fragment.html#fragment';
        var remoteURL = hostInfo.HTTP_REMOTE_ORIGIN + basePath + "/resources/blank1.html"

        var steps = [
          function() {
            assert_equals(iframe.contentWindow.location.href, localURL, 'should be on page-with-fragment page');
            // wait one animation frame to ensure layout is run and fragment scrolling is complete
            iframe.contentWindow.requestAnimationFrame(function() {
              assert_approx_equals(iframe.contentWindow.scrollY, 800, 5, 'should scroll to fragment');

              iframe.contentWindow.history.scrollRestoration = 'manual';
              assert_equals(iframe.contentWindow.history.scrollRestoration, 'manual');
              setTimeout(next, 0);
            });
          }, function() {
            // navigate to a new page from a different origin
            iframe.src = remoteURL;
          }, function() {
            // going back causes the iframe to traverse back
            history.back();
          }, function() {
            // coming back from history, scrollRestoration should be set to manual and respected
            assert_equals(iframe.contentWindow.location.href, localURL, 'should be back on page-with-fragment page');
            iframe.contentWindow.requestAnimationFrame(t.step_func_done(function() {
              assert_equals(iframe.contentWindow.history.scrollRestoration, 'manual', 'navigating back should retain scrollRestoration value');
              assert_equals(iframe.contentWindow.scrollX, 0, 'should not scroll to fragment');
              assert_equals(iframe.contentWindow.scrollY, 0, 'should not scroll to fragment');
            }));
          }
        ];

        var stepCount = 0;
        next = t.step_func(function() {
          steps[stepCount++]();
        });
        next();
      }, 'Manual scroll restoration should take precedent over scrolling to fragment in cross origin navigation');
    }
  }
</script>
<iframe src="resources/page-with-fragment.html#fragment" onload="frameOnload()"></iframe>