data-url.html (2861B)
1 <!DOCTYPE html> 2 <title>data URL dedicated workers</title> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <script> 6 // Helper assert functions -START- 7 function assert_worker_sends_pass(test_desc, mime_type, worker_code) { 8 async_test(function(t) { 9 var w = new Worker(`data:${mime_type},${worker_code}`); 10 w.onmessage = t.step_func_done(function(e) { 11 assert_equals(e.data, 'PASS'); 12 }); 13 w.postMessage('SEND_PASS'); 14 }, test_desc); 15 } 16 17 function assert_worker_throws(test_desc, worker_code) { 18 assert_worker_sends_pass(test_desc, '', `try { ${worker_code}; self.postMessage("FAIL"); } catch (e) { self.postMessage("PASS"); }`); 19 } 20 21 function assert_worker_construction_fails(test_desc, mime_type, worker_code) { 22 async_test(function(t) { 23 var w = new Worker(`data:${mime_type},${worker_code};postMessage("PASS")`); 24 w.onmessage = t.step_func_done(function(e) { 25 assert_unreached('Should not receive any message back.'); 26 }); 27 w.onerror = t.step_func_done(function(e) { 28 assert_true(true, 'Should throw ' + e.message); 29 // Stop the error from being propagated to the WPT test harness 30 e.preventDefault(); 31 }); 32 }, test_desc); 33 } 34 // Helper assert functions -END- 35 36 // Actual tests -START- 37 38 // Any MIME type allowed 39 assert_worker_sends_pass('application/javascript MIME allowed', 'application/javascript', 'self.postMessage("PASS")'); 40 assert_worker_sends_pass('text/plain MIME allowed', 'text/plain', 'self.postMessage("PASS")'); 41 assert_worker_sends_pass('empty MIME allowed', '', 'self.postMessage("PASS")'); 42 43 // Communications goes both ways 44 assert_worker_sends_pass('communication goes both ways', 'application/javascript', 'onmessage = function(e) { self.postMessage("PASS"); }'); 45 46 // test access to storage APIs 47 48 // https://w3c.github.io/IndexedDB/#dom-idbfactory-open 49 assert_worker_sends_pass('indexedDB is present', '', 'self.postMessage("indexedDB" in self ? "PASS" : "FAIL")'); 50 assert_worker_throws('indexedDB is inaccessible', 'self.indexedDB.open("someDBName")'); 51 52 // Other standardized storage APIs are either not exposed to workers 53 // (e.g. window.localStorage, window.sessionStorage), or are [SecureContext] 54 // (e.g. self.caches). 55 56 // 'data:' workers are cross-origin 57 assert_worker_sends_pass('cross-origin worker', '', 'fetch("/").then(() => self.postMessage("FAIL"), () => self.postMessage("PASS"))'); 58 59 // 'data:' workers have opaque origin 60 assert_worker_sends_pass('worker has opaque origin', 'application/javascript', 'if (self.location.origin == "null") {postMessage("PASS");} else {postMessage("FAIL");}'); 61 62 setup({allow_uncaught_exception:true}); 63 // invalid javascript will trigger an ErrorEvent 64 assert_worker_construction_fails('invalid javascript produces error', 'application/javascript', '}x=3'); 65 66 // Actual tests -END- 67 </script>