1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/test/unit/test_safeoutputstream.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,66 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +function write_atomic(file, str) { 1.10 + var stream = Cc["@mozilla.org/network/atomic-file-output-stream;1"] 1.11 + .createInstance(Ci.nsIFileOutputStream); 1.12 + stream.init(file, -1, -1, 0); 1.13 + do { 1.14 + var written = stream.write(str, str.length); 1.15 + if (written == str.length) 1.16 + break; 1.17 + str = str.substring(written); 1.18 + } while (1); 1.19 + stream.QueryInterface(Ci.nsISafeOutputStream).finish(); 1.20 + stream.close(); 1.21 +} 1.22 + 1.23 +function write(file, str) { 1.24 + var stream = Cc["@mozilla.org/network/safe-file-output-stream;1"] 1.25 + .createInstance(Ci.nsIFileOutputStream); 1.26 + stream.init(file, -1, -1, 0); 1.27 + do { 1.28 + var written = stream.write(str, str.length); 1.29 + if (written == str.length) 1.30 + break; 1.31 + str = str.substring(written); 1.32 + } while (1); 1.33 + stream.QueryInterface(Ci.nsISafeOutputStream).finish(); 1.34 + stream.close(); 1.35 +} 1.36 + 1.37 +function checkFile(file, str) { 1.38 + var stream = Cc["@mozilla.org/network/file-input-stream;1"] 1.39 + .createInstance(Ci.nsIFileInputStream); 1.40 + stream.init(file, -1, -1, 0); 1.41 + 1.42 + var scriptStream = Cc["@mozilla.org/scriptableinputstream;1"] 1.43 + .createInstance(Ci.nsIScriptableInputStream); 1.44 + scriptStream.init(stream); 1.45 + 1.46 + do_check_eq(scriptStream.read(scriptStream.available()), str); 1.47 + scriptStream.close(); 1.48 +} 1.49 + 1.50 +function run_test() 1.51 +{ 1.52 + var filename = "\u0913"; 1.53 + var file = Cc["@mozilla.org/file/directory_service;1"] 1.54 + .getService(Ci.nsIProperties) 1.55 + .get("TmpD", Ci.nsIFile); 1.56 + file.append(filename); 1.57 + 1.58 + write(file, "First write"); 1.59 + checkFile(file, "First write"); 1.60 + 1.61 + write(file, "Second write"); 1.62 + checkFile(file, "Second write"); 1.63 + 1.64 + write_atomic(file, "First write: Atomic"); 1.65 + checkFile(file, "First write: Atomic"); 1.66 + 1.67 + write_atomic(file, "Second write: Atomic"); 1.68 + checkFile(file, "Second write: Atomic"); 1.69 +}