test_open_and_databases.js (2578B)
1 /** 2 * Any copyright is dedicated to the Public Domain. 3 * http://creativecommons.org/publicdomain/zero/1.0/ 4 */ 5 6 /* exported testSteps */ 7 async function testSteps() { 8 const factory = (function () { 9 if (typeof Cc === "undefined") { 10 return indexedDB; 11 } 12 13 // In xpcshell tests (where `Cc` is defined), we avoid using the system 14 // principal because background services may create indexedDB databases 15 // concurrently under that principal. To prevent interference and ensure 16 // test isolation, we use a sandbox with a content principal instead. 17 18 const { PrincipalUtils } = ChromeUtils.importESModule( 19 "resource://testing-common/dom/quota/test/modules/PrincipalUtils.sys.mjs" 20 ); 21 22 const principal = PrincipalUtils.createPrincipal("https://example.com"); 23 24 const sandbox = new Cu.Sandbox(principal, { 25 wantGlobalProperties: ["indexedDB"], 26 }); 27 28 return Cu.evalInSandbox("indexedDB", sandbox); 29 })(); 30 31 const openInfos = [ 32 { name: "foo-a", version: 1 }, 33 { name: "foo-b", version: 1 }, 34 ]; 35 36 info("Creating databases"); 37 38 for (let index = 0; index < openInfos.length; index++) { 39 const openInfo = openInfos[index]; 40 41 const request = factory.open(openInfo.name, openInfo.version); 42 43 await expectingUpgrade(request); 44 45 const event = await expectingSuccess(request); 46 47 const database = event.target.result; 48 49 database.close(); 50 } 51 52 info("Getting databases"); 53 54 const databasesPromise = factory.databases(); 55 56 info("Opening databases"); 57 58 const openPromises = []; 59 60 for (let index = 0; index < openInfos.length; index++) { 61 const openInfo = openInfos[index]; 62 63 const request = factory.open(openInfo.name, openInfo.version); 64 65 const promise = expectingSuccess(request); 66 67 openPromises.push(promise); 68 } 69 70 info("Waiting for databases operation to complete"); 71 72 const databaseInfos = await databasesPromise; 73 74 info("Verifying databases"); 75 76 is( 77 databaseInfos.length, 78 openInfos.length, 79 "The result of databases() should contain one result per database" 80 ); 81 82 for (let index = 0; index < openInfos.length; index++) { 83 const openInfo = openInfos[index]; 84 85 ok( 86 databaseInfos.some(function (element) { 87 return ( 88 element.name === openInfo.name && element.version === openInfo.version 89 ); 90 }), 91 "The result of databases() should be a sequence of the correct names " + 92 "and versions of all databases for the origin" 93 ); 94 } 95 96 info("Waiting for open operations to complete"); 97 98 await Promise.all(openPromises); 99 }