Skip to content

Commit 3adc199

Browse files
mkruisselbrinkMarcos Cáceres
authored andcommitted
Add more tests for FileReader.result. (#7494)
In particular this adds tests to verify the value of the attribute during progress and loadstart events.
1 parent 18b76a1 commit 3adc199

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
promise_test(async t => {
2+
var reader = new FileReader();
3+
var eventWatcher = new EventWatcher(t, reader, ['loadstart', 'progress', 'abort', 'error', 'load', 'loadend']);
4+
reader.readAsText(new Blob([]));
5+
await eventWatcher.wait_for('loadstart');
6+
// No progress event for an empty blob, as no data is loaded.
7+
await eventWatcher.wait_for('load');
8+
await eventWatcher.wait_for('loadend');
9+
}, 'events are dispatched in the correct order for an empty blob');
10+
11+
promise_test(async t => {
12+
var reader = new FileReader();
13+
var eventWatcher = new EventWatcher(t, reader, ['loadstart', 'progress', 'abort', 'error', 'load', 'loadend']);
14+
reader.readAsText(new Blob(['a']));
15+
await eventWatcher.wait_for('loadstart');
16+
await eventWatcher.wait_for('progress');
17+
await eventWatcher.wait_for('load');
18+
await eventWatcher.wait_for('loadend');
19+
}, 'events are dispatched in the correct order for a non-empty blob');

FileAPI/reading-data-section/filereader_result.html

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
<div id="log"></div>
1313

1414
<script>
15-
var blob;
15+
var blob, blob2;
1616
setup(function() {
1717
blob = new Blob(["This test the result attribute"]);
18+
blob2 = new Blob(["This is a second blob"]);
1819
});
1920

2021
async_test(function() {
@@ -54,6 +55,43 @@
5455

5556
readArrayBuffer.readAsArrayBuffer(blob);
5657
}, "readAsArrayBuffer");
58+
59+
async_test(function() {
60+
var readBinaryString = new FileReader();
61+
assert_equals(readBinaryString.result, null);
62+
63+
readBinaryString.onloadend = this.step_func(function(evt) {
64+
assert_equals(typeof readBinaryString.result, "string", "The result type is string");
65+
assert_equals(readBinaryString.result, "This test the result attribute", "The result is correct");
66+
this.done();
67+
});
68+
69+
readBinaryString.readAsBinaryString(blob);
70+
}, "readAsBinaryString");
71+
72+
73+
for (let event of ['loadstart', 'progress']) {
74+
for (let method of ['readAsText', 'readAsDataURL', 'readAsArrayBuffer', 'readAsBinaryString']) {
75+
promise_test(async function(t) {
76+
var reader = new FileReader();
77+
assert_equals(reader.result, null, 'result is null before read');
78+
79+
var eventWatcher = new EventWatcher(t, reader,
80+
[event, 'loadend']);
81+
82+
reader[method](blob);
83+
assert_equals(reader.result, null, 'result is null after first read call');
84+
await eventWatcher.wait_for(event);
85+
assert_equals(reader.result, null, 'result is null during event');
86+
await eventWatcher.wait_for('loadend');
87+
assert_not_equals(reader.result, null);
88+
reader[method](blob);
89+
assert_equals(reader.result, null, 'result is null after second read call');
90+
await eventWatcher.wait_for(event);
91+
assert_equals(reader.result, null, 'result is null during second read event');
92+
}, 'result is null during "' + event + '" event for ' + method);
93+
}
94+
}
5795
</script>
5896
</body>
5997
</html>

0 commit comments

Comments
 (0)