2016-04-11 06:20:55 +03:00
|
|
|
<!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";
|
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
const { XPCOMUtils } = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2016-04-11 06:20:55 +03:00
|
|
|
|
|
|
|
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);
|
2016-06-03 06:03:15 +03:00
|
|
|
},
|
|
|
|
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);
|
|
|
|
},
|
2016-07-04 13:12:04 +03:00
|
|
|
disconnect: function(aReason) {
|
2016-07-12 05:16:46 +03:00
|
|
|
setTimeout(()=>this._localBuilder.notifyDisconnected(aReason), 0);
|
|
|
|
setTimeout(()=>this._remoteBuilder.notifyDisconnected(aReason), 0);
|
2016-06-03 06:03:15 +03:00
|
|
|
},
|
|
|
|
set remoteBuilder(aRemoteBuilder) {
|
|
|
|
this._remoteBuilder = aRemoteBuilder;
|
|
|
|
},
|
|
|
|
set localBuilder(aLocalBuilder) {
|
|
|
|
this._localBuilder = aLocalBuilder;
|
|
|
|
},
|
2016-04-11 06:20:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2016-06-03 06:03:15 +03:00
|
|
|
},
|
|
|
|
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);
|
|
|
|
},
|
2016-07-04 13:12:04 +03:00
|
|
|
disconnect: function(aReason) {
|
2016-07-12 05:16:46 +03:00
|
|
|
setTimeout(()=>this._localBuilder.notifyDisconnected(aReason), 0);
|
|
|
|
setTimeout(()=>this._remoteBuilder.notifyDisconnected(aReason), 0);
|
2016-06-03 06:03:15 +03:00
|
|
|
},
|
|
|
|
set remoteBuilder(aRemoteBuilder) {
|
|
|
|
this._remoteBuilder = aRemoteBuilder;
|
|
|
|
},
|
|
|
|
set localBuilder(aLocalBuilder) {
|
|
|
|
this._localBuilder = aLocalBuilder;
|
|
|
|
},
|
2016-04-11 06:20:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2016-06-03 06:03:15 +03:00
|
|
|
clientListener.localBuilder = clientBuilder;
|
|
|
|
clientListener.remoteBuilder = serverBuilder;
|
|
|
|
serverListener.localBuilder = serverBuilder;
|
|
|
|
serverListener.remoteBuilder = clientBuilder;
|
|
|
|
|
2016-04-11 06:20:55 +03:00
|
|
|
clientBuilder
|
2016-04-18 03:23:00 +03:00
|
|
|
.buildDataChannelTransport(Ci.nsIPresentationService.ROLE_CONTROLLER,
|
2016-04-11 06:20:55 +03:00
|
|
|
window,
|
|
|
|
clientListener);
|
|
|
|
|
|
|
|
serverBuilder
|
2016-04-18 03:23:00 +03:00
|
|
|
.buildDataChannelTransport(Ci.nsIPresentationService.ROLE_RECEIVER,
|
2016-04-11 06:20:55 +03:00
|
|
|
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);
|
2017-02-03 00:17:51 +03:00
|
|
|
gReject();
|
2016-04-11 06:20:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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>
|