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

     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>
     9 <p id="display"></p>
    10 <div id="content" style="display: none">
    11 </div>
    12 <pre id="test">
    14 <script class="testbody" type="text/javascript">
    16 function debug(msg) { 
    17    ok(1, msg);
    18 }
    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 }
    29 function createEmptyArrayBuffer()
    30 {
    31     return new ArrayBuffer(0);
    32 }
    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 }
    42 var ws = new WebSocket("ws://mochi.test:8888/tests/content/base/test/websocket_hybi/file_binary-frames");
    43 ws.binaryType = "arraybuffer";
    44 is(ws.binaryType, "arraybuffer", "should be equal to 'arraybuffer'");
    46 var closeEvent;
    47 var receivedMessages = [];
    48 var expectedValues = [createArrayBufferContainingHelloWorld(), createEmptyArrayBuffer(), createArrayBufferContainingAllDistinctBytes()];
    50 ws.onmessage = function(event)
    51 {
    52     receivedMessages.push(event.data);
    53 };
    55 ws.onclose = function(event)
    56 {
    57     closeEvent = event;
    59     is(receivedMessages.length, expectedValues.length, "lengths not equal");
    60     for (var i = 0; i < expectedValues.length; ++i)
    61         check(i);
    62     SimpleTest.finish();
    63 };
    65 var responseType;
    67 function check(index)
    68 {
    69     debug("Checking message #" + index + ".");
    70     ok(receivedMessages[index] instanceof ArrayBuffer, 
    71        "Should receive an arraybuffer!");
    72     checkArrayBuffer(index, receivedMessages[index], expectedValues[index]);
    73 }
    75 var actualArray;
    76 var expectedArray;
    78 function checkArrayBuffer(testIndex, actual, expected)
    79 {
    80     actualArray = new Uint8Array(actual);
    81     expectedArray = new Uint8Array(expected);
    82     is(actualArray.length, expectedArray.length, "lengths not equal");
    83     // Print only the first mismatched byte in order not to flood console.
    84     for (var i = 0; i < expectedArray.length; ++i) {
    85         if (actualArray[i] != expectedArray[i]) {
    86             ok(false, "Value mismatch: actualArray[" + i + "] = " + actualArray[i] + ", expectedArray[" + i + "] = " + expectedArray[i]);
    87             return;
    88         }
    89     }
    90     ok(true, "Passed: Message #" + testIndex + ".");
    91 }
    93 SimpleTest.waitForExplicitFinish();
    95 </script>
    96 </body>
    97 </html>

mercurial