tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

non-fully-active.https.html (3373B)


      1 <!DOCTYPE html>
      2 <meta charset="utf-8" />
      3 <title>Geolocation Test: non-fully active document</title>
      4 <link rel="help" href="https://github.com/w3c/geolocation-api/pull/97" />
      5 <script src="/resources/testharness.js"></script>
      6 <script src="/resources/testharnessreport.js"></script>
      7 <script src="/resources/testdriver.js"></script>
      8 <script src="/resources/testdriver-vendor.js"></script>
      9 <script src="support.js"></script>
     10 <body></body>
     11 <script>
     12  promise_setup(async () => {
     13    await test_driver.set_permission({ name: "geolocation" }, "granted");
     14  });
     15 
     16  promise_test(async (t) => {
     17    // Create the iframe, wait for it to load...
     18    const iframe = document.createElement("iframe");
     19    await new Promise((resolve) => {
     20      iframe.src = "resources/iframe.html";
     21      iframe.allow = "geolocation";
     22      iframe.addEventListener("load", resolve, { once: true });
     23      document.body.appendChild(iframe);
     24    });
     25 
     26    // Steal geolocation.
     27    const geo = iframe.contentWindow.navigator.geolocation;
     28 
     29    // No longer fully active.
     30    iframe.remove();
     31 
     32    // Try to watch a position while not fully active...
     33    const watchError = await new Promise((resolve, reject) => {
     34      const watchId = geo.watchPosition(
     35        reject, // We don't want a position
     36        resolve // We want an error!
     37      );
     38      // Always return 0.
     39      assert_equals(
     40        watchId,
     41        0,
     42        "watchId is 0 when document is not fully-active"
     43      );
     44      // And again, to make sure it's not changing
     45      const watchId2 = geo.watchPosition(
     46        () => {},
     47        () => {}
     48      );
     49      assert_equals(
     50        watchId2,
     51        0,
     52        "watchId remains 0 when document is not fully-active"
     53      );
     54    });
     55 
     56    assert_equals(
     57      watchError.code,
     58      GeolocationPositionError.POSITION_UNAVAILABLE,
     59      "watchPosition() returns an error on non-fully-active document"
     60    );
     61 
     62    // Now try to get current position while not fully active...
     63    const positionError = await new Promise((resolve, reject) => {
     64      geo.getCurrentPosition(
     65        reject, // We don't want a position
     66        resolve // We want an error!
     67      );
     68    });
     69    assert_equals(
     70      positionError.code,
     71      GeolocationPositionError.POSITION_UNAVAILABLE,
     72      "getCurrentPosition() calls the errorCallback with POSITION_UNAVAILABLE"
     73    );
     74 
     75    // Re-attach, and go back to fully active.
     76    document.body.appendChild(iframe);
     77    iframe.contentWindow.opener = window;
     78    await new Promise((resolve) =>
     79      iframe.addEventListener("load", resolve, { once: true })
     80    );
     81 
     82    // And we are back to fully active.
     83    let watchId;
     84    let position = await new Promise((resolve, reject) => {
     85      watchId = iframe.contentWindow.navigator.geolocation.watchPosition(
     86        resolve,
     87        reject
     88      );
     89    });
     90    assert_true(Number.isInteger(watchId), "Expected some number for watchId");
     91    assert_true(Boolean(position), "Expected a position");
     92 
     93    // Finally, let's get the position from the reattached document.
     94    position = await new Promise((resolve, reject) => {
     95      iframe.contentWindow.navigator.geolocation.getCurrentPosition(
     96        resolve,
     97        reject
     98      );
     99    });
    100    assert_true(Boolean(position), "Expected a position");
    101    iframe.contentWindow.navigator.geolocation.clearWatch(watchId);
    102  }, "non-fully active document behavior");
    103 </script>