Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / test / unit / test_safeoutputstream.js
blob4925394ce4364fa78ab9e7a195942aa49b9faf54
1 /* -*- indent-tabs-mode: nil; js-indent-level: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 function write_atomic(file, str) {
8 var stream = Cc[
9 "@mozilla.org/network/atomic-file-output-stream;1"
10 ].createInstance(Ci.nsIFileOutputStream);
11 stream.init(file, -1, -1, 0);
12 do {
13 var written = stream.write(str, str.length);
14 if (written == str.length) {
15 break;
17 str = str.substring(written);
18 } while (1);
19 stream.QueryInterface(Ci.nsISafeOutputStream).finish();
20 stream.close();
23 function write(file, str) {
24 var stream = Cc[
25 "@mozilla.org/network/safe-file-output-stream;1"
26 ].createInstance(Ci.nsIFileOutputStream);
27 stream.init(file, -1, -1, 0);
28 do {
29 var written = stream.write(str, str.length);
30 if (written == str.length) {
31 break;
33 str = str.substring(written);
34 } while (1);
35 stream.QueryInterface(Ci.nsISafeOutputStream).finish();
36 stream.close();
39 function checkFile(file, str) {
40 var stream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(
41 Ci.nsIFileInputStream
43 stream.init(file, -1, -1, 0);
45 var scriptStream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(
46 Ci.nsIScriptableInputStream
48 scriptStream.init(stream);
50 Assert.equal(scriptStream.read(scriptStream.available()), str);
51 scriptStream.close();
54 function run_test() {
55 var filename = "\u0913";
56 var file = Services.dirsvc.get("TmpD", Ci.nsIFile);
57 file.append(filename);
59 write(file, "First write");
60 checkFile(file, "First write");
62 write(file, "Second write");
63 checkFile(file, "Second write");
65 write_atomic(file, "First write: Atomic");
66 checkFile(file, "First write: Atomic");
68 write_atomic(file, "Second write: Atomic");
69 checkFile(file, "Second write: Atomic");