From f45b0af958a2100844118311a42911f093df69ee Mon Sep 17 00:00:00 2001 From: Mark Banner Date: Fri, 12 Sep 2014 16:49:38 +0100 Subject: [PATCH] Bug 1066506 Allow easy debugging of websockets and sdk for Loop. r=nperriault --- browser/app/profile/firefox.js | 1 + .../loop/content/shared/js/models.js | 6 ++ .../loop/content/shared/js/utils.js | 20 ++++++- .../loop/content/shared/js/websocket.js | 9 ++- .../components/loop/test/shared/index.html | 2 + .../components/loop/test/shared/utils_test.js | 58 +++++++++++++++++++ 6 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 browser/components/loop/test/shared/utils_test.js diff --git a/browser/app/profile/firefox.js b/browser/app/profile/firefox.js index 089fba84e28b..9b14825cc237 100644 --- a/browser/app/profile/firefox.js +++ b/browser/app/profile/firefox.js @@ -1613,6 +1613,7 @@ 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/"); diff --git a/browser/components/loop/content/shared/js/models.js b/browser/components/loop/content/shared/js/models.js index df1f0055ef03..e62940c750a4 100644 --- a/browser/components/loop/content/shared/js/models.js +++ b/browser/components/loop/content/shared/js/models.js @@ -68,6 +68,12 @@ loop.shared.models = (function(l10n) { 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); + } }, /** diff --git a/browser/components/loop/content/shared/js/utils.js b/browser/components/loop/content/shared/js/utils.js index 2e62943109e8..84186bba0848 100644 --- a/browser/components/loop/content/shared/js/utils.js +++ b/browser/components/loop/content/shared/js/utils.js @@ -29,7 +29,25 @@ loop.shared.utils = (function() { 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 }; })(); diff --git a/browser/components/loop/content/shared/js/websocket.js b/browser/components/loop/content/shared/js/websocket.js index bc7de924c21a..d9c1435f0e17 100644 --- a/browser/components/loop/content/shared/js/websocket.js +++ b/browser/components/loop/content/shared/js/websocket.js @@ -36,11 +36,10 @@ loop.CallConnectionWebSocket = (function() { 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); }; diff --git a/browser/components/loop/test/shared/index.html b/browser/components/loop/test/shared/index.html index 2291b467d64b..ec71b0cce675 100644 --- a/browser/components/loop/test/shared/index.html +++ b/browser/components/loop/test/shared/index.html @@ -33,6 +33,7 @@ + @@ -43,6 +44,7 @@ + diff --git a/browser/components/loop/test/shared/utils_test.js b/browser/components/loop/test/shared/utils_test.js new file mode 100644 index 000000000000..07abe8c41f51 --- /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); + }); + }); + }); +});