<!doctype html>
<title>PaymentRequest setting allow="payment" after load and then navigating</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<div id="log"></div>
<script>
async_test((t) => {
  const iframe = document.createElement('iframe');
  // no allow="payment" attribute

  let i = 0;

  const path = location.pathname.substring(0, location.pathname.lastIndexOf('/') + 1);
  iframe.src = "https://{{domains[www1]}}:{{ports[https][0]}}" + path + "echo-PaymentRequest.html";
  iframe.onload = t.step_func(() => {
    if (i === 0) {
      iframe.allow = "payment";
    }
    iframe.contentWindow.postMessage('What is the result of new PaymentRequest(...)?', '*');
  });

  window.onmessage = t.step_func((e) => {
    // Ignore messages that are not part of the test.
    if (e.source != iframe.contentWindow) return;

    i++;
    if (i === 1) {
      assert_equals(e.data.message, 'Exception', 'before navigation');
      assert_equals(4, e.data.details.length);
      // The last entry is the error stacktrace. Ignore it in comparison.
      assert_array_equals(e.data.details.slice(0, 3), [true /* ex instanceof DOMException*/, 18 /* ex.code */, 'SecurityError' /* ex.name */], 'before navigation');

      // Navigate the iframe. This will fire a second 'load' event on the iframe.
      iframe.contentWindow.location.href = iframe.src + '?2';
    } else {
      assert_equals(e.data.message, 'Success', 'after navigation');
      t.done();
    }
  });

  document.body.appendChild(iframe);
});
</script>
