<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Bug 1639195 - Test triggeringPrincipal for iframe same-origin navigations</title>
  <!-- Including SimpleTest.js so we can use waitForExplicitFinish !-->
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<iframe style="width:100%;" id="testframe" src="http://example.com/"></iframe>

<script type="text/javascript">
/* We load an third-party iframe which then gets navigated by the iframe's
 * parent by calling iframe.setAttribute("src", same-origin url) later in the
 * test. We then verify the TriggeringPrincipal and LoadingPrincipal of the
 * navigated iframe.
 *
 * +------------------------------------------+
 * |                                          |
 * |  +------------------+                    |
 * |  | testframe        |                    |
 * |  +------------------+                    |
 * |                                          |
 * | iframe.setAttribute("src",               |
 * |                     same-origin url)     |
 * |                                          |
 * +------------------------------------------+
 */

var testframe = document.getElementById("testframe");

window.addEventListener("message", receiveMessage);

const TRIGGERING_PRINCIPAL_URI =
  "http://mochi.test:8888/tests/docshell/test/navigation/test_triggeringprincipal_frame_same_origin_nav.html";

const LOADING_PRINCIPAL_URI = TRIGGERING_PRINCIPAL_URI;

function receiveMessage(event) {
  is(event.data.triggeringPrincipalURI.split("?")[0], TRIGGERING_PRINCIPAL_URI,
     "TriggeringPrincipal should be the parent iframe");
  is(event.data.loadingPrincipalURI.split("?")[0], TRIGGERING_PRINCIPAL_URI,
     "LoadingPrincipal should be the parent iframe");

  window.removeEventListener("message", receiveMessage);
  SimpleTest.finish();
}

function performNavigation() {
  testframe.removeEventListener("load", performNavigation);
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  testframe.setAttribute("src", "http://example.com/tests/docshell/test/navigation/file_triggeringprincipal_subframe_same_origin_nav.html");
}

// start the test
SimpleTest.waitForExplicitFinish();

testframe.addEventListener("load", performNavigation);
</script>

</body>
</html>
