<!DOCTYPE html>
<meta name=timeout content=long>
<title>Correct behaviour of scroll restoration mode is cross origin history traversal</title>

<style>
  iframe {
    height: 300px;
    width: 300px;
  }
</style>

<body>
  <iframe></iframe>
</body>

<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script type="text/javascript">
  'use strict';

  // The test does the following navigation steps for iframe
  // 1. load blank1
  // 2. load blank2
  // 3. go back to blank1
  async_test(function(t) {
    var iframe = document.querySelector('iframe');
    var baseURL = location.href.substring(0, location.href.lastIndexOf('/'));

    var steps = [
      function() {
        iframe.src = 'resources/blank1.html';
      },
      function() {
        assert_equals(iframe.contentWindow.location.href, baseURL + '/resources/blank1.html', 'should be on first blank page');
        iframe.contentWindow.history.scrollRestoration = 'manual';
        assert_equals(iframe.contentWindow.history.scrollRestoration, 'manual');
        iframe.contentWindow.scrollTo(500, 500);
        assert_equals(iframe.contentWindow.scrollX, 500, 'scripted scrolling should take effect');
        assert_equals(iframe.contentWindow.scrollY, 500, 'scripted scrolling should take effect');
        setTimeout(next, 0);
      },
      function() {
        // navigate to new page
        iframe.src = 'resources/blank2.html';
      },
      function() {
        assert_equals(iframe.contentWindow.location.href, baseURL + '/resources/blank2.html', 'should be on second blank page');
        assert_equals(iframe.contentWindow.history.scrollRestoration, 'auto', 'new page loads should set scrollRestoration to "auto"');
        setTimeout(next, 0);
      }, function() {
        iframe.contentWindow.history.back();
      }, function() {
        // coming back scrollRestoration should be restored to 'manual' and respected
        assert_equals(iframe.contentWindow.location.href, baseURL + '/resources/blank1.html', 'should be back on first blank page');
        assert_equals(iframe.contentWindow.history.scrollRestoration, 'manual', 'navigating back should retain scrollRestoration value');
        assert_equals(iframe.contentWindow.scrollX, 0, 'horizontal scroll offset should not be restored');
        assert_equals(iframe.contentWindow.scrollY, 0, 'vertical scroll offset should not be restored');
        t.done();
      }
    ];

    var stepCount = 0;
    var next = t.step_func(function() {
      steps[stepCount++]();
    });

    iframe.onload = next;
    next();
  }, 'Navigating to new page should reset to "auto" and navigating back should restore and respect scroll restoration mode');

</script>