Bug 1796551 [wpt PR 36570] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / netwerk / test / unit / test_bug1218029.js
blobde49952623e8469e9e1e40f1eb79e22817fa6284
1 /* Any copyright is dedicated to the Public Domain.
2    http://creativecommons.org/publicdomain/zero/1.0/ */
4 "use strict";
6 var tests = [
7   { data: "", chunks: [], status: Cr.NS_OK, consume: [], dataChunks: [""] },
8   {
9     data: "TWO-PARTS",
10     chunks: [4, 5],
11     status: Cr.NS_OK,
12     consume: [4, 5],
13     dataChunks: ["TWO-", "PARTS", ""],
14   },
15   {
16     data: "TWO-PARTS",
17     chunks: [4, 5],
18     status: Cr.NS_OK,
19     consume: [0, 0],
20     dataChunks: ["TWO-", "TWO-PARTS", "TWO-PARTS"],
21   },
22   {
23     data: "3-PARTS",
24     chunks: [1, 1, 5],
25     status: Cr.NS_OK,
26     consume: [0, 2, 5],
27     dataChunks: ["3", "3-", "PARTS", ""],
28   },
29   {
30     data: "ALL-AT-ONCE",
31     chunks: [11],
32     status: Cr.NS_OK,
33     consume: [0],
34     dataChunks: ["ALL-AT-ONCE", "ALL-AT-ONCE"],
35   },
36   {
37     data: "ALL-AT-ONCE",
38     chunks: [11],
39     status: Cr.NS_OK,
40     consume: [11],
41     dataChunks: ["ALL-AT-ONCE", ""],
42   },
43   {
44     data: "ERROR",
45     chunks: [1],
46     status: Cr.NS_ERROR_OUT_OF_MEMORY,
47     consume: [0],
48     dataChunks: ["E", "E"],
49   },
52 /**
53  * @typedef TestData
54  * @property {string} data - data for the test.
55  * @property {Array} chunks - lengths of the chunks that are incrementally sent
56  *   to the loader.
57  * @property {number} status - final status sent on onStopRequest.
58  * @property {Array} consume - lengths of consumed data that is reported at
59  *   the onIncrementalData callback.
60  * @property {Array} dataChunks - data chunks that are reported at the
61  *   onIncrementalData and onStreamComplete callbacks.
62  */
64 function execute_test(test) {
65   let stream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
66     Ci.nsIStringInputStream
67   );
68   stream.data = test.data;
70   let channel = {
71     contentLength: -1,
72     QueryInterface: ChromeUtils.generateQI(["nsIChannel"]),
73   };
75   let chunkIndex = 0;
77   let observer = {
78     onStreamComplete(loader, context, status, length, data) {
79       equal(chunkIndex, test.dataChunks.length - 1);
80       var expectedChunk = test.dataChunks[chunkIndex];
81       equal(length, expectedChunk.length);
82       equal(String.fromCharCode.apply(null, data), expectedChunk);
84       equal(status, test.status);
85     },
86     onIncrementalData(loader, context, length, data, consumed) {
87       ok(chunkIndex < test.dataChunks.length - 1);
88       var expectedChunk = test.dataChunks[chunkIndex];
89       equal(length, expectedChunk.length);
90       equal(String.fromCharCode.apply(null, data), expectedChunk);
92       consumed.value = test.consume[chunkIndex];
93       chunkIndex++;
94     },
95     QueryInterface: ChromeUtils.generateQI([
96       "nsIIncrementalStreamLoaderObserver",
97     ]),
98   };
100   let listener = Cc[
101     "@mozilla.org/network/incremental-stream-loader;1"
102   ].createInstance(Ci.nsIIncrementalStreamLoader);
103   listener.init(observer);
105   listener.onStartRequest(channel);
106   var offset = 0;
107   test.chunks.forEach(function(chunkLength) {
108     listener.onDataAvailable(channel, stream, offset, chunkLength);
109     offset += chunkLength;
110   });
111   listener.onStopRequest(channel, test.status);
114 function run_test() {
115   tests.forEach(execute_test);