gecko-dev/dom/presentation/tests/mochitest/file_presentation_1ua_recei...

217 строки
6.7 KiB
HTML

<!DOCTYPE HTML>
<!-- vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: -->
<html>
<head>
<meta charset="utf-8">
<title>Test for B2G PresentationReceiver at receiver side</title>
</head>
<body>
<div id="content"></div>
<script type="application/javascript">
"use strict";
function is(a, b, msg) {
if (a === b) {
alert("OK " + msg);
} else {
alert("KO " + msg + " | reason: " + a + " != " + b);
}
}
function ok(a, msg) {
alert((a ? "OK " : "KO ") + msg);
}
function info(msg) {
alert("INFO " + msg);
}
function command(name, data) {
alert("COMMAND " + JSON.stringify({name, data}));
}
function finish() {
alert("DONE");
}
var connection;
const DATA_ARRAY = [0, 255, 254, 0, 1, 2, 3, 0, 255, 255, 254, 0];
const DATA_ARRAY_BUFFER = new ArrayBuffer(DATA_ARRAY.length);
const TYPED_DATA_ARRAY = new Uint8Array(DATA_ARRAY_BUFFER);
TYPED_DATA_ARRAY.set(DATA_ARRAY);
function is_same_buffer(recv_data, expect_data) {
let recv_dataview = new Uint8Array(recv_data);
let expected_dataview = new Uint8Array(expect_data);
if (recv_dataview.length !== expected_dataview.length) {
return false;
}
for (let i = 0; i < recv_dataview.length; i++) {
if (recv_dataview[i] != expected_dataview[i]) {
info("discover byte differenct at " + i);
return false;
}
}
return true;
}
function testConnectionAvailable() {
return new Promise(function(aResolve, aReject) {
info("Receiver: --- testConnectionAvailable ---");
ok(navigator.presentation, "Receiver: navigator.presentation should be available.");
ok(navigator.presentation.receiver, "Receiver: navigator.presentation.receiver should be available.");
is(navigator.presentation.defaultRequest, null, "Receiver: navigator.presentation.defaultRequest should be null.");
navigator.presentation.receiver.connectionList
.then((aList) => {
is(aList.connections.length, 1, "Should get one conncetion.");
connection = aList.connections[0];
ok(connection.id, "Connection ID should be set: " + connection.id);
is(connection.state, "connected", "Connection state at receiver side should be connected.");
aResolve();
})
.catch((aError) => {
ok(false, "Receiver: Error occurred when getting the connection: " + aError);
finish();
aReject();
});
});
}
function testConnectionReady() {
return new Promise(function(aResolve, aReject) {
info("Receiver: --- testConnectionReady ---");
connection.onconnect = function() {
connection.onconnect = null;
ok(false, "Should not get |onconnect| event.");
aReject();
};
if (connection.state === "connected") {
connection.onconnect = null;
is(connection.state, "connected", "Receiver: Connection state should become connected.");
aResolve();
}
});
}
function testIncomingMessage() {
return new Promise(function(aResolve, aReject) {
info("Receiver: --- testIncomingMessage ---");
connection.addEventListener("message", function(evt) {
let msg = evt.data;
is(msg, "msg-sender-to-receiver", "Receiver: Receiver should receive message from sender.");
command("forward-command", JSON.stringify({ name: "message-from-sender-received" }));
aResolve();
}, {once: true});
command("forward-command", JSON.stringify({ name: "trigger-message-from-sender" }));
});
}
function testSendMessage() {
return new Promise(function(aResolve, aReject) {
window.addEventListener("hashchange", function hashchangeHandler(evt) {
var message = JSON.parse(decodeURIComponent(window.location.hash.substring(1)));
if (message.type === "trigger-message-from-receiver") {
info("Receiver: --- testSendMessage ---");
connection.send("msg-receiver-to-sender");
}
if (message.type === "message-from-receiver-received") {
window.removeEventListener("hashchange", hashchangeHandler);
aResolve();
}
});
});
}
function testIncomingBlobMessage() {
return new Promise(function(aResolve, aReject) {
info("Receiver: --- testIncomingBlobMessage ---");
connection.send("testIncomingBlobMessage");
connection.addEventListener("message", function(evt) {
let recvData = String.fromCharCode.apply(null, new Uint8Array(evt.data));
is(recvData, "Hello World", "expected same string data");
aResolve();
}, {once: true});
});
}
function testConnectionClosed() {
return new Promise(function(aResolve, aReject) {
info("Receiver: --- testConnectionClosed ---");
connection.onclose = function() {
connection.onclose = null;
is(connection.state, "closed", "Receiver: Connection should be closed.");
command("forward-command", JSON.stringify({ name: "receiver-closed" }));
aResolve();
};
command("forward-command", JSON.stringify({ name: "ready-to-close" }));
});
}
function testReconnectConnection() {
return new Promise(function(aResolve, aReject) {
info("Receiver: --- testReconnectConnection ---");
window.addEventListener("hashchange", function hashchangeHandler(evt) {
var message = JSON.parse(decodeURIComponent(window.location.hash.substring(1)));
if (message.type === "prepare-for-reconnect") {
command("forward-command", JSON.stringify({ name: "ready-to-reconnect" }));
}
});
connection.onconnect = function() {
connection.onconnect = null;
ok(true, "The connection is reconnected.");
aResolve();
};
});
}
function testIncomingArrayBuffer() {
return new Promise(function(aResolve, aReject) {
info("Receiver: --- testIncomingArrayBuffer ---");
connection.binaryType = "blob";
connection.send("testIncomingArrayBuffer");
connection.addEventListener("message", function(evt) {
var fileReader = new FileReader();
fileReader.onload = function() {
ok(is_same_buffer(DATA_ARRAY_BUFFER, this.result), "expected same buffer data");
aResolve();
};
fileReader.readAsArrayBuffer(evt.data);
}, {once: true});
});
}
function testIncomingArrayBufferView() {
return new Promise(function(aResolve, aReject) {
info("Receiver: --- testIncomingArrayBufferView ---");
connection.binaryType = "arraybuffer";
connection.send("testIncomingArrayBufferView");
connection.addEventListener("message", function(evt) {
ok(is_same_buffer(evt.data, TYPED_DATA_ARRAY), "expected same buffer data");
aResolve();
}, {once: true});
});
}
function runTests() {
testConnectionAvailable()
.then(testConnectionReady)
.then(testIncomingMessage)
.then(testSendMessage)
.then(testIncomingBlobMessage)
.then(testConnectionClosed)
.then(testReconnectConnection)
.then(testIncomingArrayBuffer)
.then(testIncomingArrayBufferView)
.then(testConnectionClosed);
}
runTests();
</script>
</body>
</html>