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

245 строки
6.8 KiB
HTML
Исходник Обычный вид История

<!DOCTYPE HTML>
<html>
<!-- Any copyright is dedicated to the Public Domain.
- http://creativecommons.org/publicdomain/zero/1.0/ -->
<head>
<meta charset="utf-8">
<title>Test for data channel as session transport in Presentation API</title>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1148307">Test for data channel as session transport in Presentation API</a>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
"use strict";
SimpleTest.waitForExplicitFinish();
const loadingTimeoutPref = "presentation.receiver.loading.timeout";
var clientBuilder;
var serverBuilder;
var clientTransport;
var serverTransport;
const clientMessage = "Client Message";
const serverMessage = "Server Message";
const { XPCOMUtils } = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
var isClientReady = false;
var isServerReady = false;
var isClientClosed = false;
var isServerClosed = false;
var gResolve;
var gReject;
const clientCallback = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIPresentationSessionTransportCallback]),
notifyTransportReady: function () {
info("Client transport ready.");
isClientReady = true;
if (isClientReady && isServerReady) {
gResolve();
}
},
notifyTransportClosed: function (aReason) {
info("Client transport is closed.");
isClientClosed = true;
if (isClientClosed && isServerClosed) {
gResolve();
}
},
notifyData: function(aData) {
is(aData, serverMessage, "Client transport receives data.");
gResolve();
},
};
const serverCallback = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIPresentationSessionTransportCallback]),
notifyTransportReady: function () {
info("Server transport ready.");
isServerReady = true;
if (isClientReady && isServerReady) {
gResolve();
}
},
notifyTransportClosed: function (aReason) {
info("Server transport is closed.");
isServerClosed = true;
if (isClientClosed && isServerClosed) {
gResolve();
}
},
notifyData: function(aData) {
is(aData, clientMessage, "Server transport receives data.");
gResolve()
},
};
const clientListener = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIPresentationSessionTransportBuilderListener]),
onSessionTransport: function(aTransport) {
info("Client Transport is built.");
clientTransport = aTransport;
clientTransport.callback = clientCallback;
},
onError: function(aError) {
ok(false, "client's builder reports error " + aError);
},
sendOffer: function(aOffer) {
setTimeout(()=>this._remoteBuilder.onOffer(aOffer), 0);
},
sendAnswer: function(aAnswer) {
setTimeout(()=>this._remoteBuilder.onAnswer(aAnswer), 0);
},
sendIceCandidate: function(aCandidate) {
setTimeout(()=>this._remoteBuilder.onIceCandidate(aCandidate), 0);
},
disconnect: function(aReason) {
setTimeout(()=>this._localBuilder.notifyDisconnected(aReason), 0);
setTimeout(()=>this._remoteBuilder.notifyDisconnected(aReason), 0);
},
set remoteBuilder(aRemoteBuilder) {
this._remoteBuilder = aRemoteBuilder;
},
set localBuilder(aLocalBuilder) {
this._localBuilder = aLocalBuilder;
},
}
const serverListener = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIPresentationSessionTransportBuilderListener]),
onSessionTransport: function(aTransport) {
info("Server Transport is built.");
serverTransport = aTransport;
serverTransport.callback = serverCallback;
serverTransport.enableDataNotification();
},
onError: function(aError) {
ok(false, "server's builder reports error " + aError);
},
sendOffer: function(aOffer) {
setTimeout(()=>this._remoteBuilder.onOffer(aOffer), 0);
},
sendAnswer: function(aAnswer) {
setTimeout(()=>this._remoteBuilder.onAnswer(aAnswer), 0);
},
sendIceCandidate: function(aCandidate) {
setTimeout(()=>this._remoteBuilder.onIceCandidate(aCandidate), 0);
},
disconnect: function(aReason) {
setTimeout(()=>this._localBuilder.notifyDisconnected(aReason), 0);
setTimeout(()=>this._remoteBuilder.notifyDisconnected(aReason), 0);
},
set remoteBuilder(aRemoteBuilder) {
this._remoteBuilder = aRemoteBuilder;
},
set localBuilder(aLocalBuilder) {
this._localBuilder = aLocalBuilder;
},
}
function testBuilder() {
return new Promise(function(aResolve, aReject) {
gResolve = aResolve;
gReject = aReject;
clientBuilder = Cc["@mozilla.org/presentation/datachanneltransportbuilder;1"]
.createInstance(Ci.nsIPresentationDataChannelSessionTransportBuilder);
serverBuilder = Cc["@mozilla.org/presentation/datachanneltransportbuilder;1"]
.createInstance(Ci.nsIPresentationDataChannelSessionTransportBuilder);
clientListener.localBuilder = clientBuilder;
clientListener.remoteBuilder = serverBuilder;
serverListener.localBuilder = serverBuilder;
serverListener.remoteBuilder = clientBuilder;
clientBuilder
.buildDataChannelTransport(Ci.nsIPresentationService.ROLE_CONTROLLER,
window,
clientListener);
serverBuilder
.buildDataChannelTransport(Ci.nsIPresentationService.ROLE_RECEIVER,
window,
serverListener);
});
}
function testClientSendMessage() {
return new Promise(function(aResolve, aReject) {
info("client sends message");
gResolve = aResolve;
gReject = aReject;
clientTransport.send(clientMessage);
});
}
function testServerSendMessage() {
return new Promise(function(aResolve, aReject) {
info("server sends message");
gResolve = aResolve;
gReject = aReject;
serverTransport.send(serverMessage);
setTimeout(()=>clientTransport.enableDataNotification(), 0);
});
}
function testCloseSessionTransport() {
return new Promise(function(aResolve, aReject) {
info("close session transport");
gResolve = aResolve;
gReject = aReject;
serverTransport.close(Cr.NS_OK);
});
}
function finish() {
info("test finished, teardown");
Services.prefs.clearUserPref(loadingTimeoutPref);
SimpleTest.finish();
}
function error(aError) {
ok(false, "report Error " + aError.name + ":" + aError.message);
gReject();
}
function runTests() {
Services.prefs.setIntPref(loadingTimeoutPref, 30000);
testBuilder()
.then(testClientSendMessage)
.then(testServerSendMessage)
.then(testCloseSessionTransport)
.then(finish)
.catch(error);
}
window.addEventListener("load", function() {
runTests();
});
</script>
</pre>
</body>
</html>