summaryrefslogtreecommitdiffstats
path: root/browser/components/downloads/test/browser/browser_downloads_context_menu_always_open_similar_files.js
blob: 6030d126c75770225744739dd0009c0e1468a381 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

let gMimeSvc = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
let gHandlerSvc = Cc["@mozilla.org/uriloader/handler-service;1"].getService(
  Ci.nsIHandlerService
);

let gDownloadDir;
const TestFiles = {};
let downloads = [];
const { handleInternally, saveToDisk, useSystemDefault, alwaysAsk } =
  Ci.nsIHandlerInfo;

function ensureMIMEState({ preferredAction, alwaysAskBeforeHandling = false }) {
  const mimeInfo = gMimeSvc.getFromTypeAndExtension("text/plain", "txt");
  mimeInfo.preferredAction = preferredAction;
  mimeInfo.alwaysAskBeforeHandling = alwaysAskBeforeHandling;
  gHandlerSvc.store(mimeInfo);
}

async function createDownloadFile() {
  if (!gDownloadDir) {
    gDownloadDir = await setDownloadDir();
  }
  info("Created download directory: " + gDownloadDir);
  TestFiles.txt = await createDownloadedFile(
    PathUtils.join(gDownloadDir, "downloaded.txt"),
    "Test file"
  );
  info("Created downloaded text file at:" + TestFiles.txt.path);

  info("Setting path for download file");
  // Set target for download file. Otherwise, file will default to .file instead of txt
  // when we prepare our downloads - particularly in task_addDownloads().
  let targetPath = PathUtils.join(PathUtils.tempDir, "downloaded.txt");
  let target = new FileUtils.File(targetPath);
  target.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
  downloads.push({
    state: DownloadsCommon.DOWNLOAD_FINISHED,
    contentType: "text/plain",
    target,
  });
}

async function prepareDownloadFiles(downloadList) {
  // prepare downloads
  await task_addDownloads(downloads);
  let [download] = await downloadList.getAll();
  info("Download succeeded? " + download.succeeded);
  info("Download target exists? " + download.target.exists);
}

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.download.always_ask_before_handling_new_types", false]],
  });
  const originalOpenDownload = DownloadsCommon.openDownload;
  // overwrite DownloadsCommon.openDownload to prevent file from opening during tests
  DownloadsCommon.openDownload = async () => {
    info("Overwriting openDownload for tests");
  };

  registerCleanupFunction(async () => {
    DownloadsCommon.openDownload = originalOpenDownload;
    info("Resetting downloads and closing downloads panel");
    await task_resetState();
  });

  // remove download files, empty out collections
  let downloadList = await Downloads.getList(Downloads.ALL);
  let downloadCount = (await downloadList.getAll()).length;
  is(downloadCount, 0, "At the start of the test, there should be 0 downloads");

  await createDownloadFile();
  await prepareDownloadFiles(downloadList);
});

add_task(async function test_checkbox_useSystemDefault() {
  // force mimetype pref
  ensureMIMEState({ preferredAction: useSystemDefault });

  await task_openPanel();
  await TestUtils.waitForCondition(() => {
    let downloadsListBox = document.getElementById("downloadsListBox");
    downloadsListBox.removeAttribute("disabled");
    return downloadsListBox.childElementCount == downloads.length;
  });

  info("trigger the context menu");
  let itemTarget = document.querySelector(
    "#downloadsListBox richlistitem .downloadMainArea"
  );

  let contextMenu = await openContextMenu(itemTarget);
  let alwaysOpenSimilarFilesItem = contextMenu.querySelector(
    ".downloadAlwaysOpenSimilarFilesMenuItem"
  );

  ok(
    !BrowserTestUtils.is_hidden(alwaysOpenSimilarFilesItem),
    "alwaysOpenSimilarFiles should be visible"
  );
  ok(
    alwaysOpenSimilarFilesItem.hasAttribute("checked"),
    "alwaysOpenSimilarFiles should have checkbox attribute"
  );

  contextMenu.hidePopup();
  let hiddenPromise = BrowserTestUtils.waitForEvent(
    DownloadsPanel.panel,
    "popuphidden"
  );
  DownloadsPanel.hidePanel();
  await hiddenPromise;
});

