summaryrefslogtreecommitdiffstats
path: root/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_DownloadLastDirWithCPS.js
blob: 67d99b2f5c4e69a8ebe5b2e14c1793bccda54bd2 (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* 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/. */

var gTests;
function test() {
  waitForExplicitFinish();
  requestLongerTimeout(2);
  runTest().catch(ex => ok(false, ex));
}

/*
 * ================
 * Helper functions
 * ================
 */

function createWindow(aOptions) {
  return new Promise(resolve => whenNewWindowLoaded(aOptions, resolve));
}

function getFile(downloadLastDir, aURI) {
  return downloadLastDir.getFileAsync(aURI);
}

function setFile(downloadLastDir, aURI, aValue) {
  downloadLastDir.setFile(aURI, aValue);
  return new Promise(resolve => executeSoon(resolve));
}

function clearHistoryAndWait() {
  clearHistory();
  return new Promise(resolve => executeSoon(_ => executeSoon(resolve)));
}

/*
 * ===================
 * Function with tests
 * ===================
 */

async function runTest() {
  let { FileUtils } = ChromeUtils.importESModule(
    "resource://gre/modules/FileUtils.sys.mjs"
  );
  let { DownloadLastDir } = ChromeUtils.importESModule(
    "resource://gre/modules/DownloadLastDir.sys.mjs"
  );

  let tmpDir = FileUtils.getDir("TmpD", [], true);
  let dir1 = newDirectory();
  let dir2 = newDirectory();
  let dir3 = newDirectory();

  let uri1 = Services.io.newURI("http://test1.com/");
  let uri2 = Services.io.newURI("http://test2.com/");
  let uri3 = Services.io.newURI("http://test3.com/");
  let uri4 = Services.io.newURI("http://test4.com/");

  // cleanup functions registration
  registerCleanupFunction(function () {
    Services.prefs.clearUserPref("browser.download.lastDir.savePerSite");
    Services.prefs.clearUserPref("browser.download.lastDir");
    [dir1, dir2, dir3].forEach(dir => dir.remove(true));
    win.close();
    pbWin.close();
  });

  function checkDownloadLastDir(gDownloadLastDir, aLastDir) {
    is(
      gDownloadLastDir.file.path,
      aLastDir.path,
      "gDownloadLastDir should point to the expected last directory"
    );
    return getFile(gDownloadLastDir, uri1);
  }

  function checkDownloadLastDirNull(gDownloadLastDir) {
    is(gDownloadLastDir.file, null, "gDownloadLastDir should be null");
    return getFile(gDownloadLastDir, uri1);
  }

  /*
   * ================================
   * Create a regular and a PB window
   * ================================
   */

  let win = await createWindow({ private: false });
  let pbWin = await createWindow({ private: true });

  let downloadLastDir = new DownloadLastDir(win);
  let pbDownloadLastDir = new DownloadLastDir(pbWin);

  /*
   * ==================
   * Beginning of tests
   * ==================
   */

  is(
    typeof downloadLastDir,
    "object",
    "downloadLastDir should be a valid object"
  );
  is(downloadLastDir.file, null, "LastDir pref should be null to start with");

  // set up last dir
  await setFile(downloadLastDir, null, tmpDir);
  is(
    downloadLastDir.file.path,
    tmpDir.path,
    "LastDir should point to the tmpDir"
  );
  isnot(
    downloadLastDir.file,
    tmpDir,
    "downloadLastDir.file should not be pointing to tmpDir"
  );

  // set uri1 to dir1, all should now return dir1
  // also check that a new object is returned
  await setFile(downloadLastDir, uri1, dir1);
  is(
    downloadLastDir.file.path,
    dir1.path,
    "downloadLastDir should return dir1"
  );
  isnot(
    downloadLastDir.file,
    dir1,
    "downloadLastDir.file should not return dir1"
  );
  is(
    (await getFile(downloadLastDir, uri1)).path,
    dir1.path,
    "uri1 should return dir1"
  ); // set in CPS
  isnot(
    await getFile(downloadLastDir, uri1),
    dir1,
    "getFile on uri1 should not return dir1"
  );
  is(
    (await getFile(downloadLastDir, uri2)).path,
    dir1.path,
    "uri2 should return dir1"
  ); // fallback
  isnot(
    await getFile(downloadLastDir, uri2),
    dir1,
    "getFile on uri2 should not return dir1"
  );
  is(
    (await getFile(downloadLastDir, uri3)).path,
    dir1.path,
    "uri3 should return dir1"
  ); // fallback
  isnot(
    await getFile(downloadLastDir, uri3),
    dir1,
    "getFile on uri3 should not return dir1"
  );
  is(
    (await getFile(downloadLastDir, uri4)).path,
    dir1.path,
    "uri4 should return dir1"
  ); // fallback
  isnot(
    await getFile(downloadLastDir, uri4),
    dir1,
    "getFile on uri4 should not return dir1"
  );

  // set uri2 to dir2, all except uri1 should now return dir2
  await setFile(downloadLastDir, uri2, dir2);
  is(
    downloadLastDir.file.path,
    dir2.path,
    "downloadLastDir should point to dir2"
  );
  is(
    (await getFile(downloadLastDir, uri1)).path,
    dir1.path,
    "uri1 should return dir1"
  ); // set in CPS
  is(
    (await getFile(downloadLastDir, uri2)).path,
    dir2.path,
    "uri2 should return dir2"
  ); // set in CPS
  is(
    (await getFile(downloadLastDir, uri3)).path,
    dir2.path,
    "uri3 should return dir2"
  ); // fallback
  is(
    (await getFile(downloadLastDir, uri4)).path,
    dir2.path,
    "uri4 should return dir2"
  ); // fallback

  // set uri3 to dir3, all except uri1 and uri2 should now return dir3
  await setFile(downloadLastDir, uri3, dir3);
  is(
    downloadLastDir.file.path,
    dir3.path,
    "downloadLastDir should point to dir3"
  );
  is(
    (await getFile(downloadLastDir, uri1)).path,
    dir1.path,
    "uri1 should return dir1"
  ); // set in CPS
  is(
    (await getFile(downloadLastDir, uri2)).path,
    dir2.path,
    "uri2 should return dir2"
  ); // set in CPS
  is(
    (await getFile(downloadLastDir, uri3)).path,
    dir3.path,
    "uri3 should return dir3"
  ); // set in CPS
  is(
    (await getFile(downloadLastDir, uri4)).path,
    dir3.path,
    "uri4 should return dir4"
  ); // fallback

  // set uri1 to dir2, all except uri3 should now return dir2
  await setFile(downloadLastDir, uri1, dir2);
  is(
    downloadLastDir.file.path,
    dir2.path,
    "downloadLastDir should point to dir2"
  );
  is(
    (await getFile(downloadLastDir, uri1)).path,
    dir2.path,
    "uri1 should return dir2"
  ); // set in CPS
  is(
    (await getFile(downloadLastDir, uri2)).path,
    dir2.path,
    "uri2 should return dir2"
  ); // set in CPS
  is(
    (await getFile(downloadLastDir, uri3)).path,
    dir3.path,
    "uri3 should return dir3"
  ); // set in CPS
  is(
    (await getFile(downloadLastDir, uri4)).path,
    dir2.path,
    "uri4 should return dir2"
  ); // fallback

  await clearHistoryAndWait();

  // check clearHistory removes all data
  is(downloadLastDir.file, null, "clearHistory removes all data");
  is(await getFile(downloadLastDir, uri1), null, "uri1 should point to null");
  is(await getFile(downloadLastDir, uri2), null, "uri2 should point to null");
  is(await getFile(downloadLastDir, uri3), null, "uri3 should point to null");
  is(await getFile(downloadLastDir, uri4), null, "uri4 should point to null");

  await setFile(downloadLastDir, null, tmpDir);

  // check data set outside PB mode is remembered
  is(
    (await checkDownloadLastDir(pbDownloadLastDir, tmpDir)).path,
    tmpDir.path,
    "uri1 should return the expected last directory"
  );
  is(
    (await checkDownloadLastDir(downloadLastDir, tmpDir)).path,
    tmpDir.path,
    "uri1 should return the expected last directory"
  );
  await clearHistoryAndWait();

  await setFile(downloadLastDir, uri1, dir1);

  // check data set using CPS outside PB mode is remembered
  is(
    (await checkDownloadLastDir(pbDownloadLastDir, dir1)).path,
    dir1.path,
    "uri1 should return the expected last directory"
  );
  is(
    (await checkDownloadLastDir(downloadLastDir, dir1)).path,
    dir1.path,
    "uri1 should return the expected last directory"
  );
  await clearHistoryAndWait();

  // check data set inside PB mode is forgotten
  await setFile(pbDownloadLastDir, null, tmpDir);

  is(
    (await checkDownloadLastDir(pbDownloadLastDir, tmpDir)).path,
    tmpDir.path,
    "uri1 should return the expected last directory"
  );
  is(
    await checkDownloadLastDirNull(downloadLastDir),
    null,
    "uri1 should return the expected last directory"
  );

  await clearHistoryAndWait();

  // check data set using CPS inside PB mode is forgotten
  await setFile(pbDownloadLastDir, uri1, dir1);

  is(
    (await checkDownloadLastDir(pbDownloadLastDir, dir1)).path,
    dir1.path,
    "uri1 should return the expected last directory"
  );
  is(
    await checkDownloadLastDirNull(downloadLastDir),
    null,
    "uri1 should return the expected last directory"
  );

  // check data set outside PB mode but changed inside is remembered correctly
  await setFile(downloadLastDir, uri1, dir1);
  await setFile(pbDownloadLastDir, uri1, dir2);
  is(
    (await checkDownloadLastDir(pbDownloadLastDir, dir2)).path,
    dir2.path,
    "uri1 should return the expected last directory"
  );
  is(
    (await checkDownloadLastDir(downloadLastDir, dir1)).path,
    dir1.path,
    "uri1 should return the expected last directory"
  );

  /*
   * ====================
   * Create new PB window
   * ====================
   */

  // check that the last dir store got cleared in a new PB window
  pbWin.close();
  // And give it time to close
  await new Promise(resolve => executeSoon(resolve));

  pbWin = await createWindow({ private: true });
  pbDownloadLastDir = new DownloadLastDir(pbWin);

  is(
    (await checkDownloadLastDir(pbDownloadLastDir, dir1)).path,
    dir1.path,
    "uri1 should return the expected last directory"
  );

  await clearHistoryAndWait();

  // check clearHistory inside PB mode clears data outside PB mode
  await setFile(pbDownloadLastDir, uri1, dir2);

  await clearHistoryAndWait();

  is(
    await checkDownloadLastDirNull(downloadLastDir),
    null,
    "uri1 should return the expected last directory"
  );
  is(
    await checkDownloadLastDirNull(pbDownloadLastDir),
    null,
    "uri1 should return the expected last directory"
  );

  // check that disabling CPS works
  Services.prefs.setBoolPref("browser.download.lastDir.savePerSite", false);

  await setFile(downloadLastDir, uri1, dir1);
  is(downloadLastDir.file.path, dir1.path, "LastDir should be set to dir1");
  is(
    (await getFile(downloadLastDir, uri1)).path,
    dir1.path,
    "uri1 should return dir1"
  );
  is(
    (await getFile(downloadLastDir, uri2)).path,
    dir1.path,
    "uri2 should return dir1"
  );
  is(
    (await getFile(downloadLastDir, uri3)).path,
    dir1.path,
    "uri3 should return dir1"
  );
  is(
    (await getFile(downloadLastDir, uri4)).path,
    dir1.path,
    "uri4 should return dir1"
  );

  downloadLastDir.setFile(uri2, dir2);
  is(downloadLastDir.file.path, dir2.path, "LastDir should be set to dir2");
  is(
    (await getFile(downloadLastDir, uri1)).path,
    dir2.path,
    "uri1 should return dir2"
  );
  is(
    (await getFile(downloadLastDir, uri2)).path,
    dir2.path,
    "uri2 should return dir2"
  );
  is(
    (await getFile(downloadLastDir, uri3)).path,
    dir2.path,
    "uri3 should return dir2"
  );
  is(
    (await getFile(downloadLastDir, uri4)).path,
    dir2.path,
    "uri4 should return dir2"
  );

  Services.prefs.clearUserPref("browser.download.lastDir.savePerSite");

  // check that passing null to setFile clears the stored value
  await setFile(downloadLastDir, uri3, dir3);
  is(
    (await getFile(downloadLastDir, uri3)).path,
    dir3.path,
    "LastDir should be set to dir3"
  );
  await setFile(downloadLastDir, uri3, null);
  is(await getFile(downloadLastDir, uri3), null, "uri3 should return null");

  await clearHistoryAndWait();

  finish();
}