Clean up some requestStorageAccess web platform tests.

#CLEANUP

Change-Id: I03b226e43c32e2d289ca9a4f69f95ddf54e12cf3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3892892
Commit-Queue: Chris Fredrickson <cfredric@chromium.org>
Reviewed-by: Matt Reichhoff <mreichhoff@chromium.org>
Auto-Submit: Chris Fredrickson <cfredric@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1046098}
diff --git a/storage-access-api/requestStorageAccess.sub.window.js b/storage-access-api/requestStorageAccess.sub.window.js
index 432ae1a..308ad43 100644
--- a/storage-access-api/requestStorageAccess.sub.window.js
+++ b/storage-access-api/requestStorageAccess.sub.window.js
@@ -3,24 +3,29 @@
 // META: script=/resources/testdriver-vendor.js
 'use strict';
 
-// 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;
-
 // Check if we were called with a query string of allowed=false. This would
 // indicate we expect the access to be denied.
-let queryParams = window.location.search.substring(1).split("&");
-queryParams.forEach(function (param, index) {
-  if (param.toLowerCase() == "rootdocument=false") {
-    topLevelDocument = false;
-  } else if (param.split("=")[0].toLowerCase() == "testcase") {
-    testPrefix = param.split("=")[1];
+function processQueryParams() {
+  let testPrefix = "top-level-context";
+  let topLevelDocument = true;
+  for (const param of window.location.search.substring(1).split("&")) {
+    if (param.toLowerCase() === "rootdocument=false") {
+      topLevelDocument = false;
+    } else if (param.split("=")[0].toLowerCase() === "testcase") {
+      testPrefix = param.split("=")[1];
+    }
   }
-});
+  return {testPrefix, topLevelDocument};
+}
+
+// Document-level test config flags:
+//
+// testPrefix: Prefix each test case with an indicator so we know what context
+// they are run in if they are used in multiple iframes.
+//
+// topLevelDocument: Keep track of if we run these tests in a nested context, we
+// don't want to recurse forever.
+const {testPrefix, topLevelDocument} = processQueryParams();
 
 // Common tests to run in all frames.
 test(() => {
@@ -28,11 +33,9 @@
 }, "[" + testPrefix + "] document.requestStorageAccess() should be supported on the document interface");
 
 promise_test(t => {
-  let promise = document.requestStorageAccess();
-  let description = "document.requestStorageAccess() call without user gesture";
-  return promise.then(t.unreached_func("Should have rejected: " + description)).catch(function(e) {
-    assert_equals(undefined, e, description);
-  });
+  return promise_rejects_exactly(
+    t, undefined, document.requestStorageAccess(),
+    "document.requestStorageAccess() call without user gesture");
 }, "[" + testPrefix + "] document.requestStorageAccess() should be rejected by default with no user gesture");
 
 // Logic to load test cases within combinations of iFrames.
@@ -40,39 +43,36 @@
   // This specific test will run only as a top level test (not as a worker).
   // Specific requestStorageAccess() scenarios will be tested within the context
   // of various iFrames
-  promise_test(async t => {
-    let promise = RunRequestStorageAccessInDetachedFrame();
-    let description = "document.requestStorageAccess() call in a detached frame";
-    return promise.then(t.unreached_func("Should have rejected: " + description)).catch(function (e) {
-
+  promise_test(t => {
+    const promise = RunRequestStorageAccessInDetachedFrame();
+    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) => {
       assert_equals(e.name, 'SecurityError', description);
     });
   }, "[non-fully-active] document.requestStorageAccess() should not resolve when run in a detached frame");
 
-  promise_test(async t => {
-    let promise = RunRequestStorageAccessViaDomParser();
-    let description = "document.requestStorageAccess() in a detached DOMParser result";
-    return promise.then(t.unreached_func("Should have rejected: " + description)).catch(function (e) {
-      assert_equals(e.name, 'SecurityError', description);
-    });
+  promise_test(t => {
+    return promise_rejects_dom(t, 'SecurityError', RunRequestStorageAccessViaDomParser(),
+     "document.requestStorageAccess() in a detached DOMParser result");
   }, "[non-fully-active] document.requestStorageAccess() should not resolve when run in a detached DOMParser document");
 
   // Create a test with a single-child same-origin iframe.
-  let sameOriginFramePromise = RunTestsInIFrame(
+  const sameOriginFramePromise = RunTestsInIFrame(
       'resources/requestStorageAccess-iframe.html?testCase=same-origin-frame&rootdocument=false');
 
   // Create a test with a single-child cross-origin iframe.
-  let crossOriginFramePromise = RunTestsInIFrame(
+  const crossOriginFramePromise = RunTestsInIFrame(
       'http://{{domains[www]}}:{{ports[http][0]}}/storage-access-api/resources/requestStorageAccess-iframe.html?testCase=cross-origin-frame&rootdocument=false');
 
   // Validate the nested-iframe scenario where the same-origin frame
   // containing the tests is not the first child.
-  let nestedSameOriginFramePromise = RunTestsInNestedIFrame(
+  const nestedSameOriginFramePromise = RunTestsInNestedIFrame(
       'resources/requestStorageAccess-iframe.html?testCase=nested-same-origin-frame&rootdocument=false');
 
   // Validate the nested-iframe scenario where the cross-origin frame
   // containing the tests is not the first child.
-  let nestedCrossOriginFramePromise = RunTestsInNestedIFrame(
+  const nestedCrossOriginFramePromise = RunTestsInNestedIFrame(
       'http://{{domains[www]}}:{{ports[http][0]}}/storage-access-api/resources/requestStorageAccess-iframe.html?testCase=nested-cross-origin-frame&rootdocument=false');
 
   // Because the iframe tests expect no user activation, and because they
@@ -85,19 +85,18 @@
         nestedSameOriginFramePromise,
         nestedCrossOriginFramePromise,
       ])
-      .then(x => {
+      .then(() => {
         promise_test(
-            async t => {
+            async () => {
               await test_driver.set_permission(
                   {name: 'storage-access'}, 'granted');
 
-              var access_promise;
-              let testMethod = function() {
+              let access_promise;
+              await ClickButtonWithGesture(() => {
                 access_promise = document.requestStorageAccess();
-              };
-              await ClickButtonWithGesture(testMethod);
+              });
 
-              return access_promise;
+              await access_promise;
             },
             '[' + testPrefix +
                 '] document.requestStorageAccess() should be resolved when called properly with a user gesture');