summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/browser/browser_html_sitepermission_addons.js
blob: 3c8ce5f447bb2c57a72383d12b32e4c7c2ecca7d (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

const { AddonTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/AddonTestUtils.sys.mjs"
);
const { SITEPERMS_ADDON_PROVIDER_PREF, SITEPERMS_ADDON_TYPE } =
  ChromeUtils.importESModule(
    "resource://gre/modules/addons/siteperms-addon-utils.sys.mjs"
  );

const html = `<!DOCTYPE html><h1>Test midi permission with synthetic site permission addon</h1>`;
const EXAMPLE_COM_URL = `https://example.com/document-builder.sjs?html=${html}`;
const EXAMPLE_ORG_URL = `https://example.org/document-builder.sjs?html=${html}`;

async function uninstallAllSitePermissionAddons() {
  const sitepermAddons = await AddonManager.getAddonsByTypes([
    SITEPERMS_ADDON_TYPE,
  ]);
  for (const addon of sitepermAddons) {
    await addon.uninstall();
  }
}

add_setup(async () => {
  registerCleanupFunction(uninstallAllSitePermissionAddons);
});

add_task(async function testAboutAddonUninstall() {
  if (!AddonManager.hasProvider("SitePermsAddonProvider")) {
    ok(
      !Services.prefs.getBoolPref(SITEPERMS_ADDON_PROVIDER_PREF),
      "Expect SitePermsAddonProvider to be disabled by prefs"
    );
    ok(true, "Skip test on SitePermsAddonProvider disabled");
    return;
  }

  // Grant midi permission on example.com so about:addons does have a Site Permissions section
  await SpecialPowers.addPermission("midi-sysex", true, EXAMPLE_COM_URL);

  info("Open an about:addon tab so AMO event listeners are registered");
  const aboutAddonWin = await loadInitialView("sitepermission");
  // loadInitialView sets the about:addon as the active one, so we can grab it here.
  const aboutAddonTab = gBrowser.selectedTab;

  const addonList = aboutAddonWin.document.querySelector("addon-list");
  let addonCards = aboutAddonWin.document.querySelectorAll("addon-card");
  is(
    addonCards.length,
    1,
    "There's a card displayed for the example.com addon"
  );

  info("Open an example.org tab and install the midi site permission addon");
  const exampleOrgTab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    EXAMPLE_ORG_URL,
    true /* waitForLoad */
  );

  let promiseAddonCardAdded = BrowserTestUtils.waitForEvent(addonList, "add");

  info("Install midi");
  await testInstallGatedPermission(
    exampleOrgTab,
    () => {
      content.navigator.requestMIDIAccess();
    },
    "midi"
  );

  info("Install midi-sysex as well");
  const newAddon = await testInstallGatedPermission(
    exampleOrgTab,
    () => {
      content.navigator.requestMIDIAccess({ sysex: true });
    },
    "midi-sysex"
  );

  const newAddonId = newAddon.id;
  ok(
    newAddonId,
    "Got the addon id for the newly installed sitepermission add-on"
  );

  info("Switch back to about:addon");
  gBrowser.selectedTab = aboutAddonTab;

  await promiseAddonCardAdded;

  is(
    aboutAddonWin.document.querySelectorAll("addon-card").length,
    2,
    "A new addon card has been added as expected"
  );

  const exampleOrgAddonCard = getAddonCard(aboutAddonWin, newAddonId);

  info("Remove the example.org addon");
  const promptService = mockPromptService();
  promptService._response = 0;

  let promiseRemoved = BrowserTestUtils.waitForEvent(addonList, "remove");
  exampleOrgAddonCard.querySelector("[action=remove]").click();
  await promiseRemoved;

  is(
    aboutAddonWin.document.querySelectorAll("addon-card").length,
    1,
    "addon card has been removed as expected"
  );

  ok(
    await SpecialPowers.testPermission(
      "midi",
      SpecialPowers.Services.perms.UNKNOWN_ACTION,
      { url: EXAMPLE_ORG_URL }
    ),
    "midi permission was revoked"
  );
  ok(
    await SpecialPowers.testPermission(
      "midi-sysex",
      SpecialPowers.Services.perms.UNKNOWN_ACTION,
      { url: EXAMPLE_ORG_URL }
    ),
    "midi-sysex permission was revoked as well"
  );

  await BrowserTestUtils.removeTab(exampleOrgTab);
  await close_manager(aboutAddonWin);
  await uninstallAllSitePermissionAddons();
});

/**
 *
 * Execute a function in the tab content page and check that the expected gated permission
 * is set
 *
 * @param {Tab} tab: The tab in which we want to install the gated permission
 * @param {Function} spawnCallback: function used in `SpecialPowers.spawn` that will trigger
 *                                  the install
 * @param {String} expectedPermType: The name of the permission that should be granted
 * @returns {Promise<Addon>} The installed addon instance
 */
async function testInstallGatedPermission(
  tab,
  spawnCallback,
  expectedPermType
) {
  let onInstallEnded = AddonTestUtils.promiseInstallEvent("onInstallEnded");
  let onAddonInstallBlockedNotification = promisePopupNotificationShown(
    "addon-install-blocked"
  );
  await SpecialPowers.spawn(tab.linkedBrowser, [], spawnCallback);

  let addonInstallPanel = await onAddonInstallBlockedNotification;
  let dialogPromise = promisePopupNotificationShown("addon-webext-permissions");
  addonInstallPanel.button.click();
  let installPermsDialog = await dialogPromise;
  installPermsDialog.button.click();

  const addon = await onInstallEnded.then(install => install[0].addon);
  // Close the addon-installed dialog to avoid interfering with other tests
  await acceptAppMenuNotificationWhenShown("addon-installed", addon.id);

  ok(
    await SpecialPowers.testPermission(
      expectedPermType,
      SpecialPowers.Services.perms.ALLOW_ACTION,
      { url: EXAMPLE_ORG_URL }
    ),
    `"${expectedPermType}" permission was granted`
  );
  return addon;
}