gecko/content/base/test/test_ws_basic_tests.html

184 lines
5.5 KiB
HTML

<!DOCTYPE HTML>
<html>
<!--
-->
<head>
<title>Basic websocket test</title>
<script type="text/javascript" src="/MochiKit/packed.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body onload="testWebSocket()">
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=472529">Mozilla Bug </a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
var ws;
var params = ["protocol", "resource", "origin", "end"];
var results = ["test", "/tests/content/base/test/file_ws_basic_tests", "http://mochi.test:8888", "end"];
function forcegc(){
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
Components.utils.forceGC();
var wu = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIDOMWindowUtils);
wu.garbageCollect();
}
function finishWSTest() {
SimpleTest.finish();
}
function testWebSocket () {
var url = "ws://mochi.test:8888/tests/content/base/test/file_ws_basic_tests";
ws = new WebSocket("ws://mochi.test:8888/tests/content/base/test/file_ws_basic_tests", "test");
is(ws.url, url, "Wrong Websocket.url!");
ws.onopen = function(e) {
for (var i = 0; i < params.length; ++i) {
document.getElementById('log').textContent += "sending " + params[i] + "\n";
ws.send(params[i]);
}
}
ws.onclose = function(e) {
is(results.length, 0, "All the messages should have been processed!");
ok(e.wasClean, "Connection closed cleanly");
testWebSocket2();
}
ws.onerror = function(e) {
ok(false, "onerror called!");
finishWSTest();
}
ws.onmessage = function(e) {
document.getElementById('log').textContent += "\n" + e.data;
is(e.data, results[0], "Unexpected message");
results.shift();
}
}
function testWebSocket2() {
ws = new WebSocket("ws://mochi.test:8888/tests/content/base/test/file_ws_basic_tests", "test");
var testCount = 1000; // Send lots of messages
var messageCount = 0;
var testMessage = "test message";
ws.onopen = function(e) {
for (var i = 0; i < testCount; ++i) {
ws.send(testMessage + (i + 1));
}
ws.send("end");
}
ws.onclose = function(e) {
is(messageCount, testCount, "Didn't receive all the messages!");
ok(e.wasClean, "Connection closed cleanly");
testWebSocket3();
}
ws.onerror = function(e) {
ok(false, "onerror called!");
finishWSTest();
}
ws.onmessage = function(e) {
++messageCount;
is(e.data, testMessage + messageCount, "Wrong message");
document.getElementById('log').textContent = messageCount;
if (messageCount == testCount) {
this.onmessage = null;
}
}
}
function testWebSocket3() {
ws = new WebSocket("ws://mochi.test:8888/tests/content/base/test/file_ws_basic_tests", "test");
var testCount = 100; // Send lots of messages
var messageCount = 0;
var testMessage = "test message";
ws.onopen = function(e) {
for (var i = 0; i < testCount; ++i) {
forcegc(); // Do something evil, call cycle collector a lot.
ws.send(testMessage + (i + 1));
}
ws.send("end");
}
ws.onclose = function(e) {
is(messageCount, testCount, "Didn't receive all the messages!");
ok(e.wasClean, "Connection closed cleanly");
testWebSocket4();
}
ws.onerror = function(e) {
ok(false, "onerror called!");
finishWSTest();
}
ws.onmessage = function(e) {
forcegc(); // Do something evil, call cycle collector a lot.
++messageCount;
is(e.data, testMessage + messageCount, "Wrong message");
document.getElementById('log').textContent = messageCount;
if (messageCount == testCount) {
this.onmessage = null;
}
}
}
function testWebSocket4() {
ws = new WebSocket("ws://mochi.test:8888/tests/content/base/test/file_ws_basic_tests", "test");
// String length = (10,000 - 1) * 23 = 229,977 = almost 225 KiB.
var longString = new Array(10000).join("-huge websocket message");
ws.onopen = function(e) {
is(this, ws, "'this' should point to the WebSocket. (1)");
ws.send(longString);
}
ws.onclose = function(e) {
is(this, ws, "'this' should point to the WebSocket. (2)");
ok(e.wasClean, "Connection closed cleanly");
testWebSocket5();
}
ws.onerror = function(e) {
ok(false, "onerror called!");
finishWSTest();
}
ws.onmessage = function(e) {
is(this, ws, "'this' should point to the WebSocket. (3)");
// Do not use |is(e.data, longString, "...");| that results in a _very_ long line.
is(e.data.length, longString.length, "Length of received message");
ok(e.data == longString, "Content of received message");
document.getElementById('log').textContent += "\nReceived the huge message";
this.close();
}
}
function testWebSocket5() {
ws = new WebSocket("ws://mochi.test:8888/tests/content/base/test/file_ws_basic_tests", "test");
ws.onopen = function(e) {
this.close();
}
ws.onclose = function(e) {
ok(e.wasClean, "Connection closed cleanly");
is(this.bufferedAmount, 0, "Shouldn't have anything buffered");
var msg = "some data";
this.send(msg);
is(this.bufferedAmount, msg.length, "Should have some data buffered");
finishWSTest();
}
ws.onerror = function(e) {
ok(false, "onerror called!");
finishWSTest();
}
}
SimpleTest.waitForExplicitFinish();
</script>
<pre id="log">
</pre>
</pre>
<div>
</div>
</body>
</html>