summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/siteIdentity/browser_identityPopup_clearSiteData.js
blob: efc4f343105c8f85c67ce60a79261c03ba72f0c6 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

const TEST_ORIGIN = "https://example.com";
const TEST_SUB_ORIGIN = "https://test1.example.com";
const TEST_ORIGIN_2 = "https://example.net";
const REMOVE_DIALOG_URL =
  "chrome://browser/content/preferences/dialogs/siteDataRemoveSelected.xhtml";

// Greek IDN for 'example.test'.
const TEST_IDN_ORIGIN =
  "https://\u03C0\u03B1\u03C1\u03AC\u03B4\u03B5\u03B9\u03B3\u03BC\u03B1.\u03B4\u03BF\u03BA\u03B9\u03BC\u03AE";
const TEST_PUNY_ORIGIN = "https://xn--hxajbheg2az3al.xn--jxalpdlp/";
const TEST_PUNY_SUB_ORIGIN = "https://sub1.xn--hxajbheg2az3al.xn--jxalpdlp/";

ChromeUtils.defineESModuleGetters(this, {
  SiteDataTestUtils: "resource://testing-common/SiteDataTestUtils.sys.mjs",
});

async function testClearing(
  testQuota,
  testCookies,
  testURI,
  originA,
  subOriginA,
  originB
) {
  // Create a variant of originB which is partitioned under top level originA.
  let { scheme, host } = Services.io.newURI(originA);
  let partitionKey = `(${scheme},${host})`;

  let { origin: originBPartitioned } =
    Services.scriptSecurityManager.createContentPrincipal(
      Services.io.newURI(originB),
      { partitionKey }
    );

  // Add some test quota storage.
  if (testQuota) {
    await SiteDataTestUtils.addToIndexedDB(originA);
    await SiteDataTestUtils.addToIndexedDB(subOriginA);
    await SiteDataTestUtils.addToIndexedDB(originBPartitioned);
  }

  // Add some test cookies.
  if (testCookies) {
    SiteDataTestUtils.addToCookies({
      origin: originA,
      name: "test1",
      value: "1",
    });
    SiteDataTestUtils.addToCookies({
      origin: originA,
      name: "test2",
      value: "2",
    });
    SiteDataTestUtils.addToCookies({
      origin: subOriginA,
      name: "test3",
      value: "1",
    });

    SiteDataTestUtils.addToCookies({
      origin: originBPartitioned,
      name: "test4",
      value: "1",
    });
  }

  await BrowserTestUtils.withNewTab(testURI, async function (browser) {
    // Verify we have added quota storage.
    if (testQuota) {
      let usage = await SiteDataTestUtils.getQuotaUsage(originA);
      Assert.greater(usage, 0, "Should have data for the base origin.");

      usage = await SiteDataTestUtils.getQuotaUsage(subOriginA);
      Assert.greater(usage, 0, "Should have data for the sub origin.");

      usage = await SiteDataTestUtils.getQuotaUsage(originBPartitioned);
      Assert.greater(usage, 0, "Should have data for the partitioned origin.");
    }

    // Open the identity popup.
    let { gIdentityHandler } = gBrowser.ownerGlobal;
    let promisePanelOpen = BrowserTestUtils.waitForEvent(
      gBrowser.ownerGlobal,
      "popupshown",
      true,
      event => event.target == gIdentityHandler._identityPopup
    );
    gIdentityHandler._identityIconBox.click();
    await promisePanelOpen;

    let clearFooter = document.getElementById(
      "identity-popup-clear-sitedata-footer"
    );
    let clearButton = document.getElementById(
      "identity-popup-clear-sitedata-button"
    );
    TestUtils.waitForCondition(
      () => !clearFooter.hidden,
      "The clear data footer is not hidden."
    );

    let cookiesCleared;
    if (testCookies) {
      cookiesCleared = Promise.all([
        TestUtils.topicObserved(
          "cookie-changed",
          (subj, data) => data == "deleted" && subj.name == "test1"
        ),
        TestUtils.topicObserved(
          "cookie-changed",
          (subj, data) => data == "deleted" && subj.name == "test2"
        ),
        TestUtils.topicObserved(
          "cookie-changed",
          (subj, data) => data == "deleted" && subj.name == "test3"
        ),
        TestUtils.topicObserved(
          "cookie-changed",
          (subj, data) => data == "deleted" && subj.name == "test4"
        ),
      ]);
    }

    // Click the "Clear data" button.
    let siteDataUpdated = TestUtils.topicObserved(
      "sitedatamanager:sites-updated"
    );
    let hideEvent = BrowserTestUtils.waitForEvent(
      gIdentityHandler._identityPopup,
      "popuphidden"
    );
    let removeDialogPromise = BrowserTestUtils.promiseAlertDialogOpen(
      "accept",
      REMOVE_DIALOG_URL
    );
    clearButton.click();
    await hideEvent;
    await removeDialogPromise;

    await siteDataUpdated;

    // Check that cookies were deleted.
    if (testCookies) {
      await cookiesCleared;
      let uri = Services.io.newURI(originA);
      is(
        Services.cookies.countCookiesFromHost(uri.host),
        0,
        "Cookies from the base domain should be cleared"
      );
      uri = Services.io.newURI(subOriginA);
      is(
        Services.cookies.countCookiesFromHost(uri.host),
        0,
        "Cookies from the sub domain should be cleared"
      );
      ok(
        !SiteDataTestUtils.hasCookies(originBPartitioned),
        "Partitioned cookies should be cleared"
      );
    }

    // Check that quota storage was deleted.
    if (testQuota) {
      await TestUtils.waitForCondition(async () => {
        let usage = await SiteDataTestUtils.getQuotaUsage(originA);
        return usage == 0;
      }, "Should have no data for the base origin.");

      let usage = await SiteDataTestUtils.getQuotaUsage(subOriginA);
      is(usage, 0, "Should have no data for the sub origin.");

      usage = await SiteDataTestUtils.getQuotaUsage(originBPartitioned);
      is(usage, 0, "Should have no data for the partitioned origin.");
    }

    // Open the site identity panel again to check that the button isn't shown anymore.
    promisePanelOpen = BrowserTestUtils.waitForEvent(
      gIdentityHandler._identityPopup,
      "popupshown"
    );
    gIdentityHandler._identityIconBox.click();
    await promisePanelOpen;

    // Wait for a second to see if the button is shown.
    // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
    await new Promise(c => setTimeout(c, 1000));

    ok(
      clearFooter.hidden,
      "The clear data footer is hidden after clearing data."
    );
  });
}

// Test removing quota managed storage.
add_task(async function test_ClearSiteData() {
  await testClearing(
    true,
    false,
    TEST_ORIGIN,
    TEST_ORIGIN,
    TEST_SUB_ORIGIN,
    TEST_ORIGIN_2
  );
});

// Test removing cookies.
add_task(async function test_ClearCookies() {
  await testClearing(
    false,
    true,
    TEST_ORIGIN,
    TEST_ORIGIN,
    TEST_SUB_ORIGIN,
    TEST_ORIGIN_2
  );
});

// Test removing both.
add_task(async function test_ClearCookiesAndSiteData() {
  await testClearing(
    true,
    true,
    TEST_ORIGIN,
    TEST_ORIGIN,
    TEST_SUB_ORIGIN,
    TEST_ORIGIN_2
  );
});

// Test IDN Domains
add_task(async function test_IDN_ClearCookiesAndSiteData() {
  await testClearing(
    true,
    true,
    TEST_IDN_ORIGIN,
    TEST_PUNY_ORIGIN,
    TEST_PUNY_SUB_ORIGIN,
    TEST_ORIGIN_2
  );
});