<!DOCTYPE html>
<meta charset="utf-8">
<title>Entry settings object for promise jobs when the function realm is different from the test realm</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<!-- https://github.com/whatwg/html/pull/5212 -->
<!-- https://github.com/whatwg/html/issues/1426 -->

<!-- This is what would normally be considered the entry page. However, we use functions from the
     resources/function/function.html realm. So window.open() should resolve relative to that realm
     inside promise jobs. -->

<iframe src="resources/promise-job-entry-incumbent.html"></iframe>
<iframe src="resources/function/function.html" id="function-frame"></iframe>

<script>
setup({ explicit_done: true });

const relativeURL = "resources/window-to-open.html";
const expectedURL = (new URL(relativeURL, document.querySelector("#function-frame").src)).href;

const incumbentWindow = frames[0];
const functionWindow = frames[1];
const FunctionFromAnotherWindow = frames[1].Function;

window.onload = () => {
  async_test(t => {
    const func = FunctionFromAnotherWindow(`
      const [incumbentWindow, relativeURL, t, assert_equals, expectedURL] = arguments[0];

      const w = incumbentWindow.runWindowOpenVeryIndirectly(relativeURL);
      w.onload = t.step_func_done(() => {
        t.add_cleanup(() => w.close());
        assert_equals(w.location.href, expectedURL);
      });
    `);

    Promise.resolve([incumbentWindow, relativeURL, t, assert_equals, expectedURL]).then(func);
  }, "Fulfillment handler on fulfilled promise");

  async_test(t => {
    const func = FunctionFromAnotherWindow(`
      const [incumbentWindow, relativeURL, t, assert_equals, expectedURL] = arguments[0];

      const w = incumbentWindow.runWindowOpenVeryIndirectly(relativeURL);
      w.onload = t.step_func_done(() => {
        t.add_cleanup(() => w.close());
        assert_equals(w.location.href, expectedURL);
      });
    `);

    Promise.reject([incumbentWindow, relativeURL, t, assert_equals, expectedURL]).catch(func);
  }, "Rejection handler on rejected promise");

  async_test(t => {
    let resolve;
    const p = new Promise(r => { resolve = r; });

    const func = FunctionFromAnotherWindow(`
      const [incumbentWindow, relativeURL, t, assert_equals, expectedURL] = arguments[0];

      const w = incumbentWindow.runWindowOpenVeryIndirectly(relativeURL);
      w.onload = t.step_func_done(() => {
        t.add_cleanup(() => w.close());
        assert_equals(w.location.href, expectedURL);
      });
    `);

    p.then(func);
    t.step_timeout(() => resolve([incumbentWindow, relativeURL, t, assert_equals, expectedURL]), 0);
  }, "Fulfillment handler on pending-then-fulfilled promise");

  async_test(t => {
    let reject;
    const p = new Promise((_, r) => { reject = r; });

    const func = FunctionFromAnotherWindow(`
      const [incumbentWindow, relativeURL, t, assert_equals, expectedURL] = arguments[0];

      const w = incumbentWindow.runWindowOpenVeryIndirectly(relativeURL);
      w.onload = t.step_func_done(() => {
        t.add_cleanup(() => w.close());
        assert_equals(w.location.href, expectedURL);
      });
    `);

    p.catch(func);
    t.step_timeout(() => reject([incumbentWindow, relativeURL, t, assert_equals, expectedURL]), 0);
  }, "Rejection handler on pending-then-rejected promise");

  async_test(t => {
    t.add_cleanup(() => { delete frames[1].args; });
    frames[1].args = [incumbentWindow, relativeURL, t, assert_equals, expectedURL];

    const func = FunctionFromAnotherWindow(`
      const [incumbentWindow, relativeURL, t, assert_equals, expectedURL] = window.args;

      const w = incumbentWindow.runWindowOpenVeryIndirectly(relativeURL);
      w.onload = t.step_func_done(() => {
        t.add_cleanup(() => w.close());
        assert_equals(w.location.href, expectedURL);
      });
    `);

    const thenable = { then: func };

    Promise.resolve(thenable);
  }, "Thenable resolution");

  done();
};
</script>
