|
1 <!DOCTYPE html> |
|
2 <html> |
|
3 <head> |
|
4 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
|
5 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
|
6 </head> |
|
7 <body> |
|
8 |
|
9 <p id="display"> |
|
10 <input id="fileList" type="file"></input> |
|
11 </p> |
|
12 <div id="content" style="display: none"> |
|
13 </div> |
|
14 <pre id="test"> |
|
15 |
|
16 <script class="testbody" type="text/javascript"> |
|
17 |
|
18 function startsWith(target, prefix) |
|
19 { |
|
20 return target.indexOf(prefix) === 0; |
|
21 } |
|
22 |
|
23 function createDOMFile(fileName, fileData) |
|
24 { |
|
25 // create File in profile dir |
|
26 var dirSvc = SpecialPowers.Cc["@mozilla.org/file/directory_service;1"] |
|
27 .getService(SpecialPowers.Ci.nsIProperties); |
|
28 var testFile = dirSvc.get("ProfD", SpecialPowers.Ci.nsIFile); |
|
29 testFile.append(fileName); |
|
30 var outStream = SpecialPowers.Cc["@mozilla.org/network/file-output-stream;1"] |
|
31 .createInstance(SpecialPowers.Ci.nsIFileOutputStream); |
|
32 outStream.init(testFile, 0x02 | 0x08 | 0x20, 0666, 0); |
|
33 if (fileData) { |
|
34 outStream.write(fileData, fileData.length); |
|
35 outStream.close(); |
|
36 } |
|
37 |
|
38 // Set filename into DOM <input> field, as if selected by user |
|
39 var fileList = document.getElementById('fileList'); |
|
40 SpecialPowers.wrap(fileList).value = testFile.path; |
|
41 |
|
42 // return JS File object, aka Blob |
|
43 return fileList.files[0]; |
|
44 } |
|
45 |
|
46 |
|
47 function createBlobContainingHelloWorld() |
|
48 { |
|
49 return createDOMFile("hellofile", "Hello, world!"); |
|
50 } |
|
51 |
|
52 function createEmptyBlob() |
|
53 { |
|
54 return createDOMFile("emptyfile"); |
|
55 } |
|
56 |
|
57 function createBlobContainingAllDistinctBytes() |
|
58 { |
|
59 var array = new Array(); |
|
60 for (var i = 0; i < 256; ++i) |
|
61 array[i] = i; |
|
62 // Concatenates chars into a single binary string |
|
63 binaryString = String.fromCharCode.apply(null, array); |
|
64 return createDOMFile("allchars", binaryString); |
|
65 } |
|
66 |
|
67 var ws = new WebSocket("ws://mochi.test:8888/tests/content/base/test/websocket_hybi/file_check-binary-messages"); |
|
68 var closeEvent; |
|
69 |
|
70 ws.onopen = function() |
|
71 { |
|
72 ws.send(createBlobContainingHelloWorld()); |
|
73 ws.send(createEmptyBlob()); |
|
74 ws.send(createBlobContainingAllDistinctBytes()); |
|
75 }; |
|
76 |
|
77 ws.onmessage = function(event) |
|
78 { |
|
79 var message = event.data; |
|
80 if (startsWith(message, "PASS")) |
|
81 ok(true, message); |
|
82 else |
|
83 ok(false, message); |
|
84 }; |
|
85 |
|
86 ws.onclose = function(event) |
|
87 { |
|
88 ok(event.wasClean, "should have closed cleanly"); |
|
89 SimpleTest.finish(); |
|
90 }; |
|
91 |
|
92 SimpleTest.waitForExplicitFinish(); |
|
93 |
|
94 </script> |
|
95 </body> |
|
96 </html> |