From 2877178ce9077649fac88243ecc078714191c267 Mon Sep 17 00:00:00 2001 From: Jan Varga Date: Mon, 16 Sep 2024 22:17:05 +0000 Subject: [PATCH] Bug 1917462 - IDB: Create a new module for testing; r=dom-storage-reviewers,jari Differential Revision: https://phabricator.services.mozilla.com/D219628 --- dom/indexedDB/moz.build | 2 +- .../modules/system/IndexedDBUtils.sys.mjs | 42 +++++++++++++++++++ dom/indexedDB/test/moz.build | 11 +++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 dom/indexedDB/test/modules/system/IndexedDBUtils.sys.mjs create mode 100644 dom/indexedDB/test/moz.build diff --git a/dom/indexedDB/moz.build b/dom/indexedDB/moz.build index 27e59029e54f..a102f0254fda 100644 --- a/dom/indexedDB/moz.build +++ b/dom/indexedDB/moz.build @@ -24,7 +24,7 @@ XPCSHELL_TESTS_MANIFESTS += [ "test/unit/xpcshell-parent-process.toml", ] -TEST_DIRS += ["test/gtest"] +TEST_DIRS += ["test"] XPIDL_SOURCES += [ "nsIIndexedDatabaseManager.idl", diff --git a/dom/indexedDB/test/modules/system/IndexedDBUtils.sys.mjs b/dom/indexedDB/test/modules/system/IndexedDBUtils.sys.mjs new file mode 100644 index 000000000000..3fd1c76a63fa --- /dev/null +++ b/dom/indexedDB/test/modules/system/IndexedDBUtils.sys.mjs @@ -0,0 +1,42 @@ +/** + * Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +export const IndexedDBUtils = { + /** + * Handles the completion of a request, awaiting either the `onsuccess` or + * `onerror` event before proceeding. + * + * This function is designed to handle requests of the types: + * - `IDBRequest` + * - `IDBOpenDBRequest` + * + * These requests are typically returned by IndexedDB API. + * + * @param {IDBRequest|IDBOpenDBRequest} request + * The request object, which must have `onsuccess` and `onerror` event + * handlers, as well as result and error properties. + * @returns {Promise} + * Resolves with the request's result when the operation is successful. + * @throws {Error} + * If the request encounters an error, this function throws the request's + * `error` property. + */ + async requestFinished(request) { + await new Promise(function (resolve) { + request.onerror = function () { + resolve(); + }; + request.onsuccess = function () { + resolve(); + }; + }); + + if (request.error) { + throw request.error; + } + + return request.result; + }, +}; diff --git a/dom/indexedDB/test/moz.build b/dom/indexedDB/test/moz.build new file mode 100644 index 000000000000..56be27cddc37 --- /dev/null +++ b/dom/indexedDB/test/moz.build @@ -0,0 +1,11 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +TEST_DIRS += ["gtest"] + +TESTING_JS_MODULES.dom.indexedDB.test.modules += [ + "modules/system/IndexedDBUtils.sys.mjs", +]