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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet type="text/css" href="/tests/SimpleTest/test.css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=664783
-->
<window title="Mozilla Bug 664783"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
<script type="application/javascript" src="dom_worker_helper.js"/>
<!-- test results are displayed in the html:body -->
<body xmlns="http://www.w3.org/1999/xhtml">
<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=664783"
target="_blank">Mozilla Bug 664783</a>
<div id="content" style="display: none">
<input id="fileList" type="file"></input>
</div>
</body>
<!-- test code goes here -->
<script type="application/javascript">
<![CDATA[
/** Test for Bug 664783 **/
var fileNum = 0;
/**
* Create a file which contains the given data and optionally adds the specified file extension.
*/
function createFileWithData(fileData, /** optional */ extension) {
var testFile = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("ProfD", Components.interfaces.nsIFile);
var fileExtension = (extension == undefined) ? "" : "." + extension;
testFile.append("workerFileReaderSync" + fileNum++ + fileExtension);
var outStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
.createInstance(Components.interfaces.nsIFileOutputStream);
outStream.init(testFile, 0x02 | 0x08 | 0x20, // write, create, truncate
0666, 0);
outStream.write(fileData, fileData.length);
outStream.close();
var fileList = document.getElementById('fileList');
fileList.value = testFile.path;
return fileList.files[0];
}
function convertToUTF16(s) {
res = "";
for (var i = 0; i < s.length; ++i) {
c = s.charCodeAt(i);
res += String.fromCharCode(c & 255, c >>> 8);
}
return res;
}
/**
* Converts the given string to a data URL of the specified mime type.
*/
function convertToDataURL(mime, s) {
return "data:" + mime + ";base64," + btoa(s);
}
/**
* Create a worker to read a file containing fileData using FileReaderSync and
* checks the return type against the expected type. Optionally set an encoding
* for reading the file as text.
*/
function readFileData(fileData, expectedText, /** optional */ encoding) {
var worker = new Worker("fileReaderSync_worker.js");
worker.onmessage = function(event) {
is(event.data.text, expectedText, "readAsText in worker returned incorrect result.");
is(event.data.bin, fileData, "readAsBinaryString in worker returned incorrect result.");
is(event.data.url, convertToDataURL("application/octet-stream", fileData), "readAsDataURL in worker returned incorrect result.");
is(event.data.arrayBuffer.byteLength, fileData.length, "readAsArrayBuffer returned buffer of incorrect length.");
finish();
};
worker.onerror = function(event) {
ok(false, "Worker had an error: " + event.message);
finish();
};
var params = {file: createFileWithData(fileData), encoding: encoding};
worker.postMessage(params);
waitForWorkerFinish();
}
/**
* Create a worker which reuses a FileReaderSync to read multiple files as DataURLs.
*/
function reuseReaderForURL(files, expected) {
var worker = new Worker("fileReaderSync_worker.js");
worker.onerror = function(event) {
ok(false, "Worker had an error: " + event.message);
finish();
};
var k = 0;
worker.onmessage = function(event) {
is(event.data.url, expected[k], "readAsDataURL in worker returned incorrect result when reusing FileReaderSync.");
k++;
finish();
};
for (var i = 0; i < files.length; ++i) {
var params = {file: files[i], encoding: undefined};
worker.postMessage(params);
waitForWorkerFinish();
}
}
/**
* Create a worker which reuses a FileReaderSync to read multiple files as text.
*/
function reuseReaderForText(fileData, expected) {
var worker = new Worker("fileReaderSync_worker.js");
worker.onerror = function(event) {
ok(false, "Worker had an error: " + event.message);
finish();
};
var k = 0;
worker.onmessage = function(event) {
is(event.data.text, expected[k++], "readAsText in worker returned incorrect result when reusing FileReaderSync.");
finish();
};
for (var i = 0; i < fileData.length; ++i) {
var params = {file: createFileWithData(fileData[i]), encoding: undefined};
worker.postMessage(params);
waitForWorkerFinish();
}
}
/**
* Creates a a worker which reads a file containing fileData as an ArrayBuffer.
* Verifies that the ArrayBuffer when interpreted as a string matches the original data.
*/
function readArrayBuffer(fileData) {
var worker = new Worker("fileReaderSync_worker.js");
worker.onmessage = function(event) {
var view = new Uint8Array(event.data.arrayBuffer);
is(event.data.arrayBuffer.byteLength, fileData.length, "readAsArrayBuffer returned buffer of incorrect length.");
is(String.fromCharCode.apply(String, view), fileData, "readAsArrayBuffer returned buffer containing incorrect data.");
finish();
};
worker.onerror = function(event) {
ok(false, "Worker had an error: " + event.message);
finish();
};
var params = {file: createFileWithData(fileData), encoding: undefined};
worker.postMessage(params);
waitForWorkerFinish();
}
// Empty file.
readFileData("", "");
// Typical use case.
readFileData("text", "text");
// Test reading UTF-16 characters.
readFileData(convertToUTF16("text"), "text", "UTF-16");
// First read a file of type "text/plain", then read a file of type "application/octet-stream".
reuseReaderForURL([createFileWithData("text", "txt"), createFileWithData("text")],
[convertToDataURL("text/plain", "text"),
convertToDataURL("application/octet-stream", "text")]);
// First read UTF-16 characters marked using BOM, then read UTF-8 characters.
reuseReaderForText([convertToUTF16("\ufefftext"), "text"],
["text", "text"]);
// Reading data as ArrayBuffer.
readArrayBuffer("");
readArrayBuffer("text");
]]>
</script>
</window>
|