3 var CC
= Components
.Constructor
;
5 const StreamCopier
= CC(
6 "@mozilla.org/network/async-stream-copier;1",
7 "nsIAsyncStreamCopier",
11 const ScriptableInputStream
= CC(
12 "@mozilla.org/scriptableinputstream;1",
13 "nsIScriptableInputStream",
17 const Pipe
= CC("@mozilla.org/pipe;1", "nsIPipe", "init");
24 var test_source_closed
;
31 onStopRequest(request
, statusCode
) {
33 Assert
.equal(statusCode
, test_result
);
35 // check number of copied bytes
36 Assert
.equal(pipe2
.inputStream
.available(), test_content
.length
);
39 var scinp
= new ScriptableInputStream(pipe2
.inputStream
);
40 var content
= scinp
.read(scinp
.available());
41 Assert
.equal(content
, test_content
);
45 pipe2
.outputStream
.write("closedSinkTest", 14);
46 Assert
.ok(!test_sink_closed
);
48 Assert
.ok(test_sink_closed
);
51 // check closed source
53 pipe1
.outputStream
.write("closedSourceTest", 16);
54 Assert
.ok(!test_source_closed
);
56 Assert
.ok(test_source_closed
);
59 do_timeout(0, do_test
);
62 QueryInterface
: ChromeUtils
.generateQI(["nsIRequestObserver"]),
65 function startCopier(closeSource
, closeSink
) {
67 true /* nonBlockingInput */,
68 true /* nonBlockingOutput */,
70 0xffffffff /* segmentCount */,
71 null /* segmentAllocator */
75 true /* nonBlockingInput */,
76 true /* nonBlockingOutput */,
78 0xffffffff /* segmentCount */,
79 null /* segmentAllocator */
82 copier
= new StreamCopier(
83 pipe1
.inputStream
/* aSource */,
84 pipe2
.outputStream
/* aSink */,
86 true /* aSourceBuffered */,
87 true /* aSinkBuffered */,
88 8192 /* aChunkSize */,
89 closeSource
/* aCloseSource */,
90 closeSink
/* aCloseSink */
93 copier
.asyncCopy(copyObserver
, null);
98 test_content
= "test" + test_nr
;
102 case 2: // close sink
103 case 3: // close source
104 case 4: // close both
105 // test canceling transfer
106 // use some undefined error code to check if it is successfully passed
107 // to the request observer
108 test_result
= 0x87654321;
110 test_source_closed
= (test_nr
- 1) >> 1 != 0;
111 test_sink_closed
= (test_nr
- 1) % 2 != 0;
113 startCopier(test_source_closed
, test_sink_closed
);
114 pipe1
.outputStream
.write(test_content
, test_content
.length
);
115 pipe1
.outputStream
.flush();
116 do_timeout(20, function () {
117 copier
.cancel(test_result
);
118 pipe1
.outputStream
.write("a", 1);
122 case 6: // close sink
123 case 7: // close source
124 case 8: // close both
125 // test copying with EOF on source
128 test_source_closed
= (test_nr
- 5) >> 1 != 0;
129 test_sink_closed
= (test_nr
- 5) % 2 != 0;
131 startCopier(test_source_closed
, test_sink_closed
);
132 pipe1
.outputStream
.write(test_content
, test_content
.length
);
133 // we will close the source
134 test_source_closed
= true;
135 pipe1
.outputStream
.close();
138 case 10: // close sink
139 case 11: // close source
140 case 12: // close both
141 // test copying with error on sink
142 // use some undefined error code to check if it is successfully passed
143 // to the request observer
144 test_result
= 0x87654321;
146 test_source_closed
= (test_nr
- 9) >> 1 != 0;
147 test_sink_closed
= (test_nr
- 9) % 2 != 0;
149 startCopier(test_source_closed
, test_sink_closed
);
150 pipe1
.outputStream
.write(test_content
, test_content
.length
);
151 pipe1
.outputStream
.flush();
152 // we will close the sink
153 test_sink_closed
= true;
154 do_timeout(20, function () {
156 .QueryInterface(Ci
.nsIAsyncOutputStream
)
157 .closeWithStatus(test_result
);
158 pipe1
.outputStream
.write("a", 1);
167 function run_test() {
169 do_timeout(0, do_test
);