Tracer build fixes. (b=588021, r=dvander)
[mozilla-central.git] / testing / mochitest / mozprefs.js
blob3e86f8a0cacc422f9c8d1965f9c1a05cacc83488
1 (function() {
3   // NOTE: You *must* also include this line in any test that uses this file:
4   netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
5   
6   var prefService = Components.classes["@mozilla.org/preferences-service;1"]
7                               .getService(Components.interfaces.nsIPrefService);
9   function determinePrefKind(branch, prefName) {
10     switch (branch.getPrefType(prefName)) {
11     case branch.PREF_STRING:    return "CharPref";
12     case branch.PREF_INT:       return "IntPref";
13     case branch.PREF_BOOL:      return "BoolPref";
14     default: /* PREF_INVALID */ return "ComplexValue";
15     }
16   }
17   
18   function memoize(fn, obj) {
19     var cache = {}, sep = '___',
20         join = Array.prototype.join;
21     return function() {
22       var key = join.call(arguments, sep);
23       if (!(key in cache))
24         cache[key] = fn.apply(obj, arguments);
25       return cache[key];
26     };
27   }
28   
29   var makeAccessor = memoize(function(pref) {
30     var splat = pref.split('.'),
31         basePref = splat.pop(),
32         branch, kind;
33     
34     try {
35       branch = prefService.getBranch(splat.join('.') + '.')
36     } catch (e) {
37       alert("Calling prefService.getBranch failed: " + 
38         "did you read the NOTE in mozprefs.js?");
39       throw e;
40     }
41     
42     kind = determinePrefKind(branch, basePref);
43     
44     return function(value) {
45       var oldValue = branch['get' + kind](basePref);
46       if (arguments.length > 0)
47         branch['set' + kind](basePref, value);
48       return oldValue;
49     };
50   });
52   /* function pref(name[, value[, fn[, obj]]])
53    * -----------------------------------------
54    * Use cases:
55    *
56    *   1. Get the value of the dom.disable_open_during_load preference:
57    *
58    *      pref('dom.disable_open_during_load')
59    *
60    *   2. Set the preference to true, returning the old value:
61    *
62    *      var oldValue = pref('dom.disable_open_during_load', true);
63    *
64    *   3. Set the value of the preference to true just for the duration
65    *      of the specified function's execution:
66    *
67    *      pref('dom.disable_open_during_load', true, function() {
68    *        window.open(this.getUrl()); // fails if still loading
69    *      }, this); // for convenience, allow binding
70    *
71    *      Rationale: Unless a great deal of care is taken to catch all
72    *                 exceptions and restore original preference values,
73    *                 manually setting & restoring preferences can lead
74    *                 to unpredictable test behavior.  The try-finally
75    *                 block below eliminates that risk.
76    */
77   function pref(name, /*optional:*/ value, fn, obj) {
78     var acc = makeAccessor(name);
79     switch (arguments.length) {
80     case 1: return acc();
81     case 2: return acc(value);
82     default:
83       var oldValue = acc(value),
84           extra_args = [].slice.call(arguments, 4);
85       try { return fn.apply(obj, extra_args) }
86       finally { acc(oldValue) } // reset no matter what
87     }
88   };
89   
90   window.pref = pref; // export
92 })();