Andrew Top | 61a8495 | 2019-04-30 15:07:33 -0700 | [diff] [blame] | 1 | <!DOCTYPE html> |
| 2 | <meta charset=utf-8> |
| 3 | <title>Blob slice</title> |
| 4 | <link rel=help href="http://dev.w3.org/2006/webapi/FileAPI/#slice-method-algo"> |
| 5 | <link rel=author title="Saurabh Anand" href="mailto:saurabhanandiit@gmail.com"> |
| 6 | <script src="/resources/testharness.js"></script> |
| 7 | <script src="/resources/testharnessreport.js"></script> |
| 8 | <script src="../support/Blob.js"></script> |
| 9 | <div id="log"></div> |
| 10 | <script> |
| 11 | test_blob(function() { |
| 12 | var blobTemp = new Blob(["PASS"]); |
| 13 | return blobTemp.slice(); |
| 14 | }, { |
| 15 | expected: "PASS", |
| 16 | type: "", |
| 17 | desc: "no-argument Blob slice" |
| 18 | }); |
| 19 | |
| 20 | test(function() { |
| 21 | var blob1, blob2; |
| 22 | |
| 23 | test_blob(function() { |
| 24 | return blob1 = new Blob(["squiggle"]); |
| 25 | }, { |
| 26 | expected: "squiggle", |
| 27 | type: "", |
| 28 | desc: "blob1." |
| 29 | }); |
| 30 | |
| 31 | test_blob(function() { |
| 32 | return blob2 = new Blob(["steak"], {type: "content/type"}); |
| 33 | }, { |
| 34 | expected: "steak", |
| 35 | type: "content/type", |
| 36 | desc: "blob2." |
| 37 | }); |
| 38 | |
| 39 | var arrayBuffer = new ArrayBuffer(16); |
| 40 | var int8View = new Int8Array(arrayBuffer); |
| 41 | for (var i = 0; i < 16; i++) { |
| 42 | int8View[i] = i + 65; |
| 43 | } |
| 44 | |
| 45 | var testData = [ |
| 46 | [ |
| 47 | ["PASSSTRING"], |
| 48 | [{start: -6, contents: "STRING"}, |
| 49 | {start: -12, contents: "PASSSTRING"}, |
| 50 | {start: 4, contents: "STRING"}, |
| 51 | {start: 12, contents: ""}, |
| 52 | {start: 0, end: -6, contents: "PASS"}, |
| 53 | {start: 0, end: -12, contents: ""}, |
| 54 | {start: 0, end: 4, contents: "PASS"}, |
| 55 | {start: 0, end: 12, contents: "PASSSTRING"}, |
| 56 | {start: 7, end: 4, contents: ""}] |
| 57 | ], |
| 58 | |
| 59 | // Test 3 strings |
| 60 | [ |
| 61 | ["foo", "bar", "baz"], |
| 62 | [{start: 0, end: 9, contents: "foobarbaz"}, |
| 63 | {start: 0, end: 3, contents: "foo"}, |
| 64 | {start: 3, end: 9, contents: "barbaz"}, |
| 65 | {start: 6, end: 9, contents: "baz"}, |
| 66 | {start: 6, end: 12, contents: "baz"}, |
| 67 | {start: 0, end: 9, contents: "foobarbaz"}, |
| 68 | {start: 0, end: 11, contents: "foobarbaz"}, |
| 69 | {start: 10, end: 15, contents: ""}] |
| 70 | ], |
| 71 | |
| 72 | // Test string, Blob, string |
| 73 | [ |
| 74 | ["foo", blob1, "baz"], |
| 75 | [{start: 0, end: 3, contents: "foo"}, |
| 76 | {start: 3, end: 11, contents: "squiggle"}, |
| 77 | {start: 2, end: 4, contents: "os"}, |
| 78 | {start: 10, end: 12, contents: "eb"}] |
| 79 | ], |
| 80 | |
| 81 | // Test blob, string, blob |
| 82 | [ |
| 83 | [blob1, "foo", blob1], |
| 84 | [{start: 0, end: 8, contents: "squiggle"}, |
| 85 | {start: 7, end: 9, contents: "ef"}, |
| 86 | {start: 10, end: 12, contents: "os"}, |
| 87 | {start: 1, end: 4, contents: "qui"}, |
| 88 | {start: 12, end: 15, contents: "qui"}, |
| 89 | {start: 40, end: 60, contents: ""}] |
| 90 | ], |
| 91 | |
| 92 | // Test blobs all the way down |
| 93 | [ |
| 94 | [blob2, blob1, blob2], |
| 95 | [{start: 0, end: 5, contents: "steak"}, |
| 96 | {start: 5, end: 13, contents: "squiggle"}, |
| 97 | {start: 13, end: 18, contents: "steak"}, |
| 98 | {start: 1, end: 3, contents: "te"}, |
| 99 | {start: 6, end: 10, contents: "quig"}] |
| 100 | ], |
| 101 | |
| 102 | // Test an ArrayBufferView |
| 103 | [ |
| 104 | [int8View, blob1, "foo"], |
| 105 | [{start: 0, end: 8, contents: "ABCDEFGH"}, |
| 106 | {start: 8, end: 18, contents: "IJKLMNOPsq"}, |
| 107 | {start: 17, end: 20, contents: "qui"}, |
| 108 | {start: 4, end: 12, contents: "EFGHIJKL"}] |
| 109 | ], |
| 110 | |
| 111 | // Test a partial ArrayBufferView |
| 112 | [ |
| 113 | [new Uint8Array(arrayBuffer, 3, 5), blob1, "foo"], |
| 114 | [{start: 0, end: 8, contents: "DEFGHsqu"}, |
| 115 | {start: 8, end: 18, contents: "igglefoo"}, |
| 116 | {start: 4, end: 12, contents: "Hsquiggl"}] |
| 117 | ], |
| 118 | |
| 119 | // Test type coercion of a number |
| 120 | [ |
| 121 | [3, int8View, "foo"], |
| 122 | [{start: 0, end: 8, contents: "3ABCDEFG"}, |
| 123 | {start: 8, end: 18, contents: "HIJKLMNOPf"}, |
| 124 | {start: 17, end: 21, contents: "foo"}, |
| 125 | {start: 4, end: 12, contents: "DEFGHIJK"}] |
| 126 | ], |
| 127 | |
| 128 | [ |
| 129 | [(new Uint8Array([0, 255, 0])).buffer, |
| 130 | new Blob(['abcd']), |
| 131 | 'efgh', |
| 132 | 'ijklmnopqrstuvwxyz'], |
| 133 | [{start: 1, end: 4, contents: "\uFFFD\u0000a"}, |
| 134 | {start: 4, end: 8, contents: "bcde"}, |
| 135 | {start: 8, end: 12, contents: "fghi"}, |
| 136 | {start: 1, end: 12, contents: "\uFFFD\u0000abcdefghi"}] |
| 137 | ] |
| 138 | ]; |
| 139 | |
| 140 | testData.forEach(function(data, i) { |
| 141 | var blobs = data[0]; |
| 142 | var tests = data[1]; |
| 143 | tests.forEach(function(expectations, j) { |
| 144 | test(function() { |
| 145 | var blob = new Blob(blobs); |
| 146 | assert_true(blob instanceof Blob); |
| 147 | assert_false(blob instanceof File); |
| 148 | |
| 149 | test_blob(function() { |
| 150 | return expectations.end === undefined |
| 151 | ? blob.slice(expectations.start) |
| 152 | : blob.slice(expectations.start, expectations.end); |
| 153 | }, { |
| 154 | expected: expectations.contents, |
| 155 | type: "", |
| 156 | desc: "Slicing test: slice (" + i + "," + j + ")." |
| 157 | }); |
| 158 | }, "Slicing test (" + i + "," + j + ")."); |
| 159 | }); |
| 160 | }); |
| 161 | }, "Slices"); |
| 162 | |
| 163 | var invalidTypes = [ |
| 164 | "\xFF", |
| 165 | "te(xt/plain", |
| 166 | "te)xt/plain", |
| 167 | "te<xt/plain", |
| 168 | "te>xt/plain", |
| 169 | "te@xt/plain", |
| 170 | "te,xt/plain", |
| 171 | "te;xt/plain", |
| 172 | "te:xt/plain", |
| 173 | "te\\xt/plain", |
| 174 | "te\"xt/plain", |
| 175 | "te/xt/plain", |
| 176 | "te[xt/plain", |
| 177 | "te]xt/plain", |
| 178 | "te?xt/plain", |
| 179 | "te=xt/plain", |
| 180 | "te{xt/plain", |
| 181 | "te}xt/plain", |
| 182 | "te\x20xt/plain", |
| 183 | "te\x09xt/plain", |
| 184 | "te\x00xt/plain", |
| 185 | "te\x1Fxt/plain", |
| 186 | "te\x7Fxt/plain" |
| 187 | ]; |
| 188 | invalidTypes.forEach(function(type) { |
| 189 | test_blob(function() { |
| 190 | var blob = new Blob(["PASS"]); |
| 191 | return blob.slice(0, 4, type); |
| 192 | }, { |
| 193 | expected: "PASS", |
| 194 | type: "", |
| 195 | desc: "Invalid contentType (" + format_value(type) + ")" |
| 196 | }); |
| 197 | }); |
| 198 | |
| 199 | var validTypes = [ |
| 200 | "TEXT/PLAIN", |
| 201 | "text/plain;charset = UTF-8", |
| 202 | "text/plain;charset=UTF-8" |
| 203 | ]; |
| 204 | validTypes.forEach(function(type) { |
| 205 | test_blob(function() { |
| 206 | var blob = new Blob(["PASS"]); |
| 207 | return blob.slice(0, 4, type); |
| 208 | }, { |
| 209 | expected: "PASS", |
| 210 | type: type.toLowerCase(), |
| 211 | desc: "Valid contentType (" + format_value(type) + ")" |
| 212 | }); |
| 213 | }); |
| 214 | </script> |