https://github.com/web-platform-tests/wpt
Raw File
Tip revision: 34bd5b1a3394e0ffcae29aae509057bfd1fa3935 authored by Yutaka Hirano on 03 April 2018, 03:15:14 UTC
fix
Tip revision: 34bd5b1
postmessage-to-client.https.html
<!DOCTYPE html>
<title>Service Worker: postMessage to Client</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
promise_test(t => {
    var script = 'resources/postmessage-to-client-worker.js';
    var scope = 'resources/blank.html';
    var w;

    return service_worker_unregister_and_register(t, script, scope)
      .then(registration => {
          t.add_cleanup(() => registration.unregister());
          return wait_for_state(t, registration.installing, 'activated');
        })
      .then(() => with_iframe(scope))
      .then(frame => {
          t.add_cleanup(() => frame.remove());

          return new Promise(resolve => {
              w = frame.contentWindow;
              w.navigator.serviceWorker.onmessage = resolve;
              w.navigator.serviceWorker.controller.postMessage('ping');
            });
        })
      .then(e => {
          var message = e.data;
          assert_equals(e.constructor, w.MessageEvent,
                        'message events should use MessageEvent interface.');
          assert_equals(e.type, 'message', 'type should be "message".');
          assert_equals(e.bubbles, false, 'message events should not bubble.');
          assert_equals(e.cancelable, false,
                        'message events should not be cancelable.');
          assert_equals(
              e.origin, location.origin,
              'origin of message should be origin of Service Worker.');
          assert_equals(e.lastEventId, '',
                        'lastEventId should be an empty string.');
          assert_equals(e.source.constructor, w.ServiceWorker,
                        'source should use ServiceWorker interface.');
          assert_equals(
              e.source, w.navigator.serviceWorker.controller,
              'source should be the service worker that sent the message.');
          assert_equals(e.ports.length, 0, 'ports should be an empty array.');
          assert_equals(message, 'Sending message via clients');
          return new Promise(resolve => {
              w.navigator.serviceWorker.onmessage = resolve;
            });
        })
      .then(e => { assert_equals(e.data, 'quit'); });
  }, 'postMessage from ServiceWorker to Client.');
</script>
back to top