summaryrefslogtreecommitdiffstats
path: root/modules/libpref/test/unit/test_changeType.js
blob: 573d561a403a527ba4c2ec7d10e3b42355167579 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* 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/. */

/* Tests for changing the type of a preference (bug 985998) */

const PREF_INVALID = 0;
const PREF_BOOL    = 128;
const PREF_INT     = 64;
const PREF_STRING  = 32;

function run_test() {

  var ps = Cc["@mozilla.org/preferences-service;1"].
            getService(Ci.nsIPrefService);

  let defaultBranch = ps.getDefaultBranch("");
  let userBranch = ps.getBranch("");

  //**************************************************************************//
  // Can't change the type of prefs that have default values

  defaultBranch.setBoolPref("TypeTest.existing.bool", true);
  defaultBranch.setIntPref("TypeTest.existing.int", 23);
  defaultBranch.setCharPref("TypeTest.existing.char", "hey");

  // The user branch reads back the expected default
  do_check_eq(userBranch.getBoolPref("TypeTest.existing.bool"), true);
  do_check_eq(userBranch.getIntPref("TypeTest.existing.int"), 23);
  do_check_eq(userBranch.getCharPref("TypeTest.existing.char"), "hey");

  // All the combinations of attempted type changes
  do_check_throws(function() {
    userBranch.setCharPref("TypeTest.existing.bool", "boo"); }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function() {
    userBranch.setIntPref("TypeTest.existing.bool", 5); }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function() {
    userBranch.setCharPref("TypeTest.existing.int", "boo"); }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function() {
    userBranch.setBoolPref("TypeTest.existing.int", true); }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function() {
    userBranch.setBoolPref("TypeTest.existing.char", true); }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function() {
    userBranch.setIntPref("TypeTest.existing.char", 6); }, Cr.NS_ERROR_UNEXPECTED);


  //**************************************************************************//
  // Prefs that don't have default values can mutate
  let pref = "TypeTest.user";
  userBranch.setBoolPref(pref, true);
  userBranch.setCharPref(pref, "yay");
  do_check_eq(userBranch.getCharPref(pref), "yay");
  userBranch.setIntPref(pref, 7);
  do_check_eq(userBranch.getIntPref(pref), 7);
  userBranch.setBoolPref(pref, false);
  do_check_eq(userBranch.getBoolPref(pref), false);
  userBranch.setIntPref(pref, 8);
  do_check_eq(userBranch.getIntPref(pref), 8);
  userBranch.setCharPref(pref, "whee");
  do_check_eq(userBranch.getCharPref(pref), "whee");
  userBranch.setBoolPref(pref, true);
  do_check_eq(userBranch.getBoolPref(pref), true);
}