Skip to content

Commit

Permalink
test: add wpt tests for Blob
Browse files Browse the repository at this point in the history
PR-URL: #36811
Backport-PR-URL: #39704
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
targos authored and MylesBorins committed Aug 31, 2021
1 parent df37c10 commit 72ef41c
Show file tree
Hide file tree
Showing 92 changed files with 5,272 additions and 0 deletions.
Empty file.
62 changes: 62 additions & 0 deletions test/fixtures/wpt/FileAPI/BlobURL/test2-manual.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Blob and File reference URL Test(2)</title>
<link rel=help href="http://dev.w3.org/2006/webapi/FileAPI/#convenienceAPI">
<link rel=author title="Breezewish" href="mailto:me@breeswish.org">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<form name="upload">
<input type="file" id="fileChooser"><br><input type="button" id="start" value="start">
</form>

<div>
<p>Test steps:</p>
<ol>
<li>Download the <a href="support/file_test2.txt">file</a>.</li>
<li>Select the file in the file inputbox.</li>
<li>Delete the file.</li>
<li>Click the 'start' button.</li>
</ol>
</div>

<div id="log"></div>

<script>

var fileChooser = document.querySelector('#fileChooser');

setup({explicit_done: true});
setup({explicit_timeout: true});

on_event(document.querySelector('#start'), 'click', function() {

async_test(function(t) {

var url = URL.createObjectURL(fileChooser.files[0]);

var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = t.step_func(function() {
switch (xhr.readyState) {
case xhr.DONE:
assert_equals(xhr.status, 500, 'status code should be 500.');
t.done();
return;
}
});

xhr.send();

}, 'Check whether the browser response 500 in XHR if the selected file which File/Blob URL refered is not found');

done();

});

</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>File API Test: Progress Event - bubbles, cancelable</title>
<link rel="author" title="Intel" href="http://www.intel.com">
<link rel="help" href="http://www.w3.org/TR/FileAPI/#events">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>
async_test(function(){
var blob = new Blob(["TEST"]);
var reader = new FileReader();

reader.onloadstart = this.step_func(function(evt) {
assert_false(evt.bubbles, "The bubbles must be false when the event is dispatched");
assert_false(evt.cancelable, "The cancelable must be false when the event is dispatched");
});

reader.onload = this.step_func(function(evt) {
assert_false(evt.bubbles, "The bubbles must be false when the event is dispatched");
assert_false(evt.cancelable, "The cancelable must be false when the event is dispatched");
});

reader.onloadend = this.step_func(function(evt) {
assert_false(evt.bubbles, "The bubbles must be false when the event is dispatched");
assert_false(evt.cancelable, "The cancelable must be false when the event is dispatched");
this.done();
});

reader.readAsText(blob);
}, "Check the values of bubbles and cancelable are false when the progress event is dispatched");
</script>

Empty file.
72 changes: 72 additions & 0 deletions test/fixtures/wpt/FileAPI/FileReader/test_errors-manual.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>FileReader Errors Test</title>
<link rel=help href="http://dev.w3.org/2006/webapi/FileAPI/#convenienceAPI">
<link rel=author title="Breezewish" href="mailto:me@breeswish.org">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<form name="upload">
<input type="file" id="fileChooser"><br><input type="button" id="start" value="start">
</form>

<div>
<p>Test steps:</p>
<ol>
<li>Download the <a href="support/file_test1.txt">file</a>.</li>
<li>Select the file in the file inputbox.</li>
<li>Delete the file.</li>
<li>Click the 'start' button.</li>
</ol>
</div>

<div id="log"></div>

<script>

var fileChooser = document.querySelector('#fileChooser');

setup({explicit_done: true});
setup({explicit_timeout: true});

on_event(fileChooser, 'change', function() {

async_test(function(t) {

var reader = new FileReader();
reader.readAsArrayBuffer(fileChooser.files[0]);

reader.onloadend = t.step_func_done(function(event) {
assert_equals(event.target.readyState, FileReader.DONE);
assert_equals(reader.error, null);
});

}, 'FileReader.error should be null if there are no errors when reading');

});

