Bug 972019 - Shared conversation router logic. r=dmose

This commit is contained in:
Nicolas Perriault 2014-05-29 21:13:44 +01:00
parent 4d3afa8f91
commit 0ac653959d
4 changed files with 49 additions and 104 deletions

View File

@ -27,6 +27,10 @@
</div>
</div>
<div id="call-ended" class="call-ended hide">
<p><button class="btn btn-info" data-l10n-id="close_window"></button></p>
</div>
<script type="text/javascript" src="js/fxcom.js"></script>
<script type="text/javascript" src="libs/l10n.js"></script>
<script type="text/javascript" src="shared/libs/sdk.js"></script>

View File

@ -7,7 +7,7 @@
Components.utils.import("resource://gre/modules/Services.jsm");
var loop = loop || {};
loop.conversation = (function(TB, mozL10n) {
loop.conversation = (function(OT, mozL10n) {
"use strict";
var sharedViews = loop.shared.views,
@ -22,10 +22,22 @@ loop.conversation = (function(TB, mozL10n) {
var router;
/**
* Current conversation model instance.
* @type {loop.webapp.ConversationModel}
* Call ended view.
* @type {loop.shared.views.BaseView}
*/
var conversation;
var EndedCallView = sharedViews.BaseView.extend({
el: "#call-ended",
events: {
"click button": "closeWindow"
},
closeWindow: function(event) {
event.preventDefault();
// XXX For now, we just close the window.
window.close();
}
});
/**
* Conversation router.
@ -33,79 +45,27 @@ loop.conversation = (function(TB, mozL10n) {
* Required options:
* - {loop.shared.models.ConversationModel} conversation Conversation model.
* - {loop.shared.components.Notifier} notifier Notifier component.
*
* @type {loop.shared.router.BaseConversationRouter}
*/
var ConversationRouter = loop.shared.router.BaseRouter.extend({
/**
* Current conversation.
* @type {loop.shared.models.ConversationModel}
*/
_conversation: undefined,
/**
* Notifications dispatcher.
* @type {loop.shared.views.NotificationListView}
*/
_notifier: undefined,
var ConversationRouter = loop.shared.router.BaseConversationRouter.extend({
routes: {
"start/:version": "start",
"call/ongoing": "conversation",
"call/ended": "ended"
},
initialize: function(options) {
options = options || {};
if (!options.conversation) {
throw new Error("missing required conversation");
}
this._conversation = options.conversation;
if (!options.notifier) {
throw new Error("missing required notifier");
}
this._notifier = options.notifier;
this.listenTo(this._conversation, "session:ready", this._onSessionReady);
this.listenTo(this._conversation, "session:ended", this._onSessionEnded);
this.listenTo(this._conversation, "session:peer-hung", this._onPeerHung);
this.listenTo(this._conversation, "session:network-disconnected",
this._onNetworkDisconnected);
},
/**
* Navigates to conversation when the call session is ready.
* @override {loop.shared.router.BaseConversationRouter.startCall}
*/
_onSessionReady: function() {
startCall: function() {
this.navigate("call/ongoing", {trigger: true});
},
/**
* Navigates to ended state when the call has ended.
* @override {loop.shared.router.BaseConversationRouter.endCall}
*/
_onSessionEnded: function() {
this.navigate("call/ended", {trigger: true});
},
/**
* Peer hung up. Navigates back to call initiation so the user can start
* calling again.
*
* Event properties:
* - {String} connectionId: OT session id
*
* @param {Object} event
*/
_onPeerHung: function(event) {
this._notifier.warn(__("peer_ended_conversation"));
this.navigate("call/ended", {trigger: true});
},
/**
* Network disconnected. Navigates back to call initiation so the user can
* start calling again.
*/
_onNetworkDisconnected: function() {
this._notifier.warn(__("network_disconnected"));
endCall: function() {
this.navigate("call/ended", {trigger: true});
},
@ -140,17 +100,16 @@ loop.conversation = (function(TB, mozL10n) {
this.loadView(
new loop.shared.views.ConversationView({
sdk: TB,
sdk: OT,
model: this._conversation
}));
},
/**
* ended does any necessary work to end the call.
* XXX: load a view with a close button for now?
*/
ended: function() {
// XXX Later we implement the end-of call here (bug 974873)
window.close();
this.loadView(new EndedCallView());
}
});
@ -158,9 +117,8 @@ loop.conversation = (function(TB, mozL10n) {
* Panel initialisation.
*/
function init() {
conversation = new loop.shared.models.ConversationModel();
router = new ConversationRouter({
conversation: conversation,
conversation: new loop.shared.models.ConversationModel(),
notifier: new sharedViews.NotificationListView({el: "#messages"})
});
Backbone.history.start();
@ -168,6 +126,7 @@ loop.conversation = (function(TB, mozL10n) {
return {
ConversationRouter: ConversationRouter,
EndedCallView: EndedCallView,
init: init
};
})(window.TB, document.mozL10n);
})(window.OT, document.mozL10n);

View File

@ -33,20 +33,6 @@ describe("loop.conversation", function() {
conversation = new loop.shared.models.ConversationModel();
});
describe("#constructor", function() {
it("should require a ConversationModel instance", function() {
expect(function() {
new ConversationRouter();
}).to.Throw(Error, /missing required conversation/);
});
it("should require a notifier", function() {
expect(function() {
new ConversationRouter({conversation: conversation});
}).to.Throw(Error, /missing required notifier/);
});
});
describe("Routes", function() {
var router;
@ -110,13 +96,7 @@ describe("loop.conversation", function() {
describe("#ended", function() {
// XXX When the call is ended gracefully, we should check that we
// close connections nicely
it("should close the window", function() {
sandbox.stub(window, "close");
router.ended();
sinon.assert.calledOnce(window.close);
});
it("should close the window");
});
});
@ -154,25 +134,13 @@ describe("loop.conversation", function() {
sinon.assert.calledWith(router.navigate, "call/ended");
});
it("should warn the user when peer hangs up", function() {
conversation.trigger("session:peer-hung");
sinon.assert.calledOnce(notifier.warn);
});
it("should navigate to call/ended when peer hangs up", function() {
conversation.trigger("session:peer-hung");
conversation.trigger("session:peer-hungup");
sinon.assert.calledOnce(router.navigate);
sinon.assert.calledWith(router.navigate, "call/ended");
});
it("should warn the user when network disconnects", function() {
conversation.trigger("session:network-disconnected");
sinon.assert.calledOnce(notifier.warn);
});
it("should navigate to call/{token} when network disconnects",
function() {
conversation.trigger("session:network-disconnected");
@ -181,6 +149,18 @@ describe("loop.conversation", function() {
sinon.assert.calledWith(router.navigate, "call/ended");
});
});
});
describe("EndedCallView", function() {
describe("#closeWindow", function() {
it("should close the conversation window", function() {
sandbox.stub(window, "close");
var view = new loop.conversation.EndedCallView();
view.closeWindow({preventDefault: sandbox.spy()});
sinon.assert.calledOnce(window.close);
});
});
});
});

View File

@ -8,3 +8,5 @@ cannot_start_call_session_not_ready=Can't start call, session is not ready.
network_disconnected=The network connection terminated abruptly.
peer_ended_conversation=Your peer ended the conversation.
stop=Stop
call_has_ended=Your call has ended.
close_window=Close this window