michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cr = Components.results; michael@0: michael@0: function run_test() michael@0: { michael@0: test1(); michael@0: test2(); michael@0: test3(); michael@0: test4(); michael@0: } michael@0: michael@0: /** michael@0: * Checks that getting an input stream from a storage stream which has never had michael@0: * anything written to it throws a not-initialized exception. michael@0: */ michael@0: function test1() michael@0: { michael@0: var ss = Cc["@mozilla.org/storagestream;1"] michael@0: .createInstance(Ci.nsIStorageStream); michael@0: ss.init(1024, 1024, null); michael@0: michael@0: var out = ss.getOutputStream(0); michael@0: var inp2 = ss.newInputStream(0); michael@0: do_check_eq(inp2.available(), 0); michael@0: do_check_true(inp2.isNonBlocking()); michael@0: michael@0: var sis = michael@0: Cc["@mozilla.org/scriptableinputstream;1"] michael@0: .createInstance(Ci.nsIScriptableInputStream); michael@0: sis.init(inp2); michael@0: michael@0: var threw = false; michael@0: try { michael@0: sis.read(1); michael@0: } catch (ex if ex.result == Cr.NS_BASE_STREAM_WOULD_BLOCK) { michael@0: threw = true; michael@0: } michael@0: do_check_true(threw); michael@0: } michael@0: michael@0: /** michael@0: * Checks that getting an input stream from a storage stream to which 0 bytes of michael@0: * data have been explicitly written doesn't throw an exception. michael@0: */ michael@0: function test2() michael@0: { michael@0: var ss = Cc["@mozilla.org/storagestream;1"] michael@0: .createInstance(Ci.nsIStorageStream); michael@0: ss.init(1024, 1024, null); michael@0: michael@0: var out = ss.getOutputStream(0); michael@0: out.write("", 0); michael@0: try michael@0: { michael@0: var inp2 = ss.newInputStream(0); michael@0: } michael@0: catch (e) michael@0: { michael@0: do_throw("shouldn't throw exception when new input stream created"); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Checks that reading any non-zero amount of data from a storage stream michael@0: * which has had 0 bytes written to it explicitly works correctly. michael@0: */ michael@0: function test3() michael@0: { michael@0: var ss = Cc["@mozilla.org/storagestream;1"] michael@0: .createInstance(Ci.nsIStorageStream); michael@0: ss.init(1024, 1024, null); michael@0: michael@0: var out = ss.getOutputStream(0); michael@0: out.write("", 0); michael@0: try michael@0: { michael@0: var inp = ss.newInputStream(0); michael@0: } michael@0: catch (e) michael@0: { michael@0: do_throw("newInputStream(0) shouldn't throw if write() is called: " + e); michael@0: } michael@0: michael@0: do_check_true(inp.isNonBlocking(), "next test expects a non-blocking stream"); michael@0: michael@0: try michael@0: { michael@0: var threw = false; michael@0: var bis = BIS(inp); michael@0: var dummy = bis.readByteArray(5); michael@0: } michael@0: catch (e) michael@0: { michael@0: if (e.result != Cr.NS_BASE_STREAM_WOULD_BLOCK) michael@0: do_throw("wrong error thrown: " + e); michael@0: threw = true; michael@0: } michael@0: do_check_true(threw, michael@0: "should have thrown (nsStorageInputStream is nonblocking)"); michael@0: } michael@0: michael@0: /** michael@0: * Basic functionality test for storagestream: write data to it, get an input michael@0: * stream, and read the data back to see that it matches. michael@0: */ michael@0: function test4() michael@0: { michael@0: var bytes = [65, 66, 67, 68, 69, 70, 71, 72, 73, 74]; michael@0: michael@0: var ss = Cc["@mozilla.org/storagestream;1"] michael@0: .createInstance(Ci.nsIStorageStream); michael@0: ss.init(1024, 1024, null); michael@0: michael@0: var outStream = ss.getOutputStream(0); michael@0: michael@0: var bos = Cc["@mozilla.org/binaryoutputstream;1"] michael@0: .createInstance(Ci.nsIBinaryOutputStream); michael@0: bos.setOutputStream(outStream); michael@0: michael@0: bos.writeByteArray(bytes, bytes.length); michael@0: bos.close(); michael@0: michael@0: var inp = ss.newInputStream(0); michael@0: var bis = BIS(inp); michael@0: michael@0: var count = 0; michael@0: while (count < bytes.length) michael@0: { michael@0: var data = bis.read8(1); michael@0: do_check_eq(data, bytes[count++]); michael@0: } michael@0: michael@0: var threw = false; michael@0: try michael@0: { michael@0: data = bis.read8(1); michael@0: } michael@0: catch (e) michael@0: { michael@0: if (e.result != Cr.NS_ERROR_FAILURE) michael@0: do_throw("wrong error thrown: " + e); michael@0: threw = true; michael@0: } michael@0: if (!threw) michael@0: do_throw("should have thrown but instead returned: '" + data + "'"); michael@0: } michael@0: michael@0: michael@0: function BIS(input) michael@0: { michael@0: var bis = Cc["@mozilla.org/binaryinputstream;1"] michael@0: .createInstance(Ci.nsIBinaryInputStream); michael@0: bis.setInputStream(input); michael@0: return bis; michael@0: }