summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/permissions/browser_temporary_permissions_expiry.js
blob: 6426f2fef38d9554023058347353612841d2532a (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
109
110
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */
/* eslint-disable mozilla/no-arbitrary-setTimeout */

"use strict";

const ORIGIN = "https://example.com";
const PERMISSIONS_PAGE =
  getRootDirectory(gTestPath).replace("chrome://mochitests/content", ORIGIN) +
  "permissions.html";

// Ignore promise rejection caused by clicking Deny button.
const { PromiseTestUtils } = ChromeUtils.import(
  "resource://testing-common/PromiseTestUtils.jsm"
);
PromiseTestUtils.allowMatchingRejectionsGlobally(/The request is not allowed/);

const EXPIRE_TIME_MS = 100;
const TIMEOUT_MS = 500;

const kVREnabled = SpecialPowers.getBoolPref("dom.vr.enabled");

// Test that temporary permissions can be re-requested after they expired
// and that the identity block is updated accordingly.
add_task(async function testTempPermissionRequestAfterExpiry() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["privacy.temporary_permission_expire_time_ms", EXPIRE_TIME_MS],
      ["media.navigator.permission.fake", true],
      ["dom.vr.always_support_vr", true],
    ],
  });

  let principal = Services.scriptSecurityManager.createContentPrincipalFromOrigin(
    ORIGIN
  );
  let ids = ["geo", "camera"];

  if (kVREnabled) {
    ids.push("xr");
  }

  for (let id of ids) {
    await BrowserTestUtils.withNewTab(PERMISSIONS_PAGE, async function(
      browser
    ) {
      let blockedIcon = gIdentityHandler._identityBox.querySelector(
        `.blocked-permission-icon[data-permission-id='${id}']`
      );

      SitePermissions.setForPrincipal(
        principal,
        id,
        SitePermissions.BLOCK,
        SitePermissions.SCOPE_TEMPORARY,
        browser
      );

      Assert.deepEqual(
        SitePermissions.getForPrincipal(principal, id, browser),
        {
          state: SitePermissions.BLOCK,
          scope: SitePermissions.SCOPE_TEMPORARY,
        }
      );

      ok(
        blockedIcon.hasAttribute("showing"),
        "blocked permission icon is shown"
      );

      await new Promise(c => setTimeout(c, TIMEOUT_MS));

      Assert.deepEqual(
        SitePermissions.getForPrincipal(principal, id, browser),
        {
          state: SitePermissions.UNKNOWN,
          scope: SitePermissions.SCOPE_PERSISTENT,
        }
      );

      let popupshown = BrowserTestUtils.waitForEvent(
        PopupNotifications.panel,
        "popupshown"
      );

      // Request a permission;
      await BrowserTestUtils.synthesizeMouseAtCenter(`#${id}`, {}, browser);

      await popupshown;

      ok(
        !blockedIcon.hasAttribute("showing"),
        "blocked permission icon is not shown"
      );

      let popuphidden = BrowserTestUtils.waitForEvent(
        PopupNotifications.panel,
        "popuphidden"
      );

      let notification = PopupNotifications.panel.firstElementChild;
      EventUtils.synthesizeMouseAtCenter(notification.secondaryButton, {});

      await popuphidden;

      SitePermissions.removeFromPrincipal(principal, id, browser);
    });
  }
});