|
| 1 | +<!DOCTYPE html> |
| 2 | +<script src="/resources/testharness.js"></script> |
| 3 | +<script src="/resources/testharnessreport.js"></script> |
| 4 | +<script src="resources/get-host-info.sub.js"></script> |
| 5 | +<script src="resources/test-helpers.sub.js"></script> |
| 6 | +<body> |
| 7 | +<script> |
| 8 | +const worker = 'resources/fetch-event-within-sw-worker.js'; |
| 9 | + |
| 10 | +function wait(ms) { |
| 11 | + return new Promise(r => setTimeout(r, ms)); |
| 12 | +} |
| 13 | + |
| 14 | +function reset() { |
| 15 | + for (const iframe of [...document.querySelectorAll('.test-iframe')]) { |
| 16 | + iframe.remove(); |
| 17 | + } |
| 18 | + return navigator.serviceWorker.getRegistrations().then(registrations => { |
| 19 | + return Promise.all(registrations.map(r => r.unregister())); |
| 20 | + }).then(() => caches.keys()).then(cacheKeys => { |
| 21 | + return Promise.all(cacheKeys.map(c => caches.delete(c))); |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +function regReady(reg) { |
| 26 | + return new Promise((resolve, reject) => { |
| 27 | + if (reg.active) { |
| 28 | + resolve(); |
| 29 | + return; |
| 30 | + } |
| 31 | + const nextWorker = reg.waiting || reg.installing; |
| 32 | + |
| 33 | + nextWorker.addEventListener('statechange', () => { |
| 34 | + if (nextWorker.state == 'redundant') { |
| 35 | + reject(Error(`Service worker failed to install`)); |
| 36 | + return; |
| 37 | + } |
| 38 | + if (nextWorker.state == 'activated') { |
| 39 | + resolve(); |
| 40 | + } |
| 41 | + }); |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +function closeAllNotifications() { |
| 46 | + |
| 47 | +} |
| 48 | + |
| 49 | +function registerSwAndOpenFrame() { |
| 50 | + return reset().then(() => navigator.serviceWorker.register(worker, {scope: 'resources/'})) |
| 51 | + .then(reg => regReady(reg)) |
| 52 | + .then(() => with_iframe('resources/simple.html')); |
| 53 | +} |
| 54 | + |
| 55 | +promise_test(() => { |
| 56 | + return registerSwAndOpenFrame().then(iframe => { |
| 57 | + return Promise.all([ |
| 58 | + iframe.contentWindow.fetch('dummy.txt').then(r => r.text()), |
| 59 | + iframe.contentWindow.caches.open('test') |
| 60 | + .then(cache => |
| 61 | + cache.add('dummy.txt').then(() => cache.match('dummy.txt')) |
| 62 | + ).then(response => { |
| 63 | + if (!response) return 'cache match failed'; |
| 64 | + return response.text(); |
| 65 | + }) |
| 66 | + ]) |
| 67 | + }).then(([fetchText, cacheText]) => { |
| 68 | + assert_equals(fetchText, 'intercepted', 'fetch intercepted'); |
| 69 | + assert_equals(cacheText, 'intercepted', 'cache.add intercepted'); |
| 70 | + }); |
| 71 | +}, 'Service worker intercepts requests from window'); |
| 72 | + |
| 73 | +promise_test(() => { |
| 74 | + return registerSwAndOpenFrame().then(iframe => { |
| 75 | + return Promise.all([ |
| 76 | + iframe.contentWindow.fetch('dummy.txt-inner-fetch').then(r => r.text()), |
| 77 | + iframe.contentWindow.fetch('dummy.txt-inner-cache').then(r => r.text()) |
| 78 | + ]) |
| 79 | + }).then(([fetchText, cacheText]) => { |
| 80 | + assert_equals(fetchText, 'Hello world\n', 'fetch within SW not intercepted'); |
| 81 | + assert_equals(cacheText, 'Hello world\n', 'cache.add within SW not intercepted'); |
| 82 | + }); |
| 83 | +}, `Service worker does not intercept fetch/cache requests within service worker`); |
| 84 | + |
| 85 | +promise_test(t => { |
| 86 | + return registerSwAndOpenFrame().then(iframe => { |
| 87 | + if (Notification.permission != "granted") { |
| 88 | + Notification.requestPermission(); |
| 89 | + t.set_status(this.NOTRUN, 'You must allow notifications for this origin before running this test.'); |
| 90 | + throw Error('You must allow notifications for this origin before running this test.'); |
| 91 | + } |
| 92 | + return Promise.race([ |
| 93 | + new Promise(resolve => { |
| 94 | + const bc = new BroadcastChannel('icon-request'); |
| 95 | + bc.onmessage = () => { |
| 96 | + bc.close(); |
| 97 | + resolve(); |
| 98 | + }; |
| 99 | + |
| 100 | + const notification = new iframe.contentWindow.Notification('test', { |
| 101 | + icon: 'notification-icon.png' |
| 102 | + }); |
| 103 | + notification.close(); |
| 104 | + }), |
| 105 | + wait(1000).then(() => { throw Error(`Did not capture icon request for notification created from page`); }) |
| 106 | + ]).then(() => Promise.race([ |
| 107 | + new Promise(resolve => { |
| 108 | + const bc = new BroadcastChannel('icon-request'); |
| 109 | + bc.onmessage = () => { |
| 110 | + bc.close(); |
| 111 | + resolve(); |
| 112 | + }; |
| 113 | + |
| 114 | + iframe.contentWindow.fetch('show-notification'); |
| 115 | + }), |
| 116 | + wait(1000).then(() => { throw Error(`Did not capture icon request for notification created within SW`); }) |
| 117 | + ])); |
| 118 | + }); |
| 119 | +}, `Notification requests intercepted both from window and SW`); |
| 120 | + |
| 121 | +</script> |
| 122 | +</body> |
0 commit comments