11cb0ef41Sopenharmony_ci// META: title=Blob slice 21cb0ef41Sopenharmony_ci// META: script=../support/Blob.js 31cb0ef41Sopenharmony_ci'use strict'; 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_citest_blob(function() { 61cb0ef41Sopenharmony_ci var blobTemp = new Blob(["PASS"]); 71cb0ef41Sopenharmony_ci return blobTemp.slice(); 81cb0ef41Sopenharmony_ci}, { 91cb0ef41Sopenharmony_ci expected: "PASS", 101cb0ef41Sopenharmony_ci type: "", 111cb0ef41Sopenharmony_ci desc: "no-argument Blob slice" 121cb0ef41Sopenharmony_ci}); 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_citest(function() { 151cb0ef41Sopenharmony_ci var blob1, blob2; 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci test_blob(function() { 181cb0ef41Sopenharmony_ci return blob1 = new Blob(["squiggle"]); 191cb0ef41Sopenharmony_ci }, { 201cb0ef41Sopenharmony_ci expected: "squiggle", 211cb0ef41Sopenharmony_ci type: "", 221cb0ef41Sopenharmony_ci desc: "blob1." 231cb0ef41Sopenharmony_ci }); 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci test_blob(function() { 261cb0ef41Sopenharmony_ci return blob2 = new Blob(["steak"], {type: "content/type"}); 271cb0ef41Sopenharmony_ci }, { 281cb0ef41Sopenharmony_ci expected: "steak", 291cb0ef41Sopenharmony_ci type: "content/type", 301cb0ef41Sopenharmony_ci desc: "blob2." 311cb0ef41Sopenharmony_ci }); 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci test_blob(function() { 341cb0ef41Sopenharmony_ci return new Blob().slice(0,0,null); 351cb0ef41Sopenharmony_ci }, { 361cb0ef41Sopenharmony_ci expected: "", 371cb0ef41Sopenharmony_ci type: "null", 381cb0ef41Sopenharmony_ci desc: "null type Blob slice" 391cb0ef41Sopenharmony_ci }); 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci test_blob(function() { 421cb0ef41Sopenharmony_ci return new Blob().slice(0,0,undefined); 431cb0ef41Sopenharmony_ci }, { 441cb0ef41Sopenharmony_ci expected: "", 451cb0ef41Sopenharmony_ci type: "", 461cb0ef41Sopenharmony_ci desc: "undefined type Blob slice" 471cb0ef41Sopenharmony_ci }); 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci test_blob(function() { 501cb0ef41Sopenharmony_ci return new Blob().slice(0,0); 511cb0ef41Sopenharmony_ci }, { 521cb0ef41Sopenharmony_ci expected: "", 531cb0ef41Sopenharmony_ci type: "", 541cb0ef41Sopenharmony_ci desc: "no type Blob slice" 551cb0ef41Sopenharmony_ci }); 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci var arrayBuffer = new ArrayBuffer(16); 581cb0ef41Sopenharmony_ci var int8View = new Int8Array(arrayBuffer); 591cb0ef41Sopenharmony_ci for (var i = 0; i < 16; i++) { 601cb0ef41Sopenharmony_ci int8View[i] = i + 65; 611cb0ef41Sopenharmony_ci } 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci var testData = [ 641cb0ef41Sopenharmony_ci [ 651cb0ef41Sopenharmony_ci ["PASSSTRING"], 661cb0ef41Sopenharmony_ci [{start: -6, contents: "STRING"}, 671cb0ef41Sopenharmony_ci {start: -12, contents: "PASSSTRING"}, 681cb0ef41Sopenharmony_ci {start: 4, contents: "STRING"}, 691cb0ef41Sopenharmony_ci {start: 12, contents: ""}, 701cb0ef41Sopenharmony_ci {start: 0, end: -6, contents: "PASS"}, 711cb0ef41Sopenharmony_ci {start: 0, end: -12, contents: ""}, 721cb0ef41Sopenharmony_ci {start: 0, end: 4, contents: "PASS"}, 731cb0ef41Sopenharmony_ci {start: 0, end: 12, contents: "PASSSTRING"}, 741cb0ef41Sopenharmony_ci {start: 7, end: 4, contents: ""}] 751cb0ef41Sopenharmony_ci ], 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci // Test 3 strings 781cb0ef41Sopenharmony_ci [ 791cb0ef41Sopenharmony_ci ["foo", "bar", "baz"], 801cb0ef41Sopenharmony_ci [{start: 0, end: 9, contents: "foobarbaz"}, 811cb0ef41Sopenharmony_ci {start: 0, end: 3, contents: "foo"}, 821cb0ef41Sopenharmony_ci {start: 3, end: 9, contents: "barbaz"}, 831cb0ef41Sopenharmony_ci {start: 6, end: 9, contents: "baz"}, 841cb0ef41Sopenharmony_ci {start: 6, end: 12, contents: "baz"}, 851cb0ef41Sopenharmony_ci {start: 0, end: 9, contents: "foobarbaz"}, 861cb0ef41Sopenharmony_ci {start: 0, end: 11, contents: "foobarbaz"}, 871cb0ef41Sopenharmony_ci {start: 10, end: 15, contents: ""}] 881cb0ef41Sopenharmony_ci ], 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci // Test string, Blob, string 911cb0ef41Sopenharmony_ci [ 921cb0ef41Sopenharmony_ci ["foo", blob1, "baz"], 931cb0ef41Sopenharmony_ci [{start: 0, end: 3, contents: "foo"}, 941cb0ef41Sopenharmony_ci {start: 3, end: 11, contents: "squiggle"}, 951cb0ef41Sopenharmony_ci {start: 2, end: 4, contents: "os"}, 961cb0ef41Sopenharmony_ci {start: 10, end: 12, contents: "eb"}] 971cb0ef41Sopenharmony_ci ], 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci // Test blob, string, blob 1001cb0ef41Sopenharmony_ci [ 1011cb0ef41Sopenharmony_ci [blob1, "foo", blob1], 1021cb0ef41Sopenharmony_ci [{start: 0, end: 8, contents: "squiggle"}, 1031cb0ef41Sopenharmony_ci {start: 7, end: 9, contents: "ef"}, 1041cb0ef41Sopenharmony_ci {start: 10, end: 12, contents: "os"}, 1051cb0ef41Sopenharmony_ci {start: 1, end: 4, contents: "qui"}, 1061cb0ef41Sopenharmony_ci {start: 12, end: 15, contents: "qui"}, 1071cb0ef41Sopenharmony_ci {start: 40, end: 60, contents: ""}] 1081cb0ef41Sopenharmony_ci ], 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ci // Test blobs all the way down 1111cb0ef41Sopenharmony_ci [ 1121cb0ef41Sopenharmony_ci [blob2, blob1, blob2], 1131cb0ef41Sopenharmony_ci [{start: 0, end: 5, contents: "steak"}, 1141cb0ef41Sopenharmony_ci {start: 5, end: 13, contents: "squiggle"}, 1151cb0ef41Sopenharmony_ci {start: 13, end: 18, contents: "steak"}, 1161cb0ef41Sopenharmony_ci {start: 1, end: 3, contents: "te"}, 1171cb0ef41Sopenharmony_ci {start: 6, end: 10, contents: "quig"}] 1181cb0ef41Sopenharmony_ci ], 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_ci // Test an ArrayBufferView 1211cb0ef41Sopenharmony_ci [ 1221cb0ef41Sopenharmony_ci [int8View, blob1, "foo"], 1231cb0ef41Sopenharmony_ci [{start: 0, end: 8, contents: "ABCDEFGH"}, 1241cb0ef41Sopenharmony_ci {start: 8, end: 18, contents: "IJKLMNOPsq"}, 1251cb0ef41Sopenharmony_ci {start: 17, end: 20, contents: "qui"}, 1261cb0ef41Sopenharmony_ci {start: 4, end: 12, contents: "EFGHIJKL"}] 1271cb0ef41Sopenharmony_ci ], 1281cb0ef41Sopenharmony_ci 1291cb0ef41Sopenharmony_ci // Test a partial ArrayBufferView 1301cb0ef41Sopenharmony_ci [ 1311cb0ef41Sopenharmony_ci [new Uint8Array(arrayBuffer, 3, 5), blob1, "foo"], 1321cb0ef41Sopenharmony_ci [{start: 0, end: 8, contents: "DEFGHsqu"}, 1331cb0ef41Sopenharmony_ci {start: 8, end: 18, contents: "igglefoo"}, 1341cb0ef41Sopenharmony_ci {start: 4, end: 12, contents: "Hsquiggl"}] 1351cb0ef41Sopenharmony_ci ], 1361cb0ef41Sopenharmony_ci 1371cb0ef41Sopenharmony_ci // Test type coercion of a number 1381cb0ef41Sopenharmony_ci [ 1391cb0ef41Sopenharmony_ci [3, int8View, "foo"], 1401cb0ef41Sopenharmony_ci [{start: 0, end: 8, contents: "3ABCDEFG"}, 1411cb0ef41Sopenharmony_ci {start: 8, end: 18, contents: "HIJKLMNOPf"}, 1421cb0ef41Sopenharmony_ci {start: 17, end: 21, contents: "foo"}, 1431cb0ef41Sopenharmony_ci {start: 4, end: 12, contents: "DEFGHIJK"}] 1441cb0ef41Sopenharmony_ci ], 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_ci [ 1471cb0ef41Sopenharmony_ci [(new Uint8Array([0, 255, 0])).buffer, 1481cb0ef41Sopenharmony_ci new Blob(['abcd']), 1491cb0ef41Sopenharmony_ci 'efgh', 1501cb0ef41Sopenharmony_ci 'ijklmnopqrstuvwxyz'], 1511cb0ef41Sopenharmony_ci [{start: 1, end: 4, contents: "\uFFFD\u0000a"}, 1521cb0ef41Sopenharmony_ci {start: 4, end: 8, contents: "bcde"}, 1531cb0ef41Sopenharmony_ci {start: 8, end: 12, contents: "fghi"}, 1541cb0ef41Sopenharmony_ci {start: 1, end: 12, contents: "\uFFFD\u0000abcdefghi"}] 1551cb0ef41Sopenharmony_ci ] 1561cb0ef41Sopenharmony_ci ]; 1571cb0ef41Sopenharmony_ci 1581cb0ef41Sopenharmony_ci testData.forEach(function(data, i) { 1591cb0ef41Sopenharmony_ci var blobs = data[0]; 1601cb0ef41Sopenharmony_ci var tests = data[1]; 1611cb0ef41Sopenharmony_ci tests.forEach(function(expectations, j) { 1621cb0ef41Sopenharmony_ci test(function() { 1631cb0ef41Sopenharmony_ci var blob = new Blob(blobs); 1641cb0ef41Sopenharmony_ci assert_true(blob instanceof Blob); 1651cb0ef41Sopenharmony_ci assert_false(blob instanceof File); 1661cb0ef41Sopenharmony_ci 1671cb0ef41Sopenharmony_ci test_blob(function() { 1681cb0ef41Sopenharmony_ci return expectations.end === undefined 1691cb0ef41Sopenharmony_ci ? blob.slice(expectations.start) 1701cb0ef41Sopenharmony_ci : blob.slice(expectations.start, expectations.end); 1711cb0ef41Sopenharmony_ci }, { 1721cb0ef41Sopenharmony_ci expected: expectations.contents, 1731cb0ef41Sopenharmony_ci type: "", 1741cb0ef41Sopenharmony_ci desc: "Slicing test: slice (" + i + "," + j + ")." 1751cb0ef41Sopenharmony_ci }); 1761cb0ef41Sopenharmony_ci }, "Slicing test (" + i + "," + j + ")."); 1771cb0ef41Sopenharmony_ci }); 1781cb0ef41Sopenharmony_ci }); 1791cb0ef41Sopenharmony_ci}, "Slices"); 1801cb0ef41Sopenharmony_ci 1811cb0ef41Sopenharmony_civar invalidTypes = [ 1821cb0ef41Sopenharmony_ci "\xFF", 1831cb0ef41Sopenharmony_ci "te\x09xt/plain", 1841cb0ef41Sopenharmony_ci "te\x00xt/plain", 1851cb0ef41Sopenharmony_ci "te\x1Fxt/plain", 1861cb0ef41Sopenharmony_ci "te\x7Fxt/plain" 1871cb0ef41Sopenharmony_ci]; 1881cb0ef41Sopenharmony_ciinvalidTypes.forEach(function(type) { 1891cb0ef41Sopenharmony_ci test_blob(function() { 1901cb0ef41Sopenharmony_ci var blob = new Blob(["PASS"]); 1911cb0ef41Sopenharmony_ci return blob.slice(0, 4, type); 1921cb0ef41Sopenharmony_ci }, { 1931cb0ef41Sopenharmony_ci expected: "PASS", 1941cb0ef41Sopenharmony_ci type: "", 1951cb0ef41Sopenharmony_ci desc: "Invalid contentType (" + format_value(type) + ")" 1961cb0ef41Sopenharmony_ci }); 1971cb0ef41Sopenharmony_ci}); 1981cb0ef41Sopenharmony_ci 1991cb0ef41Sopenharmony_civar validTypes = [ 2001cb0ef41Sopenharmony_ci "te(xt/plain", 2011cb0ef41Sopenharmony_ci "te)xt/plain", 2021cb0ef41Sopenharmony_ci "te<xt/plain", 2031cb0ef41Sopenharmony_ci "te>xt/plain", 2041cb0ef41Sopenharmony_ci "te@xt/plain", 2051cb0ef41Sopenharmony_ci "te,xt/plain", 2061cb0ef41Sopenharmony_ci "te;xt/plain", 2071cb0ef41Sopenharmony_ci "te:xt/plain", 2081cb0ef41Sopenharmony_ci "te\\xt/plain", 2091cb0ef41Sopenharmony_ci "te\"xt/plain", 2101cb0ef41Sopenharmony_ci "te/xt/plain", 2111cb0ef41Sopenharmony_ci "te[xt/plain", 2121cb0ef41Sopenharmony_ci "te]xt/plain", 2131cb0ef41Sopenharmony_ci "te?xt/plain", 2141cb0ef41Sopenharmony_ci "te=xt/plain", 2151cb0ef41Sopenharmony_ci "te{xt/plain", 2161cb0ef41Sopenharmony_ci "te}xt/plain", 2171cb0ef41Sopenharmony_ci "te\x20xt/plain", 2181cb0ef41Sopenharmony_ci "TEXT/PLAIN", 2191cb0ef41Sopenharmony_ci "text/plain;charset = UTF-8", 2201cb0ef41Sopenharmony_ci "text/plain;charset=UTF-8" 2211cb0ef41Sopenharmony_ci]; 2221cb0ef41Sopenharmony_civalidTypes.forEach(function(type) { 2231cb0ef41Sopenharmony_ci test_blob(function() { 2241cb0ef41Sopenharmony_ci var blob = new Blob(["PASS"]); 2251cb0ef41Sopenharmony_ci return blob.slice(0, 4, type); 2261cb0ef41Sopenharmony_ci }, { 2271cb0ef41Sopenharmony_ci expected: "PASS", 2281cb0ef41Sopenharmony_ci type: type.toLowerCase(), 2291cb0ef41Sopenharmony_ci desc: "Valid contentType (" + format_value(type) + ")" 2301cb0ef41Sopenharmony_ci }); 2311cb0ef41Sopenharmony_ci}); 232