summaryrefslogtreecommitdiffstats
path: root/toolkit/components/contentprefs/tests/unit_cps2/test_observers.js
blob: c48918cd9270a9dea0ebc9e2414c3bd60a474d49 (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
/* 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/. */

let global = this;

function run_test() {
  var allTests = [];
  for (var i = 0; i < tests.length; i++) {
    // Generate two wrappers of each test function that invoke the original test with an
    // appropriate privacy context.
    var pub = eval('var f = function* ' + tests[i].name + '() { yield tests[' + i + ']({ usePrivateBrowsing: false }); }; f');
    var priv = eval('var f = function* ' + tests[i].name + '_private() { yield tests[' + i + ']({ usePrivateBrowsing: true }); }; f');
    allTests.push(pub);
    allTests.push(priv);
  }
  allTests = allTests.concat(specialTests);
  runAsyncTests(allTests);
}

var tests = [

  function* observerForName_set(context) {
    yield set("a.com", "foo", 1, context);
    let args = yield on("Set", ["foo", null, "bar"]);
    observerArgsOK(args.foo, [["a.com", "foo", 1, context.usePrivateBrowsing]]);
    observerArgsOK(args.null, [["a.com", "foo", 1, context.usePrivateBrowsing]]);
    observerArgsOK(args.bar, []);

    yield setGlobal("foo", 2, context);
    args = yield on("Set", ["foo", null, "bar"]);
    observerArgsOK(args.foo, [[null, "foo", 2, context.usePrivateBrowsing]]);
    observerArgsOK(args.null, [[null, "foo", 2, context.usePrivateBrowsing]]);
    observerArgsOK(args.bar, []);
  },

  function* observerForName_remove(context) {
    yield set("a.com", "foo", 1, context);
    yield setGlobal("foo", 2, context);

    yield cps.removeByDomainAndName("a.com", "bogus", context, makeCallback());
    let args = yield on("Removed", ["foo", null, "bar"]);
    observerArgsOK(args.foo, []);
    observerArgsOK(args.null, []);
    observerArgsOK(args.bar, []);

    yield cps.removeByDomainAndName("a.com", "foo", context, makeCallback());
    args = yield on("Removed", ["foo", null, "bar"]);
    observerArgsOK(args.foo, [["a.com", "foo", context.usePrivateBrowsing]]);
    observerArgsOK(args.null, [["a.com", "foo", context.usePrivateBrowsing]]);
    observerArgsOK(args.bar, []);

    yield cps.removeGlobal("foo", context, makeCallback());
    args = yield on("Removed", ["foo", null, "bar"]);
    observerArgsOK(args.foo, [[null, "foo", context.usePrivateBrowsing]]);
    observerArgsOK(args.null, [[null, "foo", context.usePrivateBrowsing]]);
    observerArgsOK(args.bar, []);
  },

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

    yield cps.removeByDomain("bogus", context, makeCallback());
    let args = yield on("Removed", ["foo", null, "bar"]);
    observerArgsOK(args.foo, []);
    observerArgsOK(args.null, []);
    observerArgsOK(args.bar, []);

    yield cps.removeBySubdomain("a.com", context, makeCallback());
    args = yield on("Removed", ["foo", null, "bar"]);
    observerArgsOK(args.foo, [["a.com", "foo", context.usePrivateBrowsing]]);
    observerArgsOK(args.null, [["a.com", "foo", context.usePrivateBrowsing], ["b.a.com", "bar", context.usePrivateBrowsing]]);
    observerArgsOK(args.bar, [["b.a.com", "bar", context.usePrivateBrowsing]]);

    yield cps.removeAllGlobals(context, makeCallback());
    args = yield on("Removed", ["foo", null, "bar"]);
    observerArgsOK(args.foo, [[null, "foo", context.usePrivateBrowsing]]);
    observerArgsOK(args.null, [[null, "foo", context.usePrivateBrowsing]]);
    observerArgsOK(args.bar, []);
  },

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

    yield cps.removeAllDomains(context, makeCallback());
    let args = yield on("Removed", ["foo", null, "bar"]);
    observerArgsOK(args.foo, [["a.com", "foo", context.usePrivateBrowsing]]);
    observerArgsOK(args.null, [["a.com", "foo", context.usePrivateBrowsing], ["b.com", "bar", context.usePrivateBrowsing]]);
    observerArgsOK(args.bar, [["b.com", "bar", context.usePrivateBrowsing]]);
  },

  function* observerForName_removeByName(context) {
    yield set("a.com", "foo", 1, context);
    yield set("a.com", "bar", 2, context);
    yield setGlobal("foo", 3, context);

    yield cps.removeByName("bogus", context, makeCallback());
    let args = yield on("Removed", ["foo", null, "bar"]);
    observerArgsOK(args.foo, []);
    observerArgsOK(args.null, []);
    observerArgsOK(args.bar, []);

    yield cps.removeByName("foo", context, makeCallback());
    args = yield on("Removed", ["foo", null, "bar"]);
    observerArgsOK(args.foo, [["a.com", "foo", context.usePrivateBrowsing], [null, "foo", context.usePrivateBrowsing]]);
    observerArgsOK(args.null, [["a.com", "foo", context.usePrivateBrowsing], [null, "foo", context.usePrivateBrowsing]]);
    observerArgsOK(args.bar, []);
  },

  function* removeObserverForName(context) {
    let args = yield on("Set", ["foo", null, "bar"], true);

    cps.removeObserverForName("foo", args.foo.observer);
    yield set("a.com", "foo", 1, context);
    yield wait();
    observerArgsOK(args.foo, []);
    observerArgsOK(args.null, [["a.com", "foo", 1, context.usePrivateBrowsing]]);
    observerArgsOK(args.bar, []);
    args.reset();

    cps.removeObserverForName(null, args.null.observer);
    yield set("a.com", "foo", 2, context);
    yield wait();
    observerArgsOK(args.foo, []);
    observerArgsOK(args.null, []);
    observerArgsOK(args.bar, []);
    args.reset();
  },
];

