summaryrefslogtreecommitdiffstats
path: root/browser/components/downloads/test/browser/browser_download_spam_protection.js
blob: 8095fff18e3f76483748abfe6ad2e8db86d68f68 (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
/* 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/. */

"use strict";

ChromeUtils.defineESModuleGetters(this, {
  DownloadSpamProtection: "resource:///modules/DownloadSpamProtection.sys.mjs",
  PermissionTestUtils: "resource://testing-common/PermissionTestUtils.sys.mjs",
});

XPCOMUtils.defineLazyModuleGetters(this, {
  BrowserWindowTracker: "resource:///modules/BrowserWindowTracker.jsm",
});

const TEST_URI = "https://example.com";

const TEST_PATH = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  TEST_URI
);

add_setup(async function () {
  // Create temp directory
  let time = new Date().getTime();
  let tempDir = Services.dirsvc.get("TmpD", Ci.nsIFile);
  tempDir.append(time);
  Services.prefs.setIntPref("browser.download.folderList", 2);
  Services.prefs.setComplexValue("browser.download.dir", Ci.nsIFile, tempDir);

  PermissionTestUtils.add(
    TEST_URI,
    "automatic-download",
    Services.perms.UNKNOWN_ACTION
  );
  await SpecialPowers.pushPrefEnv({
    set: [["browser.download.enable_spam_prevention", true]],
    clear: [
      ["browser.download.alwaysOpenPanel"],
      ["browser.download.always_ask_before_handling_new_types"],
    ],
  });

  registerCleanupFunction(async () => {
    Services.prefs.clearUserPref("browser.download.folderList");
    Services.prefs.clearUserPref("browser.download.dir");
    await IOUtils.remove(tempDir.path, { recursive: true });
  });
});

add_task(async function check_download_spam_ui() {
  await task_resetState();

  let browserWin = BrowserWindowTracker.getTopWindow();
  registerCleanupFunction(async () => {
    for (let win of [browserWin, browserWin2]) {
      win.DownloadsPanel.hidePanel();
      DownloadIntegration.downloadSpamProtection.removeDownloadSpamForWindow(
        TEST_URI,
        win
      );
    }
    let publicList = await Downloads.getList(Downloads.PUBLIC);
    await publicList.removeFinished();
    BrowserTestUtils.removeTab(newTab);
    await BrowserTestUtils.closeWindow(browserWin2);
  });
  let observedBlockedDownloads = 0;
  let gotAllBlockedDownloads = TestUtils.topicObserved(
    "blocked-automatic-download",
    () => {
      return ++observedBlockedDownloads >= 99;
    }
  );

  let newTab = await BrowserTestUtils.openNewForegroundTab(
    browserWin.gBrowser,
    TEST_PATH + "test_spammy_page.html"
  );

  await BrowserTestUtils.synthesizeMouseAtCenter(
    "body",
    {},
    newTab.linkedBrowser
  );

  info("Waiting on all blocked downloads");
  await gotAllBlockedDownloads;

  let { downloadSpamProtection } = DownloadIntegration;
  let spamList = downloadSpamProtection.getSpamListForWindow(browserWin);
  is(
    spamList._downloads[0].blockedDownloadsCount,
    99,
    "99 blocked downloads recorded"
  );
  ok(
    spamList._downloads[0].error.becauseBlockedByReputationCheck,
    "Download blocked because of reputation"
  );
  is(
    spamList._downloads[0].error.reputationCheckVerdict,
    "DownloadSpam",
    "Verdict is DownloadSpam"
  );

  browserWin.focus();
  await BrowserTestUtils.waitForPopupEvent(
    browserWin.DownloadsPanel.panel,
    "shown"
  );

  ok(browserWin.DownloadsPanel.isPanelShowing, "Download panel should open");
  await Downloads.getList(Downloads.PUBLIC);

  let listbox = browserWin.document.getElementById("downloadsListBox");
  ok(listbox, "Download list box present");

  await TestUtils.waitForCondition(() => {
    return listbox.childElementCount == 2 && !listbox.getAttribute("disabled");
  }, "2 downloads = 1 allowed download and 1 for 99 downloads blocked");

  let spamElement = listbox.itemChildren[0].classList.contains(
    "temporary-block"
  )
    ? listbox.itemChildren[0]
    : listbox.itemChildren[1];

  ok(spamElement.classList.contains("temporary-block"), "Download is blocked");

  info("Testing spam protection in a second window");

  browserWin.DownloadsPanel.hidePanel();
  DownloadIntegration.downloadSpamProtection.removeDownloadSpamForWindow(
    TEST_URI,
    browserWin
  );

  ok(
    !browserWin.DownloadsPanel.isPanelShowing,
    "Download panel should be closed in first window"
  );
  is(
    listbox.childElementCount,
    1,
    "First window's download list should have one item - the download that wasn't blocked"
  );

  let browserWin2 = await BrowserTestUtils.openNewBrowserWindow();
  let observedBlockedDownloads2 = 0;
  let gotAllBlockedDownloads2 = TestUtils.topicObserved(
    "blocked-automatic-download",
    () => {
      return ++observedBlockedDownloads2 >= 100;
    }
  );

  let newTab2 = await BrowserTestUtils.openNewForegroundTab(
    browserWin2.gBrowser,
    TEST_PATH + "test_spammy_page.html"
  );
  await BrowserTestUtils.synthesizeMouseAtCenter(
    "body",
    {},
    newTab2.linkedBrowser
  );

  info("Waiting on all blocked downloads in second window");
  await gotAllBlockedDownloads2;

  let spamList2 = downloadSpamProtection.getSpamListForWindow(browserWin2);
  is(
    spamList2._downloads[0].blockedDownloadsCount,
    100,
    "100 blocked downloads recorded in second window"
  );
  ok(
    !spamList._downloads[0]?.blockedDownloadsCount,
    "No blocked downloads in first window"
  );

  browserWin2.focus();
  await BrowserTestUtils.waitForPopupEvent(
    browserWin2.DownloadsPanel.panel,
    "shown"
  );

  ok(
    browserWin2.DownloadsPanel.isPanelShowing,
    "Download panel should open in second window"
  );

  ok(
    !browserWin.DownloadsPanel.isPanelShowing,
    "Download panel should not open in first window"
  );

  let listbox2 = browserWin2.document.getElementById("downloadsListBox");
  ok(listbox2, "Download list box present");

  await TestUtils.waitForCondition(() => {
    return (
      listbox2.childElementCount == 2 && !listbox2.getAttribute("disabled")
    );
  }, "2 downloads = 1 allowed download from first window, and 1 for 100 downloads blocked in second window");

  is(
    listbox.childElementCount,
    1,
    "First window's download list should still have one item - the download that wasn't blocked"
  );

  let spamElement2 = listbox2.itemChildren[0].classList.contains(
    "temporary-block"
  )
    ? listbox2.itemChildren[0]
    : listbox2.itemChildren[1];

  ok(spamElement2.classList.contains("temporary-block"), "Download is blocked");
});