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