Attempt de-flaking of requestStorageAccess WPT tests

Analysis of the `nested-same-origin-frame` test indicated it sometimes
reported having user activation, and a call to requestStorageAccess()
would then pass when it was expected to fail. The belief is that this
is an artifact of interleaving of iframe loads with the simulated
click on the page, so we instead wait for the iframe tests to complete
before simulating activation.

Bug: 1054577,1229084
Change-Id: I3f9294bbdafc92ff95e20f82e60d09f27f5e7a45
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3846044
Reviewed-by: Brandon Maslen <brandm@microsoft.com>
Commit-Queue: Matt Reichhoff <mreichhoff@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1038771}
diff --git a/storage-access-api/helpers.js b/storage-access-api/helpers.js
index c8c4064..9bc2451 100644
--- a/storage-access-api/helpers.js
+++ b/storage-access-api/helpers.js
@@ -3,13 +3,22 @@
 function RunTestsInIFrame(sourceURL) {
   let frame = document.createElement('iframe');
   frame.src = sourceURL;
+  let result = new Promise((resolve, reject) => {
+    frame.onload = resolve;
+    frame.onerror = reject;
+  });
   document.body.appendChild(frame);
   fetch_tests_from_window(frame.contentWindow);
+  return result;
 }
 
 function RunTestsInNestedIFrame(sourceURL) {
   let nestedFrame = document.createElement('iframe');
   document.body.appendChild(nestedFrame);
+  let result = new Promise((resolve, reject) => {
+    nestedFrame.onload = resolve;
+    nestedFrame.onerror = reject;
+  });
   let content = `
     <script src="/resources/testharness.js"></script>
     <script src="helpers.js"></script>
@@ -22,6 +31,7 @@
     nestedFrame.contentDocument.write(content);
     nestedFrame.contentDocument.close();
     fetch_tests_from_window(nestedFrame.contentWindow);
+    return result;
 }
 
 let g_clickID = 0;
diff --git a/storage-access-api/requestStorageAccess.sub.window.js b/storage-access-api/requestStorageAccess.sub.window.js
index 23d190f..1366a1d 100644
--- a/storage-access-api/requestStorageAccess.sub.window.js
+++ b/storage-access-api/requestStorageAccess.sub.window.js
@@ -42,29 +42,48 @@
   // of various iFrames
 
   // Create a test with a single-child same-origin iframe.
-  RunTestsInIFrame("resources/requestStorageAccess-iframe.html?testCase=same-origin-frame&rootdocument=false");
+  let sameOriginFramePromise = RunTestsInIFrame(
+      'resources/requestStorageAccess-iframe.html?testCase=same-origin-frame&rootdocument=false');
 
   // Create a test with a single-child cross-origin iframe.
-  RunTestsInIFrame("http://{{domains[www]}}:{{ports[http][0]}}/storage-access-api/resources/requestStorageAccess-iframe.html?testCase=cross-origin-frame&rootdocument=false");
+  let 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.
-  RunTestsInNestedIFrame("resources/requestStorageAccess-iframe.html?testCase=nested-same-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(
+      '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.
-  RunTestsInNestedIFrame("http://{{domains[www]}}:{{ports[http][0]}}/storage-access-api/resources/requestStorageAccess-iframe.html?testCase=nested-cross-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(
+      'http://{{domains[www]}}:{{ports[http][0]}}/storage-access-api/resources/requestStorageAccess-iframe.html?testCase=nested-cross-origin-frame&rootdocument=false')
 
-  promise_test(async t => {
-    await test_driver.set_permission({ name: 'storage-access' }, 'granted');
+  // Because the iframe tests expect no user activation, and because they
+  // load asynchronously, we want to first run those tests before simulating
+  // clicks on the page.
+  Promise
+      .all([
+        sameOriginFramePromise,
+        crossOriginFramePromise,
+        nestedSameOriginFramePromise,
+        nestedCrossOriginFramePromise,
+      ])
+      .then(x => {
+        promise_test(
+            async t => {
+              await test_driver.set_permission(
+                  {name: 'storage-access'}, 'granted');
 
-    var access_promise;
-    let testMethod = function() {
-      access_promise = document.requestStorageAccess();
-    };
-    await ClickButtonWithGesture(testMethod);
+              var access_promise;
+              let testMethod = function() {
+                access_promise = document.requestStorageAccess();
+              };
+              await ClickButtonWithGesture(testMethod);
 
-    return access_promise;
-  }, "[" + testPrefix + "] document.requestStorageAccess() should be resolved when called properly with a user gesture");
-
+              return access_promise;
+            },
+            '[' + testPrefix +
+                '] document.requestStorageAccess() should be resolved when called properly with a user gesture');
+      });
 }