• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3function test_blob(fn, expectations) {
4  var expected = expectations.expected,
5      type = expectations.type,
6      desc = expectations.desc;
7
8  var t = async_test(desc);
9  t.step(function() {
10    var blob = fn();
11    assert_true(blob instanceof Blob);
12    assert_false(blob instanceof File);
13    assert_equals(blob.type, type);
14    assert_equals(blob.size, expected.length);
15
16    var fr = new FileReader();
17    fr.onload = t.step_func_done(function(event) {
18      assert_equals(this.result, expected);
19    }, fr);
20    fr.onerror = t.step_func(function(e) {
21      assert_unreached("got error event on FileReader");
22    });
23    fr.readAsText(blob, "UTF-8");
24  });
25}
26
27function test_blob_binary(fn, expectations) {
28  var expected = expectations.expected,
29      type = expectations.type,
30      desc = expectations.desc;
31
32  var t = async_test(desc);
33  t.step(function() {
34    var blob = fn();
35    assert_true(blob instanceof Blob);
36    assert_false(blob instanceof File);
37    assert_equals(blob.type, type);
38    assert_equals(blob.size, expected.length);
39
40    var fr = new FileReader();
41    fr.onload = t.step_func_done(function(event) {
42      assert_true(this.result instanceof ArrayBuffer,
43                  "Result should be an ArrayBuffer");
44      assert_array_equals(new Uint8Array(this.result), expected);
45    }, fr);
46    fr.onerror = t.step_func(function(e) {
47      assert_unreached("got error event on FileReader");
48    });
49    fr.readAsArrayBuffer(blob);
50  });
51}
52
53// Assert that two TypedArray objects have the same byte values
54self.assert_equals_typed_array = (array1, array2) => {
55  const [view1, view2] = [array1, array2].map((array) => {
56    assert_true(array.buffer instanceof ArrayBuffer,
57      'Expect input ArrayBuffers to contain field `buffer`');
58    return new DataView(array.buffer, array.byteOffset, array.byteLength);
59  });
60
61  assert_equals(view1.byteLength, view2.byteLength,
62    'Expect both arrays to be of the same byte length');
63
64  const byteLength = view1.byteLength;
65
66  for (let i = 0; i < byteLength; ++i) {
67    assert_equals(view1.getUint8(i), view2.getUint8(i),
68      `Expect byte at buffer position ${i} to be equal`);
69  }
70}
71