content/base/test/websocket_hybi/test_receive-arraybuffer.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial