summaryrefslogtreecommitdiffstats
path: root/dom/websocket/tests/websocket_hybi/test_receive-arraybuffer.html
blob: 6d465b76771b0264dfc95e2af309854bfcf5a6b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<!DOCTYPE html>
<html>
<head>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>

<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">

<script class="testbody" type="text/javascript">

function debug(msg) {
   ok(1, msg);
}

function createArrayBufferContainingHelloWorld()
{
    var hello = "Hello, world!";
    var array = new Uint8Array(hello.length);
    for (var i = 0; i < hello.length; ++i)
        array[i] = hello.charCodeAt(i);
    return array.buffer;
}

function createEmptyArrayBuffer()
{
    return new ArrayBuffer(0);
}

function createArrayBufferContainingAllDistinctBytes()
{
    var array = new Uint8Array(256);
    for (var i = 0; i < 256; ++i)
        array[i] = i;
    return array.buffer;
}

var ws = new WebSocket("ws://mochi.test:8888/tests/dom/websocket/tests/websocket_hybi/file_binary-frames");
ws.binaryType = "arraybuffer";
is(ws.binaryType, "arraybuffer", "should be equal to 'arraybuffer'");

var closeEvent;
var receivedMessages = [];
var expectedValues = [createArrayBufferContainingHelloWorld(), createEmptyArrayBuffer(), createArrayBufferContainingAllDistinctBytes()];

ws.onmessage = function(event)
{
    receivedMessages.push(event.data);
};

ws.onclose = function(event)
{
    closeEvent = event;

    is(receivedMessages.length, expectedValues.length, "lengths not equal");
    for (var i = 0; i < expectedValues.length; ++i)
        check(i);
    SimpleTest.finish();
};

var responseType;

function check(index)
{
    debug("Checking message #" + index + ".");
    ok(receivedMessages[index] instanceof ArrayBuffer,
       "Should receive an arraybuffer!");
    checkArrayBuffer(index, receivedMessages[index], expectedValues[index]);
}

var actualArray;
var expectedArray;

function checkArrayBuffer(testIndex, actual, expected)
{
    actualArray = new Uint8Array(actual);
    expectedArray = new Uint8Array(expected);
    is(actualArray.length, expectedArray.length, "lengths not equal");
    // Print only the first mismatched byte in order not to flood console.
    for (var i = 0; i < expectedArray.length; ++i) {
        if (actualArray[i] != expectedArray[i]) {
            ok(false, "Value mismatch: actualArray[" + i + "] = " + actualArray[i] + ", expectedArray[" + i + "] = " + expectedArray[i]);
            return;
        }
    }
    ok(true, "Passed: Message #" + testIndex + ".");
}

SimpleTest.waitForExplicitFinish();

</script>
</body>
</html>