summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/user-activation/resources/utils.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
commit43a97878ce14b72f0981164f87f2e35e14151312 (patch)
tree620249daf56c0258faa40cbdcf9cfba06de2a846 /testing/web-platform/tests/html/user-activation/resources/utils.js
parentInitial commit. (diff)
downloadfirefox-upstream.tar.xz
firefox-upstream.zip
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/html/user-activation/resources/utils.js')
-rw-r--r--testing/web-platform/tests/html/user-activation/resources/utils.js48
1 files changed, 48 insertions, 0 deletions
diff --git a/testing/web-platform/tests/html/user-activation/resources/utils.js b/testing/web-platform/tests/html/user-activation/resources/utils.js
new file mode 100644
index 0000000000..5d3302583f
--- /dev/null
+++ b/testing/web-platform/tests/html/user-activation/resources/utils.js
@@ -0,0 +1,48 @@
+function delayByFrames(f, num_frames) {
+ function recurse(depth) {
+ if (depth == 0)
+ f();
+ else
+ requestAnimationFrame(() => recurse(depth-1));
+ }
+ recurse(num_frames);
+}
+
+// Returns a Promise which is resolved with the event object when the event is
+// fired.
+function getEvent(eventType) {
+ return new Promise(resolve => {
+ document.body.addEventListener(eventType, e => resolve(e), {once: true});
+ });
+}
+
+
+// Returns a Promise which is resolved with a "true" iff transient activation
+// was available and successfully consumed.
+//
+// This function relies on Fullscreen API to check/consume user activation
+// state.
+async function consumeTransientActivation() {
+ try {
+ await document.body.requestFullscreen();
+ await document.exitFullscreen();
+ return true;
+ } catch(e) {
+ return false;
+ }
+}
+
+function receiveMessage(type) {
+ return new Promise((resolve) => {
+ window.addEventListener("message", function listener(event) {
+ if (typeof event.data !== "string") {
+ return;
+ }
+ const data = JSON.parse(event.data);
+ if (data.type === type) {
+ window.removeEventListener("message", listener);
+ resolve(data);
+ }
+ });
+ });
+}