summaryrefslogtreecommitdiff
path: root/dom/base/test/test_bug1198095.html
blob: cebf5688840962240008e47ded30d1089ee9ab49 (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
<!DOCTYPE HTML>
<html>
<head>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1198095
-->
  <title>Test for Bug 1198095</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>

<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1198095">Mozilla Bug 1198095</a>

<pre id="test">
<script class="testbody" type="text/javascript;version=1.7">

var fileData1 = '1234567890';
var fileData2 = '43210';
var r, firstBlob;

var openerURL = SimpleTest.getTestFileURL("file_bug1198095.js");

var opener = SpecialPowers.loadChromeScript(openerURL);
opener.addMessageListener("file.opened", onFileOpened);
opener.addMessageListener("file.modified", onFileModified);
opener.sendAsyncMessage("file.open", fileData1);

function onLoadEnd1(e) {
  e.target.removeEventListener('loadend', onLoadEnd1);

  is(e.target, r, "Target and r are ok");
  ok(e.target.readyState, FileReader.DONE, "The file has been read.");
  ok(e.target.result instanceof ArrayBuffer, "The result is an ArrayBuffer");

  var view = new Uint8Array(e.target.result);
  is(view.length, fileData1.length, "File data length matches");
  for (var i = 0; i < fileData1.length; ++i) {
    is(String.fromCharCode(view[i]), fileData1[i], "Byte matches");
  }

  opener.sendAsyncMessage("file.modify", fileData2);
}

function onError1(e) {
  ok(false, "This method should not be called - error1!");
}

function onError2(e) {
  e.target.removeEventListener('error', onError2);
  SimpleTest.finish();
}

function onFileOpened(blob) {
  firstBlob = blob;
  r = new FileReader();
  r.addEventListener("loadend", onLoadEnd1);
  r.addEventListener("error", onError1);
  r.readAsArrayBuffer(firstBlob);
}

function onFileModified(blob) {
  r.removeEventListener('error', onError1);
  r.addEventListener("error", onError2);
  r.readAsArrayBuffer(firstBlob);
}

SimpleTest.waitForExplicitFinish();
</script>
</pre>
</body> </html>