<!-- webkit-test-runner [ ShowModalDialogEnabled=true ] -->
<!DOCTYPE html>
<html>
<body>
<b>This test passes if it does not alert the fail.html's content when clicking the button.</b><br>
<input id="testButton" type="button" value="Click me"></input>
<script>
if (window.testRunner) {
    testRunner.dumpAsText();
    testRunner.waitUntilDone();
}

let counter = 0;
function run() {
  if (++counter == 2) {
    parent_frame = frame.contentDocument.querySelector("iframe");
    frame1 = parent_frame.appendChild(document.createElement("iframe"));
    frame2 = parent_frame.appendChild(document.createElement("iframe"));
    frame1.src = "javascript:top.runChild()";
  }
}

let child_counter = 0;
function runChild() {
  if (++child_counter == 2) {
    parent_frame.appendChild(frame2);

    a = frame2.contentDocument.createElement("a");
    a.href = cache_frame.src;
    a.click();
    
    showModalDialog(URL.createObjectURL(new Blob([`
      <script>
        let intervalID = setInterval(() => {
          try {
            opener.frame.document.foo;
          } catch (e) {
            clearInterval(intervalID);

            window.close();
            if (window.testRunner)
              testRunner.abortModal();
          }
        }, 100);
      </scr` + "ipt>"], {type: "text/html"})));
    frame2.src = "javascript:alert(document.documentElement.outerHTML)";
  }
}

testButton.onclick = _ => {
  frame = document.body.appendChild(document.createElement("iframe"));
  frame.contentWindow.location = `javascript:'<b><p><iframe`
      + ` src="javascript:top.run()"></iframe></b></p>'`;
  setTimeout(() => {
        setTimeout(() => {
            if (window.testRunner)
                testRunner.notifyDone();
        }, 0);
    }, 0);
}

cache_frame = document.body.appendChild(document.createElement("iframe"));
cache_frame.src = "http://localhost:8000/security/resources/fail.html";
cache_frame.style.display = "none";

onload = function() {
    if (!window.internals)
       return;

    internals.withUserGesture(() => {
        testButton.click();
    });
}
</script>
</body>
</html>
