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