Bug 1066506 Allow easy debugging of websockets and sdk for Loop. r=nperriault
authorMark Banner <standard8@mozilla.com>
Fri, 12 Sep 2014 16:49:38 +0100
changeset 205150 e0bfb21715fe37133460295dd74db7fbf7538793
parent 205149 0a03eb20fac5ecec9a00419f6f340d16d889e86f
child 205151 8ab67eccf920dbd94f0583fdc3c9144a4b9511f2
push id49106
push userphilringnalda@gmail.com
push dateSat, 13 Sep 2014 17:12:34 +0000
treeherdermozilla-inbound@ab04d0f2665f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnperriault
bugs1066506
milestone35.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1066506 Allow easy debugging of websockets and sdk for Loop. r=nperriault
browser/app/profile/firefox.js
browser/components/loop/content/shared/js/models.js
browser/components/loop/content/shared/js/utils.js
browser/components/loop/content/shared/js/websocket.js
browser/components/loop/test/shared/index.html
browser/components/loop/test/shared/utils_test.js
--- a/browser/app/profile/firefox.js
+++ b/browser/app/profile/firefox.js
@@ -1608,16 +1608,17 @@ pref("loop.legal.ToS_url", "https://acco
 pref("loop.legal.privacy_url", "https://www.mozilla.org/privacy/");
 pref("loop.do_not_disturb", false);
 pref("loop.ringtone", "chrome://browser/content/loop/shared/sounds/Firefox-Long.ogg");
 pref("loop.retry_delay.start", 60000);
 pref("loop.retry_delay.limit", 300000);
 pref("loop.feedback.baseUrl", "https://input.mozilla.org/api/v1/feedback");
 pref("loop.feedback.product", "Loop");
 pref("loop.debug.websocket", false);
+pref("loop.debug.sdk", false);
 
 // serverURL to be assigned by services team
 pref("services.push.serverURL", "wss://push.services.mozilla.com/");
 
 pref("social.sidebar.unload_timeout_ms", 10000);
 
 pref("dom.identity.enabled", false);
 
--- a/browser/components/loop/content/shared/js/models.js
+++ b/browser/components/loop/content/shared/js/models.js
@@ -63,16 +63,22 @@ loop.shared.models = (function(l10n) {
      * @param  {Object} options    Options object.
      */
     initialize: function(attributes, options) {
       options = options || {};
       if (!options.sdk) {
         throw new Error("missing required sdk");
       }
       this.sdk = options.sdk;
+
+      // Set loop.debug.sdk to true in the browser, or standalone:
+      // localStorage.setItem("debug.sdk", true);
+      if (loop.shared.utils.getBoolPreference("debug.sdk")) {
+        this.sdk.setLogLevel(this.sdk.DEBUG);
+      }
     },
 
     /**
      * Starts an incoming conversation.
      */
     incoming: function() {
       this.trigger("call:incoming");
     },
--- a/browser/components/loop/content/shared/js/utils.js
+++ b/browser/components/loop/content/shared/js/utils.js
@@ -24,12 +24,30 @@ loop.shared.utils = (function() {
     }
     if (navigator.platform.indexOf("Linux") !== -1) {
       platform = "linux";
     }
 
     return platform;
   }
 
+  /**
+   * Used for getting a boolean preference. It will either use the browser preferences
+   * (if navigator.mozLoop is defined) or try to get them from localStorage.
+   *
+   * @param {String} prefName The name of the preference. Note that mozLoop adds
+   *                          'loop.' to the start of the string.
+   *
+   * @return The value of the preference, or false if not available.
+   */
+  function getBoolPreference(prefName) {
+    if (navigator.mozLoop) {
+      return !!navigator.mozLoop.getLoopBoolPref(prefName);
+    }
+
+    return !!localStorage.getItem(prefName);
+  }
+
   return {
-    getTargetPlatform: getTargetPlatform
+    getTargetPlatform: getTargetPlatform,
+    getBoolPreference: getBoolPreference
   };
 })();
--- a/browser/components/loop/content/shared/js/websocket.js
+++ b/browser/components/loop/content/shared/js/websocket.js
@@ -31,21 +31,20 @@ loop.CallConnectionWebSocket = (function
     }
     if (!this.options.callId) {
       throw new Error("No callId in options");
     }
     if (!this.options.websocketToken) {
       throw new Error("No websocketToken in options");
     }
 
-    // Save the debug pref now, to avoid getting it each time.
-    if (navigator.mozLoop) {
-      this._debugWebSocket =
-        navigator.mozLoop.getLoopBoolPref("debug.websocket");
-    }
+    // Set loop.debug.sdk to true in the browser, or standalone:
+    // localStorage.setItem("debug.websocket", true);
+    this._debugWebSocket =
+      loop.shared.utils.getBoolPreference("debug.websocket");
 
     _.extend(this, Backbone.Events);
   };
 
   CallConnectionWebSocket.prototype = {
     /**
      * Start the connection to the websocket.
      *
--- a/browser/components/loop/test/shared/index.html
+++ b/browser/components/loop/test/shared/index.html
@@ -28,26 +28,28 @@
   <script src="vendor/sinon-1.9.0.js"></script>
   <script>
     /*global chai, mocha */
     chai.Assertion.includeStack = true;
     mocha.setup('bdd');
   </script>
 
   <!-- App scripts -->
+  <script src="../../content/shared/js/utils.js"></script>
   <script src="../../content/shared/js/models.js"></script>
   <script src="../../content/shared/js/mixins.js"></script>
   <script src="../../content/shared/js/views.js"></script>
   <script src="../../content/shared/js/router.js"></script>
   <script src="../../content/shared/js/websocket.js"></script>
   <script src="../../content/shared/js/feedbackApiClient.js"></script>
 
   <!-- Test scripts -->
   <script src="models_test.js"></script>
   <script src="mixins_test.js"></script>
+  <script src="utils_test.js"></script>
   <script src="views_test.js"></script>
   <script src="router_test.js"></script>
   <script src="websocket_test.js"></script>
   <script src="feedbackApiClient_test.js"></script>
   <script>
     mocha.run(function () {
       $("#mocha").append("<p id='complete'>Complete.</p>");
     });
new file mode 100644
--- /dev/null
+++ b/browser/components/loop/test/shared/utils_test.js
@@ -0,0 +1,58 @@
+/* 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/. */
+
+/* global loop, sinon */
+/* jshint newcap:false */
+
+var expect = chai.expect;
+
+describe("loop.shared.utils", function() {
+  "use strict";
+
+  var sandbox;
+  var sharedUtils = loop.shared.utils;
+
+  beforeEach(function() {
+    sandbox = sinon.sandbox.create();
+  });
+
+  afterEach(function() {
+    sandbox.restore();
+  });
+
+  describe("#getBoolPreference", function() {
+    afterEach(function() {
+      navigator.mozLoop = undefined;
+      localStorage.removeItem("test.true");
+    });
+
+    describe("mozLoop set", function() {
+      beforeEach(function() {
+        navigator.mozLoop = {
+          getLoopBoolPref: function(prefName) {
+            return prefName === "test.true";
+          }
+        };
+      });
+
+      it("should return the mozLoop preference", function() {
+        expect(sharedUtils.getBoolPreference("test.true")).eql(true);
+      });
+
+      it("should not use the localStorage value", function() {
+        localStorage.setItem("test.false", true);
+
+        expect(sharedUtils.getBoolPreference("test.false")).eql(false);
+      });
+    });
+
+    describe("mozLoop not set", function() {
+      it("should return the localStorage value", function() {
+        localStorage.setItem("test.true", true);
+
+        expect(sharedUtils.getBoolPreference("test.true")).eql(true);
+      });
+    });
+  });
+});