Bug 1490543 [wpt PR 12959] - ServiceWorker: Implement to return registration data after evaluate module scripts, and add WPT tests for ServiceWorker module scripts, a=testonly
authorAsami Doi <asamidoi@google.com>
Fri, 05 Oct 2018 14:21:51 +0000
changeset 495815 466c4ab6f9d2ae385d6ec461d67e301f35093b35
parent 495814 15142b2ebe328cced5c2f777cf2646f2bb1bf5da
child 495816 7e9790a6bd783a2be00e0b63d3e6dc225432ab9b
push id9984
push userffxbld-merge
push dateMon, 15 Oct 2018 21:07:35 +0000
treeherdermozilla-beta@183d27ea8570 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstestonly
bugs1490543, 12959, 824647, 1205916, 595369
milestone64.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1490543 [wpt PR 12959] - ServiceWorker: Implement to return registration data after evaluate module scripts, and add WPT tests for ServiceWorker module scripts, a=testonly Automatic update from web-platform-testsServiceWorker: Implement to return registration data after evaluate module scripts, and add WPT tests for ServiceWorker module scripts Implement to return registration data after evaluate module scripts and commonize WillEvaluate/DidEvaluate functions of classic and module. In addition, import module tests for ServiceWorker scripts reuse tests for DedicatedWorker. The data path in both type is "Main scirpt -> Worker script -> Main scirpt" because ServiceWorker can reply to its source only in MessageEvent. Change-Id: I2bcc1670bd43109710af30c7df3aba5ff039201a Bug: 824647 Reviewed-on: https://chromium-review.googlesource.com/1205916 Commit-Queue: Asami Doi <asamidoi@google.com> Reviewed-by: Ilya Sherman <isherman@chromium.org> Reviewed-by: Matt Falkenhagen <falken@chromium.org> Reviewed-by: Hiroki Nakagawa <nhiroki@chromium.org> Reviewed-by: Kouhei Ueno <kouhei@chromium.org> Reviewed-by: Makoto Shimazu <shimazu@chromium.org> Cr-Commit-Position: refs/heads/master@{#595369} -- wpt-commits: 0c9042da11fa00e2df7d3115b7629a65a5d0fee9 wpt-pr: 12959
testing/web-platform/tests/service-workers/service-worker/import-module-scripts.https.html
testing/web-platform/tests/workers/modules/dedicated-worker-import-blob-url.any.js
testing/web-platform/tests/workers/modules/dedicated-worker-import-data-url.any.js
testing/web-platform/tests/workers/modules/dedicated-worker-import.any.js
testing/web-platform/tests/workers/modules/resources/dynamic-import-and-then-static-import-worker.js
testing/web-platform/tests/workers/modules/resources/dynamic-import-worker.js
testing/web-platform/tests/workers/modules/resources/eval-dynamic-import-worker.js
testing/web-platform/tests/workers/modules/resources/export-on-dynamic-import-script.js
testing/web-platform/tests/workers/modules/resources/import-test-cases.js
testing/web-platform/tests/workers/modules/resources/nested-dynamic-import-worker.js
testing/web-platform/tests/workers/modules/resources/nested-static-import-worker.js
testing/web-platform/tests/workers/modules/resources/static-import-and-then-dynamic-import-worker.js
testing/web-platform/tests/workers/modules/resources/static-import-worker.js
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/tests/service-workers/service-worker/import-module-scripts.https.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>Tests for module import: ServiceWorker</title>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="resources/test-helpers.sub.js"></script>
+<script src="/workers/modules/resources/import-test-cases.js"></script>
+<body>
+<script>
+function import_test(testCase) {
+  promise_test(async t => {
+    const msgPromise = new Promise(resolve => {
+      navigator.serviceWorker.onmessage = resolve;
+    });
+    await service_worker_unregister(t, testCase.scriptURL);
+    const registration = await navigator.serviceWorker.register(
+      testCase.scriptURL,
+      { scope: testCase.scriptURL, type: 'module' });
+    registration.installing.postMessage(
+      'Send message for tests from main script.');
+    const msgEvent = await msgPromise;
+    assert_array_equals(msgEvent.data, testCase.expectation);
+  }, testCase.description);
+}
+
+testCases.forEach(import_test);
+</script>
+</body>
--- a/testing/web-platform/tests/workers/modules/dedicated-worker-import-blob-url.any.js
+++ b/testing/web-platform/tests/workers/modules/dedicated-worker-import-blob-url.any.js
@@ -4,18 +4,18 @@
 // and waits until the list of imported modules is sent from the worker. Passes
 // if the list is equal to |testCase.expectation|.
 function import_blob_url_test(testCase) {
   promise_test(async () => {
     const importURL = new URL(testCase.scriptURL, location.href);
     const blob = new Blob([`import "${importURL}";`],
                           { type: 'text/javascript' });
     const blobURL = URL.createObjectURL(blob);
-
     const worker = new Worker(blobURL, { type: 'module'});
+    worker.postMessage('Send message for tests from main script.');
     const msgEvent = await new Promise((resolve, reject) => {
       worker.onmessage = resolve;
       worker.onerror = (error) => reject(error && error.message);
     });
     assert_array_equals(msgEvent.data, testCase.expectation);
   }, testCase.description);
 }
 
