summaryrefslogtreecommitdiffstats
path: root/modules/libpref/test/unit/test_extprefs.js
blob: 37f8de1672ca7722ae8c8bfba4a5ee66c94ad172 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/licenses/publicdomain/  */

Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");

// The profile directory is already set up in the head_ files.

function arrayenumerator(a)
{
  return {
    i_: 0,
    QueryInterface: XPCOMUtils.generateQI([Ci.nsISimpleEnumerator]),
    hasMoreElements: function ae_hasMoreElements() {
      return this.i_ < a.length;
    },
    getNext: function ae_getNext() {
      return a[this.i_++];
    }
  };
}

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

  var extprefs = [do_get_file("extdata")];
  
  var extProvider = {
    QueryInterface: XPCOMUtils.generateQI([Ci.nsIDirectoryServiceProvider,
                                           Ci.nsIDirectoryServiceProvider2]),
    getFile: function ep_getFile() {
      throw Cr.NS_ERROR_FAILURE;
    },
    
    getFiles: function ep_getFiles(key) {
      if (key != "ExtPrefDL")
        throw Cr.NS_ERROR_FAILURE;

      return arrayenumerator(extprefs);
    }
  };
  
  let prefFile = do_get_file("data/testPref.js");

  do_check_throws(function() {
    ps.getBoolPref("testExtPref.bool");
  }, Cr.NS_ERROR_UNEXPECTED);
  do_check_throws(function() {
    ps.getBoolPref("testPref.bool1");
  }, Cr.NS_ERROR_UNEXPECTED);
  
  ps.readUserPrefs(prefFile);

  do_check_true(ps.getBoolPref("testPref.bool1"));
  ps.setBoolPref("testPref.bool1", false);
  do_check_false(ps.getBoolPref("testPref.bool1"));
  
  dirSvc.registerProvider(extProvider);
  Services.obs.notifyObservers(null, "load-extension-defaults", null);

  // The extension default should be available.
  do_check_true(ps.getBoolPref("testExtPref.bool"));

  // The extension default should not override existing user prefs
  do_check_false(ps.getBoolPref("testPref.bool2"));

  // The extension default should not modify existing set values
  do_check_false(ps.getBoolPref("testPref.bool1"));
}