Bug 1508580 [wpt PR 14142] - Refine wpt/service-workers/service-worker/clients-get.https.html, a=testonly
authorMakoto Shimazu <shimazu@chromium.org>
Sat, 24 Nov 2018 10:28:54 +0000
changeset 507253 156e41c2308dcd4f365edfc83ec4ebc12a69e343
parent 507252 0b200eba03b5bcee5c37774dff25fa3a15d93cf5
child 507254 cfd132d23ffa5f2e7ca9536b3577405d191f99a0
push id1905
push userffxbld-merge
push dateMon, 21 Jan 2019 12:33:13 +0000
treeherdermozilla-release@c2fca1944d8c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstestonly
bugs1508580, 14142, 778497, 1343416, 609693
milestone65.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 1508580 [wpt PR 14142] - Refine wpt/service-workers/service-worker/clients-get.https.html, a=testonly Automatic update from web-platform-testsRefine wpt/service-workers/service-worker/clients-get.https.html Used async functions, arrow functions, and const, and added some comments. Bug: 778497 Change-Id: I03f23c6adddbb0e317edb197dc8af197597b2381 Reviewed-on: https://chromium-review.googlesource.com/c/1343416 Commit-Queue: Makoto Shimazu <shimazu@chromium.org> Reviewed-by: Matt Falkenhagen <falken@chromium.org> Cr-Commit-Position: refs/heads/master@{#609693} -- wpt-commits: 0ce9a880f8ca8fab5b252787a0cf098093ceb40d wpt-pr: 14142
testing/web-platform/tests/service-workers/service-worker/clients-get-client-types.https.html
testing/web-platform/tests/service-workers/service-worker/clients-get.https.html
testing/web-platform/tests/service-workers/service-worker/resources/clients-get-cross-origin-frame.html
testing/web-platform/tests/service-workers/service-worker/resources/clients-get-resultingClientId-worker.js
testing/web-platform/tests/service-workers/service-worker/resources/clients-get-worker.js
--- a/testing/web-platform/tests/service-workers/service-worker/clients-get-client-types.https.html
+++ b/testing/web-platform/tests/service-workers/service-worker/clients-get-client-types.https.html
@@ -4,21 +4,23 @@
 <script src="/resources/testharnessreport.js"></script>
 <script src="resources/test-helpers.sub.js"></script>
 <script>
 var scope = 'resources/clients-get-client-types';
 var frame_url = scope + '-frame.html';
 var shared_worker_url = scope + '-shared-worker.js';
 var worker_url = scope + '-worker.js';
 var client_ids = [];
