Bug 1035348 - Part 2: fix tests. 1) Added hawkRequest stub to allow completion of inbound notification setup where necessary in unit tests. 2) Replaced tests of client.requestCallsInfo() with mozLoop.getCallData. r=Standard8
authorPaul Kerr [:pkerr] <pkerr@mozilla.com>
Wed, 10 Sep 2014 15:22:33 +0100
changeset 204539 a0b9b573cbb655f7b4b542c5248a53030294649a
parent 204538 4b9b045ef203e1a91d4ecdc55ad8698ba423adf7
child 204540 bf52063ed95f132867e32cbd1fc45b62f42549f1
push id8655
push usermbanner@mozilla.com
push dateWed, 10 Sep 2014 14:23:03 +0000
treeherderfx-team@a0b9b573cbb6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersStandard8
bugs1035348
milestone35.0a1
Bug 1035348 - Part 2: fix tests. 1) Added hawkRequest stub to allow completion of inbound notification setup where necessary in unit tests. 2) Replaced tests of client.requestCallsInfo() with mozLoop.getCallData. r=Standard8
browser/components/loop/test/desktop-local/client_test.js
browser/components/loop/test/desktop-local/conversation_test.js
browser/components/loop/test/xpcshell/head.js
browser/components/loop/test/xpcshell/test_loopservice_dnd.js
browser/components/loop/test/xpcshell/test_loopservice_notification.js
--- a/browser/components/loop/test/desktop-local/client_test.js
+++ b/browser/components/loop/test/desktop-local/client_test.js
@@ -177,57 +177,10 @@ describe("loop.Client", function() {
         client.requestCallUrl("foo", callback);
 
         sinon.assert.calledOnce(callback);
         sinon.assert.calledWithMatch(callback, sinon.match(function(err) {
           return /Invalid data received/.test(err.message);
         }));
       });
     });
-
-    describe("#requestCallsInfo", function() {
-      it("should prevent launching a conversation when version is missing",
-        function() {
-          expect(function() {
-            client.requestCallsInfo();
-          }).to.Throw(Error, /missing required parameter version/);
-        });
-
-      it("should perform a get on /calls", function() {
-        client.requestCallsInfo(42, callback);
-
-        sinon.assert.calledOnce(hawkRequestStub);
-        sinon.assert.calledWith(hawkRequestStub,
-                                "/calls?version=42", "GET", null);
-
-      });
-
-      it("should request data for all calls", function() {
-        hawkRequestStub.callsArgWith(3, null,
-                                     '{"calls": [{"apiKey": "fake"}]}');
-
-        client.requestCallsInfo(42, callback);
-
-        sinon.assert.calledWithExactly(callback, null, [{apiKey: "fake"}]);
-      });
-
-      it("should send an error when the request fails", function() {
-        hawkRequestStub.callsArgWith(3, fakeErrorRes);
-
-        client.requestCallsInfo(42, callback);
-
-        sinon.assert.calledWithMatch(callback, sinon.match(function(err) {
-          return /400.*invalid token/.test(err.message);
-        }));
-      });
-
-      it("should send an error if the data is not valid", function() {
-        hawkRequestStub.callsArgWith(3, null, "{}");
-
-        client.requestCallsInfo(42, callback);
-
-        sinon.assert.calledWithMatch(callback, sinon.match(function(err) {
-          return /Invalid data received/.test(err.message);
-        }));
-      });
-    });
   });
 });
--- a/browser/components/loop/test/desktop-local/conversation_test.js
+++ b/browser/components/loop/test/desktop-local/conversation_test.js
@@ -27,16 +27,17 @@ describe("loop.conversation", function()
         return JSON.stringify({textContent: "fakeText"});
       },
       get locale() {
         return "en-US";
       },
       setLoopCharPref: sandbox.stub(),
       getLoopCharPref: sandbox.stub(),
       getLoopBoolPref: sandbox.stub(),
