summaryrefslogtreecommitdiffstats
path: root/toolkit/components/contentprefs/tests/unit_cps2/test_setGet.js
blob: b10a05bbc27b06c336e5b5be24feff2a81431007 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

function run_test() {
  runAsyncTests(tests);
}

var tests = [

  function* get_nonexistent() {
    yield getOK(["a.com", "foo"], undefined);
    yield getGlobalOK(["foo"], undefined);
  },

  function* isomorphicDomains() {
    yield set("a.com", "foo", 1);
    yield dbOK([
      ["a.com", "foo", 1],
    ]);
    yield getOK(["a.com", "foo"], 1);
    yield getOK(["http://a.com/huh", "foo"], 1, "a.com");

    yield set("http://a.com/huh", "foo", 2);
    yield dbOK([
      ["a.com", "foo", 2],
    ]);
    yield getOK(["a.com", "foo"], 2);
    yield getOK(["http://a.com/yeah", "foo"], 2, "a.com");
  },

  function* names() {
    yield set("a.com", "foo", 1);
    yield dbOK([
      ["a.com", "foo", 1],
    ]);
    yield getOK(["a.com", "foo"], 1);

    yield set("a.com", "bar", 2);
    yield dbOK([
      ["a.com", "foo", 1],
      ["a.com", "bar", 2],
    ]);
    yield getOK(["a.com", "foo"], 1);
    yield getOK(["a.com", "bar"], 2);

    yield setGlobal("foo", 3);
    yield dbOK([
      ["a.com", "foo", 1],
      ["a.com", "bar", 2],
      [null, "foo", 3],
    ]);
    yield getOK(["a.com", "foo"], 1);
    yield getOK(["a.com", "bar"], 2);
    yield getGlobalOK(["foo"], 3);

    yield setGlobal("bar", 4);
    yield dbOK([
      ["a.com", "foo", 1],
      ["a.com", "bar", 2],
      [null, "foo", 3],
      [null, "bar", 4],
    ]);
    yield getOK(["a.com", "foo"], 1);
    yield getOK(["a.com", "bar"], 2);
    yield getGlobalOK(["foo"], 3);
    yield getGlobalOK(["bar"], 4);
  },

  function* subdomains() {
    yield set("a.com", "foo", 1);
    yield set("b.a.com", "foo", 2);
    yield dbOK([
      ["a.com", "foo", 1],
      ["b.a.com", "foo", 2],
    ]);
    yield getOK(["a.com", "foo"], 1);
    yield getOK(["b.a.com", "foo"], 2);
  },

  function* privateBrowsing() {
    yield set("a.com", "foo", 1);
    yield set("a.com", "bar", 2);
    yield setGlobal("foo", 3);
    yield setGlobal("bar", 4);
    yield set("b.com", "foo", 5);

    let context = { usePrivateBrowsing: true };
    yield set("a.com", "foo", 6, context);
    yield setGlobal("foo", 7, context);
    yield dbOK([
      ["a.com", "foo", 1],
      ["a.com", "bar", 2],
      [null, "foo", 3],
      [null, "bar", 4],
      ["b.com", "foo", 5],
    ]);
    yield getOK(["a.com", "foo", context], 6, "a.com");
    yield getOK(["a.com", "bar", context], 2);
    yield getGlobalOK(["foo", context], 7);
    yield getGlobalOK(["bar", context], 4);
    yield getOK(["b.com", "foo", context], 5);

    yield getOK(["a.com", "foo"], 1);
    yield getOK(["a.com", "bar"], 2);
    yield getGlobalOK(["foo"], 3);
    yield getGlobalOK(["bar"], 4);
    yield getOK(["b.com", "foo"], 5);
  },

  function* set_erroneous() {
    do_check_throws(() => cps.set(null, "foo", 1, null));
    do_check_throws(() => cps.set("", "foo", 1, null));
    do_check_throws(() => cps.set("a.com", "", 1, null));
    do_check_throws(() => cps.set("a.com", null, 1, null));
    do_check_throws(() => cps.set("a.com", "foo", undefined, null));
    do_check_throws(() => cps.set("a.com", "foo", 1, null, "bogus"));
    do_check_throws(() => cps.setGlobal("", 1, null));
    do_check_throws(() => cps.setGlobal(null, 1, null));
    do_check_throws(() => cps.setGlobal("foo", undefined, null));
    do_check_throws(() => cps.setGlobal("foo", 1, null, "bogus"));
    yield true;
  },

  function* get_erroneous() {
    do_check_throws(() => cps.getByDomainAndName(null, "foo", null, {}));
    do_check_throws(() => cps.getByDomainAndName("", "foo", null, {}));
    do_check_throws(() => cps.getByDomainAndName("a.com", "", null, {}));
    do_check_throws(() => cps.getByDomainAndName("a.com", null, null, {}));
    do_check_throws(() => cps.getByDomainAndName("a.com", "foo", null, null));
    do_check_throws(() => cps.getGlobal("", null, {}));
    do_check_throws(() => cps.getGlobal(null, null, {}));
    do_check_throws(() => cps.getGlobal("foo", null, null));
    yield true;
  },

  function* set_invalidateCache() {
    // (1) Set a pref and wait for it to finish.
    yield set("a.com", "foo", 1);

    // (2) It should be cached.
    getCachedOK(["a.com", "foo"], true, 1);

    // (3) Set the pref to a new value but don't wait for it to finish.
    cps.set("a.com", "foo", 2, null, {
      handleCompletion: function () {
        // (6) The pref should be cached after setting it.
        getCachedOK(["a.com", "foo"], true, 2);
      },
    });

    // (4) Group "a.com" and name "foo" should no longer be cached.
    getCachedOK(["a.com", "foo"], false);

    // (5) Call getByDomainAndName.
    var fetchedPref;
    cps.getByDomainAndName("a.com", "foo", null, {
      handleResult: function (pref) {
        fetchedPref = pref;
      },
      handleCompletion: function () {
        // (7) Finally, this callback should be called after set's above.
        do_check_true(!!fetchedPref);
        do_check_eq(fetchedPref.value, 2);
        next();
      },
    });

    yield;
  },

  function* get_nameOnly() {
    yield set("a.com", "foo", 1);
    yield set("a.com", "bar", 2);
    yield set("b.com", "foo", 3);
    yield setGlobal("foo", 4);

    yield getOKEx("getByName", ["foo", undefined], [
      {"domain": "a.com", "name": "foo", "value": 1},
      {"domain": "b.com", "name": "foo", "value": 3},
      {"domain": null, "name": "foo", "value": 4}
    ]);

    let context = { usePrivateBrowsing: true };
    yield set("b.com", "foo", 5, context);

    yield getOKEx("getByName", ["foo", context], [
      {"domain": "a.com", "name": "foo", "value": 1},
      {"domain": null, "name": "foo", "value": 4},
      {"domain": "b.com", "name": "foo", "value": 5}
    ]);
  },

  function* setSetsCurrentDate() {
    // Because Date.now() is not guaranteed to be monotonically increasing
    // we just do here rough sanity check with one minute tolerance.
    const MINUTE = 60 * 1000;
    let now = Date.now();
    let start = now - MINUTE;
    let end = now + MINUTE;
    yield set("a.com", "foo", 1);
    let timestamp = yield getDate("a.com", "foo");
    ok(start <= timestamp, "Timestamp is not too early (" + start + "<=" + timestamp + ").");
    ok(timestamp <= end, "Timestamp is not too late (" + timestamp + "<=" + end + ").");
  },
];