summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/test_fileReader.html
blob: 26e73bdb64f159c7dde8624b06522fdebf88ef1a (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 for FileReader in workers</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>

<body>
<script type="text/javascript;version=1.7">

const minFileSize = 20000;
SimpleTest.waitForExplicitFinish();

// Create strings containing data we'll test with. We'll want long
// strings to ensure they span multiple buffers while loading
var testTextData = "asd b\tlah\u1234w\u00a0r";
while (testTextData.length < minFileSize) {
  testTextData = testTextData + testTextData;
}

var testASCIIData = "abcdef 123456\n";
while (testASCIIData.length < minFileSize) {
  testASCIIData = testASCIIData + testASCIIData;
}

var testBinaryData = "";
for (var i = 0; i < 256; i++) {
  testBinaryData += String.fromCharCode(i);
}
while (testBinaryData.length < minFileSize) {
  testBinaryData = testBinaryData + testBinaryData;
}

var dataurldata0 = testBinaryData.substr(0, testBinaryData.length -
					 testBinaryData.length % 3);
var dataurldata1 = testBinaryData.substr(0, testBinaryData.length - 2 -
					 testBinaryData.length % 3);
var dataurldata2 = testBinaryData.substr(0, testBinaryData.length - 1 -
					 testBinaryData.length % 3);


//Set up files for testing
var openerURL = SimpleTest.getTestFileURL("fileapi_chromeScript.js");
var opener = SpecialPowers.loadChromeScript(openerURL);
opener.addMessageListener("files.opened", onFilesOpened);
opener.sendAsyncMessage("files.open", [
  testASCIIData,
  testBinaryData,
  null,
  convertToUTF8(testTextData),
  convertToUTF16(testTextData),
  "",
  dataurldata0,
  dataurldata1,
  dataurldata2,
]);

function onFilesOpened(message) {
  var worker = new Worker('worker_fileReader.js');
  worker.postMessage({ blobs: message,
                       testTextData: testTextData,
                       testASCIIData: testASCIIData,
                       testBinaryData: testBinaryData,
                       dataurldata0: dataurldata0,
                       dataurldata1: dataurldata1,
                       dataurldata2: dataurldata2 });

  worker.onmessage = function(e) {
    var msg = e.data;
    if (msg.type == 'finish') {
      SimpleTest.finish();
      return;
    }

    if (msg.type == 'check') {
      ok(msg.status, msg.msg);
      return;
    }

    ok(false, "Unknown message.");
  }
}

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;
}

function convertToUTF8(s) {
  return unescape(encodeURIComponent(s));
}

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