summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/browsers/browsing-the-web/history-traversal/scroll-restoration-order.html
blob: 8fe7d9f9770b25177b8eddc3eff9e7ecbcddd0c0 (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
<!DOCTYPE html>
<meta charset="utf-8">
<title>History restoration order test</title>
<meta name="assert" content="https://html.spec.whatwg.org/multipage/browsing-the-web.html#history-traversal">
<meta name="assert" content="Traversing history should restore scroll position after dispatching popstate and before dispatching hashchange">

<style>
  body {
    height: 200vh;
    width: 200vw;
  }
</style>

<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
  'use strict';
  async_test(function(t) {
    window.addEventListener('load', t.step_func(function() {
      // Allow 1px epsilon for fractional scrolling.
      assert_array_approx_equals(scrollPosition(), [0, 0], 1);

      history.pushState('#1', '', '#1');
      window.scrollTo(50, 100);
      assert_array_approx_equals(scrollPosition(), [50, 100], 1);

      history.pushState('#2', '', '#2');
      window.scrollTo(100, 200);
      assert_array_approx_equals(scrollPosition(), [100, 200], 1);

      setTimeout(t.step_func(function(){
        history.pushState(null, null, '#done');
        window.scrollTo(555, 555);
        assert_array_approx_equals(scrollPosition(), [555, 555], 1);
        // Kick off the verification.
        window.history.back();
      }), 0);
    }));

    window.addEventListener('popstate', t.step_func(function() {
      // Verify that scroll position is *not* restored before popstate.
      const key = location.hash;
      const expected_scroll_position = expectedScrollPositionForKey(key);
      assert_not_equals(scrollPosition()[0], expected_scroll_position[0], `scroll is restored before popstate for ${key}`);
      assert_not_equals(scrollPosition()[1], expected_scroll_position[1], `scroll is restored before popstate for ${key}`);

      if (key == '')
        t.done();
      else
        setTimeout(t.step_func(function(){ window.history.back(); }), 0);
    }));

    window.addEventListener('hashchange', t.step_func(function() {
      // Verify that scroll position is restored before hashchange.
      var key = location.hash;
      const expected_scroll_position = expectedScrollPositionForKey(key);
      assert_array_approx_equals(scrollPosition(), expected_scroll_position, 1, `scroll is restored before hashchange for ${key}`);
    }));

    function scrollPosition() {
      return [window.pageXOffset, window.pageYOffset];
    }

    function expectedScrollPositionForKey(key) {
      switch (key) {
        case '#2': return  [100, 200];
        case '#1': return  [50, 100];
        case ''  : return  [0, 0];
        default: assert_unreached();
      }
    }

  }, 'Traversing history should restore scroll position after dispatching popstate and before dispatching hashchange');
</script>