summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/popups/browser_popup_blocker_identity_block.js
blob: 30137adf5dbb992016bcfbd2babf299493dedd7e (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"use strict";

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const { SitePermissions } = ChromeUtils.import(
  "resource:///modules/SitePermissions.jsm"
);
const { PermissionTestUtils } = ChromeUtils.import(
  "resource://testing-common/PermissionTestUtils.jsm"
);

const baseURL = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  "http://example.com"
);
const URL = baseURL + "popup_blocker2.html";
const URI = Services.io.newURI(URL);
const PRINCIPAL = Services.scriptSecurityManager.createContentPrincipal(
  URI,
  {}
);

function openPermissionPopup() {
  let promise = BrowserTestUtils.waitForEvent(
    window,
    "popupshown",
    true,
    event => event.target == gPermissionPanel._permissionPopup
  );
  gPermissionPanel._identityPermissionBox.click();
  return promise;
}

function closePermissionPopup() {
  let promise = BrowserTestUtils.waitForEvent(
    gPermissionPanel._permissionPopup,
    "popuphidden"
  );
  gPermissionPanel._permissionPopup.hidePopup();
  return promise;
}

add_task(async function enable_popup_blocker() {
  // Enable popup blocker.
  await SpecialPowers.pushPrefEnv({
    set: [["dom.disable_open_during_load", true]],
  });
  await SpecialPowers.pushPrefEnv({
    set: [["dom.disable_open_click_delay", 0]],
  });
});

add_task(async function check_blocked_popup_indicator() {
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, URL);

  // Blocked popup indicator should not exist in the identity popup when there are no blocked popups.
  await openPermissionPopup();
  Assert.equal(document.getElementById("blocked-popup-indicator-item"), null);
  await closePermissionPopup();

  // Blocked popup notification icon should be hidden in the identity block when no popups are blocked.
  let icon = gPermissionPanel._identityPermissionBox.querySelector(
    ".blocked-permission-icon[data-permission-id='popup']"
  );
  Assert.equal(icon.hasAttribute("showing"), false);

  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async () => {
    let open = content.document.getElementById("pop");
    open.click();
  });

  // Wait for popup block.
  await TestUtils.waitForCondition(() =>
    gBrowser.getNotificationBox().getNotificationWithValue("popup-blocked")
  );

  // Check if blocked popup indicator text is visible in the identity popup. It should be visible.
  document.getElementById("identity-permission-box").click();
  await openPermissionPopup();
  await TestUtils.waitForCondition(
    () => document.getElementById("blocked-popup-indicator-item") !== null
  );

  // Check that the default state is correctly set to "Block".
  let menulist = document.getElementById("permission-popup-menulist");
  Assert.equal(menulist.value, "0");
  Assert.equal(menulist.label, "Block");

  await closePermissionPopup();

  // Check if blocked popup icon is visible in the identity block.
  Assert.equal(icon.getAttribute("showing"), "true");

  gBrowser.removeTab(tab);
});

// Check if clicking on "Show blocked popups" shows blocked popups.
add_task(async function check_popup_showing() {
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, URL);

  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async () => {
    let open = content.document.getElementById("pop");
    open.click();
  });

  // Wait for popup block.
  await TestUtils.waitForCondition(() =>
    gBrowser.getNotificationBox().getNotificationWithValue("popup-blocked")
  );

  // Store the popup that opens in this array.
  let popup;
  function onTabOpen(event) {
    popup = event.target;
  }
  gBrowser.tabContainer.addEventListener("TabOpen", onTabOpen);

  // Open identity popup and click on "Show blocked popups".
  await openPermissionPopup();
  let e = document.getElementById("blocked-popup-indicator-item");
  let text = e.getElementsByTagName("label")[0];
  text.click();

  await BrowserTestUtils.waitForEvent(gBrowser.tabContainer, "TabOpen");
  await TestUtils.waitForCondition(
    () => popup.linkedBrowser.currentURI.spec != "about:blank"
  );

  gBrowser.tabContainer.removeEventListener("TabOpen", onTabOpen);

  ok(
    popup.linkedBrowser.currentURI.spec.endsWith("popup_blocker_a.html"),
    "Popup a"
  );

  gBrowser.removeTab(popup);
  gBrowser.removeTab(tab);
});