on_event(document.querySelector('#start'), 'click', function() {

async_test(function(t) {

var reader = new FileReader();
reader.readAsArrayBuffer(fileChooser.files[0]);

reader.onloadend = t.step_func_done(function(event) {
assert_equals(event.target.readyState, FileReader.DONE);
assert_equals(reader.error.code, 8);
assert_true(reader.error instanceof DOMException);
});

}, 'FileReader.error should be NOT_FOUND_ERR if the file is not found when reading');

done();

});

</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>FileReader NotReadableError Test</title>
<link rel="author" title="Intel" href="http://www.intel.com">
<link rel="help" href="https://w3c.github.io/FileAPI/#dfn-error-codes">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<form name="upload">
<input type="file" id="fileChooser"><br><input type="button" id="start" value="start">
</form>

<div>
<p>Test steps:</p>
<ol>
<li>Download the <a href="support/file_test1.txt">file</a>.</li>
<li>Select the file in the file inputbox.</li>
<li>Delete the file's readable permission.</li>
<li>Click the 'start' button.</li>
</ol>
</div>

<script>

const fileChooser = document.querySelector('#fileChooser');

setup({explicit_done: true});
setup({explicit_timeout: true});

on_event(document.querySelector('#start'), 'click', () => {
async_test(t => {
const reader = new FileReader();
reader.readAsArrayBuffer(fileChooser.files[0]);
reader.onloadend = t.step_func_done(event => {
assert_equals(event.target.readyState, FileReader.DONE);
assert_equals(reader.error.name, "NotReadableError");
});
}, 'FileReader.error should be NotReadableError if the file is unreadable');
done();
});

</script>

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>FileReader SecurityError Test</title>
<link rel="author" title="Intel" href="http://www.intel.com">
<link rel="help" href="https://w3c.github.io/FileAPI/#dfn-error-codes">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<form name="upload">
<input type="file" id="fileChooser"><br><input type="button" id="start" value="start">
</form>

<div>
<p>Test steps:</p>
<ol>
<li>Select a system sensitive file (e.g. files in /usr/bin, password files,
and other native operating system executables) in the file inputbox.</li>
<li>Click the 'start' button.</li>
</ol>
</div>

<script>

const fileChooser = document.querySelector('#fileChooser');

setup({explicit_done: true});
setup({explicit_timeout: true});

on_event(document.querySelector('#start'), 'click', () => {
async_test(t => {
const reader = new FileReader();
reader.readAsArrayBuffer(fileChooser.files[0]);
reader.onloadend = t.step_func_done(event => {
assert_equals(event.target.readyState, FileReader.DONE);
assert_equals(reader.error.name, "SecurityError");
});
}, 'FileReader.error should be SECURITY_ERROR if the file is a system sensitive file');
done();
});

</script>
27 changes: 27 additions & 0 deletions test/fixtures/wpt/FileAPI/FileReader/workers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<meta charset=utf-8>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>

async_test(t => {
function workerCode() {
close();
var blob = new Blob([123]);
var fr = new FileReader();
fr.readAsText(blob);
fr.abort()
fr.readAsArrayBuffer(blob);
postMessage(true);
}

var workerBlob = new Blob([workerCode.toString() + ";workerCode();"], {type:"application/javascript"});

var w = new Worker(URL.createObjectURL(workerBlob));
w.onmessage = function(e) {
assert_true(e.data, "FileReader created during worker shutdown.");
t.done();
}
}, 'FileReader created after a worker self.close()');

</script>
56 changes: 56 additions & 0 deletions test/fixtures/wpt/FileAPI/FileReaderSync.worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
importScripts("/resources/testharness.js");

