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
This commit is contained in:
Mark Banner 2014-08-15 13:33:51 +01:00
parent 94d9c98429
commit 02f68bcd12
8 changed files with 166 additions and 99 deletions

View File

@ -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
*/

View File

@ -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.
*

View File

@ -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'

View File

@ -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");
});

View File

@ -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);
});
}

View File

@ -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);
});
}

View File

@ -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);
});
}

View File

@ -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]