<!DOCTYPE html>
<title>Test fenced frame does not allow call background fetch</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/utils.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="/common/utils.js"></script>

<body>

  <script>
    const background_fetch_register_key = token();

    async function test(url, expected, message) {
      attachFencedFrame(url)

      // Get the result for the top-level fenced frame.
      const fenced_frame_result = await nextValueFromServer(
          background_fetch_register_key);
      assert_equals(fenced_frame_result, expected, message);
    }

    promise_test(async () => {
      const method = "fetch";
      const fenced_frame_url = 'resources/background-fetch-inner.https.html';
      const url = generateURL(fenced_frame_url,
                              [background_fetch_register_key, method]);

      await test(
        url,
        '[backgroundFetch.fetch] Failed inside fencedframe as expected',
        'backgroundFetch.fetch is disallowed inside a same-origin fenced frame');

      const CROSS_ORIGIN_DESTINATION = getRemoteOriginURL(url);
      await test(
        CROSS_ORIGIN_DESTINATION,
        '[backgroundFetch.fetch] Failed inside fencedframe as expected',
        'backgroundFetch.fetch is disallowed inside a cross-origin fenced frame');
    }, 'backgroundFetch.fetch');

    promise_test(async () => {
      const method = "get";
      const fenced_frame_url = 'resources/background-fetch-inner.https.html';
      const url = generateURL(fenced_frame_url,
                              [background_fetch_register_key, method]);
      await test(
        url,
        '[backgroundFetch.get] Failed inside fencedframe as expected',
        'backgroundFetch.get is disallowed inside a same-origin fenced frame');

      const CROSS_ORIGIN_DESTINATION = getRemoteOriginURL(url);
      await test(
        CROSS_ORIGIN_DESTINATION,
        '[backgroundFetch.get] Failed inside fencedframe as expected',
        'backgroundFetch.get is disallowed inside a cross-origin fenced frame');
    }, 'backgroundFetch.get');

    promise_test(async () => {
      const method = "getIds";
      const fenced_frame_url = 'resources/background-fetch-inner.https.html';
      const url = generateURL(fenced_frame_url,
                              [background_fetch_register_key, method]);
      await test(
        url,
        '[backgroundFetch.getIds] Failed inside fencedframe as expected',
        'backgroundFetch.getIds is disallowed inside a same-origin fenced frame');

      const CROSS_ORIGIN_DESTINATION = getRemoteOriginURL(url);
      await test(
        CROSS_ORIGIN_DESTINATION,
        '[backgroundFetch.getIds] Failed inside fencedframe as expected',
        'backgroundFetch.getIds is disallowed inside a cross-origin fenced frame');
    }, 'backgroundFetch.getIds');

    promise_test(async () => {
      const method = "fetch";
      const fenced_frame_url = 'resources/background-fetch-sw-inner.https.html';
      const url = generateURL(fenced_frame_url,
                              [background_fetch_register_key, method]);

      await test(
        url,
        '[backgroundFetch.fetch] Failed inside fencedframe as expected',
        'backgroundFetch.fetch is disallowed inside a same-origin fenced frame');

      const CROSS_ORIGIN_DESTINATION = getRemoteOriginURL(url);
      await test(
        CROSS_ORIGIN_DESTINATION,
        '[backgroundFetch.fetch] Failed inside fencedframe as expected',
        'backgroundFetch.fetch is disallowed inside a cross-origin fenced frame');
    }, 'backgroundFetch.fetch in service worker');

    promise_test(async () => {
      const method = "get";
      const fenced_frame_url = 'resources/background-fetch-sw-inner.https.html';
      const url = generateURL(fenced_frame_url,
                              [background_fetch_register_key, method]);
      await test(
        url,
        '[backgroundFetch.get] Failed inside fencedframe as expected',
        'backgroundFetch.get is disallowed inside a same-origin fenced frame');

      const CROSS_ORIGIN_DESTINATION = getRemoteOriginURL(url);
      await test(
        CROSS_ORIGIN_DESTINATION,
        '[backgroundFetch.get] Failed inside fencedframe as expected',
        'backgroundFetch.get is disallowed inside a cross-origin fenced frame');
    }, 'backgroundFetch.get in service worker');

    promise_test(async () => {
      const method = "getIds";
      const fenced_frame_url = 'resources/background-fetch-sw-inner.https.html';
      const url = generateURL(fenced_frame_url,
                              [background_fetch_register_key, method]);
      await test(
        url,
        '[backgroundFetch.getIds] Failed inside fencedframe as expected',
        'backgroundFetch.getIds is disallowed inside a same-origin fenced frame');

      const CROSS_ORIGIN_DESTINATION = getRemoteOriginURL(url);
      await test(
        CROSS_ORIGIN_DESTINATION,
        '[backgroundFetch.getIds] Failed inside fencedframe as expected',
        'backgroundFetch.getIds is disallowed inside a cross-origin fenced frame');
    }, 'backgroundFetch.getIds in service worker');
  </script>

</body>
