summaryrefslogtreecommitdiffstats
path: root/dom/fetch/tests/browser_blobFromFile.js
blob: 07a8c6669e8ee6aa66529577f1ac88c3c9f4acc6 (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
add_task(async function test() {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.tabs.remote.separateFileUriProcess", true]],
  });

  let fileData = "";
  for (var i = 0; i < 100; ++i) {
    fileData += "hello world!";
  }

  let file = Cc["@mozilla.org/file/directory_service;1"]
    .getService(Ci.nsIDirectoryService)
    .QueryInterface(Ci.nsIProperties)
    .get("ProfD", Ci.nsIFile);
  file.append("file.txt");
  file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o600);

  let outStream = Cc[
    "@mozilla.org/network/file-output-stream;1"
  ].createInstance(Ci.nsIFileOutputStream);
  outStream.init(
    file,
    0x02 | 0x08 | 0x20, // write, create, truncate
    0o666,
    0
  );
  outStream.write(fileData, fileData.length);
  outStream.close();

  let fileHandler = Cc["@mozilla.org/network/io-service;1"]
    .getService(Ci.nsIIOService)
    .getProtocolHandler("file")
    .QueryInterface(Ci.nsIFileProtocolHandler);

  let fileURL = fileHandler.getURLSpecFromActualFile(file);

  info("Opening url: " + fileURL);
  let tab = BrowserTestUtils.addTab(gBrowser, fileURL);

  let browser = gBrowser.getBrowserForTab(tab);
  await BrowserTestUtils.browserLoaded(browser);

  let blob = await SpecialPowers.spawn(
    browser,
    [file.leafName],
    function (fileName) {
      return new content.window.Promise(resolve => {
        content.window
          .fetch(fileName)
          .then(r => r.blob())
          .then(blob => resolve(blob));
      });
    }
  );

  ok(File.isInstance(blob), "We have a file");

  is(blob.size, file.fileSize, "The size matches");
  is(blob.name, file.leafName, "The name is correct");

  file.remove(false);

  gBrowser.removeTab(tab);
});