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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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(true, 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 is(ws.binaryType, "blob", "should be 'blob'");
michael@0 44
michael@0 45 var closeEvent;
michael@0 46 var receivedMessages = [];
michael@0 47 var expectedValues = [createArrayBufferContainingHelloWorld(), createEmptyArrayBuffer(), createArrayBufferContainingAllDistinctBytes()];
michael@0 48
michael@0 49 ws.onmessage = function(event)
michael@0 50 {
michael@0 51 receivedMessages.push(event.data);
michael@0 52 };
michael@0 53
michael@0 54 ws.onclose = function(event)
michael@0 55 {
michael@0 56 closeEvent = event;
michael@0 57
michael@0 58 is(receivedMessages.length, expectedValues.length, "lengths not same");
michael@0 59 check(0);
michael@0 60 };
michael@0 61
michael@0 62 var responseType;
michael@0 63
michael@0 64 function check(index)
michael@0 65 {
michael@0 66 if (index == expectedValues.length) {
michael@0 67 SimpleTest.finish();
michael@0 68 return;
michael@0 69 }
michael@0 70
michael@0 71 debug("Checking message #" + index + ".");
michael@0 72 ok(receivedMessages[index] instanceof Blob,
michael@0 73 "We should be receiving a Blob");
michael@0 74 var reader = new FileReader();
michael@0 75 reader.readAsArrayBuffer(receivedMessages[index]);
michael@0 76 reader.onload = function(event)
michael@0 77 {
michael@0 78 checkArrayBuffer(index, reader.result, expectedValues[index]);
michael@0 79 check(index + 1);
michael@0 80 };
michael@0 81 reader.onerror = function(event)
michael@0 82 {
michael@0 83 ok(false, "Failed to read blob: error code = " + reader.error.code);
michael@0 84 check(index + 1);
michael@0 85 };
michael@0 86 }
michael@0 87
michael@0 88 var actualArray;
michael@0 89 var expectedArray;
michael@0 90
michael@0 91 function checkArrayBuffer(testIndex, actual, expected)
michael@0 92 {
michael@0 93 actualArray = new Uint8Array(actual);
michael@0 94 expectedArray = new Uint8Array(expected);
michael@0 95 is(actualArray.length, expectedArray.length, "lengths not same");
michael@0 96 // Print only the first mismatched byte in order not to flood console.
michael@0 97 for (var i = 0; i < expectedArray.length; ++i) {
michael@0 98 if (actualArray[i] != expectedArray[i]) {
michael@0 99 ok(false, "Value mismatch: actualArray[" + i + "] = " + actualArray[i] + ", expectedArray[" + i + "] = " + expectedArray[i]);
michael@0 100 return;
michael@0 101 }
michael@0 102 }
michael@0 103 ok(true, "Passed: Message #" + testIndex + ".");
michael@0 104 }
michael@0 105
michael@0 106 SimpleTest.waitForExplicitFinish();
michael@0 107
michael@0 108 </script>
michael@0 109 </body>
michael@0 110 </html>

mercurial