Skip to content

Commit

Permalink
Bug 1750933 - Add testing infrastructure for OPFS; r=dom-storage-revi…
Browse files Browse the repository at this point in the history
…ewers,jari

Differential Revision: https://phabricator.services.mozilla.com/D136317
  • Loading branch information
janvarga committed May 3, 2022
1 parent 3fb7637 commit 847a084
Show file tree
Hide file tree
Showing 29 changed files with 787 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,10 @@ module.exports = {
"dom/payments/**",
"dom/performance/**",
"dom/permission/**",
"dom/quota/**",
"dom/quota/test/browser/**",
"dom/quota/test/common/**",
"dom/quota/test/mochitest/**",
"dom/quota/test/xpcshell/**",
"dom/security/test/cors/**",
"dom/security/test/csp/**",
"dom/security/test/mixedcontentblocker/**",
Expand Down
2 changes: 2 additions & 0 deletions dom/fs/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# 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 += ["test"]

EXPORTS.mozilla.dom += [
"FileSystemDirectoryHandle.h",
"FileSystemDirectoryIterator.h",
Expand Down
13 changes: 13 additions & 0 deletions dom/fs/test/common/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* 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";

module.exports = {
globals: {
Assert: true,
exported_symbols: true,
require_module: true,
},
};
9 changes: 9 additions & 0 deletions dom/fs/test/common/nsresult.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/

const nsresult = {
NS_ERROR_NOT_IMPLEMENTED: Cr.NS_ERROR_NOT_IMPLEMENTED,
};
exported_symbols.nsresult = nsresult;
21 changes: 21 additions & 0 deletions dom/fs/test/common/test_basics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/

async function test1() {
const { nsresult } = await require_module("dom/fs/test/common/nsresult.js");

try {
await navigator.storage.getDirectory();

Assert.ok(false, "Should have thrown");
} catch (e) {
Assert.ok(true, "Should have thrown");
Assert.ok(
e.result === nsresult.NS_ERROR_NOT_IMPLEMENTED,
"Threw right result code"
);
}
}
exported_symbols.test1 = test1;
51 changes: 51 additions & 0 deletions dom/fs/test/mochitest/head.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/

async function require_module(id) {
if (!require_module.moduleLoader) {
const { ModuleLoader } = await import(
"/tests/dom/quota/test/modules/ModuleLoader.js"
);

const base = window.location.href;

const depth = "../../../../";

const { Assert } = await import("/tests/dom/quota/test/modules/Assert.js");

const proto = {
Assert,
Cr: SpecialPowers.Cr,
navigator,
};

require_module.moduleLoader = new ModuleLoader(base, depth, proto);
}

return require_module.moduleLoader.require(id);
}

async function run_test_in_worker(script) {
const { runTestInWorker } = await import(
"/tests/dom/quota/test/modules/WorkerDriver.js"
);
await runTestInWorker(script);
}

// XXX It would be nice if we could call add_setup here (xpcshell-test and
// browser-test support it.
add_task(async function() {
const { setStoragePrefs, clearStoragesForOrigin } = await import(
"/tests/dom/quota/test/modules/StorageUtils.js"
);

const optionalPrefsToSet = [["dom.fs.enabled", true]];

await setStoragePrefs(optionalPrefsToSet);

SimpleTest.registerCleanupFunction(async function() {
await clearStoragesForOrigin(SpecialPowers.wrap(document).nodePrincipal);
});
});
12 changes: 12 additions & 0 deletions dom/fs/test/mochitest/mochitest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 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/.

[DEFAULT]
support-files =
head.js

[test_basics.html]
scheme=https
[test_basics_worker.html]
scheme=https
24 changes: 24 additions & 0 deletions dom/fs/test/mochitest/test_basics.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<html>
<head>
<title>File System Test</title>

<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>

<script type="text/javascript" src="head.js"></script>
<script type="text/javascript">
add_task(async function test_1() {
const { test1 } = await require_module("dom/fs/test/common/test_basics.js");

await test1();
});
</script>
</head>

<body></body>

</html>
22 changes: 22 additions & 0 deletions dom/fs/test/mochitest/test_basics_worker.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<html>
<head>
<title>File System Test</title>

<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>

<script type="text/javascript" src="head.js"></script>
<script type="text/javascript">
add_task(async function worker() {
await run_test_in_worker("worker/test_basics_worker.js");
});
</script>
</head>

<body></body>

</html>
12 changes: 12 additions & 0 deletions dom/fs/test/mochitest/worker/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/

"use strict";

module.exports = {
env: {
worker: true,
},
};
20 changes: 20 additions & 0 deletions dom/fs/test/mochitest/worker/head.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/

async function require_module(id) {
if (!require_module.moduleLoader) {
importScripts("/tests/dom/quota/test/modules/worker/ModuleLoader.js");

const base = location.href;

const depth = "../../../../../";

importScripts("/tests/dom/quota/test/modules/worker/Assert.js");

require_module.moduleLoader = new globalThis.ModuleLoader(base, depth);
}

return require_module.moduleLoader.require(id);
}
10 changes: 10 additions & 0 deletions dom/fs/test/mochitest/worker/test_basics_worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/

add_task(async function test_1() {
const { test1 } = await require_module("dom/fs/test/common/test_basics.js");

await test1();
});
28 changes: 28 additions & 0 deletions dom/fs/test/moz.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- 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/.

MOCHITEST_MANIFESTS += [
"mochitest/mochitest.ini",
]

XPCSHELL_TESTS_MANIFESTS += [
"xpcshell/xpcshell.ini",
]

TEST_HARNESS_FILES.testing.mochitest.tests.dom.fs.test.common += [
"common/nsresult.js",
"common/test_basics.js",
]

TEST_HARNESS_FILES.testing.mochitest.tests.dom.fs.test.mochitest.worker += [
"mochitest/worker/head.js",
"mochitest/worker/test_basics_worker.js",
]

TEST_HARNESS_FILES.xpcshell.dom.fs.test.common += [
"common/nsresult.js",
"common/test_basics.js",
]
60 changes: 60 additions & 0 deletions dom/fs/test/xpcshell/head.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/

const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");

async function require_module(id) {
if (!require_module.moduleLoader) {
const { ModuleLoader } = ChromeUtils.import(
"resource://testing-common/dom/quota/test/modules/ModuleLoader.jsm"
);

const base = Services.io.newFileURI(do_get_file("")).spec;

const depth = "../../../../";

Cu.importGlobalProperties(["storage"]);

const proto = {
Assert,
Cr,
navigator: {
storage,
},
};

require_module.moduleLoader = new ModuleLoader(base, depth, proto);
}

return require_module.moduleLoader.require(id);
}

add_setup(async function() {
const {
setStoragePrefs,
clearStoragePrefs,
clearStoragesForOrigin,
} = ChromeUtils.import(
"resource://testing-common/dom/quota/test/modules/StorageUtils.jsm"
);

const optionalPrefsToSet = [["dom.fs.enabled", true]];

setStoragePrefs(optionalPrefsToSet);

registerCleanupFunction(async function() {
const principal = Cc["@mozilla.org/systemprincipal;1"].createInstance(
Ci.nsIPrincipal
);

await clearStoragesForOrigin(principal);

const optionalPrefsToClear = ["dom.fs.enabled"];

clearStoragePrefs(optionalPrefsToClear);
});

do_get_profile();
});
10 changes: 10 additions & 0 deletions dom/fs/test/xpcshell/test_basics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/

add_task(async function test_1() {
const { test1 } = await require_module("dom/fs/test/common/test_basics.js");

await test1();
});
8 changes: 8 additions & 0 deletions dom/fs/test/xpcshell/xpcshell.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# 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/.

[DEFAULT]
head = head.js

[test_basics.js]
9 changes: 9 additions & 0 deletions dom/localstorage/ActorsParent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8526,6 +8526,15 @@ nsresult QuotaClient::AboutToClearOrigins(
return NS_OK;
}

// There can be no data for the system principal in the archive or the shadow
// database. This early return silences potential warnings caused by failed
// `CreateAerchivedOriginScope` because it calls `GenerateOriginKey2` which
// doesn't support the system principal.
if (aOriginScope.IsOrigin() &&
aOriginScope.GetOrigin() == QuotaManager::GetOriginForChrome()) {
return NS_OK;
}

const bool shadowWrites = gShadowWrites;

QM_TRY_INSPECT(const auto& archivedOriginScope,
Expand Down
24 changes: 24 additions & 0 deletions dom/quota/test/modules/content/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/

"use strict";

module.exports = {
extends: ["plugin:mozilla/mochitest-test"],

overrides: [
{
files: [
"Assert.js",
"ModuleLoader.js",
"StorageUtils.js",
"WorkerDriver.js",
],
parserOptions: {
sourceType: "module",
},
},
],
};
11 changes: 11 additions & 0 deletions dom/quota/test/modules/content/Assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/

// Just a wrapper around SimpleTest related functions for now.
export const Assert = {
ok(value, message) {
ok(value, message);
},
};
Loading

0 comments on commit 847a084

Please sign in to comment.