toolkit/components/contentprefs/tests/unit_cps2/test_setGet.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/contentprefs/tests/unit_cps2/test_setGet.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,195 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +function run_test() {
     1.9 +  runAsyncTests(tests);
    1.10 +}
    1.11 +
    1.12 +let tests = [
    1.13 +
    1.14 +  function get_nonexistent() {
    1.15 +    yield getOK(["a.com", "foo"], undefined);
    1.16 +    yield getGlobalOK(["foo"], undefined);
    1.17 +  },
    1.18 +
    1.19 +  function isomorphicDomains() {
    1.20 +    yield set("a.com", "foo", 1);
    1.21 +    yield dbOK([
    1.22 +      ["a.com", "foo", 1],
    1.23 +    ]);
    1.24 +    yield getOK(["a.com", "foo"], 1);
    1.25 +    yield getOK(["http://a.com/huh", "foo"], 1, "a.com");
    1.26 +
    1.27 +    yield set("http://a.com/huh", "foo", 2);
    1.28 +    yield dbOK([
    1.29 +      ["a.com", "foo", 2],
    1.30 +    ]);
    1.31 +    yield getOK(["a.com", "foo"], 2);
    1.32 +    yield getOK(["http://a.com/yeah", "foo"], 2, "a.com");
    1.33 +  },
    1.34 +
    1.35 +  function names() {
    1.36 +    yield set("a.com", "foo", 1);
    1.37 +    yield dbOK([
    1.38 +      ["a.com", "foo", 1],
    1.39 +    ]);
    1.40 +    yield getOK(["a.com", "foo"], 1);
    1.41 +
    1.42 +    yield set("a.com", "bar", 2);
    1.43 +    yield dbOK([
    1.44 +      ["a.com", "foo", 1],
    1.45 +      ["a.com", "bar", 2],
    1.46 +    ]);
    1.47 +    yield getOK(["a.com", "foo"], 1);
    1.48 +    yield getOK(["a.com", "bar"], 2);
    1.49 +
    1.50 +    yield setGlobal("foo", 3);
    1.51 +    yield dbOK([
    1.52 +      ["a.com", "foo", 1],
    1.53 +      ["a.com", "bar", 2],
    1.54 +      [null, "foo", 3],
    1.55 +    ]);
    1.56 +    yield getOK(["a.com", "foo"], 1);
    1.57 +    yield getOK(["a.com", "bar"], 2);
    1.58 +    yield getGlobalOK(["foo"], 3);
    1.59 +
    1.60 +    yield setGlobal("bar", 4);
    1.61 +    yield dbOK([
    1.62 +      ["a.com", "foo", 1],
    1.63 +      ["a.com", "bar", 2],
    1.64 +      [null, "foo", 3],
    1.65 +      [null, "bar", 4],
    1.66 +    ]);
    1.67 +    yield getOK(["a.com", "foo"], 1);
    1.68 +    yield getOK(["a.com", "bar"], 2);
    1.69 +    yield getGlobalOK(["foo"], 3);
    1.70 +    yield getGlobalOK(["bar"], 4);
    1.71 +  },
    1.72 +
    1.73 +  function subdomains() {
    1.74 +    yield set("a.com", "foo", 1);
    1.75 +    yield set("b.a.com", "foo", 2);
    1.76 +    yield dbOK([
    1.77 +      ["a.com", "foo", 1],
    1.78 +      ["b.a.com", "foo", 2],
    1.79 +    ]);
    1.80 +    yield getOK(["a.com", "foo"], 1);
    1.81 +    yield getOK(["b.a.com", "foo"], 2);
    1.82 +  },
    1.83 +
    1.84 +  function privateBrowsing() {
    1.85 +    yield set("a.com", "foo", 1);
    1.86 +    yield set("a.com", "bar", 2);
    1.87 +    yield setGlobal("foo", 3);
    1.88 +    yield setGlobal("bar", 4);
    1.89 +    yield set("b.com", "foo", 5);
    1.90 +
    1.91 +    let context = { usePrivateBrowsing: true };
    1.92 +    yield set("a.com", "foo", 6, context);
    1.93 +    yield setGlobal("foo", 7, context);
    1.94 +    yield dbOK([
    1.95 +      ["a.com", "foo", 1],
    1.96 +      ["a.com", "bar", 2],
    1.97 +      [null, "foo", 3],
    1.98 +      [null, "bar", 4],
    1.99 +      ["b.com", "foo", 5],
   1.100 +    ]);
   1.101 +    yield getOK(["a.com", "foo", context], 6, "a.com");
   1.102 +    yield getOK(["a.com", "bar", context], 2);
   1.103 +    yield getGlobalOK(["foo", context], 7);
   1.104 +    yield getGlobalOK(["bar", context], 4);
   1.105 +    yield getOK(["b.com", "foo", context], 5);
   1.106 +
   1.107 +    yield getOK(["a.com", "foo"], 1);
   1.108 +    yield getOK(["a.com", "bar"], 2);
   1.109 +    yield getGlobalOK(["foo"], 3);
   1.110 +    yield getGlobalOK(["bar"], 4);
   1.111 +    yield getOK(["b.com", "foo"], 5);
   1.112 +  },
   1.113 +
   1.114 +  function set_erroneous() {
   1.115 +    do_check_throws(function () cps.set(null, "foo", 1, null));
   1.116 +    do_check_throws(function () cps.set("", "foo", 1, null));
   1.117 +    do_check_throws(function () cps.set("a.com", "", 1, null));
   1.118 +    do_check_throws(function () cps.set("a.com", null, 1, null));
   1.119 +    do_check_throws(function () cps.set("a.com", "foo", undefined, null));
   1.120 +    do_check_throws(function () cps.set("a.com", "foo", 1, null, "bogus"));
   1.121 +    do_check_throws(function () cps.setGlobal("", 1, null));
   1.122 +    do_check_throws(function () cps.setGlobal(null, 1, null));
   1.123 +    do_check_throws(function () cps.setGlobal("foo", undefined, null));
   1.124 +    do_check_throws(function () cps.setGlobal("foo", 1, null, "bogus"));
   1.125 +    yield true;
   1.126 +  },
   1.127 +
   1.128 +  function get_erroneous() {
   1.129 +    do_check_throws(function () cps.getByDomainAndName(null, "foo", null, {}));
   1.130 +    do_check_throws(function () cps.getByDomainAndName("", "foo", null, {}));
   1.131 +    do_check_throws(function () cps.getByDomainAndName("a.com", "", null, {}));
   1.132 +    do_check_throws(function ()
   1.133 +                    cps.getByDomainAndName("a.com", null, null, {}));
   1.134 +    do_check_throws(function ()
   1.135 +                    cps.getByDomainAndName("a.com", "foo", null, null));
   1.136 +    do_check_throws(function () cps.getGlobal("", null, {}));
   1.137 +    do_check_throws(function () cps.getGlobal(null, null, {}));
   1.138 +    do_check_throws(function () cps.getGlobal("foo", null, null));
   1.139 +    yield true;
   1.140 +  },
   1.141 +
   1.142 +  function set_invalidateCache() {
   1.143 +    // (1) Set a pref and wait for it to finish.
   1.144 +    yield set("a.com", "foo", 1);
   1.145 +
   1.146 +    // (2) It should be cached.
   1.147 +    getCachedOK(["a.com", "foo"], true, 1);
   1.148 +
   1.149 +    // (3) Set the pref to a new value but don't wait for it to finish.
   1.150 +    cps.set("a.com", "foo", 2, null, {
   1.151 +      handleCompletion: function () {
   1.152 +        // (6) The pref should be cached after setting it.
   1.153 +        getCachedOK(["a.com", "foo"], true, 2);
   1.154 +      },
   1.155 +    });
   1.156 +
   1.157 +    // (4) Group "a.com" and name "foo" should no longer be cached.
   1.158 +    getCachedOK(["a.com", "foo"], false);
   1.159 +
   1.160 +    // (5) Call getByDomainAndName.
   1.161 +    var fetchedPref;
   1.162 +    cps.getByDomainAndName("a.com", "foo", null, {
   1.163 +      handleResult: function (pref) {
   1.164 +        fetchedPref = pref;
   1.165 +      },
   1.166 +      handleCompletion: function () {
   1.167 +        // (7) Finally, this callback should be called after set's above.
   1.168 +        do_check_true(!!fetchedPref);
   1.169 +        do_check_eq(fetchedPref.value, 2);
   1.170 +        next();
   1.171 +      },
   1.172 +    });
   1.173 +
   1.174 +    yield;
   1.175 +  },
   1.176 +
   1.177 +  function get_nameOnly() {
   1.178 +    yield set("a.com", "foo", 1);
   1.179 +    yield set("a.com", "bar", 2);
   1.180 +    yield set("b.com", "foo", 3);
   1.181 +    yield setGlobal("foo", 4);
   1.182 +
   1.183 +    yield getOKEx("getByName", ["foo", undefined], [
   1.184 +      {"domain": "a.com", "name": "foo", "value": 1},
   1.185 +      {"domain": "b.com", "name": "foo", "value": 3},
   1.186 +      {"domain": null, "name": "foo", "value": 4}
   1.187 +    ]);
   1.188 +
   1.189 +    let context = { usePrivateBrowsing: true };
   1.190 +    yield set("b.com", "foo", 5, context);
   1.191 +
   1.192 +    yield getOKEx("getByName", ["foo", context], [
   1.193 +      {"domain": "a.com", "name": "foo", "value": 1},
   1.194 +      {"domain": null, "name": "foo", "value": 4},
   1.195 +      {"domain": "b.com", "name": "foo", "value": 5}
   1.196 +    ]);
   1.197 +  }
   1.198 +];

mercurial