+var registration;
 var frame;
 promise_test(function(t) {
     return service_worker_unregister_and_register(
         t, 'resources/clients-get-worker.js', scope)
-      .then(function(registration) {
+      .then(function(r) {
+          registration = r;
           add_completion_callback(function() { registration.unregister(); });
           return wait_for_state(t, registration.installing, 'activated');
         })
       .then(function() {
           return with_iframe(frame_url);
         })
       .then(function(f) {
           frame = f;
@@ -54,22 +56,20 @@ promise_test(function(t) {
           return new Promise(function(resolve) {
               channel.port1.onmessage = function(e) {
                 resolve(e.data.clientId);
               };
             });
         })
       .then(function(client_id) {
           client_ids.push(client_id);
-          var channel = new MessageChannel();
           var saw_message = new Promise(function(resolve) {
-              channel.port1.onmessage = resolve;
+              navigator.serviceWorker.onmessage = resolve;
             });
-          frame.contentWindow.navigator.serviceWorker.controller.postMessage(
-              {port: channel.port2, clientIds: client_ids}, [channel.port2]);
+          registration.active.postMessage({clientIds: client_ids});
           return saw_message;
         })
       .then(function(e) {
           assert_equals(e.data.length, expected.length);
           // We use these assert_not_equals because assert_array_equals doesn't
           // print the error description when passed an undefined value.
           assert_not_equals(e.data[0], undefined,
               'Window client should not be undefined');
--- a/testing/web-platform/tests/service-workers/service-worker/clients-get.https.html
+++ b/testing/web-platform/tests/service-workers/service-worker/clients-get.https.html
@@ -1,224 +1,154 @@
 <!DOCTYPE html>
 <title>Service Worker: Clients.get</title>
 <script src="/resources/testharness.js"></script>
 <script src="/resources/testharnessreport.js"></script>
 <script src="resources/test-helpers.sub.js"></script>
 <script>
-var scope = 'resources/clients-get-frame.html';
-var client_ids = [];
-var frame;
-promise_test(function(t) {
-    return service_worker_unregister_and_register(
-        t, 'resources/clients-get-worker.js', scope)
-      .then(function(registration) {
-          add_completion_callback(function() { registration.unregister(); });
-          return wait_for_state(t, registration.installing, 'activated');
-        })
-      .then(function() {
-          return with_iframe(scope + '#1');
-        })
-      .then(function(frame1) {
-          add_completion_callback(function() { frame1.remove(); });
-          frame1.focus();
-          return wait_for_clientId();
-        })
-      .then(function(client_id) {
-          client_ids.push(client_id);
-          return with_iframe(scope + '#2');
-        })
-      .then(function(frame2) {
-          frame = frame2;
-          add_completion_callback(function() { frame2.remove(); });
-          return wait_for_clientId();
-        })
-      .then(function(client_id) {
-          client_ids.push(client_id, 'invalid-id');
-          var channel = new MessageChannel();
-          var saw_message = new Promise(function(resolve) {
-              channel.port1.onmessage = resolve;
-            });
-          frame.contentWindow.navigator.serviceWorker.controller.postMessage(
-              {port: channel.port2, clientIds: client_ids}, [channel.port2]);
-          return saw_message;
-        })
-      .then(function(e) {
-          assert_equals(e.data.length, 3);
-          assert_array_equals(e.data[0], expected[0]);
-          assert_array_equals(e.data[1], expected[1]);
-          assert_equals(e.data[2], expected[2]);
-        });
-  }, 'Test Clients.get()');
-
-promise_test((t) => {
-  let frame = null;
-  const scope = 'resources/simple.html';
-  const outerSwContainer = navigator.serviceWorker;
-  let innerSwReg = null;
-  let innerSw = null;
-
-  return service_worker_unregister_and_register(
-    t, 'resources/clients-get-resultingClientId-worker.js', scope)
-    .then((registration) => {
-        innerSwReg = registration;
-        add_completion_callback(function() { registration.unregister(); });
-        return wait_for_state(t, registration.installing, 'activated');
-    })
-    .then(() => {
-        // load frame and get resulting client id
-      let channel = new MessageChannel();
-      innerSw = innerSwReg.active;
-
-      let p = new Promise(resolve => {
-        function getResultingClientId(e) {
-          if (e.data.msg == 'getResultingClientId') {
-            const { resultingClientId } = e.data;
-
-            channel.port1.removeEventListener('message', getResultingClientId);
-
-            resolve({ resultingClientId, port: channel.port1 });
-          }
-        }
-
-        channel.port1.onmessage = getResultingClientId;
-      });
-
-
-      return with_iframe(scope).then((iframe) => {
-        innerSw.postMessage(
-          { port: channel.port2, msg: 'getResultingClientId' },
-          [channel.port2],
-        );
-
-        frame = iframe;
-        frame.focus();
-        add_completion_callback(() => iframe.remove());
-
-        return p;
-      });
-    })
-    .then(({ resultingClientId, port }) => {
-      // query service worker for clients.get(resultingClientId)
-      let channel = new MessageChannel();
-
-      let p = new Promise(resolve => {
-        function getIsResultingClientUndefined(e) {
-          if (e.data.msg == 'getIsResultingClientUndefined') {
-            let { isResultingClientUndefined } = e.data;
-
-            port.removeEventListener('message', getIsResultingClientUndefined);
-
-            resolve(isResultingClientUndefined);
-          }
-        }
-
-        port.onmessage = getIsResultingClientUndefined;
-      });
-
-      innerSw.postMessage(
-        { port: channel.port2, msg: 'getIsResultingClientUndefined', resultingClientId },
-        [channel.port2],
-      );
-
-      return p;
-    })
-    .then((isResultingClientUndefined) => {
-      assert_false(isResultingClientUndefined, 'Clients.get(FetchEvent.resultingClientId) resolved with a Client');
-    });
-}, 'Test successful Clients.get(FetchEvent.resultingClientId)');
-
-promise_test((t) => {
-  const scope = 'resources/simple.html?fail';
-  const outerSwContainer = navigator.serviceWorker;
-  let innerSwReg = null;
-  let innerSw = null;
-
-  return service_worker_unregister_and_register(
-    t, 'resources/clients-get-resultingClientId-worker.js', scope)
-    .then((registration) => {
-        innerSwReg = registration;
-        add_completion_callback(function() { registration.unregister(); });
-        return wait_for_state(t, registration.installing, 'activated');
-    })
-    .then(() => {
-        // load frame, destroying it while loading, and get resulting client id
-        innerSw = innerSwReg.active;
-
-        let iframe = document.createElement('iframe');
-        iframe.className = 'test-iframe';
-        iframe.src = scope;
-
-        function destroyIframe(e) {
-          if (e.data.msg == 'destroyResultingClient') {
-            iframe.remove();
-            iframe = null;
-
-            innerSw.postMessage({ msg: 'resultingClientDestroyed' });
-          }
-        }
-
-        outerSwContainer.addEventListener('message', destroyIframe);
-
-        let p = new Promise(resolve => {
-          function resultingClientDestroyedAck(e) {
-            if (e.data.msg == 'resultingClientDestroyedAck') {
-              let { resultingDestroyedClientId } = e.data;
-
-              outerSwContainer.removeEventListener('message', resultingClientDestroyedAck);
-              resolve(resultingDestroyedClientId);
-            }
-          }
-
-          outerSwContainer.addEventListener('message', resultingClientDestroyedAck);
-        });
-
-        document.body.appendChild(iframe);
-
-        return p;
-    })
-    .then((resultingDestroyedClientId) => {
-        // query service worker for clients.get(resultingDestroyedClientId)
-        let channel = new MessageChannel();
-
-        let p = new Promise((resolve, reject) => {
-          function getIsResultingClientUndefined(e) {
-            if (e.data.msg == 'getIsResultingClientUndefined') {
-              let { isResultingClientUndefined } = e.data;
-
-              channel.port1.removeEventListener('message', getIsResultingClientUndefined);
-
-              resolve(isResultingClientUndefined);
-            }
-          }
-
-          channel.port1.onmessage = getIsResultingClientUndefined;
-        });
-
-        innerSw.postMessage(
-          { port: channel.port2, msg: 'getIsResultingClientUndefined', resultingClientId: resultingDestroyedClientId },
-          [channel.port2],
-        );
-
-        return p;
-    })
-    .then((isResultingClientUndefined) => {
-      assert_true(isResultingClientUndefined, 'Clients.get(FetchEvent.resultingClientId) resolved with `undefined`');
-    });
-}, 'Test unsuccessful Clients.get(FetchEvent.resultingClientId)');
-
 function wait_for_clientId() {
   return new Promise(function(resolve, reject) {
-      function get_client_id(e) {
-        window.removeEventListener('message', get_client_id);
-        resolve(e.data.clientId);
-      }
-      window.addEventListener('message', get_client_id, false);
-    });
+    window.onmessage = e => {
+      resolve(e.data.clientId);
+    };
+  });
 }
 
-var expected = [
+promise_test(async t => {
+  // Register service worker.
+  const scope = 'resources/clients-get-frame.html';
+  const client_ids = [];
+  const registration = await service_worker_unregister_and_register(
+    t, 'resources/clients-get-worker.js', scope);
+  t.add_cleanup(() => registration.unregister());
+  await wait_for_state(t, registration.installing, 'activated');
+
+  // Prepare for test cases.
+  // Case 1: frame1 which is focused.
+  const frame1 = await with_iframe(scope + '#1');
+  t.add_cleanup(() => frame1.remove());
+  frame1.focus();
+  client_ids.push(await wait_for_clientId());
+  // Case 2: frame2 which is not focused.
+  const frame2 = await with_iframe(scope + '#2');
+  t.add_cleanup(() =>  frame2.remove());
+  client_ids.push(await wait_for_clientId());
+  // Case 3: invalid id.
+  client_ids.push('invalid-id');
+
+  // Call clients.get() for each id on the service worker.
+  const message_event = await new Promise(resolve => {
+    navigator.serviceWorker.onmessage = resolve;
+    registration.active.postMessage({clientIds: client_ids});
+  });
+
+  const expected = [
     // visibilityState, focused, url, type, frameType
     ['visible', true, normalizeURL(scope) + '#1', 'window', 'nested'],
     ['visible', false, normalizeURL(scope) + '#2', 'window', 'nested'],
     undefined
-];
+  ];
+  assert_equals(message_event.data.length, 3);
+  assert_array_equals(message_event.data[0], expected[0]);
+  assert_array_equals(message_event.data[1], expected[1]);
+  assert_equals(message_event.data[2], expected[2]);
+}, 'Test Clients.get()');
+
+promise_test(async t => {
+  // Register service worker.
+  const scope = 'resources/simple.html';
+  const registration = await service_worker_unregister_and_register(
+    t, 'resources/clients-get-resultingClientId-worker.js', scope)
+  t.add_cleanup(() =>  registration.unregister());
+  await wait_for_state(t, registration.installing, 'activated');
+  const worker = registration.active;
+
+  // Load frame within the scope.
+  const frame = await with_iframe(scope);
+  t.add_cleanup(() => frame.remove());
+  frame.focus();
+
+  // Get resulting client id.
+  const resultingClientId = await new Promise(resolve => {
+    navigator.serviceWorker.onmessage = e => {
+      if (e.data.msg == 'getResultingClientId') {
+        resolve(e.data.resultingClientId);
+      }
+    };
+    worker.postMessage({msg: 'getResultingClientId'});
+  });
+
+  // Query service worker for clients.get(resultingClientId).
+  const isResultingClientUndefined = await new Promise(resolve => {
+    navigator.serviceWorker.onmessage = e => {
+      if (e.data.msg == 'getIsResultingClientUndefined') {
+        resolve(e.data.isResultingClientUndefined);
+      }
+    };
+    worker.postMessage({msg: 'getIsResultingClientUndefined',
+                        resultingClientId});
+  });
+
+  assert_false(
+    isResultingClientUndefined,
+    'Clients.get(FetchEvent.resultingClientId) resolved with a Client');
+}, 'Test successful Clients.get(FetchEvent.resultingClientId)');
+
+promise_test(async t => {
+  // Register service worker.
+  const scope = 'resources/simple.html?fail';
+  const registration = await service_worker_unregister_and_register(
+    t, 'resources/clients-get-resultingClientId-worker.js', scope);
+  t.add_cleanup(() =>  registration.unregister());
+  await wait_for_state(t, registration.installing, 'activated');
+
+  // Load frame, and destroy it while loading.
+  const worker = registration.active;
+  let frame = document.createElement('iframe');
+  frame.src = scope;
+  t.add_cleanup(() => {
+    if (frame) {
+      frame.remove();
+    }
+  });
+
+  await new Promise(resolve => {
+    navigator.serviceWorker.onmessage = e => {
+      // The service worker posts a message to remove the iframe during fetch
+      // event.
+      if (e.data.msg == 'destroyResultingClient') {
+        frame.remove();
+        frame = null;
+        worker.postMessage({msg: 'resultingClientDestroyed'});
+        resolve();
+      }
+    };
+    document.body.appendChild(frame);
+  });
+
+  resultingDestroyedClientId = await new Promise(resolve => {
+    navigator.serviceWorker.onmessage = e => {
+      // The worker sends a message back when it receives the message
+      // 'resultingClientDestroyed' with the resultingClientId.
+      if (e.data.msg == 'resultingClientDestroyedAck') {
+        assert_equals(frame, null, 'Frame should be destroyed at this point.');
+        resolve(e.data.resultingDestroyedClientId);
+      }
+    };
+  });
+
+  // Query service worker for clients.get(resultingDestroyedClientId).
+  const isResultingClientUndefined = await new Promise(resolve => {
+    navigator.serviceWorker.onmessage = e => {
+      if (e.data.msg == 'getIsResultingClientUndefined') {
+        resolve(e.data.isResultingClientUndefined);
+      }
+    };
+    worker.postMessage({msg: 'getIsResultingClientUndefined',
+                        resultingClientId: resultingDestroyedClientId });
+  });
+
+  assert_true(
+    isResultingClientUndefined,
+    'Clients.get(FetchEvent.resultingClientId) resolved with `undefined`');
+}, 'Test unsuccessful Clients.get(FetchEvent.resultingClientId)');
+
 </script>
--- a/testing/web-platform/tests/service-workers/service-worker/resources/clients-get-cross-origin-frame.html
+++ b/testing/web-platform/tests/service-workers/service-worker/resources/clients-get-cross-origin-frame.html
@@ -33,21 +33,18 @@ window.addEventListener('message', funct
   var cross_origin_client_ids = [];
   cross_origin_client_ids.push(e.data.clientId);
   wait_for_worker_promise
     .then(function() {
         return with_iframe(scope);
       })
     .then(function(iframe) {
         add_completion_callback(function() { iframe.remove(); });
-        var channel = new MessageChannel();
-        channel.port1.onmessage = function(e) {
+        navigator.serviceWorker.onmessage = function(e) {
           registration.unregister();
           window.parent.postMessage(
             { type: 'clientId', value: e.data }, host_info['HTTPS_ORIGIN']
           );
         };
-        iframe.contentWindow.navigator.serviceWorker.controller.postMessage(
-            {port:channel.port2, clientIds: cross_origin_client_ids},
-            [channel.port2]);
+        registration.active.postMessage({clientIds: cross_origin_client_ids});
       });
 });
 </script>
--- a/testing/web-platform/tests/service-workers/service-worker/resources/clients-get-resultingClientId-worker.js
+++ b/testing/web-platform/tests/service-workers/service-worker/resources/clients-get-resultingClientId-worker.js
@@ -1,64 +1,60 @@
 let savedPort = null;
 let savedResultingClientId = null;
 
-async function destroyResultingClient(e) {
-  const outer = await self.clients.matchAll({ type: 'window', includeUncontrolled: true })
-    .then((clientList) => {
-    for (let c of clientList) {
-      if (c.url.endsWith('clients-get.https.html')) {
-        c.focus();
-        return c;
-      }
+async function getTestingPage() {
+  const clientList = await self.clients.matchAll({ type: 'window', includeUncontrolled: true });
+  for (let c of clientList) {
+    if (c.url.endsWith('clients-get.https.html')) {
+      c.focus();
+      return c;
     }
-  });
+  }
+  return null;
+}
 
-  const p = new Promise(resolve => {
-    function resultingClientDestroyed(evt) {
-      if (evt.data.msg == 'resultingClientDestroyed') {
-        self.removeEventListener('message', resultingClientDestroyed);
-        resolve(outer);
+async function destroyResultingClient(testingPage) {
+  const destroyedPromise = new Promise(resolve => {
+    self.addEventListener('message', e => {
+      if (e.data.msg == 'resultingClientDestroyed') {
+        resolve();
       }
-    }
-
-    self.addEventListener('message', resultingClientDestroyed);
+    }, {once: true});
   });
-
-  outer.postMessage({ msg: 'destroyResultingClient' });
-
-  return await p;
+  testingPage.postMessage({ msg: 'destroyResultingClient' });
+  return destroyedPromise;
 }
 
 self.addEventListener('fetch', async (e) => {
   let { resultingClientId } = e;
   savedResultingClientId = resultingClientId;
 
   if (e.request.url.endsWith('simple.html?fail')) {
-    e.waitUntil(new Promise(async (resolve) => {
-        let outer = await destroyResultingClient(e);
+    e.waitUntil((async () => {
+      const testingPage = await getTestingPage();
+      await destroyResultingClient(testingPage);
+      testingPage.postMessage({ msg: 'resultingClientDestroyedAck',
+                                resultingDestroyedClientId: savedResultingClientId });
+    })());
+    return;
+  }
 
-        outer.postMessage({ msg: 'resultingClientDestroyedAck',
-                            resultingDestroyedClientId: savedResultingClientId });
-        resolve();
-    }));
-  } else {
-    e.respondWith(fetch(e.request));
-  }
+  e.respondWith(fetch(e.request));
 });
 
 self.addEventListener('message', (e) => {
-  let { msg, port, resultingClientId } = e.data;
-  savedPort = savedPort || port;
-
-  if (msg == 'getIsResultingClientUndefined') {
-    self.clients.get(resultingClientId).then((client) => {
+  let { msg, resultingClientId } = e.data;
+  e.waitUntil((async () => {
+    if (msg == 'getIsResultingClientUndefined') {
+      const client = await self.clients.get(resultingClientId);
       let isUndefined = typeof client == 'undefined';
-      savedPort.postMessage({ msg: 'getIsResultingClientUndefined',
+      e.source.postMessage({ msg: 'getIsResultingClientUndefined',
         isResultingClientUndefined: isUndefined });
-    });
-  }
-
-  if (msg == 'getResultingClientId') {
-    savedPort.postMessage({ msg: 'getResultingClientId',
-      resultingClientId: savedResultingClientId });
-  }
+      return;
+    }
+    if (msg == 'getResultingClientId') {
+      e.source.postMessage({ msg: 'getResultingClientId',
+                             resultingClientId: savedResultingClientId });
+      return;
+    }
+  })());
 });
--- a/testing/web-platform/tests/service-workers/service-worker/resources/clients-get-worker.js
+++ b/testing/web-platform/tests/service-workers/service-worker/resources/clients-get-worker.js
@@ -6,37 +6,36 @@
 self.onfetch = function(e) {
   if (/\/clientId$/.test(e.request.url)) {
     e.respondWith(new Response(e.clientId));
     return;
   }
 };
 
 self.onmessage = function(e) {
-  var port = e.data.port;
   var client_ids = e.data.clientIds;
   var message = [];
 
   e.waitUntil(Promise.all(
       client_ids.map(function(client_id) {
           return self.clients.get(client_id);
         }))
       .then(function(clients) {
           // No matching client for a given id or a matched client is off-origin
           // from the service worker.
           if (clients.length == 1 && clients[0] == undefined) {
-            port.postMessage(clients[0]);
+            e.source.postMessage(clients[0]);
           } else {
             clients.forEach(function(client) {
                 if (client instanceof Client) {
                   message.push([client.visibilityState,
                                 client.focused,
                                 client.url,
                                 client.type,
                                 client.frameType]);
                 } else {
                   message.push(client);
                 }
               });
-            port.postMessage(message);
+            e.source.postMessage(message);
           }
         }));
 };