--- a/testing/web-platform/tests/workers/modules/dedicated-worker-import-data-url.any.js
+++ b/testing/web-platform/tests/workers/modules/dedicated-worker-import-data-url.any.js
@@ -8,14 +8,15 @@ function import_data_url_test(testCase) 
     // The Access-Control-Allow-Origin header is necessary because a worker
     // loaded from a data URL has a null origin and import() on the worker
     // without the header is blocked.
     const importURL = new URL(testCase.scriptURL, location.href) +
         '?pipe=header(Access-Control-Allow-Origin, *)';
     const dataURL = `data:text/javascript,import "${importURL}";`;
 
     const worker = new Worker(dataURL, { type: 'module'});
+    worker.postMessage('Send message for tests from main script.');
     const msgEvent = await new Promise(resolve => worker.onmessage = resolve);
     assert_array_equals(msgEvent.data, testCase.expectation);
   }, testCase.description);
 }
 
 testCases.forEach(import_data_url_test);
--- a/testing/web-platform/tests/workers/modules/dedicated-worker-import.any.js
+++ b/testing/web-platform/tests/workers/modules/dedicated-worker-import.any.js
@@ -1,16 +1,17 @@
 // META: script=/workers/modules/resources/import-test-cases.js
 
 // Starts a dedicated worker for |testCase.scriptURL| and waits until the list
 // of imported modules is sent from the worker. Passes if the list is equal to
 // |testCase.expectation|.
 function import_test(testCase) {
   promise_test(async () => {
     const worker = new Worker(testCase.scriptURL, { type: 'module' });
+    worker.postMessage('Send message for tests from main script.');
     const msgEvent = await new Promise((resolve, reject) => {
       worker.onmessage = resolve;
       worker.onerror = (error) => reject(error && error.message);
     });
     assert_array_equals(msgEvent.data, testCase.expectation);
   }, testCase.description);
 }
 
