summaryrefslogtreecommitdiffstats
path: root/dom/base/test/chrome/test_fileconstructor_tempfile.xul
blob: e2ff3a0d29a66429ed127f23e95df75d6014e9dc (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
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet
  href="chrome://mochikit/content/tests/SimpleTest/test.css"
  type="text/css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=982874

Tests building a DOMFile with the "temporary" option and checks that
the underlying file is removed when the DOMFile is gc'ed.
-->
<window title="Mozilla Bug 982874"
  xmlns:html="http://www.w3.org/1999/xhtml"
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <script type="application/javascript"
    src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>

<body  xmlns="http://www.w3.org/1999/xhtml">
<a target="_blank"
   href="https://bugzilla.mozilla.org/show_bug.cgi?id=982874">
   Mozilla Bug 982874</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>
</body>

<script class="testbody" type="application/javascript">
<![CDATA[

/** Test for Bug 982874 **/
var Cc = Components.classes;
var Ci = Components.interfaces;
var Cu = Components.utils;

SimpleTest.waitForExplicitFinish();

function cleanup(tmp) {
  // Force cycle and garbage collection and check that we removed the file.
  for (let i = 0; i < 10; i++) {
    Cu.forceCC();
    Cu.forceGC();
  }
  if (tmp.exists()) {
    ok(false, "Failed to remove temporary file!");
  } else {
    ok(true, "Temporary file removed when gc-ing the DOMFile");
  }
  SimpleTest.finish();
}

try {
  // Create a file in $TMPDIR/mozilla-temp-files
  let tmp = Cc["@mozilla.org/file/directory_service;1"]
              .getService(Ci.nsIProperties)
              .get("TmpD", Ci.nsIFile);
  tmp.append("mozilla-temp-files");
  tmp.append("test.txt");
  tmp.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0600);

  // Add some content to the file.
  let fileData = "I'm a temporary file!";
  let outStream = Cc["@mozilla.org/network/file-output-stream;1"]
                    .createInstance(Ci.nsIFileOutputStream);
  outStream.init(tmp, 0x02 | 0x08 | 0x20, // write, create, truncate
                 0666, 0);
  outStream.write(fileData, fileData.length);
  outStream.close();

  // Create a scoped DOMFile so the gc will happily get rid of it.
  {
    let dirfile = File.createFromNsIFile(tmp, { temporary: true });
    ok(true, "Temporary File() created");
    let reader = new FileReader();
    reader.readAsArrayBuffer(dirfile);
    reader.onload = function(event) {
      let buffer = event.target.result;
      ok(buffer.byteLength > 0,
         "Blob size should be > 0 : " + buffer.byteLength);
      cleanup(tmp);
    }
  }
} catch (e) {
  ok(false, "Unable to create the File() object : " + e);
  SimpleTest.finish();
}
]]>
</script>

</window>