[WPT] Test forcible worker termination (modules/dynamic imports)

This CL adds tests for forcible worker termination during:

- Module top-level script evaluation,
- Evaluation of dynamic import()ed script evaluation
  (from module/classic workers),

The failure of
"Worker is terminated during top-level script evaluation (module)"
test on Chromium is tracked by crbug.com/1129793.

Bug: 1111134, 1129785, 1129793
Change-Id: I7babf738848a2de51a62679e080a389519fd5884
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2418751
Reviewed-by: Makoto Shimazu <shimazu@chromium.org>
Commit-Queue: Hiroshige Hayashizaki <hiroshige@chromium.org>
Cr-Commit-Position: refs/heads/master@{#810096}
diff --git a/third_party/blink/web_tests/external/wpt/workers/Worker-terminate-forever-during-evaluation-expected.txt b/third_party/blink/web_tests/external/wpt/workers/Worker-terminate-forever-during-evaluation-expected.txt
new file mode 100644
index 0000000..80d722c4
--- /dev/null
+++ b/third_party/blink/web_tests/external/wpt/workers/Worker-terminate-forever-during-evaluation-expected.txt
@@ -0,0 +1,9 @@
+This is a testharness.js-based test.
+PASS Worker is terminated during top-level script evaluation
+FAIL Worker is terminated during top-level script evaluation (module) assert_unreached: onerror Reached unreachable code
+PASS Worker is terminated during importScripts() call
+PASS Worker is terminated during nested importScripts() call
+PASS Worker is terminated during dynamic import()
+PASS Worker is terminated during dynamic import() (module)
+Harness: the test ran to completion.
+
diff --git a/third_party/blink/web_tests/external/wpt/workers/Worker-terminate-forever-during-evaluation.html b/third_party/blink/web_tests/external/wpt/workers/Worker-terminate-forever-during-evaluation.html
index a45a4cd..4513c5b 100644
--- a/third_party/blink/web_tests/external/wpt/workers/Worker-terminate-forever-during-evaluation.html
+++ b/third_party/blink/web_tests/external/wpt/workers/Worker-terminate-forever-during-evaluation.html
@@ -5,32 +5,60 @@
 <script>
 // The tests here are to provide execution coverage for the code paths for
 // forcible worker termination.
-// Currently, no specific expectation are set here, i.e. expectation here is
-// not to crush, not to cause assertion failures, etc.
+// Expectation here is:
+// - Not to crash, not to cause assertion failures
+// - No WorkerGlobalScope error events and thus Worker error events are fired
+//   (#report-the-exception is not called when a script is terminated during
+//   #run-a-classic-script/#run-a-module-script according to the spec)
+// - Nothing after infinte loop, promise resolusion/rejection etc. occurs.
 
-async_test((t) => {
-  const worker = new Worker('support/Worker-run-forever.js');
-  worker.onmessage = () => {
-    worker.terminate();
-    // To make the worker forcibly terminated, because in Chromium worker is
-    // forcibly terminated after 2 seconds.
-    t.step_timeout(function() { t.done(); }, 4000);
-  };
-}, 'Worker is terminated during top-level script evaluation');
-
-async_test((t) => {
-  const worker = new Worker('support/Worker-run-forever-during-importScripts.js');
-  worker.onmessage = () => {
-    worker.terminate();
-    t.step_timeout(function() { t.done(); }, 4000);
-  };
-}, 'Worker is terminated during importScripts() call');
-
-async_test((t) => {
-  const worker = new Worker('support/Worker-run-forever-during-nested-importScripts.js');
-  worker.onmessage = () => {
-    worker.terminate();
-    t.step_timeout(function() { t.done(); }, 4000);
-  };
-}, 'Worker is terminated during nested importScripts() call');
+for (const test of [
+  {
+    url: 'support/Worker-run-forever.js',
+    description: 'Worker is terminated during top-level script evaluation'
+  },
+  {
+    url: 'support/Worker-run-forever.js',
+    options: {type: 'module'},
+    description: 'Worker is terminated during top-level script evaluation (module)'
+  },
+  {
+    url: 'support/Worker-run-forever-during-importScripts.js',
+    description: 'Worker is terminated during importScripts() call'
+  },
+  {
+    url: 'support/Worker-run-forever-during-nested-importScripts.js',
+    description: 'Worker is terminated during nested importScripts() call'
+  },
+  {
+    url: 'support/Worker-run-forever-during-dynamic-import.js',
+    description: 'Worker is terminated during dynamic import()'
+  },
+  {
+    url: 'support/Worker-run-forever-during-dynamic-import.js',
+    options: {type: 'module'},
+    description: 'Worker is terminated during dynamic import() (module)'
+  }
+]) {
+  async_test((t) => {
+    const worker = new Worker(test.url, test.options);
+    worker.onerror = t.step_func(e => {
+      // Calls preventDefault() to prevent this event from being considered
+      // as an uncaught exception.
+      e.preventDefault();
+      assert_unreached('onerror');
+    });
+    worker.onmessage = t.step_func((e) => {
+      if (e.data === 'start') {
+        worker.terminate();
+        // To make the worker forcibly terminated, because in Chromium worker is
+        // forcibly terminated after 2 seconds.
+        t.step_timeout(function() { t.done(); }, 4000);
+      }
+      else {
+        assert_unreached('Unexpected message received: ' + e.data);
+      }
+    });
+  }, test.description);
+}
 </script>
diff --git a/third_party/blink/web_tests/external/wpt/workers/support/Worker-run-forever-during-dynamic-import.js b/third_party/blink/web_tests/external/wpt/workers/support/Worker-run-forever-during-dynamic-import.js
new file mode 100644
index 0000000..62840bd
--- /dev/null
+++ b/third_party/blink/web_tests/external/wpt/workers/support/Worker-run-forever-during-dynamic-import.js
@@ -0,0 +1,3 @@
+import('./Worker-run-forever.js')
+  .then(r => postMessage('resolved: ' + r))
+  .catch(e => postMessage('rejected: ' + e));
diff --git a/third_party/blink/web_tests/external/wpt/workers/support/Worker-run-forever-during-importScripts.js b/third_party/blink/web_tests/external/wpt/workers/support/Worker-run-forever-during-importScripts.js
index 3a0fb4b..6a3083f 100644
--- a/third_party/blink/web_tests/external/wpt/workers/support/Worker-run-forever-during-importScripts.js
+++ b/third_party/blink/web_tests/external/wpt/workers/support/Worker-run-forever-during-importScripts.js
@@ -1 +1,4 @@
 importScripts('Worker-run-forever.js');
+
+// This is not expected to run.
+postMessage('after importScripts()');
diff --git a/third_party/blink/web_tests/external/wpt/workers/support/Worker-run-forever.js b/third_party/blink/web_tests/external/wpt/workers/support/Worker-run-forever.js
index 679f908..2d822d4 100644
--- a/third_party/blink/web_tests/external/wpt/workers/support/Worker-run-forever.js
+++ b/third_party/blink/web_tests/external/wpt/workers/support/Worker-run-forever.js
@@ -1,2 +1,3 @@
 postMessage('start');
+onerror = () => postMessage('onerror');
 while(1);