summaryrefslogtreecommitdiffstats
path: root/modules/libpref/test/unit/test_changeType.js
blob: 94f3a84f133728f06b360a3d35203790869f13a9 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* 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) */

function run_test() {
  var ps = Services.prefs;

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

  // Prefs that only have a default value -- we can't change their type.
  defaultBranch.setBoolPref("TypeTest.default.bool", true);
  defaultBranch.setIntPref("TypeTest.default.int", 23);
  defaultBranch.setCharPref("TypeTest.default.char", "hey");

  Assert.equal(userBranch.getBoolPref("TypeTest.default.bool"), true);
  Assert.equal(userBranch.getIntPref("TypeTest.default.int"), 23);
  Assert.equal(userBranch.getCharPref("TypeTest.default.char"), "hey");

  // Prefs that only have a user value -- we can change their type, but only
  // when we set the user value.
  userBranch.setBoolPref("TypeTest.user.bool", false);
  userBranch.setIntPref("TypeTest.user.int", 24);
  userBranch.setCharPref("TypeTest.user.char", "hi");

  Assert.equal(userBranch.getBoolPref("TypeTest.user.bool"), false);
  Assert.equal(userBranch.getIntPref("TypeTest.user.int"), 24);
  Assert.equal(userBranch.getCharPref("TypeTest.user.char"), "hi");

  // Prefs that have both a default and a user value -- we can't change their
  // type.
  defaultBranch.setBoolPref("TypeTest.both.bool", true);
  userBranch.setBoolPref("TypeTest.both.bool", false);
  defaultBranch.setIntPref("TypeTest.both.int", 25);
  userBranch.setIntPref("TypeTest.both.int", 26);
  defaultBranch.setCharPref("TypeTest.both.char", "yo");
  userBranch.setCharPref("TypeTest.both.char", "ya");

  Assert.equal(userBranch.getBoolPref("TypeTest.both.bool"), false);
  Assert.equal(userBranch.getIntPref("TypeTest.both.int"), 26);
  Assert.equal(userBranch.getCharPref("TypeTest.both.char"), "ya");

  // We only have a default value, and we try to set a default value of a
  // different type --> fails.
  do_check_throws(function () {
    defaultBranch.setCharPref("TypeTest.default.bool", "boo");
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    defaultBranch.setIntPref("TypeTest.default.bool", 5);
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    defaultBranch.setCharPref("TypeTest.default.int", "boo");
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    defaultBranch.setBoolPref("TypeTest.default.int", true);
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    defaultBranch.setBoolPref("TypeTest.default.char", true);
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    defaultBranch.setIntPref("TypeTest.default.char", 6);
  }, Cr.NS_ERROR_UNEXPECTED);

  // We only have a default value, and we try to set a user value of a
  // different type --> fails.
  do_check_throws(function () {
    userBranch.setCharPref("TypeTest.default.bool", "boo");
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    userBranch.setIntPref("TypeTest.default.bool", 5);
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    userBranch.setCharPref("TypeTest.default.int", "boo");
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    userBranch.setBoolPref("TypeTest.default.int", true);
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    userBranch.setBoolPref("TypeTest.default.char", true);
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    userBranch.setIntPref("TypeTest.default.char", 6);
  }, Cr.NS_ERROR_UNEXPECTED);

  // We only have a user value, and we try to set a default value of a
  // different type --> fails.
  do_check_throws(function () {
    defaultBranch.setCharPref("TypeTest.user.bool", "boo");
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    defaultBranch.setIntPref("TypeTest.user.bool", 5);
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    defaultBranch.setCharPref("TypeTest.user.int", "boo");
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    defaultBranch.setBoolPref("TypeTest.user.int", true);
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    defaultBranch.setBoolPref("TypeTest.user.char", true);
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    defaultBranch.setIntPref("TypeTest.user.char", 6);
  }, Cr.NS_ERROR_UNEXPECTED);

  // We only have a user value, and we try to set a user value of a
  // different type --> SUCCEEDS.
  userBranch.setCharPref("TypeTest.user.bool", "boo");
  Assert.equal(userBranch.getCharPref("TypeTest.user.bool"), "boo");
  userBranch.setIntPref("TypeTest.user.bool", 5);
  Assert.equal(userBranch.getIntPref("TypeTest.user.bool"), 5);
  userBranch.setCharPref("TypeTest.user.int", "boo");
  Assert.equal(userBranch.getCharPref("TypeTest.user.int"), "boo");
  userBranch.setBoolPref("TypeTest.user.int", true);
  Assert.equal(userBranch.getBoolPref("TypeTest.user.int"), true);
  userBranch.setBoolPref("TypeTest.user.char", true);
  Assert.equal(userBranch.getBoolPref("TypeTest.user.char"), true);
  userBranch.setIntPref("TypeTest.user.char", 6);
  Assert.equal(userBranch.getIntPref("TypeTest.user.char"), 6);

  // We have both a default value and user value, and we try to set a default
  // value of a different type --> fails.
  do_check_throws(function () {
    defaultBranch.setCharPref("TypeTest.both.bool", "boo");
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    defaultBranch.setIntPref("TypeTest.both.bool", 5);
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    defaultBranch.setCharPref("TypeTest.both.int", "boo");
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    defaultBranch.setBoolPref("TypeTest.both.int", true);
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    defaultBranch.setBoolPref("TypeTest.both.char", true);
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    defaultBranch.setIntPref("TypeTest.both.char", 6);
  }, Cr.NS_ERROR_UNEXPECTED);

  // We have both a default value and user value, and we try to set a user
  // value of a different type --> fails.
  do_check_throws(function () {
    userBranch.setCharPref("TypeTest.both.bool", "boo");
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    userBranch.setIntPref("TypeTest.both.bool", 5);
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    userBranch.setCharPref("TypeTest.both.int", "boo");
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    userBranch.setBoolPref("TypeTest.both.int", true);
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    userBranch.setBoolPref("TypeTest.both.char", true);
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function () {
    userBranch.setIntPref("TypeTest.both.char", 6);
  }, Cr.NS_ERROR_UNEXPECTED);
}