summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/workers/examples
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /testing/web-platform/tests/workers/examples
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/workers/examples')
-rw-r--r--testing/web-platform/tests/workers/examples/fetch_tests_from_worker.html21
-rw-r--r--testing/web-platform/tests/workers/examples/fetch_tests_from_worker.js28
-rw-r--r--testing/web-platform/tests/workers/examples/general.any.js34
-rw-r--r--testing/web-platform/tests/workers/examples/general.worker.js35
-rw-r--r--testing/web-platform/tests/workers/examples/onconnect.any.js4
5 files changed, 122 insertions, 0 deletions
diff --git a/testing/web-platform/tests/workers/examples/fetch_tests_from_worker.html b/testing/web-platform/tests/workers/examples/fetch_tests_from_worker.html
new file mode 100644
index 0000000000..5ac765c7ee
--- /dev/null
+++ b/testing/web-platform/tests/workers/examples/fetch_tests_from_worker.html
@@ -0,0 +1,21 @@
+<!doctype html>
+<!--
+ This file is an example of a hand-written test using
+ fetch_tests_from_worker().
+ Unlike *.any.js or *.worker.js tests, fetch_tests_from_worker.html/js files
+ are manually written and no generated glue code are involved.
+-->
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script>
+fetch_tests_from_worker(new Worker("fetch_tests_from_worker.js"));
+
+// If you want to test on SharedWorker,
+// fetch_tests_from_worker(new SharedWorker("fetch_tests_from_worker.js"));
+
+// See ServiceWorkersHandler in
+// https://github.com/web-platform-tests/wpt/blob/master/tools/serve/serve.py
+// for the generated snippet used in .any.js for service workers.
+// Note: when testing service workers, also add ".https." file flag in the
+// main HTML's file name to run the test on HTTPS.
+</script>
diff --git a/testing/web-platform/tests/workers/examples/fetch_tests_from_worker.js b/testing/web-platform/tests/workers/examples/fetch_tests_from_worker.js
new file mode 100644
index 0000000000..01ba12a622
--- /dev/null
+++ b/testing/web-platform/tests/workers/examples/fetch_tests_from_worker.js
@@ -0,0 +1,28 @@
+// This file is an example of a hand-written test using
+// fetch_tests_from_worker().
+// Unlike *.any.js or *.worker.js tests, fetch_tests_from_worker.html/js files
+// are manually written and no generated glue code are involved.
+
+// fetch_tests_from_worker() requires testharness.js both on the parent
+// document and on the worker.
+importScripts("/resources/testharness.js");
+
+// ============================================================================
+
+// Test body.
+test(() => {
+ assert_equals(1, 1, "1 == 1");
+ },
+ "Test that should pass"
+);
+
+// ============================================================================
+
+// `done()` is always needed at the bottom for dedicated workers and shared
+// workers, even if you write `async_test()` or `promise_test()`.
+// `async_test()` and `promise_test()` called before this `done()`
+// will continue and assertions/failures after this `done()` are not ignored.
+// See
+// https://web-platform-tests.org/writing-tests/testharness-api.html#determining-when-all-tests-are-complete
+// for details.
+done();
diff --git a/testing/web-platform/tests/workers/examples/general.any.js b/testing/web-platform/tests/workers/examples/general.any.js
new file mode 100644
index 0000000000..cb5d61eafb
--- /dev/null
+++ b/testing/web-platform/tests/workers/examples/general.any.js
@@ -0,0 +1,34 @@
+// META: global=worker
+
+// See
+// https://web-platform-tests.org/writing-tests/testharness.html#multi-global-tests
+// for how to specify in which global scopes to run this tests,
+// how to specify additional scripts needed, etc.
+
+// testharness.js is imported (via importScripts()) by generated glue code by
+// WPT server.
+// See ClassicWorkerHandler in
+// https://github.com/web-platform-tests/wpt/blob/master/tools/serve/serve.py.
+
+// ============================================================================
+
+// Test body.
+// .any.js tests are always testharness.js-based.
+test(() => {
+ assert_equals(1, 1, "1 == 1");
+ },
+ "Test that should pass"
+);
+
+test(() => {
+ // This file is "general.any.js" but the worker top-level script is
+ // "general.any.worker.js", which is generated by the WPT server.
+ assert_equals(location.pathname, "/workers/examples/general.any.worker.js");
+ },
+ "Worker top-level script is a generated script."
+);
+
+// done() is NOT needed in .any.js tests, as it is called by generated
+// glue code by the WPT server.
+// See ClassicWorkerHandler in
+// https://github.com/web-platform-tests/wpt/blob/master/tools/serve/serve.py.
diff --git a/testing/web-platform/tests/workers/examples/general.worker.js b/testing/web-platform/tests/workers/examples/general.worker.js
new file mode 100644
index 0000000000..aeca236781
--- /dev/null
+++ b/testing/web-platform/tests/workers/examples/general.worker.js
@@ -0,0 +1,35 @@
+// This file is an example of a test using *.worker.js mechanism.
+// The parent document that calls fetch_tests_from_worker() is auto-generated
+// but there are no generated code in the worker side.
+
+// fetch_tests_from_worker() requires testharness.js both on the parent
+// document and on the worker.
+importScripts("/resources/testharness.js");
+
+// ============================================================================
+
+// Test body.
+test(() => {
+ assert_equals(1, 1, "1 == 1");
+ },
+ "Test that should pass"
+);
+
+test(() => {
+ // This file is "general.worker.js" and this file itself is the worker
+ // top-level script (which is different from the .any.js case).
+ assert_equals(location.pathname, "/workers/examples/general.worker.js");
+ },
+ "Worker top-level script is the .worker.js file itself."
+);
+
+// ============================================================================
+
+// `done()` is always needed at the bottom for dedicated workers and shared
+// workers, even if you write `async_test()` or `promise_test()`.
+// `async_test()` and `promise_test()` called before this `done()`
+// will continue and assertions/failures after this `done()` are not ignored.
+// See
+// https://web-platform-tests.org/writing-tests/testharness-api.html#determining-when-all-tests-are-complete
+// for details.
+done();
diff --git a/testing/web-platform/tests/workers/examples/onconnect.any.js b/testing/web-platform/tests/workers/examples/onconnect.any.js
new file mode 100644
index 0000000000..d43a992183
--- /dev/null
+++ b/testing/web-platform/tests/workers/examples/onconnect.any.js
@@ -0,0 +1,4 @@
+// META: global=sharedworker
+const t = async_test("onconnect is called");
+onconnect = t.step_func_done((event) => {
+});