diff --git a/services/common/tests/unit/test_hawkrequest.js b/services/common/tests/unit/test_hawkrequest.js index ac42d5a7bb3c..3b7d77c7d619 100644 --- a/services/common/tests/unit/test_hawkrequest.js +++ b/services/common/tests/unit/test_hawkrequest.js @@ -43,7 +43,7 @@ add_test(function test_intl_accept_language() { "fa-CG;ik", // Congolese dialect of Farsei, defaulting to Inupiaq ]; - function setLanguage(lang) { + function setLanguagePref(lang) { let acceptLanguage = Cc["@mozilla.org/supports-string;1"] .createInstance(Ci.nsISupportsString); acceptLanguage.data = lang; @@ -53,20 +53,27 @@ add_test(function test_intl_accept_language() { let hawk = new HAWKAuthenticatedRESTRequest("https://example.com"); - Services.prefs.addObserver("intl.accept_languages", nextTest, false); - setLanguage(languages[testCount]); + Services.prefs.addObserver("intl.accept_languages", checkLanguagePref, false); + setLanguagePref(languages[testCount]); - function nextTest() { + function checkLanguagePref() { + var _done = false; CommonUtils.nextTick(function() { - if (testCount < 2) { - do_check_eq(hawk._intl.accept_languages, languages[testCount]); + // Ensure we're only called for the number of entries in languages[]. + do_check_true(testCount < languages.length); - testCount += 1; - setLanguage(languages[testCount]); - nextTest(); + do_check_eq(hawk._intl.accept_languages, languages[testCount]); + + testCount++; + if (testCount < languages.length) { + // Set next language in prefs; Pref service will call checkNextLanguage. + setLanguagePref(languages[testCount]); return; } - Services.prefs.removeObserver("intl.accept_languages", nextTest); + + // We've checked all the entries in languages[]. Cleanup and move on. + do_print("Checked " + testCount + " languages. Removing checkLanguagePref as pref observer."); + Services.prefs.removeObserver("intl.accept_languages", checkLanguagePref); run_next_test(); return; });