summaryrefslogtreecommitdiffstats
path: root/extensions/pref/autoconfig/test/unit/test_autoconfig_nonascii.js
blob: a42bc1219ff75a78e9c29e4b1bc33e30ca693e0d (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

var {classes: Cc, interfaces: Ci, results: Cr} = Components;

function run_test() {
  let dirSvc = Cc["@mozilla.org/file/directory_service;1"].
               getService(Ci.nsIProperties);
  let obsvc = Cc['@mozilla.org/observer-service;1'].
              getService(Ci.nsIObserverService);
  let ps = Cc["@mozilla.org/preferences-service;1"].
           getService(Ci.nsIPrefService);
  let defaultPrefs = ps.getDefaultBranch(null);
  let prefs = ps.getBranch(null);

  let greD = dirSvc.get("GreD", Ci.nsIFile);
  let defaultPrefD = dirSvc.get("PrfDef", Ci.nsIFile);
  let testDir = do_get_cwd();

  try {
    let autoConfigJS = testDir.clone();
    autoConfigJS.append("autoconfig.js");
    autoConfigJS.copyTo(defaultPrefD, "autoconfig.js");

    // Make sure nsReadConfig is initialized.
    Cc["@mozilla.org/readconfig;1"].getService(Ci.nsISupports);
    ps.resetPrefs();

    var tests = [{
      filename: "autoconfig-utf8.cfg",
      prefs: {
        "_test.string.ASCII": "UTF-8",
        "_test.string.non-ASCII": "日本語",
        "_test.string.getPref": "日本語",
        "_test.string.gIsUTF8": "true"
      }
    }, {
      filename: "autoconfig-latin1.cfg",
      prefs: {
        "_test.string.ASCII": "ASCII",
        "_test.string.non-ASCII": "日本語",
        "_test.string.getPref": "日本語",
        "_test.string.gIsUTF8": "false",
      }
    }];

    function testAutoConfig(test) {
      // Make sure pref values are unset.
      for (let prefName in test.prefs) {
        do_check_eq(Ci.nsIPrefBranch.PREF_INVALID, prefs.getPrefType(prefName));
      }

      let autoConfigCfg = testDir.clone();
      autoConfigCfg.append(test.filename);
      autoConfigCfg.copyTo(greD, "autoconfig.cfg");
  
      obsvc.notifyObservers(ps, "prefservice:before-read-userprefs", null);
  
      for (let prefName in test.prefs) {
        do_check_eq(test.prefs[prefName],
                    prefs.getComplexValue(prefName, Ci.nsISupportsString).data);
      }
  
      ps.resetPrefs();
      // Make sure pref values are reset.
      for (let prefName in test.prefs) {
        do_check_eq(Ci.nsIPrefBranch.PREF_INVALID, prefs.getPrefType(prefName));
      }
    }

    tests.forEach(testAutoConfig);

  } finally {
    try {
      let autoConfigJS = defaultPrefD.clone();
      autoConfigJS.append("autoconfig.js");
      autoConfigJS.remove(false);
    } catch (e) {
      if (e.result != Cr.NS_ERROR_FILE_NOT_FOUND) {
        throw e;
      }
    }

    try {
      let autoConfigCfg = greD.clone();
      autoConfigCfg.append("autoconfig.cfg");
      autoConfigCfg.remove(false);
    } catch (e) {
      if (e.result != Cr.NS_ERROR_FILE_NOT_FOUND) {
        throw e;
      }
    }

    ps.resetPrefs();
  }
}