1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/tests/unit/test_storagestream.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,162 @@ 1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +const Cc = Components.classes; 1.11 +const Ci = Components.interfaces; 1.12 +const Cr = Components.results; 1.13 + 1.14 +function run_test() 1.15 +{ 1.16 + test1(); 1.17 + test2(); 1.18 + test3(); 1.19 + test4(); 1.20 +} 1.21 + 1.22 +/** 1.23 + * Checks that getting an input stream from a storage stream which has never had 1.24 + * anything written to it throws a not-initialized exception. 1.25 + */ 1.26 +function test1() 1.27 +{ 1.28 + var ss = Cc["@mozilla.org/storagestream;1"] 1.29 + .createInstance(Ci.nsIStorageStream); 1.30 + ss.init(1024, 1024, null); 1.31 + 1.32 + var out = ss.getOutputStream(0); 1.33 + var inp2 = ss.newInputStream(0); 1.34 + do_check_eq(inp2.available(), 0); 1.35 + do_check_true(inp2.isNonBlocking()); 1.36 + 1.37 + var sis = 1.38 + Cc["@mozilla.org/scriptableinputstream;1"] 1.39 + .createInstance(Ci.nsIScriptableInputStream); 1.40 + sis.init(inp2); 1.41 + 1.42 + var threw = false; 1.43 + try { 1.44 + sis.read(1); 1.45 + } catch (ex if ex.result == Cr.NS_BASE_STREAM_WOULD_BLOCK) { 1.46 + threw = true; 1.47 + } 1.48 + do_check_true(threw); 1.49 +} 1.50 + 1.51 +/** 1.52 + * Checks that getting an input stream from a storage stream to which 0 bytes of 1.53 + * data have been explicitly written doesn't throw an exception. 1.54 + */ 1.55 +function test2() 1.56 +{ 1.57 + var ss = Cc["@mozilla.org/storagestream;1"] 1.58 + .createInstance(Ci.nsIStorageStream); 1.59 + ss.init(1024, 1024, null); 1.60 + 1.61 + var out = ss.getOutputStream(0); 1.62 + out.write("", 0); 1.63 + try 1.64 + { 1.65 + var inp2 = ss.newInputStream(0); 1.66 + } 1.67 + catch (e) 1.68 + { 1.69 + do_throw("shouldn't throw exception when new input stream created"); 1.70 + } 1.71 +} 1.72 + 1.73 +/** 1.74 + * Checks that reading any non-zero amount of data from a storage stream 1.75 + * which has had 0 bytes written to it explicitly works correctly. 1.76 + */ 1.77 +function test3() 1.78 +{ 1.79 + var ss = Cc["@mozilla.org/storagestream;1"] 1.80 + .createInstance(Ci.nsIStorageStream); 1.81 + ss.init(1024, 1024, null); 1.82 + 1.83 + var out = ss.getOutputStream(0); 1.84 + out.write("", 0); 1.85 + try 1.86 + { 1.87 + var inp = ss.newInputStream(0); 1.88 + } 1.89 + catch (e) 1.90 + { 1.91 + do_throw("newInputStream(0) shouldn't throw if write() is called: " + e); 1.92 + } 1.93 + 1.94 + do_check_true(inp.isNonBlocking(), "next test expects a non-blocking stream"); 1.95 + 1.96 + try 1.97 + { 1.98 + var threw = false; 1.99 + var bis = BIS(inp); 1.100 + var dummy = bis.readByteArray(5); 1.101 + } 1.102 + catch (e) 1.103 + { 1.104 + if (e.result != Cr.NS_BASE_STREAM_WOULD_BLOCK) 1.105 + do_throw("wrong error thrown: " + e); 1.106 + threw = true; 1.107 + } 1.108 + do_check_true(threw, 1.109 + "should have thrown (nsStorageInputStream is nonblocking)"); 1.110 +} 1.111 + 1.112 +/** 1.113 + * Basic functionality test for storagestream: write data to it, get an input 1.114 + * stream, and read the data back to see that it matches. 1.115 + */ 1.116 +function test4() 1.117 +{ 1.118 + var bytes = [65, 66, 67, 68, 69, 70, 71, 72, 73, 74]; 1.119 + 1.120 + var ss = Cc["@mozilla.org/storagestream;1"] 1.121 + .createInstance(Ci.nsIStorageStream); 1.122 + ss.init(1024, 1024, null); 1.123 + 1.124 + var outStream = ss.getOutputStream(0); 1.125 + 1.126 + var bos = Cc["@mozilla.org/binaryoutputstream;1"] 1.127 + .createInstance(Ci.nsIBinaryOutputStream); 1.128 + bos.setOutputStream(outStream); 1.129 + 1.130 + bos.writeByteArray(bytes, bytes.length); 1.131 + bos.close(); 1.132 + 1.133 + var inp = ss.newInputStream(0); 1.134 + var bis = BIS(inp); 1.135 + 1.136 + var count = 0; 1.137 + while (count < bytes.length) 1.138 + { 1.139 + var data = bis.read8(1); 1.140 + do_check_eq(data, bytes[count++]); 1.141 + } 1.142 + 1.143 + var threw = false; 1.144 + try 1.145 + { 1.146 + data = bis.read8(1); 1.147 + } 1.148 + catch (e) 1.149 + { 1.150 + if (e.result != Cr.NS_ERROR_FAILURE) 1.151 + do_throw("wrong error thrown: " + e); 1.152 + threw = true; 1.153 + } 1.154 + if (!threw) 1.155 + do_throw("should have thrown but instead returned: '" + data + "'"); 1.156 +} 1.157 + 1.158 + 1.159 +function BIS(input) 1.160 +{ 1.161 + var bis = Cc["@mozilla.org/binaryinputstream;1"] 1.162 + .createInstance(Ci.nsIBinaryInputStream); 1.163 + bis.setInputStream(input); 1.164 + return bis; 1.165 +}