<!DOCTYPE html>
<title>Test fenced frame navigations (by a parent frame setting its src). </title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="resources/utils.js"></script>

<body>

<script>
promise_test(async () => {
  // This test checks that fenced frames can be navigated (given a new src).
  // The access pattern is as follows, to exercise same- and cross-origin
  // navigations in root and nested fenced frames.

  // [root]                 [nested]
  // simple (origin 1)
  // create-nested (origin 2)
  //                        simple (origin 1)
  //                        simple (origin 2)
  //                        simple (origin 2)
  // simple (origin 2)

  const navigation_key = token();
  const navigation_ack_key = token();

  // Create URL prefixes to simulate different origins.
  // (www1 and www2 are different origins)
  const simple_url = generateURL(
      "resources/frame-navigation-inner-simple.https.html",
      [navigation_key, navigation_ack_key]);
  const nested_url = generateURL(
      "resources/frame-navigation-inner-create-nested.https.html",
      [navigation_key, navigation_ack_key]);
  const origin1_simple_url = getRemoteOriginURL(simple_url);
  const origin2_nested_url = getRemoteOriginURL(nested_url)
      .toString().replace("www1", "www2");
  const origin2_simple_url = getRemoteOriginURL(simple_url)
      .toString().replace("www1", "www2");

  // Create a root fenced frame.
  root_frame = attachFencedFrame(origin1_simple_url);
  const root_load1_actual = await nextValueFromServer(navigation_key);
  const root_load1_expected = "pass";
  assert_equals(root_load1_actual, root_load1_expected,
                "The 1st root fenced frame navigation succeeded");

  // Navigate the root fenced frame to a second site (which will nest).
  root_frame.config = new FencedFrameConfig(origin2_nested_url);
  const root_load2_actual = await nextValueFromServer(navigation_key);
  const root_load2_expected = "create-nested";
  assert_equals(root_load2_actual, root_load2_expected,
      "The 2nd root fenced frame navigation (cross-origin) succeeded");
  writeValueToServer(navigation_ack_key, "ACK");

  // Check that all of the nested navigations succeed.
  const nested_load1_actual = await nextValueFromServer(navigation_key);
  const nested_load1_expected = "pass";
  assert_equals(nested_load1_actual, nested_load1_expected,
                "The 1st nested fenced frame navigation succeeded");
  writeValueToServer(navigation_ack_key, "ACK");

  const nested_load2_actual = await nextValueFromServer(navigation_key);
  const nested_load2_expected = "pass";
  assert_equals(nested_load2_actual, nested_load2_expected,
      "The 2nd nested fenced frame navigation (cross-origin) succeeded");
  writeValueToServer(navigation_ack_key, "ACK");

  const nested_load3_actual = await nextValueFromServer(navigation_key);
  const nested_load3_expected = "pass";
  assert_equals(nested_load3_actual, nested_load3_expected,
         "The 3rd nested fenced frame navigation (same-origin) succeeded");
  writeValueToServer(navigation_ack_key, "ACK");

  // Navigate the root fenced frame.
  root_frame.config = root_frame.config = new FencedFrameConfig(origin2_simple_url);
  const root_load3_actual = await nextValueFromServer(navigation_key);
  const root_load3_expected = "pass";
  assert_equals(root_load3_actual, root_load3_expected,
                "The 3rd root fenced frame navigation (same-origin) succeeded");

}, "Fenced frame navigation should succeed");
</script>

</body>
