Skip to content

Commit

Permalink
Simplify RunWithUserGesture helper.
Browse files Browse the repository at this point in the history
It turns out that test_driver.bless already creates a button, so
there's no point in creating our own button as well. Next,
test_driver.bless already returns a promise, so we can just use that
instead of creating our own around it. Finally, all of the tests
unconditionally await the wrapped promise that we return, so we should
just take advantage of promise auto-flattening instead.

Change-Id: Ib6f36ac6bbecddf247282f31c10eae4af07d8f1e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4017034
Reviewed-by: Johann Hofmann <[email protected]>
Commit-Queue: Johann Hofmann <[email protected]>
Auto-Submit: Chris Fredrickson <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1069808}
  • Loading branch information
cfredric authored and pull[bot] committed Aug 18, 2023
1 parent 75f25f1 commit 1031667
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 41 deletions.
33 changes: 2 additions & 31 deletions storage-access-api/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,35 +58,6 @@ function RunRequestStorageAccessViaDomParser() {
return doc.requestStorageAccess();
}

async function RunCallbackWithGesture(buttonId, callback) {
// Append some formatting and information so non WebDriver instances can complete this test too.
const info = document.createElement('p');
info.innerText = "This test case requires user-interaction and TestDriver. If you're running it manually please click the 'Request Access' button below exactly once.";
document.body.appendChild(info);

const button = document.createElement('button');
button.innerText = "Request Access";
button.id = buttonId;
button.style = "background-color:#FF0000;"

// Insert the button and use test driver to click the button with a gesture.
document.body.appendChild(button);

const promise = new Promise((resolve, reject) => {
const wrappedCallback = () => {
callback().then(resolve, reject);
};

// In automated tests, we call the callback via test_driver.
test_driver.bless('run callback with user interaction', wrappedCallback);

// But we still allow the button to trigger the callback, for use on
// https://wpt.live.
button.addEventListener('click', e => {
wrappedCallback();
button.style = "background-color:#00FF00;"
}, {once: true});
});

return {promise};
function RunCallbackWithGesture(callback) {
return test_driver.bless('run callback with user gesture', callback);
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,10 @@ if (topLevelDocument) {
.then(() => {
promise_test(
async t => {
const {promise} = await RunCallbackWithGesture('b1', () => {
await RunCallbackWithGesture(() => {
return promise_rejects_dom(t, "NotAllowedError", document.requestStorageAccess(),
"should reject in insecure context");
});
return promise;
},
'[' + testPrefix +
'] document.requestStorageAccess() should be rejected when called with a user gesture in insecure context');
Expand Down
11 changes: 3 additions & 8 deletions storage-access-api/requestStorageAccess.sub.https.window.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ promise_test(
await test_driver.set_permission(
{name: 'storage-access'}, 'granted');

const {promise} = await RunCallbackWithGesture('b1', () => document.requestStorageAccess());
await promise;
await RunCallbackWithGesture(() => document.requestStorageAccess());

// Cleanup
await test_driver.set_permission(
Expand All @@ -40,12 +39,10 @@ if (testPrefix == 'cross-origin-frame' || testPrefix == 'nested-cross-origin-fra
await test_driver.set_permission(
{name: 'storage-access'}, 'prompt');

const {promise} = RunCallbackWithGesture('b2', () => {
await RunCallbackWithGesture(() => {
return promise_rejects_dom(t, "NotAllowedError", document.requestStorageAccess(),
"document.requestStorageAccess() call without permission");
});

await promise;
},
'[' + testPrefix +
'] document.requestStorageAccess() should be rejected with a NotAllowedError without permission grant');
Expand All @@ -55,13 +52,11 @@ if (testPrefix == 'cross-origin-frame' || testPrefix == 'nested-cross-origin-fra
await test_driver.set_permission(
{name: 'storage-access'}, 'denied');

const {promise} = RunCallbackWithGesture('b3', () => {
await RunCallbackWithGesture(() => {
return promise_rejects_dom(t, "NotAllowedError", document.requestStorageAccess(),
"document.requestStorageAccess() call without permission");
});

await promise;

// Cleanup
await test_driver.set_permission(
{name: 'storage-access'}, 'prompt');
Expand Down

0 comments on commit 1031667

Please sign in to comment.