• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<html>
3  <head>
4    <meta charset="utf-8">
5    <title>FileAPI Test: filereader_result</title>
6    <link rel="author" title="Intel" href="http://www.intel.com">
7    <link rel="help" href="http://dev.w3.org/2006/webapi/FileAPI/#filedata-attr">
8    <script src="/resources/testharness.js"></script>
9    <script src="/resources/testharnessreport.js"></script>
10  </head>
11  <body>
12    <div id="log"></div>
13
14    <script>
15    var blob, blob2;
16    setup(function() {
17      blob = new Blob(["This test the result attribute"]);
18      blob2 = new Blob(["This is a second blob"]);
19    });
20
21    async_test(function() {
22      var readText = new FileReader();
23      assert_equals(readText.result, null);
24
25      readText.onloadend = this.step_func(function(evt) {
26        assert_equals(typeof readText.result, "string", "The result type is string");
27        assert_equals(readText.result, "This test the result attribute", "The result is correct");
28        this.done();
29      });
30
31      readText.readAsText(blob);
32    }, "readAsText");
33
34    async_test(function() {
35      var readDataURL = new FileReader();
36      assert_equals(readDataURL.result, null);
37
38      readDataURL.onloadend = this.step_func(function(evt) {
39        assert_equals(typeof readDataURL.result, "string", "The result type is string");
40        assert_true(readDataURL.result.indexOf("VGhpcyB0ZXN0IHRoZSByZXN1bHQgYXR0cmlidXRl") != -1, "return the right base64 string");
41        this.done();
42      });
43
44      readDataURL.readAsDataURL(blob);
45    }, "readAsDataURL");
46
47    async_test(function() {
48      var readArrayBuffer = new FileReader();
49      assert_equals(readArrayBuffer.result, null);
50
51      readArrayBuffer.onloadend = this.step_func(function(evt) {
52        assert_true(readArrayBuffer.result instanceof ArrayBuffer, "The result is instanceof ArrayBuffer");
53        this.done();
54      });
55
56      readArrayBuffer.readAsArrayBuffer(blob);
57    }, "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    }
95    </script>
96  </body>
97</html>
98