|
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim:set ts=2 sw=2 sts=2 et: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 const Cc = Components.classes; |
|
8 const Ci = Components.interfaces; |
|
9 const Cr = Components.results; |
|
10 |
|
11 function run_test() |
|
12 { |
|
13 test1(); |
|
14 test2(); |
|
15 test3(); |
|
16 test4(); |
|
17 } |
|
18 |
|
19 /** |
|
20 * Checks that getting an input stream from a storage stream which has never had |
|
21 * anything written to it throws a not-initialized exception. |
|
22 */ |
|
23 function test1() |
|
24 { |
|
25 var ss = Cc["@mozilla.org/storagestream;1"] |
|
26 .createInstance(Ci.nsIStorageStream); |
|
27 ss.init(1024, 1024, null); |
|
28 |
|
29 var out = ss.getOutputStream(0); |
|
30 var inp2 = ss.newInputStream(0); |
|
31 do_check_eq(inp2.available(), 0); |
|
32 do_check_true(inp2.isNonBlocking()); |
|
33 |
|
34 var sis = |
|
35 Cc["@mozilla.org/scriptableinputstream;1"] |
|
36 .createInstance(Ci.nsIScriptableInputStream); |
|
37 sis.init(inp2); |
|
38 |
|
39 var threw = false; |
|
40 try { |
|
41 sis.read(1); |
|
42 } catch (ex if ex.result == Cr.NS_BASE_STREAM_WOULD_BLOCK) { |
|
43 threw = true; |
|
44 } |
|
45 do_check_true(threw); |
|
46 } |
|
47 |
|
48 /** |
|
49 * Checks that getting an input stream from a storage stream to which 0 bytes of |
|
50 * data have been explicitly written doesn't throw an exception. |
|
51 */ |
|
52 function test2() |
|
53 { |
|
54 var ss = Cc["@mozilla.org/storagestream;1"] |
|
55 .createInstance(Ci.nsIStorageStream); |
|
56 ss.init(1024, 1024, null); |
|
57 |
|
58 var out = ss.getOutputStream(0); |
|
59 out.write("", 0); |
|
60 try |
|
61 { |
|
62 var inp2 = ss.newInputStream(0); |
|
63 } |
|
64 catch (e) |
|
65 { |
|
66 do_throw("shouldn't throw exception when new input stream created"); |
|
67 } |
|
68 } |
|
69 |
|
70 /** |
|
71 * Checks that reading any non-zero amount of data from a storage stream |
|
72 * which has had 0 bytes written to it explicitly works correctly. |
|
73 */ |
|
74 function test3() |
|
75 { |
|
76 var ss = Cc["@mozilla.org/storagestream;1"] |
|
77 .createInstance(Ci.nsIStorageStream); |
|
78 ss.init(1024, 1024, null); |
|
79 |
|
80 var out = ss.getOutputStream(0); |
|
81 out.write("", 0); |
|
82 try |
|
83 { |
|
84 var inp = ss.newInputStream(0); |
|
85 } |
|
86 catch (e) |
|
87 { |
|
88 do_throw("newInputStream(0) shouldn't throw if write() is called: " + e); |
|
89 } |
|
90 |
|
91 do_check_true(inp.isNonBlocking(), "next test expects a non-blocking stream"); |
|
92 |
|
93 try |
|
94 { |
|
95 var threw = false; |
|
96 var bis = BIS(inp); |
|
97 var dummy = bis.readByteArray(5); |
|
98 } |
|
99 catch (e) |
|
100 { |
|
101 if (e.result != Cr.NS_BASE_STREAM_WOULD_BLOCK) |
|
102 do_throw("wrong error thrown: " + e); |
|
103 threw = true; |
|
104 } |
|
105 do_check_true(threw, |
|
106 "should have thrown (nsStorageInputStream is nonblocking)"); |
|
107 } |
|
108 |
|
109 /** |
|
110 * Basic functionality test for storagestream: write data to it, get an input |
|
111 * stream, and read the data back to see that it matches. |
|
112 */ |
|
113 function test4() |
|
114 { |
|
115 var bytes = [65, 66, 67, 68, 69, 70, 71, 72, 73, 74]; |
|
116 |
|
117 var ss = Cc["@mozilla.org/storagestream;1"] |
|
118 .createInstance(Ci.nsIStorageStream); |
|
119 ss.init(1024, 1024, null); |
|
120 |
|
121 var outStream = ss.getOutputStream(0); |
|
122 |
|
123 var bos = Cc["@mozilla.org/binaryoutputstream;1"] |
|
124 .createInstance(Ci.nsIBinaryOutputStream); |
|
125 bos.setOutputStream(outStream); |
|
126 |
|
127 bos.writeByteArray(bytes, bytes.length); |
|
128 bos.close(); |
|
129 |
|
130 var inp = ss.newInputStream(0); |
|
131 var bis = BIS(inp); |
|
132 |
|
133 var count = 0; |
|
134 while (count < bytes.length) |
|
135 { |
|
136 var data = bis.read8(1); |
|
137 do_check_eq(data, bytes[count++]); |
|
138 } |
|
139 |
|
140 var threw = false; |
|
141 try |
|
142 { |
|
143 data = bis.read8(1); |
|
144 } |
|
145 catch (e) |
|
146 { |
|
147 if (e.result != Cr.NS_ERROR_FAILURE) |
|
148 do_throw("wrong error thrown: " + e); |
|
149 threw = true; |
|
150 } |
|
151 if (!threw) |
|
152 do_throw("should have thrown but instead returned: '" + data + "'"); |
|
153 } |
|
154 |
|
155 |
|
156 function BIS(input) |
|
157 { |
|
158 var bis = Cc["@mozilla.org/binaryinputstream;1"] |
|
159 .createInstance(Ci.nsIBinaryInputStream); |
|
160 bis.setInputStream(input); |
|
161 return bis; |
|
162 } |