Bug 1002414 - Part 2: Add additional tests for new functional paths. r=standard8
authorPaul Kerr [:pkerr] <pkerr@mozilla.com>
Thu, 17 Jul 2014 17:18:52 -0700
changeset 194856 89b195298ca341b4797682c4a3659ab60dc844da
parent 194855 a561499de7a4a22429d3e82a0f4a259a4abce734
child 194857 daa82439f77ff7bac1b577e98b7773ce04bcb3c4
push id7810
push userryanvm@gmail.com
push dateFri, 18 Jul 2014 20:08:45 +0000
treeherderfx-team@85d5040f1ad2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersstandard8
bugs1002414
milestone33.0a1
Bug 1002414 - Part 2: Add additional tests for new functional paths. r=standard8
browser/components/loop/test/xpcshell/head.js
browser/components/loop/test/xpcshell/test_looppush_initialize.js
--- a/browser/components/loop/test/xpcshell/head.js
+++ b/browser/components/loop/test/xpcshell/head.js
@@ -13,16 +13,18 @@ XPCOMUtils.defineLazyModuleGetter(this, 
 
 XPCOMUtils.defineLazyModuleGetter(this, "MozLoopPushHandler",
                                   "resource:///modules/loop/MozLoopPushHandler.jsm");
 
 const kMockWebSocketChannelName = "Mock WebSocket Channel";
 const kWebSocketChannelContractID = "@mozilla.org/network/protocol;1?name=wss";
 
 const kServerPushUrl = "http://localhost:3456";
+const kEndPointUrl = "http://example.com/fake";
+const kUAID = "f47ac11b-58ca-4372-9567-0e02b2c3d479";
 
 // Fake loop server
 var loopServer;
 
 function setupFakeLoopServer() {
   loopServer = new HttpServer();
   loopServer.start(-1);
 
@@ -62,17 +64,18 @@ let mockPushHandler = {
   }
 };
 
 /**
  * Mock nsIWebSocketChannel for tests. This mocks the WebSocketChannel, and
  * enables us to check parameters and return messages similar to the push
  * server.
  */
-let MockWebSocketChannel = function() {
+let MockWebSocketChannel = function(initRegStatus) {
+  this.initRegStatus = initRegStatus;
 };
 
 MockWebSocketChannel.prototype = {
   QueryInterface: XPCOMUtils.generateQI(Ci.nsIWebSocketChannel),
 
   /**
    * nsIWebSocketChannel implementations.
    * See nsIWebSocketChannel.idl for API details.
@@ -81,33 +84,51 @@ MockWebSocketChannel.prototype = {
     this.uri = aURI;
     this.origin = aOrigin;
     this.listener = aListener;
     this.context = aContext;
 
     this.listener.onStart(this.context);
   },
 
-  notify: function(version) {
-    this.listener.onMessageAvailable(this.context,
-      JSON.stringify({
-        messageType: "notification", updates: [{
-          channelID: "8b1081ce-9b35-42b5-b8f5-3ff8cb813a50",
-          version: version
-        }]
-    }));
-  },
-
   sendMsg: function(aMsg) {
     var message = JSON.parse(aMsg);
 
     switch(message.messageType) {
       case "hello":
         this.listener.onMessageAvailable(this.context,
-          JSON.stringify({messageType: "hello"}));
+          JSON.stringify({messageType: "hello",
+                          uaid: kUAID}));
         break;
       case "register":
+        this.channelID = message.channelID;
+        let statusCode = 200;
+        if (this.initRegStatus) {
+          statusCode = this.initRegStatus;
+          this.initRegStatus = 0;
+        }
         this.listener.onMessageAvailable(this.context,
-          JSON.stringify({messageType: "register", pushEndpoint: "http://example.com/fake"}));
+          JSON.stringify({messageType: "register",
+                          status: statusCode,
+                          channelID: this.channelID,
+                          pushEndpoint: kEndPointUrl}));
         break;
     }
-  }
+  },
+
+  notify: function(version) {
+    this.listener.onMessageAvailable(this.context,
+      JSON.stringify({
+        messageType: "notification", updates: [{
+          channelID: this.channelID,
+          version: version
+        }]
+    }));
+  },
+
+  stop: function (err) {
+    this.listener.onStop(this.context, err || -1);
+  },
+
+  serverClose: function (err) {
+    this.listener.onServerClose(this.context, err || -1);
+  },
 };
--- a/browser/components/loop/test/xpcshell/test_looppush_initialize.js
+++ b/browser/components/loop/test/xpcshell/test_looppush_initialize.js
@@ -1,36 +1,63 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
-
-add_test(function test_initalize_offline() {
-  Services.io.offline = true;
-
-  MozLoopPushHandler.initialize(function(err) {
-    Assert.equal(err, "offline", "Should error with 'offline' when offline");
+{
+  add_test(function test_initalize_offline() {
+    Services.io.offline = true;
 
-    Services.io.offline = false;
-    run_next_test();
+    MozLoopPushHandler.initialize(function(err) {
+      Assert.equal(err, "offline", "Should error with 'offline' when offline");
+
+      Services.io.offline = false;
+      run_next_test();
+    });
   });
-});
 
-add_test(function test_initalize_websocket() {
   let mockWebSocket = new MockWebSocketChannel();
 
-  MozLoopPushHandler.initialize(function(err) {
-    Assert.equal(err, null, "Should return null for success");
+  add_test(function test_initalize_websocket() {
+    MozLoopPushHandler.initialize(
+      function(err, url) {
+        Assert.equal(err, null, "Should return null for success");
+        Assert.equal(url, kEndPointUrl, "Should return push server application URL");
+        Assert.equal(mockWebSocket.uri.prePath, kServerPushUrl,
+                     "Should have the url from preferences");
+        Assert.equal(mockWebSocket.origin, kServerPushUrl,
+                     "Should have the origin url from preferences");
+        Assert.equal(mockWebSocket.protocol, "push-notification",
+                     "Should have the protocol set to push-notifications");
+        mockWebSocket.notify(15);
+      },
+      function(version) {
+        Assert.equal(version, 15, "Should have version number 15");
+        run_next_test();
+      }, 
+      mockWebSocket);
+  });
 
-    Assert.equal(mockWebSocket.uri.prePath, kServerPushUrl,
-                 "Should have the url from preferences");
-    Assert.equal(mockWebSocket.origin, kServerPushUrl,
-                 "Should have the origin url from preferences");
-    Assert.equal(mockWebSocket.protocol, "push-notification",
-                 "Should have the protocol set to push-notifications");
+  add_test(function test_reconnect_websocket() {
+    MozLoopPushHandler.uaID = undefined;
+    MozLoopPushHandler.pushUrl = undefined; //Do this to force a new registration callback.
+    mockWebSocket.stop();
+  });
+
+  add_test(function test_reopen_websocket() {
+    MozLoopPushHandler.uaID = undefined;
+    MozLoopPushHandler.pushUrl = undefined; //Do this to force a new registration callback.
+    mockWebSocket.serverClose();
+  });
+
+  add_test(function test_retry_registration() {
+    MozLoopPushHandler.uaID = undefined;
+    MozLoopPushHandler.pushUrl = undefined; //Do this to force a new registration callback.
+    mockWebSocket.initRegStatus = 500;
+    mockWebSocket.stop();
+  });
+
+  function run_test() {
+    Services.prefs.setCharPref("services.push.serverURL", kServerPushUrl);
+    Services.prefs.setIntPref("loop.retry_delay.start", 10); // 10 ms
+    Services.prefs.setIntPref("loop.retry_delay.limit", 20); // 20 ms
 
     run_next_test();
-  }, function() {}, mockWebSocket);
-});
-
-function run_test() {
-  Services.prefs.setCharPref("services.push.serverURL", kServerPushUrl);
-
-  run_next_test();
-};
+  };
+}