tenfourfox/dom/presentation/tests/mochitest/file_presentation_receiver.html
Cameron Kaiser c9b2922b70 hello FPR
2017-04-19 00:56:45 -07:00

105 lines
2.6 KiB
HTML

<!DOCTYPE HTML>
<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;version=1.7">
"use strict";
function is(a, b, msg) {
window.parent.postMessage((a === b ? 'OK ' : 'KO ') + msg, '*');
}
function ok(a, msg) {
window.parent.postMessage((a ? 'OK ' : 'KO ') + msg, '*');
}
function info(msg) {
window.parent.postMessage('INFO ' + msg, '*');
}
function command(msg) {
window.parent.postMessage('COMMAND ' + JSON.stringify(msg), '*');
}
function finish() {
window.parent.postMessage('DONE', '*');
}
var connection;
function testConnectionAvailable() {
return new Promise(function(aResolve, aReject) {
ok(navigator.presentation, "navigator.presentation should be available.");
ok(navigator.presentation.receiver, "navigator.presentation.receiver should be available.");
navigator.presentation.receiver.getConnection().then(
function(aConnection) {
connection = aConnection;
ok(connection.id, "Connection ID should be set: " + connection.id);
is(connection.state, "closed", "Connection state at receiver side should be closed by default.");
aResolve();
},
function(aError) {
ok(false, "Error occurred when getting the connection: " + aError);
finish();
aReject();
}
);
});
}
function testConnectionReady() {
return new Promise(function(aResolve, aReject) {
connection.onstatechange = function() {
connection.onstatechange = null;
is(connection.state, "connected", "Connection state should become connected.");
aResolve();
};
command({ name: 'trigger-incoming-offer' });
});
}
function testIncomingMessage() {
return new Promise(function(aResolve, aReject) {
const incomingMessage = "test incoming message";
connection.addEventListener('message', function messageHandler(aEvent) {
connection.removeEventListener('message', messageHandler);
is(aEvent.data, incomingMessage, "An incoming message should be received.");
aResolve();
});
command({ name: 'trigger-incoming-message',
data: incomingMessage });
});
}
function testTerminateConnection() {
return new Promise(function(aResolve, aReject) {
connection.onstatechange = function() {
connection.onstatechange = null;
is(connection.state, "terminated", "Connection should be terminated.");
aResolve();
};
connection.terminate();
});
}
testConnectionAvailable().
then(testConnectionReady).
then(testIncomingMessage).
then(testTerminateConnection).
then(finish);
</script>
</body>
</html>