browser/fuel/test/browser_ApplicationPrefs.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/fuel/test/browser_ApplicationPrefs.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,179 @@
     1.4 +// The various properties that we'll be testing
     1.5 +var testdata = {
     1.6 +  missing: "fuel.fuel-test-missing",
     1.7 +  dummy: "fuel.fuel-test",
     1.8 +  string: "browser.active_color",
     1.9 +  integer: "permissions.default.image",
    1.10 +  boolean: "browser.underline_anchors"
    1.11 +};
    1.12 +
    1.13 +function test() {
    1.14 +  // test getting nonexistent values
    1.15 +  var itemValue = Application.prefs.getValue(testdata.missing, "default");
    1.16 +  is(itemValue, "default", "Check 'Application.prefs.getValue' for nonexistent item");
    1.17 +
    1.18 +  is(Application.prefs.get(testdata.missing), null, "Check 'Application.prefs.get' for nonexistent item");
    1.19 +
    1.20 +  // test setting and getting a value
    1.21 +  Application.prefs.setValue(testdata.dummy, "dummy");
    1.22 +  itemValue = Application.prefs.getValue(testdata.dummy, "default");
    1.23 +  is(itemValue, "dummy", "Check 'Application.prefs.getValue' for existing item");
    1.24 +
    1.25 +  // test for overwriting an existing value
    1.26 +  Application.prefs.setValue(testdata.dummy, "smarty");
    1.27 +  itemValue = Application.prefs.getValue(testdata.dummy, "default");
    1.28 +  is(itemValue, "smarty", "Check 'Application.prefs.getValue' for overwritten item");
    1.29 +
    1.30 +  // test setting and getting a value
    1.31 +  Application.prefs.get(testdata.dummy).value = "dummy2";
    1.32 +  itemValue = Application.prefs.get(testdata.dummy).value;
    1.33 +  is(itemValue, "dummy2", "Check 'Application.prefs.get().value' for existing item");
    1.34 +
    1.35 +  // test resetting a pref [since there is no default value, the pref should disappear]
    1.36 +  Application.prefs.get(testdata.dummy).reset();
    1.37 +  itemValue = Application.prefs.getValue(testdata.dummy, "default");
    1.38 +  is(itemValue, "default", "Check 'Application.prefs.getValue' for reset pref");
    1.39 +
    1.40 +  // test to see if a non-existant property exists
    1.41 +  ok(!Application.prefs.has(testdata.dummy), "Check non-existant property for existence");
    1.42 +
    1.43 +  // PREF: string browser.active_color == #EE0000
    1.44 +
    1.45 +  // test to see if an existing string property exists
    1.46 +  ok(Application.prefs.has(testdata.string), "Check existing string property for existence");
    1.47 +
    1.48 +  // test accessing a non-existant string property
    1.49 +  var val = Application.prefs.getValue(testdata.dummy, "default");
    1.50 +  is(val, "default", "Check non-existant string property for expected value");
    1.51 +
    1.52 +  // test accessing an existing string property
    1.53 +  var val = Application.prefs.getValue(testdata.string, "default");
    1.54 +  is(val, "#EE0000", "Check existing string property for expected value");
    1.55 +
    1.56 +  // test manipulating an existing string property
    1.57 +  Application.prefs.setValue(testdata.string, "#EF0000");
    1.58 +  var val = Application.prefs.getValue(testdata.string, "default");
    1.59 +  is(val, "#EF0000", "Set existing string property");
    1.60 +
    1.61 +  // test getting the type of an existing string property
    1.62 +  var type = Application.prefs.get(testdata.string).type;
    1.63 +  is(type, "String", "Check 'Application.prefs.get().type' for string pref");
    1.64 +
    1.65 +  // test resetting an existing string property
    1.66 +  Application.prefs.get(testdata.string).reset();
    1.67 +  var val = Application.prefs.getValue(testdata.string, "default");
    1.68 +  is(val, "#EE0000", "Reset existing string property");
    1.69 +
    1.70 +  // PREF: integer permissions.default.image == 1
    1.71 +
    1.72 +  // test to see if an existing integer property exists
    1.73 +  ok(Application.prefs.has(testdata.integer), "Check existing integer property for existence");
    1.74 +
    1.75 +  // test accessing a non-existant integer property
    1.76 +  var val = Application.prefs.getValue(testdata.dummy, 0);
    1.77 +  is(val, 0, "Check non-existant integer property for expected value");
    1.78 +
    1.79 +  // test accessing an existing integer property
    1.80 +  var val = Application.prefs.getValue(testdata.integer, 0);
    1.81 +  is(val, 1, "Check existing integer property for expected value");
    1.82 +
    1.83 +  // test manipulating an existing integer property
    1.84 +  Application.prefs.setValue(testdata.integer, 0);
    1.85 +  var val = Application.prefs.getValue(testdata.integer, 1);
    1.86 +  is(val, 0, "Set existing integer property");
    1.87 +
    1.88 +  // test getting the type of an existing integer property
    1.89 +  var type = Application.prefs.get(testdata.integer).type;
    1.90 +  is(type, "Number", "Check 'Application.prefs.get().type' for integer pref");
    1.91 +
    1.92 +  // test resetting an existing integer property
    1.93 +  Application.prefs.get(testdata.integer).reset();
    1.94 +  var val = Application.prefs.getValue(testdata.integer, 0);
    1.95 +  is(val, 1, "Reset existing integer property");
    1.96 +
    1.97 +  // PREF: boolean browser.underline_anchors == true
    1.98 +
    1.99 +  // test to see if an existing boolean property exists
   1.100 +  ok(Application.prefs.has(testdata.boolean), "Check existing boolean property for existence");
   1.101 +
   1.102 +  // test accessing a non-existant boolean property
   1.103 +  var val = Application.prefs.getValue(testdata.dummy, true);
   1.104 +  ok(val, "Check non-existant boolean property for expected value");
   1.105 +
   1.106 +  // test accessing an existing boolean property
   1.107 +  var val = Application.prefs.getValue(testdata.boolean, false);
   1.108 +  ok(val, "Check existing boolean property for expected value");
   1.109 +
   1.110 +  // test manipulating an existing boolean property
   1.111 +  Application.prefs.setValue(testdata.boolean, false);
   1.112 +  var val = Application.prefs.getValue(testdata.boolean, true);
   1.113 +  ok(!val, "Set existing boolean property");
   1.114 +
   1.115 +  // test getting the type of an existing boolean property
   1.116 +  var type = Application.prefs.get(testdata.boolean).type;
   1.117 +  is(type, "Boolean", "Check 'Application.prefs.get().type' for boolean pref");
   1.118 +
   1.119 +  // test resetting an existing boolean property
   1.120 +  Application.prefs.get(testdata.boolean).reset();
   1.121 +  var val = Application.prefs.getValue(testdata.boolean, false);
   1.122 +  ok(val, "Reset existing string property for expected value");
   1.123 +
   1.124 +  // test getting all preferences
   1.125 +  var allPrefs = Application.prefs.all;
   1.126 +  ok(allPrefs.length >= 800, "Check 'Application.prefs.all' for the right number of preferences");
   1.127 +  ok(allPrefs[0].name.length > 0, "Check 'Application.prefs.all' for a valid name in the starting preference");
   1.128 +
   1.129 +  // test the value of the preference root
   1.130 +  is(Application.prefs.root, "", "Check the Application preference root");
   1.131 +
   1.132 +  // test for user changed preferences
   1.133 +  ok(Application.prefs.get("browser.shell.checkDefaultBrowser").modified, "A single preference is marked as modified.");
   1.134 +  ok(!Application.prefs.get(testdata.string).modified, "A single preference is marked as not modified.");
   1.135 +
   1.136 +  // test for a locked preference
   1.137 +  var pref = Application.prefs.get(testdata.string);
   1.138 +  ok(!pref.locked, "A single preference should not be locked.");
   1.139 +
   1.140 +  pref.locked = true;
   1.141 +  ok(pref.locked, "A single preference should be locked.");
   1.142 +
   1.143 +  try {
   1.144 +    prev.value = "test value";
   1.145 +
   1.146 +    ok(false, "A locked preference could be modified.");
   1.147 +  } catch(e){
   1.148 +    ok(true, "A locked preference should not be able to be modified.");
   1.149 +  }
   1.150 +
   1.151 +  pref.locked = false;
   1.152 +  ok(!pref.locked, "A single preference is unlocked.");
   1.153 +
   1.154 +  // check for change event when setting a value
   1.155 +  waitForExplicitFinish();
   1.156 +  Application.prefs.events.addListener("change", onPrefChange);
   1.157 +  Application.prefs.setValue("fuel.fuel-test", "change event");
   1.158 +}
   1.159 +
   1.160 +function onPrefChange(evt) {
   1.161 +  is(evt.data, testdata.dummy, "Check 'Application.prefs.setValue' fired a change event");
   1.162 +  Application.prefs.events.removeListener("change", onPrefChange);
   1.163 +
   1.164 +  // We are removing the old listener after adding the new listener so we can test that
   1.165 +  // removing a listener does not remove all listeners
   1.166 +  Application.prefs.get("fuel.fuel-test").events.addListener("change", onPrefChangeDummy);
   1.167 +  Application.prefs.get("fuel.fuel-test").events.addListener("change", onPrefChange2);
   1.168 +  Application.prefs.get("fuel.fuel-test").events.removeListener("change", onPrefChangeDummy);
   1.169 +
   1.170 +  Application.prefs.setValue("fuel.fuel-test", "change event2");
   1.171 +}
   1.172 +
   1.173 +function onPrefChange2(evt) {
   1.174 +  is(evt.data, testdata.dummy, "Check 'Application.prefs.setValue' fired a change event for a single preference");
   1.175 +  Application.prefs.events.removeListener("change", onPrefChange2);
   1.176 +
   1.177 +  finish();
   1.178 +}
   1.179 +
   1.180 +function onPrefChangeDummy(evt) {
   1.181 +  ok(false, "onPrefChangeDummy shouldn't be invoked!");
   1.182 +}

mercurial