--- a/testing/web-platform/tests/workers/modules/resources/dynamic-import-and-then-static-import-worker.js
+++ b/testing/web-platform/tests/workers/modules/resources/dynamic-import-and-then-static-import-worker.js
@@ -1,2 +1,20 @@
-import('./export-on-static-import-script.js')
-  .then(module => postMessage(module.importedModules));
+// This script is meant to be imported by a module worker. It receives a
+// message from the worker and responds with the list of imported modules.
+const sourcePromise = new Promise(resolve => {
+  self.onmessage = e => {
+    // DedicatedWorkerGlobalScope doesn't fill in e.source,
+    // so use e.target instead.
+    const source = e.source ? e.source : e.target;
+    resolve(source);
+  };
+});
+
+const importedModulesPromise =
+  import('./export-on-static-import-script.js')
+    .then(module => module.importedModules)
+    .catch(error => `Failed to do dynamic import: ${error}`);
+
+Promise.all([sourcePromise, importedModulesPromise]).then(results => {
+  const [source, importedModules] = results;
+  source.postMessage(importedModules);
+});
--- a/testing/web-platform/tests/workers/modules/resources/dynamic-import-worker.js
+++ b/testing/web-platform/tests/workers/modules/resources/dynamic-import-worker.js
@@ -1,2 +1,20 @@
-import('./export-on-load-script.js')
-  .then(module => postMessage(module.importedModules));
+// This script is meant to be imported by a module worker. It receives a
+// message from the worker and responds with the list of imported modules.
+const sourcePromise = new Promise(resolve => {
+  self.onmessage = e => {
+    // DedicatedWorkerGlobalScope doesn't fill in e.source,
+    // so use e.target instead.
+    const source = e.source ? e.source : e.target;
+    resolve(source);
+  };
+});
+
+const importedModulesPromise =
+  import('./export-on-load-script.js')
+    .then(module => module.importedModules)
+    .catch(error => `Failed to do dynamic import: ${error}`);
+
+Promise.all([sourcePromise, importedModulesPromise]).then(results => {
+  const [source, importedModules] = results;
+  source.postMessage(importedModules);
+});
--- a/testing/web-platform/tests/workers/modules/resources/eval-dynamic-import-worker.js
+++ b/testing/web-platform/tests/workers/modules/resources/eval-dynamic-import-worker.js
@@ -1,3 +1,18 @@
-const code = "import('./export-on-load-script.js')" +
-             "  .then(module => postMessage(module.importedModules));"
+// This script is meant to be imported by a module worker. It receives a
+// message from the worker and responds with the list of imported modules.
+const code =
+  "const sourcePromise = new Promise(resolve => {" +
+  "  self.onmessage = e => {" +
+  "    const source = e.source ? e.source : e.target;" +
+  "    resolve(source);" +
+  "  };" +
+  "});" +
+  "const importedModulesPromise =" +
+  "  import('./export-on-load-script.js')" +
+  "    .then(module => module.importedModules)" +
+  "    .catch(error => `Failed to do dynamic import: ${error}`);" +
+  "Promise.all([sourcePromise, importedModulesPromise]).then(results => {" +
+  "  const [source, importedModules] = results;" +
+  "  source.postMessage(importedModules);" +
+  "});";
 eval(code);
--- a/testing/web-platform/tests/workers/modules/resources/export-on-dynamic-import-script.js
+++ b/testing/web-platform/tests/workers/modules/resources/export-on-dynamic-import-script.js
@@ -1,7 +1,8 @@
 // Export the list of imported modules. It's available after the |ready| promise
 // is resolved.
 export let importedModules = ['export-on-dynamic-import-script.js'];
 export let ready = import('./export-on-load-script.js')
   .then(module => {
-      Array.prototype.push.apply(importedModules, module.importedModules);
+    Array.prototype.push.apply(importedModules, module.importedModules);
+    return importedModules;
   });
--- a/testing/web-platform/tests/workers/modules/resources/import-test-cases.js
+++ b/testing/web-platform/tests/workers/modules/resources/import-test-cases.js
@@ -1,49 +1,49 @@
 const testCases = [
     {
-        scriptURL: 'resources/static-import-worker.js',
+        scriptURL: '/workers/modules/resources/static-import-worker.js',
         expectation: ['export-on-load-script.js'],
         description: 'Static import.'
     },
     {
-        scriptURL: 'resources/nested-static-import-worker.js',
+        scriptURL: '/workers/modules/resources/nested-static-import-worker.js',
         expectation: [
             'export-on-static-import-script.js',
             'export-on-load-script.js'
         ],
         description: 'Nested static import.'
     },
     {
-        scriptURL: 'resources/static-import-and-then-dynamic-import-worker.js',
+        scriptURL: '/workers/modules/resources/static-import-and-then-dynamic-import-worker.js',
         expectation: [
             'export-on-dynamic-import-script.js',
             'export-on-load-script.js'
         ],
         description: 'Static import and then dynamic import.'
     },
     {
-        scriptURL: 'resources/dynamic-import-worker.js',
+        scriptURL: '/workers/modules/resources/dynamic-import-worker.js',
         expectation: ['export-on-load-script.js'],
         description: 'Dynamic import.'
     },
     {
-        scriptURL: 'resources/nested-dynamic-import-worker.js',
+        scriptURL: '/workers/modules/resources/nested-dynamic-import-worker.js',
         expectation: [
             'export-on-dynamic-import-script.js',
             'export-on-load-script.js'
         ],
         description: 'Nested dynamic import.'
     },
     {
-        scriptURL: 'resources/dynamic-import-and-then-static-import-worker.js',
+        scriptURL: '/workers/modules/resources/dynamic-import-and-then-static-import-worker.js',
         expectation: [
             'export-on-static-import-script.js',
             'export-on-load-script.js'
         ],
         description: 'Dynamic import and then static import.'
     },
     {
-        scriptURL: 'resources/eval-dynamic-import-worker.js',
+        scriptURL: '/workers/modules/resources/eval-dynamic-import-worker.js',
         expectation: ['export-on-load-script.js'],
         description: 'eval(import()).'
     }
 ];
