no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / xpcom / tests / unit / test_seek_multiplex.js
blob7a469d9f48ecfabd9249bece4e8a916cfb8e0b2f
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 // The string we use as data.
6 const data = "0123456789";
7 // Number of streams in the multiplex stream.
8 const count = 10;
10 function test_multiplex_streams() {
11   var MultiplexStream = CC(
12     "@mozilla.org/io/multiplex-input-stream;1",
13     "nsIMultiplexInputStream"
14   );
15   Assert.equal(1, 1);
17   var multiplex = new MultiplexStream();
18   for (var i = 0; i < count; ++i) {
19     let s = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
20       Ci.nsIStringInputStream
21     );
22     s.setData(data, data.length);
24     multiplex.appendStream(s);
25   }
26   var seekable = multiplex.QueryInterface(Ci.nsISeekableStream);
27   var sis = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(
28     Ci.nsIScriptableInputStream
29   );
30   sis.init(seekable);
31   // Read some data.
32   var readData = sis.read(20);
33   Assert.equal(readData, data + data);
34   // -- Tests for NS_SEEK_SET
35   // Seek to a non-zero, non-stream-boundary offset.
36   seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 2);
37   Assert.equal(seekable.tell(), 2);
38   Assert.equal(seekable.available(), 98);
39   seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 9);
40   Assert.equal(seekable.tell(), 9);
41   Assert.equal(seekable.available(), 91);
42   // Seek across stream boundary.
43   seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 35);
44   Assert.equal(seekable.tell(), 35);
45   Assert.equal(seekable.available(), 65);
46   readData = sis.read(5);
47   Assert.equal(readData, data.slice(5));
48   Assert.equal(seekable.available(), 60);
49   // Seek at stream boundaries.
50   seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 40);
51   Assert.equal(seekable.tell(), 40);
52   Assert.equal(seekable.available(), 60);
53   readData = sis.read(10);
54   Assert.equal(readData, data);
55   Assert.equal(seekable.tell(), 50);
56   Assert.equal(seekable.available(), 50);
57   // Rewind and read across streams.
58   seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 39);
59   Assert.equal(seekable.tell(), 39);
60   Assert.equal(seekable.available(), 61);
61   readData = sis.read(11);
62   Assert.equal(readData, data.slice(9) + data);
63   Assert.equal(seekable.tell(), 50);
64   Assert.equal(seekable.available(), 50);
65   // Rewind to the beginning.
66   seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);
67   Assert.equal(seekable.tell(), 0);
68   Assert.equal(seekable.available(), 100);
69   // Seek to some random location
70   seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 50);
71   // -- Tests for NS_SEEK_CUR
72   // Positive seek.
73   seekable.seek(Ci.nsISeekableStream.NS_SEEK_CUR, 15);
74   Assert.equal(seekable.tell(), 65);
75   Assert.equal(seekable.available(), 35);
76   readData = sis.read(10);
77   Assert.equal(readData, data.slice(5) + data.slice(0, 5));
78   Assert.equal(seekable.tell(), 75);
79   Assert.equal(seekable.available(), 25);
80   // Negative seek.
81   seekable.seek(Ci.nsISeekableStream.NS_SEEK_CUR, -15);
82   Assert.equal(seekable.tell(), 60);
83   Assert.equal(seekable.available(), 40);
84   readData = sis.read(10);
85   Assert.equal(readData, data);
86   Assert.equal(seekable.tell(), 70);
87   Assert.equal(seekable.available(), 30);
89   // -- Tests for NS_SEEK_END
90   // Normal read.
91   seekable.seek(Ci.nsISeekableStream.NS_SEEK_END, -5);
92   Assert.equal(seekable.tell(), data.length * count - 5);
93   readData = sis.read(5);
94   Assert.equal(readData, data.slice(5));
95   Assert.equal(seekable.tell(), data.length * count);
96   // Read across streams.
97   seekable.seek(Ci.nsISeekableStream.NS_SEEK_END, -15);
98   Assert.equal(seekable.tell(), data.length * count - 15);
99   readData = sis.read(15);
100   Assert.equal(readData, data.slice(5) + data);
101   Assert.equal(seekable.tell(), data.length * count);
103   // -- Try to do various edge cases
104   // Forward seek from the end, should throw.
105   var caught = false;
106   try {
107     seekable.seek(Ci.nsISeekableStream.NS_SEEK_END, 15);
108   } catch (e) {
109     caught = true;
110   }
111   Assert.equal(caught, true);
112   Assert.equal(seekable.tell(), data.length * count);
113   // Backward seek from the beginning, should be clamped.
114   seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);
115   Assert.equal(seekable.tell(), 0);
116   seekable.seek(Ci.nsISeekableStream.NS_SEEK_CUR, -15);
117   Assert.equal(seekable.tell(), 0);
118   // Seek too far: should be clamped.
119   seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);
120   Assert.equal(seekable.tell(), 0);
121   seekable.seek(Ci.nsISeekableStream.NS_SEEK_CUR, 3 * data.length * count);
122   Assert.equal(seekable.tell(), 100);
123   seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, data.length * count);
124   Assert.equal(seekable.tell(), 100);
125   seekable.seek(Ci.nsISeekableStream.NS_SEEK_CUR, -2 * data.length * count);
126   Assert.equal(seekable.tell(), 0);
129 function test_multiplex_bug797871() {
130   var data2 = "1234567890123456789012345678901234567890";
132   var MultiplexStream = CC(
133     "@mozilla.org/io/multiplex-input-stream;1",
134     "nsIMultiplexInputStream"
135   );
136   Assert.equal(1, 1);
138   var multiplex = new MultiplexStream();
139   let s = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
140     Ci.nsIStringInputStream
141   );
142   s.setData(data2, data2.length);
144   multiplex.appendStream(s);
146   var seekable = multiplex.QueryInterface(Ci.nsISeekableStream);
147   var sis = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(
148     Ci.nsIScriptableInputStream
149   );
150   sis.init(seekable);
152   seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 8);
153   Assert.equal(seekable.tell(), 8);
154   sis.read(2);
155   Assert.equal(seekable.tell(), 10);
157   seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 20);
158   Assert.equal(seekable.tell(), 20);
161 function run_test() {
162   test_multiplex_streams();
163   test_multiplex_bug797871();