Bug 997178 - Restart a call, r=dmose
authorNicolas Perriault <nperriault@gmail.com>
Thu, 29 May 2014 21:20:11 +0100
changeset 187764 b0a81d18e4f9f815b330998a30f191ef014f5f92
parent 187763 d601d88a01a2e6af033213f0de9e7bec657ff9d3
child 187765 6e244a575f21deb59b6c0bb64374429d63885dd5
push idunknown
push userunknown
push dateunknown
reviewersdmose
bugs997178
milestone32.0a1
Bug 997178 - Restart a call, r=dmose
browser/components/loop/content/shared/js/models.js
browser/components/loop/content/shared/js/views.js
browser/components/loop/test/shared/models_test.js
--- a/browser/components/loop/content/shared/js/models.js
+++ b/browser/components/loop/content/shared/js/models.js
@@ -70,21 +70,16 @@ loop.shared.models = (function() {
      *
      * - `session:ready` when the session information have been successfully
      *   retrieved from the server;
      * - `session:error` when the request failed.
      *
      * @param {Object} options Options object
      */
     initiate: function(options) {
-      // Check if the session is already set
-      if (this.isSessionReady()) {
-        return this.trigger("session:ready", this);
-      }
-
       var client = new loop.shared.Client({
         baseServerUrl: options.baseServerUrl
       });
 
       function handleResult(err, sessionData) {
         /*jshint validthis:true */
         if (err) {
           this.trigger("session:error", new Error(
@@ -139,32 +134,33 @@ loop.shared.models = (function() {
     /**
      * Starts a SDK session and subscribe to call events.
      */
     startSession: function() {
       if (!this.isSessionReady()) {
         throw new Error("Can't start session as it's not ready");
       }
       this.session = this.sdk.initSession(this.get("sessionId"));
-      this.session.connect(this.get("apiKey"), this.get("sessionToken"));
       this.listenTo(this.session, "sessionConnected", this._sessionConnected);
       this.listenTo(this.session, "streamCreated", this._streamCreated);
       this.listenTo(this.session, "connectionDestroyed",
                                   this._connectionDestroyed);
       this.listenTo(this.session, "sessionDisconnected",
                                   this._sessionDisconnected);
       this.listenTo(this.session, "networkDisconnected",
                                   this._networkDisconnected);
+      this.session.connect(this.get("apiKey"), this.get("sessionToken"));
     },
 
     /**
      * Ends current session.
      */
     endSession: function() {
       this.session.disconnect();
+      this.once("session:ended", this.stopListening, this);
       this.set("ongoing", false);
     },
 
     /**
      * Session is created.
      * http://tokbox.com/opentok/libraries/client/js/reference/SessionConnectEvent.html
      *
      * @param  {SessionConnectEvent} event
@@ -187,17 +183,17 @@ loop.shared.models = (function() {
     /**
      * Local user hung up.
      * http://tokbox.com/opentok/libraries/client/js/reference/SessionDisconnectEvent.html
      *
      * @param  {SessionDisconnectEvent} event
      */
     _sessionDisconnected: function(event) {
       this.trigger("session:ended");
-      this.endSession();
+      this.set("ongoing", false);
     },
 
     /**
      * Peer hung up. Disconnects local session.
      * http://tokbox.com/opentok/libraries/client/js/reference/ConnectionEvent.html
      *
      * @param  {ConnectionEvent} event
      */
--- a/browser/components/loop/content/shared/js/views.js
+++ b/browser/components/loop/content/shared/js/views.js
@@ -121,23 +121,22 @@ loop.shared.views = (function(_, OT, l10
      */
     initialize: function(options) {
       options = options || {};
       if (!options.sdk) {
         throw new Error("missing required sdk");
       }
       this.sdk = options.sdk;
 
-      this.model.startSession();
-
       this.listenTo(this.model, "session:connected", this.publish);
       this.listenTo(this.model, "session:stream-created", this._streamCreated);
       this.listenTo(this.model, ["session:peer-hungup",
                                  "session:network-disconnected",
                                  "session:ended"].join(" "), this.unpublish);
+      this.model.startSession();
     },
 
     /**
      * Subscribes and attaches each created stream to a DOM element.
      *
      * XXX: for now we only support a single remote stream, hence a single DOM
      *      element.
      *
--- a/browser/components/loop/test/shared/models_test.js
+++ b/browser/components/loop/test/shared/models_test.js
@@ -114,30 +114,16 @@ describe("loop.shared.models", function(
               baseServerUrl: fakeBaseServerUrl,
               outgoing: true
             });
 
             sinon.assert.calledOnce(conversation.setReady);
             sinon.assert.calledWith(conversation.setReady, fakeSessionData);
           });
 
-        it("should trigger session:ready without fetching session data over " +
-           "HTTP when already set", function(done) {
-            conversation.set(fakeSessionData);
-
-            conversation.on("session:ready", function() {
-              sinon.assert.notCalled(reqCallInfoStub);
-              done();
-            }).initiate({
-              baseServerUrl: fakeBaseServerUrl,
-              outgoing: true
-            });
-
-          });
-
         it("should trigger a `session:error` on failure", function(done) {
           reqCallInfoStub.callsArgWith(1,
             new Error("failed: HTTP 400 Bad Request; fake"));
 
           conversation.on("session:error", function(err) {
             expect(err.message).to.match(/failed: HTTP 400 Bad Request; fake/);
             done();
           }).initiate({
@@ -187,16 +173,26 @@ describe("loop.shared.models", function(
 
           it("should trigger a session:ended event on sessionDisconnected",
             function(done) {
               model.once("session:ended", function(){ done(); });
 
               fakeSession.trigger("sessionDisconnected", {reason: "ko"});
             });
 
+          it("should set the ongoing attribute to false on sessionDisconnected",
+            function(done) {
+              model.once("session:ended", function() {
+                expect(model.get("ongoing")).eql(false);
+                done();
+              });
+
+              fakeSession.trigger("sessionDisconnected", {reason: "ko"});
+            });
+
           describe("connectionDestroyed event received", function() {
             var fakeEvent = {reason: "ko", connection: {connectionId: 42}};
 
             it("should trigger a session:peer-hungup model event",
               function(done) {
                 model.once("session:peer-hungup", function(event) {
                   expect(event.connectionId).eql(42);
                   done();
@@ -251,12 +247,22 @@ describe("loop.shared.models", function(
           sinon.assert.calledOnce(fakeSession.disconnect);
         });
 
         it("should set the ongoing attribute to false", function() {
           model.endSession();
 
           expect(model.get("ongoing")).eql(false);
         });
+
+        it("should stop listening to session events once the session is " +
+           "actually disconnected", function() {
+            sandbox.stub(model, "stopListening");
+
+            model.endSession();
+            model.trigger("session:ended");
+
+            sinon.assert.calledOnce(model.stopListening);
+          });
       });
     });
   });
 });