summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/geolocation/test_not_fully_active.html
blob: 6fc7689c8c83e601bae45b6c1c84736e852ace16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<!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>