<html>
<head>
</head>
<body>
Navigates a subframe and edits a field, then goes back and forward
to test if the field state is restored.
<pre id="result">FAIL</pre>

<script>

function clickLink() {
  // Simulate a mouse click on a link targeting the frame.
  var evt = document.createEvent("MouseEvents");
  evt.initMouseEvent("click", true, true, window,
      0, 0, 0, 0, 0, false, false, false, false, 0, null);
  document.getElementById("link").dispatchEvent(evt);
}

function getTextInput() {
  var frame = document.getElementById("frame");
  return frame.contentDocument.getElementById("textInput");
}

var stage = "before edit";
var textInputValue;

function frameLoaded() {
  if (stage == "before edit") {
    var textInput = getTextInput();
    if (textInput != null) {
      // Edit the form and go back.
      textInput.value = "test";
      stage = "after edit";
      history.back();
    }
  } else if (stage == "after edit") {
    // Go forward.
    stage = "after forward";
    history.forward();
  } else if (stage == "after forward") {
    // Check that the value is still in the form.
    if (getTextInput().value == "test")
      document.getElementById("result").innerHTML = "PASS";

    if (window.testRunner) {
      testRunner.notifyDone();
    }
  }
}

onload = function () {
  if (window.testRunner) {
    testRunner.dumpAsText();
    testRunner.waitUntilDone();
  }
  clickLink();
}

</script>

<a href="resources/subframe-with-form.html" id="link" target="frame">Link</a>
<br>
<iframe id="frame" name="frame" src="resources/subframe.html" onload="frameLoaded()"></iframe>

</body>
</html>
