Bug 1811262 - [devtools] Add jest fixture for devtools/shared/indexed-db.js. r=jdescottes.

Differential Revision: https://phabricator.services.mozilla.com/D167350
This commit is contained in:
Nicolas Chevobbe 2023-01-25 08:11:26 +00:00
Родитель 0b3da2309e
Коммит 2495b179a0
4 изменённых файлов: 50 добавлений и 52 удалений

Просмотреть файл

@ -0,0 +1,15 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
"use strict";
const store = {};
module.exports = {
open: () => ({}),
getItem: async key => store[key],
setItem: async (key, value) => {
store[key] = value;
},
};

Просмотреть файл

@ -33,6 +33,7 @@ module.exports = {
// Sometimes returning an empty object is enough
"^resource://devtools/client/shared/link": `${fixturesDir}/empty-module`,
"^devtools/shared/flags": `${fixturesDir}/empty-module`,
"^resource://devtools/shared/indexed-db.js": `${fixturesDir}/indexed-db`,
"^devtools/shared/layout/utils": `${fixturesDir}/empty-module`,
"^devtools/client/shared/components/tree/TreeView": `${fixturesDir}/empty-module`,
// Map all require("devtools/...") to the real devtools root.

Просмотреть файл

@ -46,10 +46,6 @@ function setMocksInGlobal() {
global.loader = {
lazyGetter: (context, name, fn) => {
if (global.hasOwnProperty(name)) {
return;
}
Object.defineProperty(global, name, {
get() {
delete global[name];
@ -119,15 +115,6 @@ function setMocksInGlobal() {
};
}
const store = {};
global.indexedDB = {
open: () => ({}),
getItem: async key => store[key],
setItem: async (key, value) => {
store[key] = value;
},
};
if (typeof global.TextEncoder === "undefined") {
const { TextEncoder } = require("util");
global.TextEncoder = TextEncoder;

Просмотреть файл

@ -9,46 +9,41 @@
* a principal dedicated to DevTools.
*/
// When running in jest, we can't use Cu.Sandbox and only expose the native indexedDB object
if (globalThis.indexedDB) {
module.exports = globalThis.indexedDB;
} else {
const PSEUDOURI = "indexeddb://fx-devtools";
const principaluri = Services.io.newURI(PSEUDOURI);
const principal = Services.scriptSecurityManager.createContentPrincipal(
principaluri,
{}
);
const PSEUDOURI = "indexeddb://fx-devtools";
const principaluri = Services.io.newURI(PSEUDOURI);
const principal = Services.scriptSecurityManager.createContentPrincipal(
principaluri,
{}
);
// indexedDB is only exposed to document globals.
// We are retrieving an instance from a Sandbox, which has to be loaded
// from the system principal in order to avoid having wrappers around
// all indexed DB objects.
const systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal();
const sandbox = Cu.Sandbox(systemPrincipal, {
wantGlobalProperties: ["indexedDB"],
});
const { indexedDB } = sandbox;
// indexedDB is only exposed to document globals.
// We are retrieving an instance from a Sandbox, which has to be loaded
// from the system principal in order to avoid having wrappers around
// all indexed DB objects.
const systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal();
const sandbox = Cu.Sandbox(systemPrincipal, {
wantGlobalProperties: ["indexedDB"],
});
const { indexedDB } = sandbox;
module.exports = Object.freeze({
/**
* Only the standard version of indexedDB.open is supported.
*/
open(name, version) {
const options = {};
if (typeof version === "number") {
options.version = version;
}
return indexedDB.openForPrincipal(principal, name, options);
},
module.exports = Object.freeze({
/**
* Only the standard version of indexedDB.open is supported.
*/
open(name, version) {
const options = {};
if (typeof version === "number") {
options.version = version;
}
return indexedDB.openForPrincipal(principal, name, options);
},
/**
* Only the standard version of indexedDB.deleteDatabase is supported.
*/
deleteDatabase(name) {
return indexedDB.deleteForPrincipal(principal, name);
},
/**
* Only the standard version of indexedDB.deleteDatabase is supported.
*/
deleteDatabase(name) {
return indexedDB.deleteForPrincipal(principal, name);
},
cmp: indexedDB.cmp.bind(indexedDB),
});
}
cmp: indexedDB.cmp.bind(indexedDB),
});