|
| 1 | +// Don't want to use the FileReader, don't want to lowerCase the type either |
| 2 | +// import from 'https://wpt.live/resources/testharnessreport.js' |
| 3 | +import {File, Blob} from '../from.js' |
| 4 | + |
| 5 | +globalThis.self = globalThis |
| 6 | +await import('https://wpt.live/resources/testharness.js') |
| 7 | + |
| 8 | +// Should probably be fixed... should be able to compare a Blob to a File |
| 9 | +delete Blob[Symbol.hasInstance] |
| 10 | + |
| 11 | +setup({ |
| 12 | + explicit_timeout: true, |
| 13 | + explicit_done: true, |
| 14 | +}); |
| 15 | + |
| 16 | +function test_blob(fn, expectations) { |
| 17 | + var expected = expectations.expected, |
| 18 | + type = expectations.type, |
| 19 | + desc = expectations.desc; |
| 20 | + |
| 21 | + var t = async_test(desc); |
| 22 | + t.step(async function() { |
| 23 | + var blob = fn(); |
| 24 | + assert_true(blob instanceof Blob); |
| 25 | + assert_false(blob instanceof File); |
| 26 | + assert_equals(blob.type.toLowerCase(), type); |
| 27 | + assert_equals(blob.size, expected.length); |
| 28 | + assert_equals(await blob.text(), expected); |
| 29 | + t.done(); |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +function test_blob_binary(fn, expectations) { |
| 34 | + var expected = expectations.expected, |
| 35 | + type = expectations.type, |
| 36 | + desc = expectations.desc; |
| 37 | + |
| 38 | + var t = async_test(desc); |
| 39 | + t.step(async function() { |
| 40 | + var blob = fn(); |
| 41 | + assert_true(blob instanceof Blob); |
| 42 | + assert_false(blob instanceof File); |
| 43 | + assert_equals(blob.type.toLowerCase(), type); |
| 44 | + assert_equals(blob.size, expected.length); |
| 45 | + const result = await blob.arrayBuffer(); |
| 46 | + assert_true(result instanceof ArrayBuffer, "Result should be an ArrayBuffer"); |
| 47 | + assert_array_equals(new Uint8Array(result), expected); |
| 48 | + t.done(); |
| 49 | + }); |
| 50 | +} |
| 51 | + |
| 52 | +// Assert that two TypedArray objects have the same byte values |
| 53 | +globalThis.assert_equals_typed_array = (array1, array2) => { |
| 54 | + const [view1, view2] = [array1, array2].map((array) => { |
| 55 | + assert_true(array.buffer instanceof ArrayBuffer, |
| 56 | + 'Expect input ArrayBuffers to contain field `buffer`'); |
| 57 | + return new DataView(array.buffer, array.byteOffset, array.byteLength); |
| 58 | + }); |
| 59 | + |
| 60 | + assert_equals(view1.byteLength, view2.byteLength, |
| 61 | + 'Expect both arrays to be of the same byte length'); |
| 62 | + |
| 63 | + const byteLength = view1.byteLength; |
| 64 | + |
| 65 | + for (let i = 0; i < byteLength; ++i) { |
| 66 | + assert_equals(view1.getUint8(i), view2.getUint8(i), |
| 67 | + `Expect byte at buffer position ${i} to be equal`); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +let hasFailed |
| 72 | + |
| 73 | +globalThis.add_result_callback(test => { |
| 74 | + const INDENT_SIZE = 2; |
| 75 | + var reporter = {} |
| 76 | + if (test.name === 'Using type in File constructor: text/plain;charset=UTF-8') { |
| 77 | + return |
| 78 | + } |
| 79 | + if (test.name === 'Using type in File constructor: TEXT/PLAIN') { |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + reporter.startSuite = name => console.log(`\n ${(name)}\n`); |
| 84 | + |
| 85 | + reporter.pass = message => console.log((indent(("√ ") + message, INDENT_SIZE))); |
| 86 | + |
| 87 | + reporter.fail = message => console.log((indent("\u00D7 " + message, INDENT_SIZE))); |
| 88 | + |
| 89 | + reporter.reportStack = stack => console.log((indent(stack, INDENT_SIZE * 2))); |
| 90 | + |
| 91 | + function indent(string, times) { |
| 92 | + const prefix = " ".repeat(times); |
| 93 | + return string.split("\n").map(l => prefix + l).join("\n"); |
| 94 | + } |
| 95 | + |
| 96 | + if (test.status === 0) { |
| 97 | + reporter.pass(test.name); |
| 98 | + } else if (test.status === 1) { |
| 99 | + reporter.fail(`${test.name}\n`); |
| 100 | + reporter.reportStack(`${test.message}\n${test.stack}`); |
| 101 | + hasFailed = true; |
| 102 | + } else if (test.status === 2) { |
| 103 | + reporter.fail(`${test.name} (timeout)\n`); |
| 104 | + reporter.reportStack(`${test.message}\n${test.stack}`); |
| 105 | + hasFailed = true; |
| 106 | + } else if (test.status === 3) { |
| 107 | + reporter.fail(`${test.name} (incomplete)\n`); |
| 108 | + reporter.reportStack(`${test.message}\n${test.stack}`); |
| 109 | + hasFailed = true; |
| 110 | + } else if (test.status === 4) { |
| 111 | + reporter.fail(`${test.name} (precondition failed)\n`); |
| 112 | + reporter.reportStack(`${test.message}\n${test.stack}`); |
| 113 | + hasFailed = true; |
| 114 | + } else { |
| 115 | + reporter.fail(`unknown test status: ${test.status}`); |
| 116 | + hasFailed = true; |
| 117 | + } |
| 118 | + hasFailed && process.exit(1); |
| 119 | +}) |
| 120 | + |
| 121 | +globalThis.File = File |
| 122 | +globalThis.Blob = Blob |
| 123 | +globalThis.garbageCollect = () => {} |
| 124 | +globalThis.document = {body: '[object HTMLBodyElement]'} |
| 125 | +globalThis.test_blob = test_blob; |
| 126 | +globalThis.test_blob_binary = test_blob_binary; |
| 127 | + |
| 128 | +import("https://wpt.live/FileAPI/file/File-constructor.any.js") |
| 129 | +import("https://wpt.live/FileAPI/blob/Blob-array-buffer.any.js") |
| 130 | +import("https://wpt.live/FileAPI/blob/Blob-slice-overflow.any.js") |
| 131 | +import("https://wpt.live/FileAPI/blob/Blob-slice.any.js") |
| 132 | +import("https://wpt.live/FileAPI/blob/Blob-stream.any.js") |
| 133 | +import("https://wpt.live/FileAPI/blob/Blob-text.any.js") |
0 commit comments