add_task(async function test_checkbox_saveToDisk() {
  // force mimetype pref
  ensureMIMEState({ preferredAction: saveToDisk });

  await task_openPanel();
  await TestUtils.waitForCondition(() => {
    let downloadsListBox = document.getElementById("downloadsListBox");
    downloadsListBox.removeAttribute("disabled");
    return downloadsListBox.childElementCount == downloads.length;
  });

  info("trigger the context menu");
  let itemTarget = document.querySelector(
    "#downloadsListBox richlistitem .downloadMainArea"
  );

  let contextMenu = await openContextMenu(itemTarget);
  let alwaysOpenSimilarFilesItem = contextMenu.querySelector(
    ".downloadAlwaysOpenSimilarFilesMenuItem"
  );

  ok(
    !BrowserTestUtils.is_hidden(alwaysOpenSimilarFilesItem),
    "alwaysOpenSimilarFiles should be visible"
  );
  ok(
    !alwaysOpenSimilarFilesItem.hasAttribute("checked"),
    "alwaysOpenSimilarFiles should not have checkbox attribute"
  );

  contextMenu.hidePopup();
  let hiddenPromise = BrowserTestUtils.waitForEvent(
    DownloadsPanel.panel,
    "popuphidden"
  );
  DownloadsPanel.hidePanel();
  await hiddenPromise;
});

add_task(async function test_preferences_enable_alwaysOpenSimilarFiles() {
  // Force mimetype pref
  ensureMIMEState({ preferredAction: saveToDisk });

  // open panel
  await task_openPanel();
  await TestUtils.waitForCondition(() => {
    let downloadsListBox = document.getElementById("downloadsListBox");
    downloadsListBox.removeAttribute("disabled");
    return downloadsListBox.childElementCount == downloads.length;
  });

  info("trigger the context menu");
  let itemTarget = document.querySelector(
    "#downloadsListBox richlistitem .downloadMainArea"
  );

  let contextMenu = await openContextMenu(itemTarget);
  let alwaysOpenSimilarFilesItem = contextMenu.querySelector(
    ".downloadAlwaysOpenSimilarFilesMenuItem"
  );

  alwaysOpenSimilarFilesItem.click();

  await TestUtils.waitForCondition(() => {
    let mimeInfo = gMimeSvc.getFromTypeAndExtension("text/plain", "txt");
    return mimeInfo.preferredAction === useSystemDefault;
  });
  let mimeInfo = gMimeSvc.getFromTypeAndExtension("text/plain", "txt");

  is(
    mimeInfo.preferredAction,
    useSystemDefault,
    "Preference should switch to useSystemDefault"
  );

  contextMenu.hidePopup();
  DownloadsPanel.hidePanel();
});

add_task(async function test_preferences_disable_alwaysOpenSimilarFiles() {
  // Force mimetype pref
  ensureMIMEState({ preferredAction: useSystemDefault });

  await task_openPanel();
  await TestUtils.waitForCondition(() => {
    let downloadsListBox = document.getElementById("downloadsListBox");
    downloadsListBox.removeAttribute("disabled");
    return downloadsListBox.childElementCount == downloads.length;
  });

  info("trigger the context menu");
  let itemTarget = document.querySelector(
    "#downloadsListBox richlistitem .downloadMainArea"
  );

  let contextMenu = await openContextMenu(itemTarget);
  let alwaysOpenSimilarFilesItem = contextMenu.querySelector(
    ".downloadAlwaysOpenSimilarFilesMenuItem"
  );

  alwaysOpenSimilarFilesItem.click();

  await TestUtils.waitForCondition(() => {
    let mimeInfo = gMimeSvc.getFromTypeAndExtension("text/plain", "txt");
    return mimeInfo.preferredAction === saveToDisk;
  });
  let mimeInfo = gMimeSvc.getFromTypeAndExtension("text/plain", "txt");

  is(
    mimeInfo.preferredAction,
    saveToDisk,
    "Preference should switch to saveToDisk"
  );

  contextMenu.hidePopup();
  DownloadsPanel.hidePanel();
});