summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/workers/support/sandboxed-tests.html
blob: 811c925f56f11934cf3cb9ec508b73d601525c27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<!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>