var blob, empty_blob, readerSync;
setup(() => {
readerSync = new FileReaderSync();
blob = new Blob(["test"]);
empty_blob = new Blob();
});

test(() => {
assert_true(readerSync instanceof FileReaderSync);
}, "Interface");

test(() => {
var text = readerSync.readAsText(blob);
assert_equals(text, "test");
}, "readAsText");

test(() => {
var text = readerSync.readAsText(empty_blob);
assert_equals(text, "");
}, "readAsText with empty blob");

test(() => {
var data = readerSync.readAsDataURL(blob);
assert_equals(data.indexOf("data:"), 0);
}, "readAsDataURL");

test(() => {
var data = readerSync.readAsDataURL(empty_blob);
assert_equals(data.indexOf("data:"), 0);
}, "readAsDataURL with empty blob");

test(() => {
var data = readerSync.readAsBinaryString(blob);
assert_equals(data, "test");
}, "readAsBinaryString");

test(() => {
var data = readerSync.readAsBinaryString(empty_blob);
assert_equals(data, "");
}, "readAsBinaryString with empty blob");

test(() => {
var data = readerSync.readAsArrayBuffer(blob);
assert_true(data instanceof ArrayBuffer);
assert_equals(data.byteLength, "test".length);
}, "readAsArrayBuffer");

test(() => {
var data = readerSync.readAsArrayBuffer(empty_blob);
assert_true(data instanceof ArrayBuffer);
assert_equals(data.byteLength, 0);
}, "readAsArrayBuffer with empty blob");

done();
6 changes: 6 additions & 0 deletions test/fixtures/wpt/FileAPI/META.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
spec: https://w3c.github.io/FileAPI/
suggested_reviewers:
- inexorabletash
- zqzhang
- jdm
- mkruisselbrink
45 changes: 45 additions & 0 deletions test/fixtures/wpt/FileAPI/blob/Blob-array-buffer.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// META: title=Blob Array Buffer
// META: script=../support/Blob.js
'use strict';

promise_test(async () => {
const input_arr = new TextEncoder().encode("PASS");
const blob = new Blob([input_arr]);
const array_buffer = await blob.arrayBuffer();
assert_true(array_buffer instanceof ArrayBuffer);
assert_equals_typed_array(new Uint8Array(array_buffer), input_arr);
}, "Blob.arrayBuffer()")

promise_test(async () => {
const input_arr = new TextEncoder().encode("");
const blob = new Blob([input_arr]);
const array_buffer = await blob.arrayBuffer();
assert_true(array_buffer instanceof ArrayBuffer);
assert_equals_typed_array(new Uint8Array(array_buffer), input_arr);
}, "Blob.arrayBuffer() empty Blob data")

promise_test(async () => {
const input_arr = new TextEncoder().encode("\u08B8\u000a");
const blob = new Blob([input_arr]);
const array_buffer = await blob.arrayBuffer();
assert_equals_typed_array(new Uint8Array(array_buffer), input_arr);
}, "Blob.arrayBuffer() non-ascii input")

promise_test(async () => {
const input_arr = [8, 241, 48, 123, 151];
const typed_arr = new Uint8Array(input_arr);
const blob = new Blob([typed_arr]);
const array_buffer = await blob.arrayBuffer();
assert_equals_typed_array(new Uint8Array(array_buffer), typed_arr);
}, "Blob.arrayBuffer() non-unicode input")

promise_test(async () => {
const input_arr = new TextEncoder().encode("PASS");
const blob = new Blob([input_arr]);
const array_buffer_results = await Promise.all([blob.arrayBuffer(),
blob.arrayBuffer(), blob.arrayBuffer()]);
for (let array_buffer of array_buffer_results) {
assert_true(array_buffer instanceof ArrayBuffer);
assert_equals_typed_array(new Uint8Array(array_buffer), input_arr);
}
}, "Blob.arrayBuffer() concurrent reads")
Loading

0 comments on commit 72ef41c

Please sign in to comment.