+      getCallData: sandbox.stub(),
       startAlerting: function() {},
       stopAlerting: function() {},
       ensureRegistered: function() {},
       get appVersionInfo() {
         return {
           version: "42",
           channel: "test",
           platform: "test"
@@ -107,17 +108,16 @@ describe("loop.conversation", function()
     var conversation, client;
 
     beforeEach(function() {
       client = new loop.Client();
       conversation = new loop.shared.models.ConversationModel({}, {
         sdk: {},
         pendingCallTimeout: 1000,
       });
-      sandbox.stub(client, "requestCallsInfo");
       sandbox.spy(conversation, "setIncomingSessionData");
       sandbox.stub(conversation, "setOutgoingSessionData");
     });
 
     describe("Routes", function() {
       var router;
 
       beforeEach(function() {
@@ -152,57 +152,41 @@ describe("loop.conversation", function()
 
         it("should start alerting", function() {
           sandbox.stub(navigator.mozLoop, "startAlerting");
           router.incoming("fakeVersion");
 
           sinon.assert.calledOnce(navigator.mozLoop.startAlerting);
         });
 
-        it("should set the loopVersion on the conversation model", function() {
-          router.incoming("fakeVersion");
-
-          expect(conversation.get("loopVersion")).to.equal("fakeVersion");
-        });
-
-        it("should call requestCallsInfo on the client",
+        it("should call getCallData on navigator.mozLoop",
           function() {
             router.incoming(42);
 
-            sinon.assert.calledOnce(client.requestCallsInfo);
-            sinon.assert.calledWith(client.requestCallsInfo, 42);
+            sinon.assert.calledOnce(navigator.mozLoop.getCallData);
+            sinon.assert.calledWith(navigator.mozLoop.getCallData, 42);
           });
 
-        it("should display an error if requestCallsInfo returns an error",
-          function(){
-            sandbox.stub(notifications, "errorL10n");
-            client.requestCallsInfo.callsArgWith(1, "failed");
-
-            router.incoming(42);
-
-            sinon.assert.calledOnce(notifications.errorL10n);
-          });
-
-        describe("requestCallsInfo successful", function() {
+        describe("getCallData successful", function() {
           var fakeSessionData, resolvePromise, rejectPromise;
 
           beforeEach(function() {
             fakeSessionData  = {
               sessionId:      "sessionId",
               sessionToken:   "sessionToken",
               apiKey:         "apiKey",
               callType:       "callType",
               callId:         "Hello",
               progressURL:    "http://progress.example.com",
               websocketToken: 123
             };
 
             sandbox.stub(router, "_setupWebSocketAndCallView");
 
-            client.requestCallsInfo.callsArgWith(1, null, [fakeSessionData]);
+            navigator.mozLoop.getCallData.returns(fakeSessionData);
           });
 
           it("should store the session data", function() {
             router.incoming("fakeVersion");
 
             sinon.assert.calledOnce(conversation.setIncomingSessionData);
             sinon.assert.calledWithExactly(conversation.setIncomingSessionData,
                                            fakeSessionData);
--- a/browser/components/loop/test/xpcshell/head.js
+++ b/browser/components/loop/test/xpcshell/head.js
@@ -22,16 +22,23 @@ const kEndPointUrl = "http://example.com
 const kUAID = "f47ac11b-58ca-4372-9567-0e02b2c3d479";
 
 // Fake loop server
 var loopServer;
 
 // Ensure loop is always enabled for tests
 Services.prefs.setBoolPref("loop.enabled", true);
 
+function hawkGetCallsRequest() {
+  let response = {body: JSON.stringify({calls: [{callId: 4444333221, websocketToken: "0deadbeef0"}]})},
+      // Call the first non-null then(resolve) function attached to the fakePromise.
+      fakePromise = {then: (resolve) => {return resolve ? resolve(response) : fakePromise;},
+                     catch: () => {return fakePromise;}};
+  return fakePromise;
+}
 
 function setupFakeLoopServer() {
   loopServer = new HttpServer();
   loopServer.start(-1);
 
   Services.prefs.setCharPref("services.push.serverURL", kServerPushUrl);
 
   Services.prefs.setCharPref("loop.server",
--- a/browser/components/loop/test/xpcshell/test_loopservice_dnd.js
+++ b/browser/components/loop/test/xpcshell/test_loopservice_dnd.js
@@ -1,16 +1,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 XPCOMUtils.defineLazyModuleGetter(this, "Chat",
                                   "resource:///modules/Chat.jsm");
+let openChatOrig = Chat.open;
 
-let openChatOrig = Chat.open;
+const loopServiceModule = Cu.import("resource:///modules/loop/MozLoopService.jsm", {});
 
 add_test(function test_get_do_not_disturb() {
   Services.prefs.setBoolPref("loop.do_not_disturb", false);
 
   do_check_false(MozLoopService.doNotDisturb);
 
   Services.prefs.setBoolPref("loop.do_not_disturb", true);
 
@@ -33,20 +34,25 @@ add_test(function test_do_not_disturb_di
   MozLoopService.doNotDisturb = false;
 
   MozLoopService.register(mockPushHandler).then(() => {
     let opened = false;
     Chat.open = function() {
       opened = true;
     };
 
+    let savedHawkClient = loopServiceModule.gHawkClient;
+    loopServiceModule.gHawkClient = {request: hawkGetCallsRequest};
+
     mockPushHandler.notify(1);
 
     do_check_true(opened, "should open a chat window");
 
+    loopServiceModule.gHawkClient = savedHawkClient;
+
     run_next_test();
   });
 });
 
 add_task(function test_do_not_disturb_enabled_shouldnt_open_chat_window() {
   MozLoopService.doNotDisturb = true;
 
   // We registered in the previous test, so no need to do that on this one.
--- a/browser/components/loop/test/xpcshell/test_loopservice_notification.js
+++ b/browser/components/loop/test/xpcshell/test_loopservice_notification.js
@@ -2,32 +2,39 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 XPCOMUtils.defineLazyModuleGetter(this, "Chat",
                                   "resource:///modules/Chat.jsm");
 
 let openChatOrig = Chat.open;
 
+const loopServiceModule = Cu.import("resource:///modules/loop/MozLoopService.jsm", {});
+
 add_test(function test_openChatWindow_on_notification() {
   Services.prefs.setCharPref("loop.seenToS", "unseen");
 
   MozLoopService.register(mockPushHandler).then(() => {
     let opened = false;
     Chat.open = function() {
       opened = true;
     };
 
+    let savedHawkClient = loopServiceModule.gHawkClient;
+    loopServiceModule.gHawkClient = {request: hawkGetCallsRequest};
+
     mockPushHandler.notify(1);
 
     do_check_true(opened, "should open a chat window");
 
     do_check_eq(Services.prefs.getCharPref("loop.seenToS"), "seen",
                 "should set the pref to 'seen'");
 
+    loopServiceModule.gHawkClient = savedHawkClient;
+
     run_next_test();
   });
 });
 
 function run_test()
 {
   setupFakeLoopServer();