diff --git a/storage-access-api/helpers.js b/storage-access-api/helpers.js index 01a9d9f0ce6fe6..728b098087e4ab 100644 --- a/storage-access-api/helpers.js +++ b/storage-access-api/helpers.js @@ -57,18 +57,18 @@ function RunTestsInNestedIFrame(sourceURL) { }, true); } -function RunRequestStorageAccessInDetachedFrame() { +function CreateDetachedFrame() { const frame = document.createElement('iframe'); document.body.append(frame); const inner_doc = frame.contentDocument; frame.remove(); - return inner_doc.requestStorageAccess(); + return inner_doc; } -function RunRequestStorageAccessViaDomParser() { +function CreateDocumentViaDOMParser() { const parser = new DOMParser(); const doc = parser.parseFromString('', 'text/html'); - return doc.requestStorageAccess(); + return doc; } function RunCallbackWithGesture(callback) { diff --git a/storage-access-api/requestStorageAccess-insecure.sub.window.js b/storage-access-api/requestStorageAccess-insecure.sub.window.js index 34d275b5ae88b3..6131456b4ab9cc 100644 --- a/storage-access-api/requestStorageAccess-insecure.sub.window.js +++ b/storage-access-api/requestStorageAccess-insecure.sub.window.js @@ -30,14 +30,14 @@ if (topLevelDocument) { promise_test(t => { const description = "document.requestStorageAccess() call in a detached frame"; // Can't use `promise_rejects_dom` here, since the error comes from the wrong global. - return RunRequestStorageAccessInDetachedFrame() + return CreateDetachedFrame().requestStorageAccess() .then(t.unreached_func("Should have rejected: " + description), (e) => { assert_equals(e.name, 'InvalidStateError', description); }); }, "[non-fully-active] document.requestStorageAccess() should reject when run in a detached frame"); promise_test(t => { - return promise_rejects_dom(t, 'InvalidStateError', RunRequestStorageAccessViaDomParser(), + return promise_rejects_dom(t, 'InvalidStateError', CreateDocumentViaDOMParser().requestStorageAccess(), "document.requestStorageAccess() in a detached DOMParser result"); }, "[non-fully-active] document.requestStorageAccess() should reject when run in a detached DOMParser document"); diff --git a/storage-access-api/requestStorageAccess-non-fully-active.sub.https.window.js b/storage-access-api/requestStorageAccess-non-fully-active.sub.https.window.js index b3aa19c25c0f09..18f451975ad313 100644 --- a/storage-access-api/requestStorageAccess-non-fully-active.sub.https.window.js +++ b/storage-access-api/requestStorageAccess-non-fully-active.sub.https.window.js @@ -4,7 +4,7 @@ 'use strict'; promise_test(t => { - const promise = RunRequestStorageAccessInDetachedFrame(); + const promise = CreateDetachedFrame().requestStorageAccess(); const description = "document.requestStorageAccess() call in a detached frame"; // Can't use `promise_rejects_dom` here, since the error comes from the wrong global. return promise.then(t.unreached_func("Should have rejected: " + description), (e) => { @@ -13,6 +13,6 @@ promise_test(t => { }, "[non-fully-active] document.requestStorageAccess() should not resolve when run in a detached frame"); promise_test(t => { - return promise_rejects_dom(t, 'InvalidStateError', RunRequestStorageAccessViaDomParser(), + return promise_rejects_dom(t, 'InvalidStateError', CreateDocumentViaDOMParser().requestStorageAccess(), "document.requestStorageAccess() in a detached DOMParser result"); }, "[non-fully-active] document.requestStorageAccess() should not resolve when run in a detached DOMParser document"); diff --git a/top-level-storage-access-api/tentative/requestStorageAccessFor-insecure.sub.window.js b/top-level-storage-access-api/tentative/requestStorageAccessFor-insecure.sub.window.js index cd8e78f8f4bdcc..dc66cee6eb31f2 100644 --- a/top-level-storage-access-api/tentative/requestStorageAccessFor-insecure.sub.window.js +++ b/top-level-storage-access-api/tentative/requestStorageAccessFor-insecure.sub.window.js @@ -9,39 +9,9 @@ // Some tests are run at the top-level, and an iframe is added to validate API // behavior in that context. -// Prefix each test case with an indicator so we know what context they are run -// in if they are used in multiple iframes. -let testPrefix = 'insecure-context'; - -// Keep track of if we run these tests in a nested context, we don't want to -// recurse forever. -let topLevelDocument = true; - -// The query string allows derivation of test conditions, like whether the tests -// are running in a top-level context. -const queryParams = window.location.search.substring(1).split('&'); -queryParams.forEach((param) => { - if (param.toLowerCase() == 'rootdocument=false') { - topLevelDocument = false; - } else if (param.split('=')[0].toLowerCase() == 'testcase') { - testPrefix = param.split('=')[1]; - } -}); - -// TODO(crbug.com/1410556): when/if requestStorageAccessFor is standardized, -// we should consider upstreaming these helpers. -function RunRequestStorageAccessForInDetachedFrame(origin) { - const nestedFrame = document.createElement('iframe'); - document.body.append(nestedFrame); - const inner_doc = nestedFrame.contentDocument; - nestedFrame.remove(); - return inner_doc.requestStorageAccessFor(origin); -} - -function RunRequestStorageAccessForViaDomParser(origin) { - const parser = new DOMParser(); - const doc = parser.parseFromString('', 'text/html'); - return doc.requestStorageAccessFor(origin); +const {testPrefix, topLevelDocument} = processQueryParams(); +if (!topLevelDocument) { + test_driver.set_test_context(window.top); } // Common tests to run in all frames. @@ -66,7 +36,7 @@ if (topLevelDocument) { const description = 'document.requestStorageAccessFor() call in a detached frame'; // Can't use promise_rejects_dom here because the exception is from the wrong global. - return RunRequestStorageAccessForInDetachedFrame('https://foo.com') + return CreateDetachedFrame().requestStorageAccessFor('https://foo.com') .then(t.unreached_func('Should have rejected: ' + description)) .catch((e) => { assert_equals(e.name, 'InvalidStateError', description); @@ -76,7 +46,7 @@ if (topLevelDocument) { promise_test(async t => { const description = 'document.requestStorageAccessFor() in a detached DOMParser result'; - return RunRequestStorageAccessForViaDomParser('https://foo.com') + return CreateDocumentViaDOMParser().requestStorageAccessFor('https://foo.com') .then(t.unreached_func('Should have rejected: ' + description)) .catch((e) => { assert_equals(e.name, 'InvalidStateError', description); @@ -86,8 +56,10 @@ if (topLevelDocument) { // Create a test with a single-child same-origin iframe. // This will validate that calls to requestStorageAccessFor are rejected // in non-top-level contexts. - RunTestsInIFrame( - './resources/requestStorageAccessFor-iframe.html?testCase=frame-on-insecure-page&rootdocument=false'); + promise_test(() => { + return RunTestsInIFrame( + './resources/requestStorageAccessFor-iframe.html?testCase=frame-on-insecure-page'); + }); promise_test( async t => { diff --git a/top-level-storage-access-api/tentative/requestStorageAccessFor.sub.https.window.js b/top-level-storage-access-api/tentative/requestStorageAccessFor.sub.https.window.js index 1d8f22bb982fd6..03d9c2822235d0 100644 --- a/top-level-storage-access-api/tentative/requestStorageAccessFor.sub.https.window.js +++ b/top-level-storage-access-api/tentative/requestStorageAccessFor.sub.https.window.js @@ -9,42 +9,12 @@ // Some tests are run at the top-level, and an iframe is added to validate API // behavior in that context. -// Prefix each test case with an indicator so we know what context they are run -// in if they are used in multiple iframes. -let testPrefix = 'top-level-context'; - -// Keep track of if we run these tests in a nested context, we don't want to -// recurse forever. -let topLevelDocument = true; - -// The query string allows derivation of test conditions, like whether the tests -// are running in a top-level context. -const queryParams = window.location.search.substring(1).split('&'); -queryParams.forEach((param) => { - if (param.toLowerCase() == 'rootdocument=false') { - topLevelDocument = false; - } else if (param.split('=')[0].toLowerCase() == 'testcase') { - testPrefix = param.split('=')[1]; - } -}); - -const requestedOrigin = 'https://foo.com'; - -// TODO(crbug.com/1351540): when/if requestStorageAccessFor is standardized, -// upstream with the Storage Access API helpers file. -function RunRequestStorageAccessForInDetachedFrame(origin) { - const nestedFrame = document.createElement('iframe'); - document.body.append(nestedFrame); - const inner_doc = nestedFrame.contentDocument; - nestedFrame.remove(); - return inner_doc.requestStorageAccessFor(origin); +const {testPrefix, topLevelDocument} = processQueryParams(); +if (!topLevelDocument) { + test_driver.set_test_context(window.top); } -function RunRequestStorageAccessForViaDomParser(origin) { - const parser = new DOMParser(); - const doc = parser.parseFromString('', 'text/html'); - return doc.requestStorageAccessFor(origin); -} +const requestedOrigin = 'https://foo.com'; // Common tests to run in all frames. test( @@ -84,7 +54,7 @@ if (topLevelDocument) { const description = 'document.requestStorageAccessFor() call in a detached frame'; // Can't use promise_rejects_dom here because the exception is from the wrong global. - return RunRequestStorageAccessForInDetachedFrame(requestedOrigin) + return CreateDetachedFrame().requestStorageAccessFor(requestedOrigin) .then(t.unreached_func('Should have rejected: ' + description)) .catch((e) => { assert_equals(e.name, 'InvalidStateError', description); @@ -94,7 +64,7 @@ if (topLevelDocument) { promise_test(async t => { const description = 'document.requestStorageAccessFor() in a detached DOMParser result'; - return RunRequestStorageAccessForViaDomParser(requestedOrigin) + return CreateDocumentViaDOMParser().requestStorageAccessFor(requestedOrigin) .then(t.unreached_func('Should have rejected: ' + description)) .catch((e) => { assert_equals(e.name, 'InvalidStateError', description); @@ -125,8 +95,8 @@ if (topLevelDocument) { 'granted'); await RunCallbackWithGesture(() => { - document.requestStorageAccessFor(altOrigin).then(() => { - RunTestsInIFrame( + return document.requestStorageAccessFor(altOrigin).then(() => { + return RunTestsInIFrame( 'https://{{hosts[alt][www]}}:{{ports[https][0]}}/top-level-storage-access-api/tentative/resources/requestStorageAccess-integration-iframe.https.html'); }); }); @@ -134,11 +104,13 @@ if (topLevelDocument) { '[' + testPrefix + '] document.requestStorageAccess() should be resolved without a user gesture after a successful requestStorageAccessFor() call'); + promise_test(() => { // Create a test with a single-child same-origin iframe. // This will validate that calls to requestStorageAccessFor are rejected // in non-top-level contexts. - RunTestsInIFrame( - './resources/requestStorageAccessFor-iframe.https.html?testCase=same-origin-frame&rootdocument=false'); + return RunTestsInIFrame( + './resources/requestStorageAccessFor-iframe.https.html?testCase=same-origin-frame'); + }); promise_test( async t => {