// Test if changing menulist values of blocked popup indicator changes permission state and popup behavior.
add_task(async function check_permission_state_change() {
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, URL);

  // Initially the permission state is BLOCK for popups (set by the prefs).
  let state = SitePermissions.getForPrincipal(PRINCIPAL, "popup", gBrowser)
    .state;
  Assert.equal(state, SitePermissions.BLOCK);

  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async () => {
    let open = content.document.getElementById("pop");
    open.click();
  });

  // Wait for popup block.
  await TestUtils.waitForCondition(() =>
    gBrowser.getNotificationBox().getNotificationWithValue("popup-blocked")
  );

  // Open identity popup and change permission state to allow.
  await openPermissionPopup();
  let menulist = document.getElementById("permission-popup-menulist");
  menulist.menupopup.openPopup(); // Open the allow/block menu
  let menuitem = menulist.getElementsByTagName("menuitem")[0];
  menuitem.click();
  await closePermissionPopup();

  state = SitePermissions.getForPrincipal(PRINCIPAL, "popup", gBrowser).state;
  Assert.equal(state, SitePermissions.ALLOW);

  // Store the popup that opens in this array.
  let popup;
  function onTabOpen(event) {
    popup = event.target;
  }
  gBrowser.tabContainer.addEventListener("TabOpen", onTabOpen);

  // Check if a popup opens.
  await Promise.all([
    SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
      let open = content.document.getElementById("pop");
      open.click();
    }),
    BrowserTestUtils.waitForEvent(gBrowser.tabContainer, "TabOpen"),
  ]);
  await TestUtils.waitForCondition(
    () => popup.linkedBrowser.currentURI.spec != "about:blank"
  );

  gBrowser.tabContainer.removeEventListener("TabOpen", onTabOpen);

  ok(
    popup.linkedBrowser.currentURI.spec.endsWith("popup_blocker_a.html"),
    "Popup a"
  );

  gBrowser.removeTab(popup);

  // Open identity popup and change permission state to block.
  await openPermissionPopup();
  menulist = document.getElementById("permission-popup-menulist");
  menulist.menupopup.openPopup(); // Open the allow/block menu
  menuitem = menulist.getElementsByTagName("menuitem")[1];
  menuitem.click();
  await closePermissionPopup();

  // Clicking on the "Block" menuitem should remove the permission object(same behavior as UNKNOWN state).
  // We have already confirmed that popups are blocked when the permission state is BLOCK.
  state = SitePermissions.getForPrincipal(PRINCIPAL, "popup", gBrowser).state;
  Assert.equal(state, SitePermissions.BLOCK);

  gBrowser.removeTab(tab);
});

// Explicitly set the permission to the otherwise default state and check that
// the label still displays correctly.
add_task(async function check_explicit_default_permission() {
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, URL);

  // DENY only works if triggered through Services.perms (it's very edge-casey),
  // since SitePermissions.jsm considers setting default permissions to be removal.
  PermissionTestUtils.add(URI, "popup", Ci.nsIPermissionManager.DENY_ACTION);

  await openPermissionPopup();
  let menulist = document.getElementById("permission-popup-menulist");
  Assert.equal(menulist.value, "0");
  Assert.equal(menulist.label, "Block");
  await closePermissionPopup();

  PermissionTestUtils.add(URI, "popup", Services.perms.ALLOW_ACTION);

  await openPermissionPopup();
  menulist = document.getElementById("permission-popup-menulist");
  Assert.equal(menulist.value, "1");
  Assert.equal(menulist.label, "Allow");
  await closePermissionPopup();

  PermissionTestUtils.remove(URI, "popup");
  gBrowser.removeTab(tab);
});