summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/permissions/non-fully-active.https.html
blob: 1c11afa3d94936ce8691a82dda052942f81b1cc2 (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
107
108
<!DOCTYPE html>
<meta charset="utf-8" />
<title>Geolocation Test: non-fully active document</title>
<link rel="help" href="https://github.com/w3c/permissions/pull/365" />
<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>
<body></body>
<script>
  // Creates the iframe, wait for it to load...
  async function attachIframe() {
    const iframe = document.createElement("iframe");
    await new Promise((resolve) => {
      iframe.src = "resources/empty.html";
      iframe.addEventListener("load", resolve, { once: true });
      document.body.appendChild(iframe);
    });
    return iframe;
  }

  promise_test(async (t) => {
    const iframe = await attachIframe();

    // Steal the needed references
    const { permissions } = iframe.contentWindow.navigator;
    const TypeErrorCtor = iframe.contentWindow.TypeError;
    const DOMExceptionCtor = iframe.contentWindow.DOMException;

    // Let's check that ordering is correct.
    await promise_rejects_js(
      t,
      TypeErrorCtor,
      permissions.query({ name: "xxxxx-not-supported" }),
      "query() should reject if the feature is not supported"
    );

    // no longer fully active, let's try that again...
    iframe.remove();

    // Now, let's try with Geolocation as it's supported by all browsers.
    await promise_rejects_dom(
      t,
      "InvalidStateError",
      DOMExceptionCtor,
      permissions.query({ name: "whatever" }),
      "must reject in the right global when the document is not fully active"
    );

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

    // And we are back to fully active, so this should not reject.
    const status = await iframe.contentWindow.navigator.permissions.query({
      name: "geolocation",
    });
    assert_equals(status.name, "geolocation");
    iframe.remove();
  }, "Trying to query() a non-fully active document rejects with a InvalidStateError");

  promise_test(async (t) => {
    // Create the iframe, wait for it to load...
    const iframe = await attachIframe();

    // Get the status
    const initialStatus =
      await iframe.contentWindow.navigator.permissions.query({
        name: "geolocation",
      });

    assert_true("onchange" in initialStatus, "onchange is supported");

    // Let's check events are firing...
    await new Promise(async (resolve) => {
      const newState = initialStatus.state === "prompt" ? "denied" : "granted";
      initialStatus.addEventListener("change", resolve, { once: true });
      await test_driver.set_permission({ name: "geolocation" }, newState);
    });

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

    await new Promise(async (resolve, reject) => {
      // Gets dropped on the floor.
      initialStatus.addEventListener("change", reject, { once: true });
      await test_driver.set_permission({ name: "geolocation" }, "prompt");
      // Try to force it synthetically. This would trigger the event synchronously.
      initialStatus.dispatchEvent(new Event("change"));
      resolve();
    });

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

    // Finally, let's recheck that permission state.
    const finalStatus = await iframe.contentWindow.navigator.permissions.query({
      name: "geolocation",
    });
    assert_equals(finalStatus.state, "prompt", "expected prompt state");
    iframe.remove();
  }, "Permission change events shouldn't fire on non-fully active document");
</script>