test_bug1218029.js (3656B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 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 }, 50 { 51 data: "123456789", 52 chunks: [2, 3, 4], 53 status: Cr.NS_OK, 54 consume: [1, 2, 6], 55 dataChunks: ["12", "2345", "456789", ""], 56 }, 57 ]; 58 59 /** 60 * @typedef TestData 61 * @property {string} data - data for the test. 62 * @property {Array} chunks - lengths of the chunks that are incrementally sent 63 * to the loader. 64 * @property {number} status - final status sent on onStopRequest. 65 * @property {Array} consume - lengths of consumed data that is reported at 66 * the onIncrementalData callback. 67 * @property {Array} dataChunks - data chunks that are reported at the 68 * onIncrementalData and onStreamComplete callbacks. 69 */ 70 71 function execute_test(test) { 72 let stream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance( 73 Ci.nsIStringInputStream 74 ); 75 stream.setByteStringData(test.data); 76 77 let contentTypeCalled = 0; 78 79 let channel = { 80 contentLength: -1, 81 QueryInterface: ChromeUtils.generateQI(["nsIChannel"]), 82 get contentType() { 83 contentTypeCalled++; 84 return "application/test"; 85 }, 86 }; 87 88 let chunkIndex = 0; 89 90 let observer = { 91 onStartRequest(request) { 92 const chan = request.QueryInterface(Ci.nsIChannel); 93 const before = contentTypeCalled; 94 const type = chan.contentType; 95 const after = contentTypeCalled; 96 equal(type, "application/test"); 97 equal(after, before + 1); 98 }, 99 onStreamComplete(loader, context, status, length, data) { 100 equal(chunkIndex, test.dataChunks.length - 1); 101 var expectedChunk = test.dataChunks[chunkIndex]; 102 equal(length, expectedChunk.length); 103 equal(String.fromCharCode.apply(null, data), expectedChunk); 104 105 equal(status, test.status); 106 }, 107 onIncrementalData(loader, context, length, data, consumed) { 108 Assert.less(chunkIndex, test.dataChunks.length - 1); 109 var expectedChunk = test.dataChunks[chunkIndex]; 110 equal(length, expectedChunk.length); 111 equal(String.fromCharCode.apply(null, data), expectedChunk); 112 113 consumed.value = test.consume[chunkIndex]; 114 chunkIndex++; 115 }, 116 QueryInterface: ChromeUtils.generateQI([ 117 "nsIIncrementalStreamLoaderObserver", 118 ]), 119 }; 120 121 let listener = Cc[ 122 "@mozilla.org/network/incremental-stream-loader;1" 123 ].createInstance(Ci.nsIIncrementalStreamLoader); 124 listener.init(observer); 125 126 listener.onStartRequest(channel); 127 var offset = 0; 128 test.chunks.forEach(function (chunkLength) { 129 listener.onDataAvailable(channel, stream, offset, chunkLength); 130 offset += chunkLength; 131 }); 132 listener.onStopRequest(channel, test.status); 133 } 134 135 function run_test() { 136 tests.forEach(execute_test); 137 }