• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<title>FileReader: starting new reads while one is in progress</title>
3<link rel="author" title="Yinkan Li" href="mailto:liyinkan.biz@gmail.com">
4<link rel="help" href="http://dev.w3.org/2006/webapi/FileAPI/#MultipleReads">
5<script src="/resources/testharness.js"></script>
6<script src="/resources/testharnessreport.js"></script>
7<div id="log"></div>
8<script>
9test(function() {
10  var blob_1 = new Blob(['TEST000000001'])
11  var blob_2 = new Blob(['TEST000000002'])
12  var reader = new FileReader();
13  reader.readAsText(blob_1)
14  assert_equals(reader.readyState, FileReader.LOADING, "readyState Must be LOADING")
15  assert_throws_dom("InvalidStateError", function () {
16    reader.readAsText(blob_2)
17  })
18}, 'test FileReader InvalidStateError exception for readAsText');
19
20test(function() {
21  var blob_1 = new Blob(['TEST000000001'])
22  var blob_2 = new Blob(['TEST000000002'])
23  var reader = new FileReader();
24  reader.readAsDataURL(blob_1)
25  assert_equals(reader.readyState, FileReader.LOADING, "readyState Must be LOADING")
26  assert_throws_dom("InvalidStateError", function () {
27    reader.readAsDataURL(blob_2)
28  })
29}, 'test FileReader InvalidStateError exception for readAsDataURL');
30
31test(function() {
32  var blob_1 = new Blob(['TEST000000001'])
33  var blob_2 = new Blob(['TEST000000002'])
34  var reader = new FileReader();
35  reader.readAsArrayBuffer(blob_1)
36  assert_equals(reader.readyState, FileReader.LOADING, "readyState Must be LOADING")
37  assert_throws_dom("InvalidStateError", function () {
38    reader.readAsArrayBuffer(blob_2)
39  })
40}, 'test FileReader InvalidStateError exception for readAsArrayBuffer');
41
42async_test(function() {
43  var blob_1 = new Blob(['TEST000000001'])
44  var blob_2 = new Blob(['TEST000000002'])
45  var reader = new FileReader();
46  var triggered = false;
47  reader.onloadstart = this.step_func_done(function() {
48    assert_false(triggered, "Only one loadstart event should be dispatched");
49    triggered = true;
50    assert_equals(reader.readyState, FileReader.LOADING,
51                  "readyState must be LOADING")
52    assert_throws_dom("InvalidStateError", function () {
53      reader.readAsArrayBuffer(blob_2)
54    })
55  });
56  reader.readAsArrayBuffer(blob_1)
57  assert_equals(reader.readyState, FileReader.LOADING, "readyState Must be LOADING")
58}, 'test FileReader InvalidStateError exception in onloadstart event for readAsArrayBuffer');
59
60async_test(function() {
61  var blob_1 = new Blob(['TEST000000001'])
62  var blob_2 = new Blob(['TEST000000002'])
63  var reader = new FileReader();
64  reader.onloadend = this.step_func_done(function() {
65    assert_equals(reader.readyState, FileReader.DONE,
66                  "readyState must be DONE")
67    reader.readAsArrayBuffer(blob_2)
68    assert_equals(reader.readyState, FileReader.LOADING, "readyState Must be LOADING")
69  });
70  reader.readAsArrayBuffer(blob_1)
71  assert_equals(reader.readyState, FileReader.LOADING, "readyState Must be LOADING")
72}, 'test FileReader no InvalidStateError exception in loadend event handler for readAsArrayBuffer');
73
74async_test(function() {
75  var blob_1 = new Blob([new Uint8Array(0x414141)]);
76  var blob_2 = new Blob(['TEST000000002']);
77  var reader = new FileReader();
78  reader.onloadstart = this.step_func(function() {
79    reader.abort();
80    reader.onloadstart = null;
81    reader.onloadend = this.step_func_done(function() {
82      assert_equals('TEST000000002', reader.result);
83    });
84    reader.readAsText(blob_2);
85  });
86  reader.readAsText(blob_1);
87}, 'test abort and restart in onloadstart event for readAsText');
88
89</script>
90