diff --git a/dom/quota/test/xpcshell/test_listCachedOrigins.js b/dom/quota/test/xpcshell/test_listCachedOrigins.js new file mode 100644 index 000000000000..4deb33c99040 --- /dev/null +++ b/dom/quota/test/xpcshell/test_listCachedOrigins.js @@ -0,0 +1,113 @@ +/** + * Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +const { PrincipalUtils } = ChromeUtils.importESModule( + "resource://testing-common/dom/quota/test/modules/PrincipalUtils.sys.mjs" +); +const { QuotaUtils } = ChromeUtils.importESModule( + "resource://testing-common/dom/quota/test/modules/QuotaUtils.sys.mjs" +); + +async function testSteps() { + const origins = [ + "https://example.com", + "https://localhost", + "https://www.mozilla.org", + ]; + + function verifyResult(result, expectedOrigins) { + ok(Array.isArray(result), "Got an array object"); + Assert.equal( + result.length, + expectedOrigins.length, + "Correct number of elements" + ); + + info("Sorting elements"); + + result.sort(function (a, b) { + if (a < b) { + return -1; + } + if (a > b) { + return 1; + } + return 0; + }); + + info("Verifying elements"); + + for (let i = 0; i < result.length; i++) { + Assert.equal( + result[i], + expectedOrigins[i], + "Result matches expected origin" + ); + } + } + + info("Clearing"); + + { + const request = Services.qms.clear(); + await QuotaUtils.requestFinished(request); + } + + info("Listing cached origins"); + + const originsBeforeInit = await (async function () { + const request = Services.qms.listCachedOrigins(); + const result = await QuotaUtils.requestFinished(request); + return result; + })(); + + info("Verifying result"); + + verifyResult(originsBeforeInit, []); + + info("Clearing"); + + { + const request = Services.qms.clear(); + await QuotaUtils.requestFinished(request); + } + + info("Initializing"); + + { + const request = Services.qms.init(); + await QuotaUtils.requestFinished(request); + } + + info("Initializing temporary storage"); + + { + const request = Services.qms.initTemporaryStorage(); + await QuotaUtils.requestFinished(request); + } + + info("Initializing origins"); + + for (const origin of origins) { + const request = Services.qms.initializeTemporaryOrigin( + "default", + PrincipalUtils.createPrincipal(origin), + /* aCreateIfNonExistent */ true + ); + await QuotaUtils.requestFinished(request); + } + + info("Listing cached origins"); + + const originsAfterInit = await (async function () { + const request = Services.qms.listCachedOrigins(); + const result = await QuotaUtils.requestFinished(request); + return result; + })(); + + info("Verifying result"); + + verifyResult(originsAfterInit, origins); +} diff --git a/dom/quota/test/xpcshell/xpcshell.toml b/dom/quota/test/xpcshell/xpcshell.toml index 92f0ca4d6d0e..ab4c27dd542a 100644 --- a/dom/quota/test/xpcshell/xpcshell.toml +++ b/dom/quota/test/xpcshell/xpcshell.toml @@ -89,6 +89,8 @@ skip-if = ["condprof"] # frequent perma fail, then goes away. ["test_initializeTemporaryOrigin.js"] +["test_listCachedOrigins.js"] + ["test_listOrigins.js"] ["test_originEndsWithDot.js"]