From 02f68bcd125275489c7b77d14a34c0c4216d435b Mon Sep 17 00:00:00 2001 From: Mark Banner Date: Fri, 15 Aug 2014 13:33:51 +0100 Subject: [PATCH] Bug 1022594 Part 1a Add a getLoopBoolPref function to the MozLoopAPI. r=mikedeboer --HG-- rename : browser/components/loop/test/mochitest/browser_mozLoop_charPref.js => browser/components/loop/test/mochitest/browser_mozLoop_prefs.js rename : browser/components/loop/test/xpcshell/test_loopservice_get_loop_char_pref.js => browser/components/loop/test/xpcshell/test_loopservice_loop_prefs.js --- browser/components/loop/MozLoopAPI.jsm | 21 ++++ browser/components/loop/MozLoopService.jsm | 23 ++++ .../loop/test/mochitest/browser.ini | 2 +- ...p_charPref.js => browser_mozLoop_prefs.js} | 14 +++ .../test_loopservice_get_loop_char_pref.js | 47 -------- .../xpcshell/test_loopservice_loop_prefs.js | 106 ++++++++++++++++++ .../test_loopservice_set_loop_char_pref.js | 49 -------- .../loop/test/xpcshell/xpcshell.ini | 3 +- 8 files changed, 166 insertions(+), 99 deletions(-) rename browser/components/loop/test/mochitest/{browser_mozLoop_charPref.js => browser_mozLoop_prefs.js} (66%) delete mode 100644 browser/components/loop/test/xpcshell/test_loopservice_get_loop_char_pref.js create mode 100644 browser/components/loop/test/xpcshell/test_loopservice_loop_prefs.js delete mode 100644 browser/components/loop/test/xpcshell/test_loopservice_set_loop_char_pref.js diff --git a/browser/components/loop/MozLoopAPI.jsm b/browser/components/loop/MozLoopAPI.jsm index 5c65efb9a90..4b91874f308 100644 --- a/browser/components/loop/MozLoopAPI.jsm +++ b/browser/components/loop/MozLoopAPI.jsm @@ -160,6 +160,27 @@ function injectLoopAPI(targetWindow) { } }, + /** + * Return any preference under "loop." that's coercible to a boolean + * preference. + * + * @param {String} prefName The name of the pref without the preceding + * "loop." + * + * Any errors thrown by the Mozilla pref API are logged to the console + * and cause null to be returned. This includes the case of the preference + * not being found. + * + * @return {String} on success, null on error + */ + getLoopBoolPref: { + enumerable: true, + writable: true, + value: function(prefName) { + return MozLoopService.getLoopBoolPref(prefName); + } + }, + /** * Starts alerting the user about an incoming call */ diff --git a/browser/components/loop/MozLoopService.jsm b/browser/components/loop/MozLoopService.jsm index a9b2c417d17..7f1c611f863 100644 --- a/browser/components/loop/MozLoopService.jsm +++ b/browser/components/loop/MozLoopService.jsm @@ -613,6 +613,29 @@ this.MozLoopService = { } }, + /** + * Return any preference under "loop." that's coercible to a character + * preference. + * + * @param {String} prefName The name of the pref without the preceding + * "loop." + * + * Any errors thrown by the Mozilla pref API are logged to the console + * and cause null to be returned. This includes the case of the preference + * not being found. + * + * @return {String} on success, null on error + */ + getLoopBoolPref: function(prefName) { + try { + return Services.prefs.getBoolPref("loop." + prefName); + } catch (ex) { + console.log("getLoopBoolPref had trouble getting " + prefName + + "; exception: " + ex); + return null; + } + }, + /** * Performs a hawk based request to the loop server. * diff --git a/browser/components/loop/test/mochitest/browser.ini b/browser/components/loop/test/mochitest/browser.ini index 15d82239682..780e76aae23 100644 --- a/browser/components/loop/test/mochitest/browser.ini +++ b/browser/components/loop/test/mochitest/browser.ini @@ -3,6 +3,6 @@ support-files = head.js [browser_mozLoop_appVersionInfo.js] -[browser_mozLoop_charPref.js] +[browser_mozLoop_prefs.js] [browser_mozLoop_doNotDisturb.js] skip-if = buildapp == 'mulet' diff --git a/browser/components/loop/test/mochitest/browser_mozLoop_charPref.js b/browser/components/loop/test/mochitest/browser_mozLoop_prefs.js similarity index 66% rename from browser/components/loop/test/mochitest/browser_mozLoop_charPref.js rename to browser/components/loop/test/mochitest/browser_mozLoop_prefs.js index 8e8ef7ddacb..a669d27cfcc 100644 --- a/browser/components/loop/test/mochitest/browser_mozLoop_charPref.js +++ b/browser/components/loop/test/mochitest/browser_mozLoop_prefs.js @@ -24,3 +24,17 @@ add_task(function* test_mozLoop_charPref() { Assert.equal(gMozLoopAPI.getLoopCharPref("test"), "foo", "should get loop pref value correctly"); }); + +add_task(function* test_mozLoop_boolPref() { + registerCleanupFunction(function () { + Services.prefs.clearUserPref("loop.testBool"); + }); + + Assert.ok(gMozLoopAPI, "mozLoop should exist"); + + Services.prefs.setBoolPref("loop.testBool", true); + + // Test getLoopCharPref + Assert.equal(gMozLoopAPI.getLoopBoolPref("testBool"), true, + "should get loop pref value correctly"); +}); diff --git a/browser/components/loop/test/xpcshell/test_loopservice_get_loop_char_pref.js b/browser/components/loop/test/xpcshell/test_loopservice_get_loop_char_pref.js deleted file mode 100644 index f2f0230eaef..00000000000 --- a/browser/components/loop/test/xpcshell/test_loopservice_get_loop_char_pref.js +++ /dev/null @@ -1,47 +0,0 @@ -/* Any copyright is dedicated to the Public Domain. - http://creativecommons.org/publicdomain/zero/1.0/ */ -/*global XPCOMUtils, Services, Assert */ - -var fakePrefName = "color"; -var fakePrefValue = "green"; - -function test_getLoopCharPref() -{ - Services.prefs.setCharPref("loop." + fakePrefName, fakePrefValue); - - var returnedPref = MozLoopService.getLoopCharPref(fakePrefName); - - Assert.equal(returnedPref, fakePrefValue, - "Should return a char pref under the loop. branch"); - Services.prefs.clearUserPref("loop." + fakePrefName); -} - -function test_getLoopCharPref_not_found() -{ - var returnedPref = MozLoopService.getLoopCharPref(fakePrefName); - - Assert.equal(returnedPref, null, - "Should return null if a preference is not found"); -} - -function test_getLoopCharPref_non_coercible_type() -{ - Services.prefs.setBoolPref("loop." + fakePrefName, false ); - - var returnedPref = MozLoopService.getLoopCharPref(fakePrefName); - - Assert.equal(returnedPref, null, - "Should return null if the preference exists & is of a non-coercible type"); -} - - -function run_test() -{ - test_getLoopCharPref(); - test_getLoopCharPref_not_found(); - test_getLoopCharPref_non_coercible_type(); - - do_register_cleanup(function() { - Services.prefs.clearUserPref("loop." + fakePrefName); - }); -} diff --git a/browser/components/loop/test/xpcshell/test_loopservice_loop_prefs.js b/browser/components/loop/test/xpcshell/test_loopservice_loop_prefs.js new file mode 100644 index 00000000000..c9af9a6a4dc --- /dev/null +++ b/browser/components/loop/test/xpcshell/test_loopservice_loop_prefs.js @@ -0,0 +1,106 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ +/*global XPCOMUtils, Services, Assert */ + +var fakeCharPrefName = "color"; +var fakeBoolPrefName = "boolean"; +var fakePrefValue = "green"; + +function test_getLoopCharPref() +{ + Services.prefs.setCharPref("loop." + fakeCharPrefName, fakePrefValue); + + var returnedPref = MozLoopService.getLoopCharPref(fakeCharPrefName); + + Assert.equal(returnedPref, fakePrefValue, + "Should return a char pref under the loop. branch"); + Services.prefs.clearUserPref("loop." + fakeCharPrefName); +} + +function test_getLoopCharPref_not_found() +{ + var returnedPref = MozLoopService.getLoopCharPref(fakeCharPrefName); + + Assert.equal(returnedPref, null, + "Should return null if a preference is not found"); +} + +function test_getLoopCharPref_non_coercible_type() +{ + Services.prefs.setBoolPref("loop." + fakeCharPrefName, false); + + var returnedPref = MozLoopService.getLoopCharPref(fakeCharPrefName); + + Assert.equal(returnedPref, null, + "Should return null if the preference exists & is of a non-coercible type"); +} + +function test_setLoopCharPref() +{ + Services.prefs.setCharPref("loop." + fakeCharPrefName, "red"); + MozLoopService.setLoopCharPref(fakeCharPrefName, fakePrefValue); + + var returnedPref = Services.prefs.getCharPref("loop." + fakeCharPrefName); + + Assert.equal(returnedPref, fakePrefValue, + "Should set a char pref under the loop. branch"); + Services.prefs.clearUserPref("loop." + fakeCharPrefName); +} + +function test_setLoopCharPref_new() +{ + Services.prefs.clearUserPref("loop." + fakeCharPrefName); + MozLoopService.setLoopCharPref(fakeCharPrefName, fakePrefValue); + + var returnedPref = Services.prefs.getCharPref("loop." + fakeCharPrefName); + + Assert.equal(returnedPref, fakePrefValue, + "Should set a new char pref under the loop. branch"); + Services.prefs.clearUserPref("loop." + fakeCharPrefName); +} + +function test_setLoopCharPref_non_coercible_type() +{ + MozLoopService.setLoopCharPref(fakeCharPrefName, true); + + ok(true, "Setting non-coercible type should not fail"); +} + + +function test_getLoopBoolPref() +{ + Services.prefs.setBoolPref("loop." + fakeBoolPrefName, true); + + var returnedPref = MozLoopService.getLoopBoolPref(fakeBoolPrefName); + + Assert.equal(returnedPref, true, + "Should return a bool pref under the loop. branch"); + Services.prefs.clearUserPref("loop." + fakeBoolPrefName); +} + +function test_getLoopBoolPref_not_found() +{ + var returnedPref = MozLoopService.getLoopBoolPref(fakeBoolPrefName); + + Assert.equal(returnedPref, null, + "Should return null if a preference is not found"); +} + + +function run_test() +{ + test_getLoopCharPref(); + test_getLoopCharPref_not_found(); + test_getLoopCharPref_non_coercible_type(); + test_setLoopCharPref(); + test_setLoopCharPref_new(); + test_setLoopCharPref_non_coercible_type(); + + test_getLoopBoolPref(); + test_getLoopBoolPref_not_found(); + + do_register_cleanup(function() { + Services.prefs.clearUserPref("loop." + fakeCharPrefName); + Services.prefs.clearUserPref("loop." + fakeBoolPrefName); + }); +} diff --git a/browser/components/loop/test/xpcshell/test_loopservice_set_loop_char_pref.js b/browser/components/loop/test/xpcshell/test_loopservice_set_loop_char_pref.js deleted file mode 100644 index 98213588b4f..00000000000 --- a/browser/components/loop/test/xpcshell/test_loopservice_set_loop_char_pref.js +++ /dev/null @@ -1,49 +0,0 @@ -/* Any copyright is dedicated to the Public Domain. - http://creativecommons.org/publicdomain/zero/1.0/ */ -/*global XPCOMUtils, Services, Assert */ - -var fakePrefName = "color"; -var fakePrefValue = "green"; - -function test_setLoopCharPref() -{ - Services.prefs.setCharPref("loop." + fakePrefName, "red"); - MozLoopService.setLoopCharPref(fakePrefName, fakePrefValue); - - var returnedPref = Services.prefs.getCharPref("loop." + fakePrefName); - - Assert.equal(returnedPref, fakePrefValue, - "Should set a char pref under the loop. branch"); - Services.prefs.clearUserPref("loop." + fakePrefName); -} - -function test_setLoopCharPref_new() -{ - Services.prefs.clearUserPref("loop." + fakePrefName); - MozLoopService.setLoopCharPref(fakePrefName, fakePrefValue); - - var returnedPref = Services.prefs.getCharPref("loop." + fakePrefName); - - Assert.equal(returnedPref, fakePrefValue, - "Should set a new char pref under the loop. branch"); - Services.prefs.clearUserPref("loop." + fakePrefName); -} - -function test_setLoopCharPref_non_coercible_type() -{ - MozLoopService.setLoopCharPref(fakePrefName, true); - - ok(true, "Setting non-coercible type should not fail"); -} - - -function run_test() -{ - test_setLoopCharPref(); - test_setLoopCharPref_new(); - test_setLoopCharPref_non_coercible_type(); - - do_register_cleanup(function() { - Services.prefs.clearUserPref("loop." + fakePrefName); - }); -} diff --git a/browser/components/loop/test/xpcshell/xpcshell.ini b/browser/components/loop/test/xpcshell/xpcshell.ini index 95bfa172646..5ad16005a4f 100644 --- a/browser/components/loop/test/xpcshell/xpcshell.ini +++ b/browser/components/loop/test/xpcshell/xpcshell.ini @@ -6,8 +6,7 @@ firefox-appdir = browser [test_looppush_initialize.js] [test_loopservice_dnd.js] [test_loopservice_expiry.js] -[test_loopservice_get_loop_char_pref.js] -[test_loopservice_set_loop_char_pref.js] +[test_loopservice_loop_prefs.js] [test_loopservice_initialize.js] [test_loopservice_locales.js] [test_loopservice_registration.js]