testing/web-platform/tests/workers/support/sandboxed-tests.html
author Florian Quèze <florian@queze.net>
Tue, 08 Jul 2025 20:48:53 +0000 (10 hours ago)
changeset 795809 b01190f141a12f4b506acb1f4476561c4b0f2407
parent 330130 b16ebc0970fbd699e7012610ff74a518e5d8a328
permissions -rw-r--r--
Bug 1960567 - remove the last C++ and scriptable APIs to accumulate data to legacy telemetry histograms, r=chutten. Differential Revision: https://phabricator.services.mozilla.com/D255582
<!DOCTYPE html>
<meta charset=utf-8>
<script>
const channel = new BroadcastChannel('echochannel');
channel.onmessage = e => channel.postMessage(e.data);
</script>
<div id='workersrc' style='display:none'>
importScripts("http://{{host}}:{{ports[http][0]}}/resources/testharness.js");
onmessage = e => {
  const external_blob = e.data.blob;
  const external_blob_url = e.data.url;

  test(t => {
      assert_equals('null', self.location.origin);
    }, 'Worker has an opaque origin.');

  async_test(t => {
      const r = new FileReader();
      r.onloadend = t.step_func_done(e => assert_equals(r.result, 'from worker'));
      r.readAsText(new Blob(['from worker']));
    }, 'Worker can read its own blobs.');

  async_test(t => {
      const r = new FileReader();
      r.onloadend = t.step_func_done(e => assert_equals(r.result, 'from page'));
      r.readAsText(external_blob);
    }, 'Worker can read its owners blobs.');

  async_test(t => {
      const request = new XMLHttpRequest();
      request.open('GET', external_blob_url);
      request.onreadystatechange = t.step_func(() => {
          if (request.readyState == 4) {
            assert_equals(request.responseText, 'from page');
            t.done();
          }
        });
      request.send();
    }, 'Worker can XHR fetch a blob.');

  promise_test(t =>
      fetch(external_blob_url).then(r => r.text()).then(text => assert_equals(text, 'from page'))
    , 'Worker can fetch a blob.');

  async_test(t => {
      const channel = new BroadcastChannel('echochannel');
      channel.onmessage = t.step_func_done(e => assert_equals('ping', e.data));
      channel.postMessage('ping');
    }, 'Worker can access BroadcastChannel');
  done();
};
</div>
<script>
// Create a worker with a blob URL to get a same opaque origin worker.
const b = new Blob([document.getElementById('workersrc').textContent]);
const url = URL.createObjectURL(b);
const worker = new Worker(url);
// Pass back any messages from the worker to our parent to collect test results.
worker.onmessage = e => {
  parent.postMessage(e.data, '*');
};

// Send test data to the worker.
const b2 = new Blob(['from page']);
const url2 = URL.createObjectURL(b2);
worker.postMessage({url: url2, blob: b2});
</script>