https://github.com/web-platform-tests/wpt
Raw File
Tip revision: 0cb962b995d06ee5a8e581277aa7906b039563af authored by Anne van Kesteren on 29 March 2018, 12:32:41 UTC
document.open() and the load event
Tip revision: 0cb962b
clients-matchall-client-types.https.html
<!DOCTYPE html>
<title>Service Worker: Clients.matchAll with various clientTypes</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-matchall-client-types';
var iframe_url = scope + '-iframe.html';
var shared_worker_url = scope + '-shared-worker.js';
var dedicated_worker_url = scope + '-dedicated-worker.js';

/* visibilityState, focused, url, type, frameType */
var expected_only_window = [
    ['visible', true, new URL(iframe_url, location).href, 'window', 'nested']
];
var expected_only_shared_worker = [
    [undefined, undefined, new URL(shared_worker_url, location).href, 'sharedworker', 'none']
];
var expected_only_dedicated_worker = [
    [undefined, undefined, new URL(dedicated_worker_url, location).href, 'worker', 'none']
];

// These are explicitly sorted by URL in the service worker script.
var expected_all_clients = [
    expected_only_dedicated_worker[0],
    expected_only_window[0],
    expected_only_shared_worker[0],
];

function test_matchall(frame, expected, query_options) {
  // Make sure the frame gets focus.
  frame.focus();
  return new Promise(function(resolve, reject) {
    var channel = new MessageChannel();
    channel.port1.onmessage = function(e) { resolve(e.data); };
    frame.contentWindow.navigator.serviceWorker.controller.postMessage(
        {port:channel.port2, options:query_options},
        [channel.port2]);
  }).then(function(data) {
    if (typeof data === 'string') {
      throw new Error(data);
    }

    assert_equals(data.length, expected.length, 'result count');

    for (var i = 0; i < data.length; ++i) {
      assert_array_equals(data[i], expected[i]);
    }
  });
}

promise_test(function(t) {
    var frame;
    return service_worker_unregister_and_register(
        t, 'resources/clients-matchall-worker.js', scope)
      .then(function(registration) {
          return wait_for_state(t, registration.installing, 'activated');
        })
      .then(function() { return with_iframe(iframe_url); })
      .then(function(f) {
          frame = f;
          return test_matchall(frame, expected_only_window, {});
        })
      .then(function() {
          return test_matchall(frame, expected_only_window, {type:'window'});
        })
      .then(function() {
          frame.remove();
          return service_worker_unregister_and_done(t, scope);
        })
      .catch(unreached_rejection(t));
  }, 'Verify matchAll() with window client type');

promise_test(function(t) {
    var frame;
    return service_worker_unregister_and_register(
        t, 'resources/clients-matchall-worker.js', scope)
      .then(function(registration) {
          return wait_for_state(t, registration.installing, 'activated');
        })
      .then(function() { return with_iframe(iframe_url); })
      .then(function(f) {
          frame = f;
          return new Promise(function(resolve, reject) {
              var w = new SharedWorker(shared_worker_url);
              w.port.onmessage = resolve;
            });
        })
      .then(function() {
          return new Promise(function(resolve, reject) {
              var w = new Worker(dedicated_worker_url);
              w.onmessage = resolve;
              w.postMessage('Start');
            });
        })
      .then(function() {
          return test_matchall(frame, expected_only_window, {});
        })
      .then(function() {
          return test_matchall(frame, expected_only_window, {type:'window'});
        })
      .then(function() {
          return test_matchall(frame, expected_only_shared_worker,
                               {type:'sharedworker'});
        })
      .then(function() {
          return test_matchall(frame, expected_only_dedicated_worker,
                               {type:'worker'});
        })
      .then(function() {
          return test_matchall(frame, expected_all_clients, {type:'all'});
        })
      .then(function() {
          frame.remove();
          return service_worker_unregister_and_done(t, scope);
        });
}, 'Verify matchAll() with {window, sharedworker, worker} client types');

</script>
back to top