<!DOCTYPE html>
<html>
  <head>
    <title>
      Test for when geolocation is used on non fully active documents
    </title>
    <script src="/tests/SimpleTest/SimpleTest.js"></script>
    <script src="geolocation_common.js"></script>
    <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  </head>
  <body>
    <script>
      SimpleTest.waitForExplicitFinish();

      async function runTest() {
        // Create the iframe, wait for it to load...
        const iframe = document.createElement("iframe");

        // We rely on this popup.html to acquire prompt privileges.
        iframe.src = "popup.html";
        document.body.appendChild(iframe);
        iframe.contentWindow.opener = window;
        await new Promise(r => window.addEventListener("message", r, { once: true }));

        // Steal geolocation.
        const geo = iframe.contentWindow.navigator.geolocation;

        // No longer fully active.
        iframe.remove();

        // Try to watch a position while not fully active...
        const watchError = await new Promise((resolve, reject) => {
          const result = geo.watchPosition(
            reject, // We don't want a position
            resolve // We want an error!
          );
          is(result, 0, "watchPosition returns 0 on non-fully-active document");
        });
        is(
          watchError.code,
          GeolocationPositionError.POSITION_UNAVAILABLE,
          "watchPosition returns an error on non-fully-active document"
        );

        // Now try to get current position while not fully active...
        const positionError = await new Promise((resolve, reject) => {
          const result = geo.getCurrentPosition(
            reject, // We don't want a position
            resolve // We want an error!
          );
        });
        is(
          positionError.code,
          GeolocationPositionError.POSITION_UNAVAILABLE,
          "getCurrentPosition returns an error on non-fully-active document"
        );

        // Re-attach, and go back to fully active.
        document.body.appendChild(iframe);
        iframe.contentWindow.opener = window;
        await new Promise(r => window.addEventListener("message", r, { once: true }));

        // And we are back to fully active.
        let watchId;
        let position = await new Promise((resolve, reject) => {
          watchId = iframe.contentWindow.navigator.geolocation.watchPosition(
            resolve,
            reject
          );
        });
        ok(watchId > 0, "Expected anything greater than 0");
        ok(position, "Expected a position");

        // Finally, let's get the position from the reattached document.
        position = await new Promise((resolve, reject) => {
          iframe.contentWindow.navigator.geolocation.getCurrentPosition(
            resolve,
            reject
          );
        });
        ok(position, "Expected a position");
        iframe.contentWindow.navigator.geolocation.clearWatch(watchId);
        iframe.remove();
      }

      resume_geolocationProvider(async () => {
        await new Promise(r => force_prompt(true, r));
        await runTest();
        SimpleTest.finish();
      });
    </script>
  </body>
</html>
