summaryrefslogtreecommitdiffstats
path: root/modules/libpref/test/unit/test_dirtyPrefs.js
blob: 3611716165f8405e9b0d20475e932ba894a0214d (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
/* 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 handling of the preferences 'dirty' flag (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("");

  let prefFile = do_get_profile();
  prefFile.append("prefs.js");

  //**************************************************************************//
  // prefs are not dirty after a write
  ps.savePrefFile(prefFile);
  do_check_false(ps.dirty);

  // set a new a user value, we should become dirty
  userBranch.setBoolPref("DirtyTest.new.bool", true);
  do_check_true(ps.dirty);
  ps.savePrefFile(prefFile);
  // Overwrite a pref with the same value => not dirty
  userBranch.setBoolPref("DirtyTest.new.bool", true);
  do_check_false(ps.dirty);

  // Repeat for the other two types
  userBranch.setIntPref("DirtyTest.new.int", 1);
  do_check_true(ps.dirty);
  ps.savePrefFile(prefFile);
  // Overwrite a pref with the same value => not dirty
  userBranch.setIntPref("DirtyTest.new.int", 1);
  do_check_false(ps.dirty);

  userBranch.setCharPref("DirtyTest.new.char", "oop");
  do_check_true(ps.dirty);
  ps.savePrefFile(prefFile);
  // Overwrite a pref with the same value => not dirty
  userBranch.setCharPref("DirtyTest.new.char", "oop");
  do_check_false(ps.dirty);

  // change *type* of a user value -> dirty
  userBranch.setBoolPref("DirtyTest.new.char", false);
  do_check_true(ps.dirty);
  ps.savePrefFile(prefFile);

  // Set a default pref => not dirty (defaults don't go into prefs.js)
  defaultBranch.setBoolPref("DirtyTest.existing.bool", true);
  do_check_false(ps.dirty);
  // Fail to change type of a pref with default value -> not dirty
  do_check_throws(function() {
    userBranch.setCharPref("DirtyTest.existing.bool", "boo"); }, Cr.NS_ERROR_UNEXPECTED);
  do_check_false(ps.dirty);

  // Set user value same as default, not dirty
  userBranch.setBoolPref("DirtyTest.existing.bool", true);
  do_check_false(ps.dirty);
  // User value different from default, dirty
  userBranch.setBoolPref("DirtyTest.existing.bool", false);
  do_check_true(ps.dirty);
  ps.savePrefFile(prefFile);
  // Back to default value, dirty again
  userBranch.setBoolPref("DirtyTest.existing.bool", true);
  do_check_true(ps.dirty);
  ps.savePrefFile(prefFile);
}