summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/browser/browser_save_folder_standalone_image.js
blob: 68b5a1f1134dc97fcf62a8d6fb6f7412adbaffa1 (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
/* Any copyright is dedicated to the Public Domain.
 * https://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * TestCase for bug 1726801
 * <https://bugzilla.mozilla.org/show_bug.cgi?id=1726801>
 *
 * Load an image in a standalone tab and verify that the per-site download
 * folder is correctly retrieved when using "Save Page As" to save the image.
 */

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

async function setFile(downloadLastDir, aURI, aValue) {
  downloadLastDir.setFile(aURI, aValue);
  await TestUtils.waitForTick();
}

function newDirectory() {
  let tmpDir = FileUtils.getDir("TmpD", [], true);
  let dir = tmpDir.clone();
  dir.append("testdir");
  dir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
  return dir;
}

function clearHistory() {
  Services.obs.notifyObservers(null, "browser:purge-session-history");
}

async function clearHistoryAndWait() {
  clearHistory();
  await TestUtils.waitForTick();
  await TestUtils.waitForTick();
}

/*
 * ====
 * Test
 * ====
 */

let MockFilePicker = SpecialPowers.MockFilePicker;
MockFilePicker.init(window);

add_task(async function () {
  const IMAGE_URL =
    "http://mochi.test:8888/browser/toolkit/content/tests/browser/doggy.png";

  await BrowserTestUtils.withNewTab(IMAGE_URL, async function (browser) {
    let tmpDir = FileUtils.getDir("TmpD", [], true);
    let dir = newDirectory();
    let downloadLastDir = new DownloadLastDir(null);
    // Set the desired target directory for the IMAGE_URL
    await setFile(downloadLastDir, IMAGE_URL, dir);
    // Ensure that "browser.download.lastDir" points to a different directory
    await setFile(downloadLastDir, null, tmpDir);
    registerCleanupFunction(async function () {
      await clearHistoryAndWait();
      dir.remove(true);
    });

    // Prepare mock file picker.
    let showFilePickerPromise = new Promise(resolve => {
      MockFilePicker.showCallback = fp => resolve(fp.displayDirectory.path);
    });
    registerCleanupFunction(function () {
      MockFilePicker.cleanup();
    });

    // Run "Save Page As"
    EventUtils.synthesizeKey("s", { accelKey: true });

    let dirPath = await showFilePickerPromise;
    is(dirPath, dir.path, "Verify proposed download folder.");
  });
});