Skip to content

Commit 40e1109

Browse files
Bug 1868603 - (part 2) Tests for unblocking clear history dialog from data size loading. r=pbz
Differential Revision: https://phabricator.services.mozilla.com/D202343
1 parent 9c91d11 commit 40e1109

File tree

3 files changed

+166
-26
lines changed

3 files changed

+166
-26
lines changed

browser/base/content/test/sanitize/browser_sanitizeDialog_v2.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ async function performActionsOnDialog({
200200
cache = false,
201201
siteSettings = false,
202202
}) {
203-
let dh = new ClearHistoryDialogHelper(context);
203+
let dh = new ClearHistoryDialogHelper({ mode: context });
204204
dh.onload = function () {
205205
this.selectDuration(timespan);
206206
this.checkPrefCheckbox(
@@ -262,7 +262,7 @@ add_task(async function test_cancel() {
262262

263263
// test remembering user options for various entry points
264264
add_task(async function test_pref_remembering() {
265-
let dh = new ClearHistoryDialogHelper("clearSiteData");
265+
let dh = new ClearHistoryDialogHelper({ mode: "clearSiteData" });
266266
dh.onload = function () {
267267
this.checkPrefCheckbox("cookiesAndStorage", false);
268268
this.checkPrefCheckbox("siteSettings", true);
@@ -273,7 +273,7 @@ add_task(async function test_pref_remembering() {
273273
await dh.promiseClosed;
274274

275275
// validate if prefs are remembered
276-
dh = new ClearHistoryDialogHelper("clearSiteData");
276+
dh = new ClearHistoryDialogHelper({ mode: "clearSiteData" });
277277
dh.onload = function () {
278278
this.validateCheckbox("cookiesAndStorage", false);
279279
this.validateCheckbox("siteSettings", true);
@@ -289,7 +289,7 @@ add_task(async function test_pref_remembering() {
289289
await dh.promiseClosed;
290290

291291
// validate if prefs did not change since we cancelled the dialog
292-
dh = new ClearHistoryDialogHelper("clearSiteData");
292+
dh = new ClearHistoryDialogHelper({ mode: "clearSiteData" });
293293
dh.onload = function () {
294294
this.validateCheckbox("cookiesAndStorage", false);
295295
this.validateCheckbox("siteSettings", true);
@@ -302,7 +302,7 @@ add_task(async function test_pref_remembering() {
302302
// test rememebering prefs from the clear history context
303303
// since clear history and clear site data have seperate remembering
304304
// of prefs
305-
dh = new ClearHistoryDialogHelper("clearHistory");
305+
dh = new ClearHistoryDialogHelper({ mode: "clearHistory" });
306306
dh.onload = function () {
307307
this.checkPrefCheckbox("cookiesAndStorage", true);
308308
this.checkPrefCheckbox("siteSettings", false);
@@ -314,7 +314,7 @@ add_task(async function test_pref_remembering() {
314314
await dh.promiseClosed;
315315

316316
// validate if prefs are remembered across both clear history and browser
317-
dh = new ClearHistoryDialogHelper("browser");
317+
dh = new ClearHistoryDialogHelper({ mode: "browser" });
318318
dh.onload = function () {
319319
this.validateCheckbox("cookiesAndStorage", true);
320320
this.validateCheckbox("siteSettings", false);
@@ -448,7 +448,7 @@ add_task(async function testAcceptButtonDisabled() {
448448
* Tests to see if the warning box is hidden when opened in the clear on shutdown context
449449
*/
450450
add_task(async function testWarningBoxInClearOnShutdown() {
451-
let dh = new ClearHistoryDialogHelper("clearSiteData");
451+
let dh = new ClearHistoryDialogHelper({ mode: "clearSiteData" });
452452
dh.onload = function () {
453453
this.selectDuration(Sanitizer.TIMESPAN_EVERYTHING);
454454
is(
@@ -461,7 +461,7 @@ add_task(async function testWarningBoxInClearOnShutdown() {
461461
dh.open();
462462
await dh.promiseClosed;
463463

464-
dh = new ClearHistoryDialogHelper("clearOnShutdown");
464+
dh = new ClearHistoryDialogHelper({ mode: "clearOnShutdown" });
465465
dh.onload = function () {
466466
is(
467467
BrowserTestUtils.isVisible(this.getWarningPanel()),
@@ -639,7 +639,7 @@ add_task(async function test_clear_on_shutdown() {
639639
set: [["privacy.sanitize.sanitizeOnShutdown", true]],
640640
});
641641

642-
let dh = new ClearHistoryDialogHelper("clearOnShutdown");
642+
let dh = new ClearHistoryDialogHelper({ mode: "clearOnShutdown" });
643643
dh.onload = async function () {
644644
this.uncheckAllCheckboxes();
645645
this.checkPrefCheckbox("historyFormDataAndDownloads", false);
@@ -708,7 +708,7 @@ add_task(async function test_clear_on_shutdown() {
708708
await ensureDownloadsClearedState(downloadIDs, false);
709709
await ensureDownloadsClearedState(olderDownloadIDs, false);
710710

711-
dh = new ClearHistoryDialogHelper("clearOnShutdown");
711+
dh = new ClearHistoryDialogHelper({ mode: "clearOnShutdown" });
712712
dh.onload = async function () {
713713
this.uncheckAllCheckboxes();
714714
this.checkPrefCheckbox("historyFormDataAndDownloads", true);
@@ -908,7 +908,7 @@ add_task(async function testClearHistoryCheckboxStatesAfterMigration() {
908908
],
909909
});
910910

911-
let dh = new ClearHistoryDialogHelper("clearHistory");
911+
let dh = new ClearHistoryDialogHelper({ mode: "clearHistory" });
912912
dh.onload = function () {
913913
this.validateCheckbox("cookiesAndStorage", true);
914914
this.validateCheckbox("historyFormDataAndDownloads", false);
@@ -929,7 +929,7 @@ add_task(async function testClearHistoryCheckboxStatesAfterMigration() {
929929
);
930930

931931
// make sure the migration doesn't run again
932-
dh = new ClearHistoryDialogHelper("clearHistory");
932+
dh = new ClearHistoryDialogHelper({ mode: "clearHistory" });
933933
dh.onload = function () {
934934
this.validateCheckbox("siteSettings", true);
935935
this.validateCheckbox("cookiesAndStorage", false);

browser/base/content/test/sanitize/browser_sanitizeDialog_v2_dataSizes.js

Lines changed: 131 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
/**
66
* This tests the new clear history dialog's data size display functionality
77
*/
8+
ChromeUtils.defineESModuleGetters(this, {
9+
sinon: "resource://testing-common/Sinon.sys.mjs",
10+
Sanitizer: "resource:///modules/Sanitizer.sys.mjs",
11+
});
812

913
add_setup(async function () {
1014
await blankSlate();
@@ -84,13 +88,12 @@ async function clearAndValidateDataSizes({
8488
}) {
8589
await blankSlate();
8690

87-
await addToDownloadList();
8891
await addToSiteUsage();
8992
let promiseSanitized = promiseSanitizationComplete();
9093

9194
await openPreferencesViaOpenPreferencesAPI("privacy", { leaveOpen: true });
9295

93-
let dh = new ClearHistoryDialogHelper();
96+
let dh = new ClearHistoryDialogHelper({ checkingDataSizes: true });
9497
dh.onload = async function () {
9598
await validateDataSizes(this);
9699
this.checkPrefCheckbox("cache", clearCache);
@@ -105,7 +108,7 @@ async function clearAndValidateDataSizes({
105108
dh.open();
106109
await dh.promiseClosed;
107110

108-
let dh2 = new ClearHistoryDialogHelper();
111+
let dh2 = new ClearHistoryDialogHelper({ checkingDataSizes: true });
109112
// Check if the newly cleared values are reflected
110113
dh2.onload = async function () {
111114
await validateDataSizes(this);
@@ -180,3 +183,128 @@ add_task(async function test_all_data_sizes() {
180183
timespan: Sanitizer.TIMESPAN_EVERYTHING,
181184
});
182185
});
186+
187+
// This test makes sure that the user can change their timerange option
188+
// even if the data sizes are not loaded yet.
189+
add_task(async function testUIWithDataSizesLoading() {
190+
await blankSlate();
191+
await addToSiteUsage();
192+
193+
let origGetQuotaUsageForTimeRanges =
194+
SiteDataManager.getQuotaUsageForTimeRanges.bind(SiteDataManager);
195+
let resolveStubFn;
196+
let resolverAssigned = false;
197+
198+
let dh = new ClearHistoryDialogHelper();
199+
// Create a sandbox for isolated stubbing within the test
200+
let sandbox = sinon.createSandbox();
201+
sandbox
202+
.stub(SiteDataManager, "getQuotaUsageForTimeRanges")
203+
.callsFake(async (...args) => {
204+
info("stub called");
205+
206+
let dataSizesReadyToLoadPromise = new Promise(resolve => {
207+
resolveStubFn = resolve;
208+
info("Sending message to notify dialog that the resolver is assigned");
209+
window.postMessage("resolver-assigned", "*");
210+
resolverAssigned = true;
211+
});
212+
await dataSizesReadyToLoadPromise;
213+
return origGetQuotaUsageForTimeRanges(...args);
214+
});
215+
dh.onload = async function () {
216+
// we add this event listener in the case where init finishes before the resolver is assigned
217+
if (!resolverAssigned) {
218+
await new Promise(resolve => {
219+
let listener = event => {
220+
if (event.data === "resolver-assigned") {
221+
window.removeEventListener("message", listener);
222+
// we are ready to test the dialog without any data sizes loaded
223+
resolve();
224+
}
225+
};
226+
window.addEventListener("message", listener);
227+
});
228+
}
229+
230+
ok(
231+
!this.win.gSanitizePromptDialog._dataSizesUpdated,
232+
"Data sizes should not have loaded yet"
233+
);
234+
this.selectDuration(Sanitizer.TIMESPAN_2HOURS);
235+
236+
info("triggering loading state end");
237+
resolveStubFn();
238+
239+
await this.win.gSanitizePromptDialog.dataSizesFinishedUpdatingPromise;
240+
241+
validateDataSizes(this);
242+
this.cancelDialog();
243+
};
244+
dh.open();
245+
await dh.promiseClosed;
246+
247+
// Restore the sandbox after the test is complete
248+
sandbox.restore();
249+
});
250+
251+
add_task(async function testClearingBeforeDataSizesLoad() {
252+
await blankSlate();
253+
await addToSiteUsage();
254+
255+
// add site data that we can verify if it gets cleared
256+
await createDummyDataForHost("example.org");
257+
await createDummyDataForHost("example.com");
258+
259+
ok(
260+
await SiteDataTestUtils.hasIndexedDB("https://example.org"),
261+
"We have indexedDB data for example.org"
262+
);
263+
ok(
264+
await SiteDataTestUtils.hasIndexedDB("https://example.com"),
265+
"We have indexedDB data for example.com"
266+
);
267+
268+
let dh = new ClearHistoryDialogHelper();
269+
let promiseSanitized = promiseSanitizationComplete();
270+
// Create a sandbox for isolated stubbing within the test
271+
let sandbox = sinon.createSandbox();
272+
sandbox
273+
.stub(SiteDataManager, "getQuotaUsageForTimeRanges")
274+
.callsFake(async () => {
275+
info("stub called");
276+
277+
info("This promise should never resolve");
278+
await new Promise(resolve => {});
279+
});
280+
dh.onload = async function () {
281+
// we don't need to initiate a event listener to wait for the resolver to be assigned for this
282+
// test since we do not want the data sizes to load
283+
ok(
284+
!this.win.gSanitizePromptDialog._dataSizesUpdated,
285+
"Data sizes should not be loaded yet"
286+
);
287+
this.selectDuration(Sanitizer.TIMESPAN_2HOURS);
288+
this.checkPrefCheckbox("cookiesAndStorage", true);
289+
this.acceptDialog();
290+
};
291+
dh.onunload = async () => {
292+
await promiseSanitized;
293+
};
294+
dh.open();
295+
await dh.promiseClosed;
296+
297+
// Data for example.org should be cleared
298+
ok(
299+
!(await SiteDataTestUtils.hasIndexedDB("https://example.org")),
300+
"We don't have indexedDB data for example.org"
301+
);
302+
// Data for example.com should be cleared
303+
ok(
304+
!(await SiteDataTestUtils.hasIndexedDB("https://example.com")),
305+
"We don't have indexedDB data for example.com"
306+
);
307+
308+
// Restore the sandbox after the test is complete
309+
sandbox.restore();
310+
});

browser/base/content/test/sanitize/head.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -501,21 +501,29 @@ function promiseSanitizationComplete() {
501501
* This wraps the dialog and provides some convenience methods for interacting
502502
* with it.
503503
*
504-
* @param browserWin (optional)
504+
* @param {Window} browserWin (optional)
505505
* The browser window that the dialog is expected to open in. If not
506506
* supplied, the initial browser window of the test run is used.
507-
* @param mode (optional)
508-
* One of
509-
* clear on shutdown settings context ("clearOnShutdown"),
510-
* clear site data settings context ("clearSiteData"),
511-
* clear history context ("clearHistory"),
512-
* browser context ("browser")
513-
* "browser" by default
507+
* @param {Object} {mode, checkingDataSizes}
508+
* mode: context to open the dialog in
509+
* One of
510+
* clear on shutdown settings context ("clearOnShutdown"),
511+
* clear site data settings context ("clearSiteData"),
512+
* clear history context ("clearHistory"),
513+
* browser context ("browser")
514+
* "browser" by default
515+
* checkingDataSizes: boolean check if we should wait for the data sizes
516+
* to load
517+
*
514518
*/
515-
function ClearHistoryDialogHelper(openContext = "browser") {
519+
function ClearHistoryDialogHelper({
520+
mode = "browser",
521+
checkingDataSizes = false,
522+
} = {}) {
516523
this._browserWin = window;
517524
this.win = null;
518-
this._mode = openContext;
525+
this._mode = mode;
526+
this._checkingDataSizes = checkingDataSizes;
519527
this.promiseClosed = new Promise(resolve => {
520528
this._resolveClosed = resolve;
521529
});
@@ -673,7 +681,11 @@ ClearHistoryDialogHelper.prototype = {
673681
() => {
674682
// Run onload on next tick so that gSanitizePromptDialog.init can run first.
675683
executeSoon(async () => {
676-
await this.win.gSanitizePromptDialog.dataSizesFinishedUpdatingPromise;
684+
if (this._checkingDataSizes) {
685+
// we wait for the data sizes to load to avoid async errors when validating sizes
686+
await this.win.gSanitizePromptDialog
687+
.dataSizesFinishedUpdatingPromise;
688+
}
677689
this.onload();
678690
});
679691
},

0 commit comments

Comments
 (0)