summaryrefslogtreecommitdiffstats
path: root/test/wpt/tests/FileAPI/blob/Blob-slice.any.js
blob: 1f85d44d269191d9b9338a3a9fe48cbc5200045f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// META: title=Blob slice
// META: script=../support/Blob.js
'use strict';

test_blob(function() {
  var blobTemp = new Blob(["PASS"]);
  return blobTemp.slice();
}, {
  expected: "PASS",
  type: "",
  desc: "no-argument Blob slice"
});

test(function() {
  var blob1, blob2;

  test_blob(function() {
    return blob1 = new Blob(["squiggle"]);
  }, {
    expected: "squiggle",
    type: "",
    desc: "blob1."
  });

  test_blob(function() {
    return blob2 = new Blob(["steak"], {type: "content/type"});
  }, {
    expected: "steak",
    type: "content/type",
    desc: "blob2."
  });

  test_blob(function() {
    return new Blob().slice(0,0,null);
  }, {
    expected: "",
    type: "null",
    desc: "null type Blob slice"
  });

  test_blob(function() {
    return new Blob().slice(0,0,undefined);
  }, {
    expected: "",
    type: "",
    desc: "undefined type Blob slice"
  });

  test_blob(function() {
    return new Blob().slice(0,0);
  }, {
    expected: "",
    type: "",
    desc: "no type Blob slice"
  });

  var arrayBuffer = new ArrayBuffer(16);
  var int8View = new Int8Array(arrayBuffer);
  for (var i = 0; i < 16; i++) {
    int8View[i] = i + 65;
  }

  var testData = [
    [
      ["PASSSTRING"],
      [{start:  -6, contents: "STRING"},
       {start: -12, contents: "PASSSTRING"},
       {start:   4, contents: "STRING"},
       {start:  12, contents: ""},
       {start: 0, end:  -6, contents: "PASS"},
       {start: 0, end: -12, contents: ""},
       {start: 0, end:   4, contents: "PASS"},
       {start: 0, end:  12, contents: "PASSSTRING"},
       {start: 7, end:   4, contents: ""}]
    ],

    // Test 3 strings
    [
      ["foo", "bar", "baz"],
      [{start:  0, end:  9, contents: "foobarbaz"},
       {start:  0, end:  3, contents: "foo"},
       {start:  3, end:  9, contents: "barbaz"},
       {start:  6, end:  9, contents: "baz"},
       {start:  6, end: 12, contents: "baz"},
       {start:  0, end:  9, contents: "foobarbaz"},
       {start:  0, end: 11, contents: "foobarbaz"},
       {start: 10, end: 15, contents: ""}]
    ],

    // Test string, Blob, string
    [
      ["foo", blob1, "baz"],
      [{start:  0, end:  3, contents: "foo"},
       {start:  3, end: 11, contents: "squiggle"},
       {start:  2, end:  4, contents: "os"},
       {start: 10, end: 12, contents: "eb"}]
    ],

    // Test blob, string, blob
    [
      [blob1, "foo", blob1],
      [{start:  0, end:  8, contents: "squiggle"},
       {start:  7, end:  9, contents: "ef"},
       {start: 10, end: 12, contents: "os"},
       {start:  1, end:  4, contents: "qui"},
       {start: 12, end: 15, contents: "qui"},
       {start: 40, end: 60, contents: ""}]
    ],

    // Test blobs all the way down
    [
      [blob2, blob1, blob2],
      [{start: 0,  end:  5, contents: "steak"},
       {start: 5,  end: 13, contents: "squiggle"},
       {start: 13, end: 18, contents: "steak"},
       {start:  1, end:  3, contents: "te"},
       {start:  6, end: 10, contents: "quig"}]
    ],

    // Test an ArrayBufferView
    [
      [int8View, blob1, "foo"],
      [{start:  0, end:  8, contents: "ABCDEFGH"},
       {start:  8, end: 18, contents: "IJKLMNOPsq"},
       {start: 17, end: 20, contents: "qui"},
       {start:  4, end: 12, contents: "EFGHIJKL"}]
    ],

    // Test a partial ArrayBufferView
    [
      [new Uint8Array(arrayBuffer, 3, 5), blob1, "foo"],
      [{start:  0, end:  8, contents: "DEFGHsqu"},
       {start:  8, end: 18, contents: "igglefoo"},
       {start:  4, end: 12, contents: "Hsquiggl"}]
    ],

    // Test type coercion of a number
    [
      [3, int8View, "foo"],
      [{start:  0, end:  8, contents: "3ABCDEFG"},
       {start:  8, end: 18, contents: "HIJKLMNOPf"},
       {start: 17, end: 21, contents: "foo"},
       {start:  4, end: 12, contents: "DEFGHIJK"}]
    ],

    [
      [(new Uint8Array([0, 255, 0])).buffer,
       new Blob(['abcd']),
       'efgh',
       'ijklmnopqrstuvwxyz'],
      [{start:  1, end:  4, contents: "\uFFFD\u0000a"},
       {start:  4, end:  8, contents: "bcde"},
       {start:  8, end: 12, contents: "fghi"},
       {start:  1, end: 12, contents: "\uFFFD\u0000abcdefghi"}]
    ]
  ];

  testData.forEach(function(data, i) {
    var blobs = data[0];
    var tests = data[1];
    tests.forEach(function(expectations, j) {
      test(function() {
        var blob = new Blob(blobs);
        assert_true(blob instanceof Blob);
        assert_false(blob instanceof File);

        test_blob(function() {
          return expectations.end === undefined
                 ? blob.slice(expectations.start)
                 : blob.slice(expectations.start, expectations.end);
        }, {
          expected: expectations.contents,
          type: "",
          desc: "Slicing test: slice (" + i + "," + j + ")."
        });
      }, "Slicing test (" + i + "," + j + ").");
    });
  });
}, "Slices");

var invalidTypes = [
  "\xFF",
  "te\x09xt/plain",
  "te\x00xt/plain",
  "te\x1Fxt/plain",
  "te\x7Fxt/plain"
];
invalidTypes.forEach(function(type) {
  test_blob(function() {
    var blob = new Blob(["PASS"]);
    return blob.slice(0, 4, type);
  }, {
    expected: "PASS",
    type: "",
    desc: "Invalid contentType (" + format_value(type) + ")"
  });
});

var validTypes = [
  "te(xt/plain",
  "te)xt/plain",
  "te<xt/plain",
  "te>xt/plain",
  "te@xt/plain",
  "te,xt/plain",
  "te;xt/plain",
  "te:xt/plain",
  "te\\xt/plain",
  "te\"xt/plain",
  "te/xt/plain",
  "te[xt/plain",
  "te]xt/plain",
  "te?xt/plain",
  "te=xt/plain",
  "te{xt/plain",
  "te}xt/plain",
  "te\x20xt/plain",
  "TEXT/PLAIN",
  "text/plain;charset = UTF-8",
  "text/plain;charset=UTF-8"
];
validTypes.forEach(function(type) {
  test_blob(function() {
    var blob = new Blob(["PASS"]);
    return blob.slice(0, 4, type);
  }, {
    expected: "PASS",
    type: type.toLowerCase(),
    desc: "Valid contentType (" + format_value(type) + ")"
  });
});