--- a/testing/web-platform/tests/workers/modules/resources/nested-dynamic-import-worker.js
+++ b/testing/web-platform/tests/workers/modules/resources/nested-dynamic-import-worker.js
@@ -1,5 +1,22 @@
-import('./export-on-dynamic-import-script.js')
-  .then(async module => {
-    await module.ready;
-    postMessage(module.importedModules);
-  });
+// This script is meant to be imported by a module worker. It receives a
+// message from the worker and responds with the list of imported modules.
+
+const sourcePromise = new Promise(resolve => {
+  self.onmessage = e => {
+    // DedicatedWorkerGlobalScope doesn't fill in e.source,
+    // so use e.target instead.
+    const source = e.source ? e.source : e.target;
+    resolve(source);
+  };
+});
+
+const importedModulesPromise =
+  import('./export-on-dynamic-import-script.js')
+    .then(module => module.ready)
+    .then(importedModules => importedModules)
+    .catch(error => `Failed to do dynamic import: ${error}`);
+
+Promise.all([sourcePromise, importedModulesPromise]).then(results => {
+  const [source, importedModules] = results;
+  source.postMessage(importedModules);
+});
--- a/testing/web-platform/tests/workers/modules/resources/nested-static-import-worker.js
+++ b/testing/web-platform/tests/workers/modules/resources/nested-static-import-worker.js
@@ -1,2 +1,9 @@
+// This script is meant to be imported by a module worker. It receives a
+// message from the worker and responds with the list of imported modules.
 import * as module from './export-on-static-import-script.js';
-postMessage(module.importedModules);
+self.onmessage = e => {
+  // DedicatedWorkerGlobalScope doesn't fill in e.source,
+  // so use e.target instead.
+  const source = e.source ? e.source : e.target;
+  source.postMessage(module.importedModules);
+};
--- a/testing/web-platform/tests/workers/modules/resources/static-import-and-then-dynamic-import-worker.js
+++ b/testing/web-platform/tests/workers/modules/resources/static-import-and-then-dynamic-import-worker.js
@@ -1,2 +1,22 @@
+// This script is meant to be imported by a module worker. It receives a
+// message from the worker and responds with the list of imported modules.
 import * as module from './export-on-dynamic-import-script.js';
-module.ready.then(() => postMessage(module.importedModules));
+
+const sourcePromise = new Promise(resolve => {
+  self.onmessage = e => {
+    // DedicatedWorkerGlobalScope doesn't fill in e.source,
+    // so use e.target instead.
+    const source = e.source ? e.source : e.target;
+    resolve(source);
+  };
+});
+
+export let importedModules = ['export-on-dynamic-import-script.js'];
+const importedModulesPromise = module.ready
+  .then(importedModules => importedModules)
+  .catch(error => `Failed to do dynamic import: ${error}`);
+
+Promise.all([sourcePromise, importedModulesPromise]).then(results => {
+  const [source, importedModules] = results;
+  source.postMessage(importedModules);
+});
--- a/testing/web-platform/tests/workers/modules/resources/static-import-worker.js
+++ b/testing/web-platform/tests/workers/modules/resources/static-import-worker.js
@@ -1,2 +1,9 @@
+// This script is meant to be imported by a module worker. It receives a
+// message from the worker and responds with the list of imported modules.
 import * as module from './export-on-load-script.js';
-postMessage(module.importedModules);
+self.onmessage = e => {
+  // DedicatedWorkerGlobalScope doesn't fill in e.source,
+  // so use e.target instead.
+  const source = e.source ? e.source : e.target;
+  source.postMessage(module.importedModules);
+};