// These tests are for functionality that doesn't behave the same way in private and public
// contexts, so the expected results cannot be automatically generated like the previous tests.
var specialTests = [
  function* observerForName_removeAllDomainsSince() {
    yield setWithDate("a.com", "foo", 1, 100, null);
    yield setWithDate("b.com", "foo", 2, 200, null);
    yield setWithDate("c.com", "foo", 3, 300, null);

    yield setWithDate("a.com", "bar", 1, 0, null);
    yield setWithDate("b.com", "bar", 2, 100, null);
    yield setWithDate("c.com", "bar", 3, 200, null);
    yield setGlobal("foo", 2, null);

    yield cps.removeAllDomainsSince(200, null, makeCallback());

    let args = yield on("Removed", ["foo", "bar", null]);

    observerArgsOK(args.foo, [["b.com", "foo", false], ["c.com", "foo", false]]);
    observerArgsOK(args.bar, [["c.com", "bar", false]]);
    observerArgsOK(args.null, [["b.com", "foo", false], ["c.com", "bar", false], ["c.com", "foo", false]]);
  },

  function* observerForName_removeAllDomainsSince_private() {
    let context = {usePrivateBrowsing: true};
    yield setWithDate("a.com", "foo", 1, 100, context);
    yield setWithDate("b.com", "foo", 2, 200, context);
    yield setWithDate("c.com", "foo", 3, 300, context);

    yield setWithDate("a.com", "bar", 1, 0, context);
    yield setWithDate("b.com", "bar", 2, 100, context);
    yield setWithDate("c.com", "bar", 3, 200, context);
    yield setGlobal("foo", 2, context);

    yield cps.removeAllDomainsSince(200, context, makeCallback());

    let args = yield on("Removed", ["foo", "bar", null]);

    observerArgsOK(args.foo, [["a.com", "foo", true], ["b.com", "foo", true], ["c.com", "foo", true]]);
    observerArgsOK(args.bar, [["a.com", "bar", true], ["b.com", "bar", true], ["c.com", "bar", true]]);
    observerArgsOK(args.null, [["a.com", "foo", true], ["a.com", "bar", true],
                               ["b.com", "foo", true], ["b.com", "bar", true],
                               ["c.com", "foo", true], ["c.com", "bar", true]]);
  },
];