summaryrefslogtreecommitdiffstats
path: root/browser/components/preferences/in-content/tests/browser_cookies_exceptions.js
blob: 89313d7366fed0c88f98546c38cfe582f09f53eb (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

requestLongerTimeout(2);

function test() {
  waitForExplicitFinish();
  requestLongerTimeout(3);
  testRunner.runTests();
}

var testRunner = {

  tests:
    [
      {
        test: function(params) {
          params.url.value = "test.com";
          params.btnAllow.doCommand();
          is(params.tree.view.rowCount, 1, "added exception shows up in treeview");
          is(params.tree.view.getCellText(0, params.nameCol), "http://test.com",
                                          "origin name should be set correctly");
          is(params.tree.view.getCellText(0, params.statusCol), params.allowText,
                                          "permission text should be set correctly");
          params.btnApplyChanges.doCommand();
        },
        observances: [{ type: "cookie", origin: "http://test.com", data: "added",
                        capability: Ci.nsIPermissionManager.ALLOW_ACTION }],
      },
      {
        test: function(params) {
          params.url.value = "test.com";
          params.btnBlock.doCommand();
          is(params.tree.view.getCellText(0, params.nameCol), "http://test.com",
                                          "origin name should be set correctly");
          is(params.tree.view.getCellText(0, params.statusCol), params.denyText,
                                          "permission should change to deny in UI");
          params.btnApplyChanges.doCommand();
        },
        observances: [{ type: "cookie", origin: "http://test.com", data: "changed",
                        capability: Ci.nsIPermissionManager.DENY_ACTION  }],
      },
      {
        test: function(params) {
          params.url.value = "test.com";
          params.btnAllow.doCommand();
          is(params.tree.view.getCellText(0, params.nameCol), "http://test.com",
                                          "origin name should be set correctly");
          is(params.tree.view.getCellText(0, params.statusCol), params.allowText,
                                          "permission should revert back to allow");
          params.btnApplyChanges.doCommand();
        },
        observances: [{ type: "cookie", origin: "http://test.com", data: "changed",
                        capability: Ci.nsIPermissionManager.ALLOW_ACTION }],
      },
      {
        test: function(params) {
          params.url.value = "test.com";
          params.btnRemove.doCommand();
          is(params.tree.view.rowCount, 0, "exception should be removed");
          params.btnApplyChanges.doCommand();
        },
        observances: [{ type: "cookie", origin: "http://test.com", data: "deleted" }],
      },
      {
        expectPermObservancesDuringTestFunction: true,
        test: function(params) {
          let uri = params.ioService.newURI("http://test.com", null, null);
          params.pm.add(uri, "popup", Ci.nsIPermissionManager.DENY_ACTION);
          is(params.tree.view.rowCount, 0, "adding unrelated permission should not change display");
          params.btnApplyChanges.doCommand();
        },
        observances: [{ type: "popup", origin: "http://test.com", data: "added",
                        capability: Ci.nsIPermissionManager.DENY_ACTION }],
        cleanUp: function(params) {
          let uri = params.ioService.newURI("http://test.com", null, null);
          params.pm.remove(uri, "popup");
        },
      },
      {
        test: function(params) {
          params.url.value = "https://test.com:12345";
          params.btnAllow.doCommand();
          is(params.tree.view.rowCount, 1, "added exception shows up in treeview");
          is(params.tree.view.getCellText(0, params.nameCol), "https://test.com:12345",
                                          "origin name should be set correctly");
          is(params.tree.view.getCellText(0, params.statusCol), params.allowText,
                                          "permission text should be set correctly");
          params.btnApplyChanges.doCommand();
        },
        observances: [{ type: "cookie", origin: "https://test.com:12345", data: "added",
                        capability: Ci.nsIPermissionManager.ALLOW_ACTION }],
      },
      {
        test: function(params) {
          params.url.value = "https://test.com:12345";
          params.btnBlock.doCommand();
          is(params.tree.view.getCellText(0, params.nameCol), "https://test.com:12345",
                                          "origin name should be set correctly");
          is(params.tree.view.getCellText(0, params.statusCol), params.denyText,
                                          "permission should change to deny in UI");
          params.btnApplyChanges.doCommand();
        },
        observances: [{ type: "cookie", origin: "https://test.com:12345", data: "changed",
                        capability: Ci.nsIPermissionManager.DENY_ACTION  }],
      },
      {
        test: function(params) {
          params.url.value = "https://test.com:12345";
          params.btnAllow.doCommand();
          is(params.tree.view.getCellText(0, params.nameCol), "https://test.com:12345",
                                          "origin name should be set correctly");
          is(params.tree.view.getCellText(0, params.statusCol), params.allowText,
                                          "permission should revert back to allow");
          params.btnApplyChanges.doCommand();
        },
        observances: [{ type: "cookie", origin: "https://test.com:12345", data: "changed",
                        capability: Ci.nsIPermissionManager.ALLOW_ACTION }],
      },
      {
        test: function(params) {
          params.url.value = "https://test.com:12345";
          params.btnRemove.doCommand();
          is(params.tree.view.rowCount, 0, "exception should be removed");
          params.btnApplyChanges.doCommand();
        },
        observances: [{ type: "cookie", origin: "https://test.com:12345", data: "deleted" }],
      },
      {
        test: function(params) {
          params.url.value = "localhost:12345";
          params.btnAllow.doCommand();
          is(params.tree.view.rowCount, 1, "added exception shows up in treeview");
          is(params.tree.view.getCellText(0, params.nameCol), "http://localhost:12345",
                                          "origin name should be set correctly");
          is(params.tree.view.getCellText(0, params.statusCol), params.allowText,
                                          "permission text should be set correctly");
          params.btnApplyChanges.doCommand();
        },
        observances: [{ type: "cookie", origin: "http://localhost:12345", data: "added",
                        capability: Ci.nsIPermissionManager.ALLOW_ACTION }],
      },
      {
        test: function(params) {
          params.url.value = "localhost:12345";
          params.btnBlock.doCommand();
          is(params.tree.view.getCellText(0, params.nameCol), "http://localhost:12345",
                                          "origin name should be set correctly");
          is(params.tree.view.getCellText(0, params.statusCol), params.denyText,
                                          "permission should change to deny in UI");
          params.btnApplyChanges.doCommand();
        },
        observances: [{ type: "cookie", origin: "http://localhost:12345", data: "changed",
                        capability: Ci.nsIPermissionManager.DENY_ACTION  }],
      },
      {
        test: function(params) {
          params.url.value = "localhost:12345";
          params.btnAllow.doCommand();
          is(params.tree.view.getCellText(0, params.nameCol), "http://localhost:12345",
                                          "origin name should be set correctly");
          is(params.tree.view.getCellText(0, params.statusCol), params.allowText,
                                          "permission should revert back to allow");
          params.btnApplyChanges.doCommand();
        },
        observances: [{ type: "cookie", origin: "http://localhost:12345", data: "changed",
                        capability: Ci.nsIPermissionManager.ALLOW_ACTION }],
      },
      {
        test: function(params) {
          params.url.value = "localhost:12345";
          params.btnRemove.doCommand();
          is(params.tree.view.rowCount, 0, "exception should be removed");
          params.btnApplyChanges.doCommand();
        },
        observances: [{ type: "cookie", origin: "http://localhost:12345", data: "deleted" }],
      },
      {
        expectPermObservancesDuringTestFunction: true,
        test(params) {
          for (let URL of ["http://a", "http://z", "http://b"]) {
            let URI = params.ioService.newURI(URL, null, null);
            params.pm.add(URI, "cookie", Ci.nsIPermissionManager.ALLOW_ACTION);
          }

          is(params.tree.view.rowCount, 3, "Three permissions should be present");
          is(params.tree.view.getCellText(0, params.nameCol), "http://a",
             "site should be sorted. 'a' should be first");
          is(params.tree.view.getCellText(1, params.nameCol), "http://b",
             "site should be sorted. 'b' should be second");
          is(params.tree.view.getCellText(2, params.nameCol), "http://z",
             "site should be sorted. 'z' should be third");

          // Sort descending then check results in cleanup since sorting isn't synchronous.
          EventUtils.synthesizeMouseAtCenter(params.doc.getElementById("siteCol"), {},
                                             params.doc.defaultView);
          params.btnApplyChanges.doCommand();
        },
        observances: [{ type: "cookie", origin: "http://a", data: "added",
                        capability: Ci.nsIPermissionManager.ALLOW_ACTION },
                      { type: "cookie", origin: "http://z", data: "added",
                        capability: Ci.nsIPermissionManager.ALLOW_ACTION },
                      { type: "cookie", origin: "http://b", data: "added",
                        capability: Ci.nsIPermissionManager.ALLOW_ACTION }],
        cleanUp(params) {
          is(params.tree.view.getCellText(0, params.nameCol), "http://z",
             "site should be sorted. 'z' should be first");
          is(params.tree.view.getCellText(1, params.nameCol), "http://b",
             "site should be sorted. 'b' should be second");
          is(params.tree.view.getCellText(2, params.nameCol), "http://a",
             "site should be sorted. 'a' should be third");

          for (let URL of ["http://a", "http://z", "http://b"]) {
            let uri = params.ioService.newURI(URL, null, null);
            params.pm.remove(uri, "cookie");
          }
        },
      },
    ],

  _currentTest: -1,

  runTests: function() {
    this._currentTest++;

    info("Running test #" + (this._currentTest + 1) + "\n");
    let that = this;
    let p = this.runCurrentTest(this._currentTest + 1);
    p.then(function() {
      if (that._currentTest == that.tests.length - 1) {
        finish();
      }
      else {
        that.runTests();
      }
    });
  },

  runCurrentTest: function(testNumber) {
    return new Promise(function(resolve, reject) {

      let helperFunctions = {
        windowLoad: function(win) {
          let doc = win.document;
          let params = {
            doc,
            tree: doc.getElementById("permissionsTree"),
            nameCol: doc.getElementById("permissionsTree").treeBoxObject.columns.getColumnAt(0),
            statusCol: doc.getElementById("permissionsTree").treeBoxObject.columns.getColumnAt(1),
            url: doc.getElementById("url"),
            btnAllow: doc.getElementById("btnAllow"),
            btnBlock: doc.getElementById("btnBlock"),
            btnApplyChanges: doc.getElementById("btnApplyChanges"),
            btnRemove: doc.getElementById("removePermission"),
            pm: Cc["@mozilla.org/permissionmanager;1"]
                       .getService(Ci.nsIPermissionManager),
            ioService: Cc["@mozilla.org/network/io-service;1"]
                              .getService(Ci.nsIIOService),
            allowText: win.gPermissionManager._getCapabilityString(
                                Ci.nsIPermissionManager.ALLOW_ACTION),
            denyText: win.gPermissionManager._getCapabilityString(
                               Ci.nsIPermissionManager.DENY_ACTION),
            allow: Ci.nsIPermissionManager.ALLOW_ACTION,
            deny: Ci.nsIPermissionManager.DENY_ACTION,
          };

          let permObserver = {
            observe: function(aSubject, aTopic, aData) {
              if (aTopic != "perm-changed")
                return;

              if (testRunner.tests[testRunner._currentTest].observances.length == 0) {
                // Should fail here as we are not expecting a notification, but we don't.
                // See bug 1063410.
                return;
              }

              let permission = aSubject.QueryInterface(Ci.nsIPermission);
              let expected = testRunner.tests[testRunner._currentTest].observances.shift();

              is(aData, expected.data, "type of message should be the same");
              for (let prop of ["type", "capability"]) {
                if (expected[prop])
                  is(permission[prop], expected[prop],
                    "property: \"" + prop  + "\" should be equal");
              }

              if (expected.origin) {
                is(permission.principal.origin, expected.origin,
                   "property: \"origin\" should be equal");
              }

              os.removeObserver(permObserver, "perm-changed");

              let test = testRunner.tests[testRunner._currentTest];
              if (!test.expectPermObservancesDuringTestFunction) {
                if (test.cleanUp) {
                  test.cleanUp(params);
                }

                gBrowser.removeCurrentTab();
                resolve();
              }
            },
          };

          let os = Cc["@mozilla.org/observer-service;1"]
                     .getService(Ci.nsIObserverService);

          os.addObserver(permObserver, "perm-changed", false);

          if (testRunner._currentTest == 0) {
            is(params.tree.view.rowCount, 0, "no cookie exceptions");
          }

          try {
            let test = testRunner.tests[testRunner._currentTest];
            test.test(params);
            if (test.expectPermObservancesDuringTestFunction) {
              if (test.cleanUp) {
                test.cleanUp(params);
              }

              gBrowser.removeCurrentTab();
              resolve();
            }
          } catch (ex) {
            ok(false, "exception while running test #" +
                      testNumber + ": " + ex);
          }
        },
      };

      openPreferencesViaOpenPreferencesAPI("panePrivacy", null, {leaveOpen: true}).then(function() {
        let doc = gBrowser.contentDocument;
        let historyMode = doc.getElementById("historyMode");
        historyMode.value = "custom";
        historyMode.doCommand();
        doc.getElementById("cookieExceptions").doCommand();

        let subDialogURL = "chrome://browser/content/preferences/permissions.xul";
        promiseLoadSubDialog(subDialogURL).then(function(win) {
          helperFunctions.windowLoad(win);
        });
      });
    });
  },
};