• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<html>
3  <head>
4    <meta charset="utf-8">
5    <title>FileAPI Test: filereader_file</title>
6    <link rel="author" title="Intel" href="http://www.intel.com">
7    <link rel="help" href="http://dev.w3.org/2006/webapi/FileAPI/#FileReader-interface">
8    <link rel="help" href="http://dev.w3.org/2006/webapi/FileAPI/#file">
9    <script src="/resources/testharness.js"></script>
10    <script src="/resources/testharnessreport.js"></script>
11  </head>
12  <body>
13    <div>
14      <p>Test step:</p>
15      <ol>
16        <li>Download <a href="support/blue-100x100.png">blue-100x100.png</a> to local.</li>
17        <li>Select the local file (blue-100x100.png) to run the test.</li>
18      </ol>
19    </div>
20
21    <form name="uploadData">
22      <input type="file" id="fileChooser">
23    </form>
24
25    <div id="log"></div>
26    <script>
27      var fileInput = document.querySelector('#fileChooser');
28      var reader = new FileReader();
29
30      //readType: 1-> ArrayBuffer, 2-> Text, 3-> DataURL
31      var readType = 1;
32
33      setup({
34        explicit_done: true,
35        explicit_timeout: true,
36      });
37
38      on_event(fileInput, "change", function(evt) {
39        reader.readAsArrayBuffer(fileInput.files[0]);
40      });
41
42      on_event(reader, "load", function(evt) {
43        if (readType == 1) {
44          test(function() {
45            assert_true(reader.result instanceof ArrayBuffer, "The result is instanceof ArrayBuffer");
46          }, "Check if the readAsArrayBuffer works");
47
48          readType++;
49          reader.readAsText(fileInput.files[0]);
50        } else if (readType == 2) {
51          test(function() {
52            assert_equals(typeof reader.result, "string", "The result is typeof string");
53          }, "Check if the readAsText works");
54
55          readType++;
56          reader.readAsDataURL(fileInput.files[0]);
57        } else if (readType == 3) {
58          test(function() {
59            assert_equals(typeof reader.result, "string", "The result is typeof string");
60            assert_equals(reader.result.indexOf("data"), 0, "The result starts with 'data'");
61            assert_true(reader.result.indexOf("base64") > 0, "The result contains 'base64'");
62          }, "Check if the readAsDataURL works");
63
64          done();
65        }
66      });
67    </script>
68  </body>
69</html>
70