summaryrefslogtreecommitdiffstats
path: root/dom/base/test/chrome/test_permission_hasValidTransientUserActivation.xhtml
blob: 06675a90ad5876335264bd11367f799e1ce2a05d (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
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
<!--
  Tests that the hasValidTransientUserGestureActivation attribute on permission requests is set correctly.
-->
<window title="hasValidTransientUserGestureActivation test" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>

  <body xmlns="http://www.w3.org/1999/xhtml">
    <iframe id="frame" src="https://example.com/chrome/dom/base/test/chrome/dummy.html" />
  </body>

  <script type="application/javascript">
  <![CDATA[
  const {Integration} = ChromeUtils.import("resource://gre/modules/Integration.jsm");

  SimpleTest.waitForExplicitFinish();

  let frame = document.getElementById("frame");

  function checkPermissionRequest(permission, hasValidTransientUserGestureActivation) {
    return new Promise(function(resolve) {
      let TestIntegration = (base) => ({
        __proto__: base,
        createPermissionPrompt(type, request) {
          is(type, permission, `Has correct permission type ${permission}.`);
          is(request.hasValidTransientUserGestureActivation, hasValidTransientUserGestureActivation,
             "The hasValidTransientUserGestureActivation attribute is set correctly.");
          Integration.contentPermission.unregister(TestIntegration);
          resolve();
          return { prompt() {} };
        },
      });
      Integration.contentPermission.register(TestIntegration);
    });
  }

  async function runTest() {
    await SpecialPowers.setBoolPref("dom.webnotifications.allowcrossoriginiframe", true);

    // Test programmatic request for persistent storage.
    let request = checkPermissionRequest("persistent-storage", false);
    navigator.storage.persist();
    await request;

    // Test user-initiated request for persistent storage.
    request = checkPermissionRequest("persistent-storage", true);
    document.notifyUserGestureActivation();
    navigator.storage.persist();
    await request;
    content.document.clearUserGestureActivation();

    // Test programmatic request for geolocation.
    request = checkPermissionRequest("geolocation", false);
    navigator.geolocation.getCurrentPosition(() => {});
    await request;

    // Test user-initiated request for geolocation.
    request = checkPermissionRequest("geolocation", true);
    document.notifyUserGestureActivation();
    navigator.geolocation.getCurrentPosition(() => {});
    await request;
    document.clearUserGestureActivation();

    // Notifications need to be tested in an HTTPS frame, because
    // chrome:// URLs are whitelisted.
    let frameWin = frame.contentWindow;

    // Test programmatic request for notifications.
    request = checkPermissionRequest("desktop-notification", false);
    frameWin.Notification.requestPermission();
    await request;

    // Test user-initiated request for notifications.
    request = checkPermissionRequest("desktop-notification", true);
    frameWin.document.notifyUserGestureActivation();
    frameWin.Notification.requestPermission();
    await request;
    frameWin.document.clearUserGestureActivation();

    await SpecialPowers.clearUserPref("dom.webnotifications.allowcrossoriginiframe");
  }

  frame.addEventListener("load", function() {
    runTest().then(() => SimpleTest.finish());
  });
  ]]>
  </script>
</window>