Skip to content

Add more tests for FileReader.result. #7494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions FileAPI/reading-data-section/filereader_events.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
promise_test(async t => {
var reader = new FileReader();
var eventWatcher = new EventWatcher(t, reader, ['loadstart', 'progress', 'abort', 'error', 'load', 'loadend']);
reader.readAsText(new Blob([]));
await eventWatcher.wait_for('loadstart');
// No progress event for an empty blob, as no data is loaded.
await eventWatcher.wait_for('load');
await eventWatcher.wait_for('loadend');
}, 'events are dispatched in the correct order for an empty blob');

promise_test(async t => {
var reader = new FileReader();
var eventWatcher = new EventWatcher(t, reader, ['loadstart', 'progress', 'abort', 'error', 'load', 'loadend']);
reader.readAsText(new Blob(['a']));
await eventWatcher.wait_for('loadstart');
await eventWatcher.wait_for('progress');
await eventWatcher.wait_for('load');
await eventWatcher.wait_for('loadend');
}, 'events are dispatched in the correct order for a non-empty blob');
40 changes: 39 additions & 1 deletion FileAPI/reading-data-section/filereader_result.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
<div id="log"></div>

<script>
var blob;
var blob, blob2;
setup(function() {
blob = new Blob(["This test the result attribute"]);
blob2 = new Blob(["This is a second blob"]);
});

async_test(function() {
Expand Down Expand Up @@ -54,6 +55,43 @@

readArrayBuffer.readAsArrayBuffer(blob);
}, "readAsArrayBuffer");

async_test(function() {
var readBinaryString = new FileReader();
assert_equals(readBinaryString.result, null);

readBinaryString.onloadend = this.step_func(function(evt) {
assert_equals(typeof readBinaryString.result, "string", "The result type is string");
assert_equals(readBinaryString.result, "This test the result attribute", "The result is correct");
this.done();
});

readBinaryString.readAsBinaryString(blob);
}, "readAsBinaryString");


for (let event of ['loadstart', 'progress']) {
for (let method of ['readAsText', 'readAsDataURL', 'readAsArrayBuffer', 'readAsBinaryString']) {
promise_test(async function(t) {
var reader = new FileReader();
assert_equals(reader.result, null, 'result is null before read');

var eventWatcher = new EventWatcher(t, reader,
[event, 'loadend']);

reader[method](blob);
assert_equals(reader.result, null, 'result is null after first read call');
await eventWatcher.wait_for(event);
assert_equals(reader.result, null, 'result is null during event');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice... and all browsers have converged on including the microtask execution within the event dispatch.

await eventWatcher.wait_for('loadend');
assert_not_equals(reader.result, null);
reader[method](blob);
assert_equals(reader.result, null, 'result is null after second read call');
await eventWatcher.wait_for(event);
assert_equals(reader.result, null, 'result is null during second read event');
}, 'result is null during "' + event + '" event for ' + method);
}
}
</script>
</body>
</html>