test_receive-arraybuffer.html (2491B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <script 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"></p> 10 <div id="content" style="display: none"> 11 </div> 12 <pre id="test"> 13 14 <script class="testbody" type="text/javascript"> 15 16 function debug(msg) { 17 ok(1, msg); 18 } 19 20 function createArrayBufferContainingHelloWorld() 21 { 22 var hello = "Hello, world!"; 23 var array = new Uint8Array(hello.length); 24 for (var i = 0; i < hello.length; ++i) 25 array[i] = hello.charCodeAt(i); 26 return array.buffer; 27 } 28 29 function createEmptyArrayBuffer() 30 { 31 return new ArrayBuffer(0); 32 } 33 34 function createArrayBufferContainingAllDistinctBytes() 35 { 36 var array = new Uint8Array(256); 37 for (var i = 0; i < 256; ++i) 38 array[i] = i; 39 return array.buffer; 40 } 41 42 var ws = new WebSocket("ws://mochi.test:8888/tests/dom/websocket/tests/websocket_hybi/file_binary-frames"); 43 ws.binaryType = "arraybuffer"; 44 is(ws.binaryType, "arraybuffer", "should be equal to 'arraybuffer'"); 45 46 var receivedMessages = []; 47 var expectedValues = [createArrayBufferContainingHelloWorld(), createEmptyArrayBuffer(), createArrayBufferContainingAllDistinctBytes()]; 48 49 ws.onmessage = function(event) 50 { 51 receivedMessages.push(event.data); 52 }; 53 54 ws.onclose = function() 55 { 56 is(receivedMessages.length, expectedValues.length, "lengths not equal"); 57 for (var i = 0; i < expectedValues.length; ++i) 58 check(i); 59 SimpleTest.finish(); 60 }; 61 62 var responseType; 63 64 function check(index) 65 { 66 debug("Checking message #" + index + "."); 67 ok(receivedMessages[index] instanceof ArrayBuffer, 68 "Should receive an arraybuffer!"); 69 checkArrayBuffer(index, receivedMessages[index], expectedValues[index]); 70 } 71 72 var actualArray; 73 var expectedArray; 74 75 function checkArrayBuffer(testIndex, actual, expected) 76 { 77 actualArray = new Uint8Array(actual); 78 expectedArray = new Uint8Array(expected); 79 is(actualArray.length, expectedArray.length, "lengths not equal"); 80 // Print only the first mismatched byte in order not to flood console. 81 for (var i = 0; i < expectedArray.length; ++i) { 82 if (actualArray[i] != expectedArray[i]) { 83 ok(false, "Value mismatch: actualArray[" + i + "] = " + actualArray[i] + ", expectedArray[" + i + "] = " + expectedArray[i]); 84 return; 85 } 86 } 87 ok(true, "Passed: Message #" + testIndex + "."); 88 } 89 90 SimpleTest.waitForExplicitFinish(); 91 92 </script> 93 </body> 94 </html>