summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/geolocation-API/non-fully-active.https.html
blob: 13853c0ef860a6d099172c91db960b720760662a (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
94
95
96
97
98
99
100
101
102
103
104
105
106
<!DOCTYPE html>
<meta charset="utf-8" />
<title>Geolocation Test: non-fully active document</title>
<link rel="help" href="https://github.com/w3c/geolocation-api/pull/97" />
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="support.js"></script>
<body></body>
<script>
  promise_test(async (t) => {
    t.add_cleanup(() => {
      return test_driver.set_permission({ name: "geolocation" }, "prompt");
    });
    await test_driver.set_permission({ name: "geolocation" }, "granted");

    // Create the iframe, wait for it to load...
    const iframe = document.createElement("iframe");
    iframe.src = "resources/iframe.html";
    iframe.allow = "geolocation";
    document.body.appendChild(iframe);

    // ...wait for the iframe to load...
    await new Promise((resolve) =>
      iframe.addEventListener("load", resolve, { 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 watchId = geo.watchPosition(
        reject, // We don't want a position
        resolve // We want an error!
      );
      // Always return 0.
      assert_equals(
        watchId,
        0,
        "watchId is 0 when document is not fully-active"
      );
      // And again, to make sure it's not changing
      const watchId2 = geo.watchPosition(
        () => {},
        () => {}
      );
      assert_equals(
        watchId2,
        0,
        "watchId remains 0 when document is not fully-active"
      );
    });

    assert_equals(
      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) => {
      geo.getCurrentPosition(
        reject, // We don't want a position
        resolve // We want an error!
      );
    });
    assert_equals(
      positionError.code,
      GeolocationPositionError.POSITION_UNAVAILABLE,
      "getCurrentPosition() calls the errorCallback with POSITION_UNAVAILABLE"
    );

    // Re-attach, and go back to fully active.
    document.body.appendChild(iframe);
    iframe.contentWindow.opener = window;
    await new Promise((resolve) =>
      iframe.addEventListener("load", resolve, { 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
      );
    });
    assert_true(Number.isInteger(watchId), "Expected some number for watchId");
    assert_true(Boolean(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
      );
    });
    assert_true(Boolean(position), "Expected a position");
    iframe.contentWindow.navigator.geolocation.clearWatch(watchId);
  }, "non-fully active document behavior");
</script>