summaryrefslogtreecommitdiffstats
path: root/dom/file/ipc/tests/test_ipcBlob_fileReaderSync.html
blob: f37d4b79ed6ea3a68303fdece4a1f61833e7121d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<!DOCTYPE HTML>
<html>
<head>
  <title>Test IPCBlob and FileReaderSync</title>
  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<script type="text/javascript">

function workerScript() {
  onmessage = function(event) {
    let readerMemoryBlob = new FileReaderSync();
    let status = readerMemoryBlob.readAsText(new Blob(['hello world'])) == 'hello world';
    postMessage({ status, message: "FileReaderSync with memory blob still works" });

    let readerIPCBlob1 = new FileReaderSync();
    postMessage({ blob: event.data, method: 'readAsText',
                  data: readerIPCBlob1.readAsText(event.data)});

    let readerIPCBlob2 = new FileReaderSync();
    postMessage({ blob: event.data, method: 'readAsArrayBuffer',
                  data: readerIPCBlob2.readAsArrayBuffer(event.data)});

    let readerIPCBlob3 = new FileReaderSync();
    postMessage({ blob: event.data, method: 'readAsDataURL',
                  data: readerIPCBlob3.readAsDataURL(event.data)});

    let multipartBlob = new Blob(['wow', event.data]);

    let readerIPCBlobMultipart1 = new FileReaderSync();
    postMessage({ blob: multipartBlob, method: 'readAsText',
                  data: readerIPCBlobMultipart1.readAsText(multipartBlob)});

    let readerIPCBlobMultipart2 = new FileReaderSync();
    postMessage({ blob: multipartBlob, method: 'readAsArrayBuffer',
                  data: readerIPCBlobMultipart2.readAsArrayBuffer(multipartBlob)});

    let readerIPCBlobMultipart3 = new FileReaderSync();
    postMessage({ blob: multipartBlob, method: 'readAsDataURL',
                  data: readerIPCBlobMultipart3.readAsDataURL(multipartBlob)});

    postMessage({ finish: true });
  }
}

let completed = false;
let pendingTasks = 0;
function maybeFinish() {
  if (completed && !pendingTasks) {
    SimpleTest.finish();
  }
}

let workerUrl = URL.createObjectURL(new Blob(["(", workerScript.toString(), ")()"]));
let worker = new Worker(workerUrl);
worker.onmessage = event => {
  if ("status" in event.data) {
    ok(event.data.status, event.data.message);
    return;
  }

  if ("blob" in event.data) {
    let fr = new FileReader();
    fr[event.data.method](event.data.blob);
    ++pendingTasks;
    fr.onload = () => {
      if (event.data.method != 'readAsArrayBuffer') {
        is(event.data.data, fr.result, "The file has been read");
      } else {
        is(event.data.data.byteLength, fr.result.byteLength, "The file has been read");
      }
      --pendingTasks;
      maybeFinish();
    }

    return;
  }

  if ("finish" in event.data) {
    completed = true;
    maybeFinish();
  }
};

let url = SimpleTest.getTestFileURL("script_file.js");
let script = SpecialPowers.loadChromeScript(url);
script.addMessageListener("file.opened", message => {
  worker.postMessage(message.file);
});

script.sendAsyncMessage("file.open");

SimpleTest.waitForExplicitFinish();

</script>
</pre>
</body>
</html>