clients-matchall-include-uncontrolled.https.html (5121B)
1 <!DOCTYPE html> 2 <title>Service Worker: Clients.matchAll with includeUncontrolled</title> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <script src="resources/test-helpers.sub.js"></script> 6 <script> 7 function test_matchall(service_worker, expected, query_options) { 8 expected.sort((a, b) => a[2] > b[2] ? 1 : -1); 9 return new Promise((resolve, reject) => { 10 const channel = new MessageChannel(); 11 channel.port1.onmessage = e => { 12 const data = e.data.filter(info => { 13 return info[2].indexOf('clients-matchall') > -1; 14 }); 15 data.sort((a, b) => a[2] > b[2] ? 1 : -1); 16 assert_equals(data.length, expected.length); 17 for (let i = 0; i < data.length; i++) 18 assert_array_equals(data[i], expected[i]); 19 resolve(); 20 }; 21 service_worker.postMessage({port:channel.port2, options:query_options}, 22 [channel.port2]); 23 }); 24 } 25 26 // Run clients.matchAll without and with includeUncontrolled=true. 27 // (We want to run the two tests sequentially in the same promise_test 28 // so that we can use the same set of iframes without intefering each other. 29 promise_test(async t => { 30 // |base_url| is out-of-scope. 31 const base_url = 'resources/blank.html?clients-matchall'; 32 const scope = base_url + '-includeUncontrolled'; 33 34 const registration = 35 await service_worker_unregister_and_register( 36 t, 'resources/clients-matchall-worker.js', scope); 37 t.add_cleanup(() => service_worker_unregister(t, scope)); 38 const service_worker = registration.installing; 39 await wait_for_state(t, service_worker, 'activated'); 40 41 // Creates 3 iframes, 2 for in-scope and 1 for out-of-scope. 42 let frames = []; 43 frames.push(await with_iframe(base_url)); 44 frames.push(await with_iframe(scope + '#1')); 45 frames.push(await with_iframe(scope + '#2')); 46 47 // Make sure we have focus for '#2' frame and its parent window. 48 frames[2].focus(); 49 frames[2].contentWindow.focus(); 50 51 const expected_without_include_uncontrolled = [ 52 // visibilityState, focused, url, type, frameType 53 ['visible', false, new URL(scope + '#1', location).toString(), 'window', 'nested'], 54 ['visible', true, new URL(scope + '#2', location).toString(), 'window', 'nested'] 55 ]; 56 const expected_with_include_uncontrolled = [ 57 // visibilityState, focused, url, type, frameType 58 ['visible', true, location.href, 'window', 'top-level'], 59 ['visible', false, new URL(scope + '#1', location).toString(), 'window', 'nested'], 60 ['visible', true, new URL(scope + '#2', location).toString(), 'window', 'nested'], 61 ['visible', false, new URL(base_url, location).toString(), 'window', 'nested'] 62 ]; 63 64 await test_matchall(service_worker, expected_without_include_uncontrolled); 65 await test_matchall(service_worker, expected_with_include_uncontrolled, 66 { includeUncontrolled: true }); 67 }, 'Verify matchAll() with windows respect includeUncontrolled'); 68 69 // TODO: Add tests for clients.matchAll for dedicated workers. 70 71 async function create_shared_worker(script_url) { 72 const shared_worker = new SharedWorker(script_url); 73 const msgEvent = await new Promise(r => shared_worker.port.onmessage = r); 74 assert_equals(msgEvent.data, 'started'); 75 return shared_worker; 76 } 77 78 // Run clients.matchAll for shared workers without and with 79 // includeUncontrolled=true. 80 promise_test(async t => { 81 const script_url = 'resources/clients-matchall-client-types-shared-worker.js'; 82 const uncontrolled_script_url = 83 new URL(script_url + '?uncontrolled', location).toString(); 84 const controlled_script_url = 85 new URL(script_url + '?controlled', location).toString(); 86 87 // Start a shared worker that is not controlled by a service worker. 88 const uncontrolled_shared_worker = 89 await create_shared_worker(uncontrolled_script_url); 90 91 // Register a service worker. 92 const registration = 93 await service_worker_unregister_and_register( 94 t, 'resources/clients-matchall-worker.js', script_url); 95 t.add_cleanup(() => service_worker_unregister(t, script_url)); 96 const service_worker = registration.installing; 97 await wait_for_state(t, service_worker, 'activated'); 98 99 // Start another shared worker controlled by the service worker. 100 await create_shared_worker(controlled_script_url); 101 102 const expected_without_include_uncontrolled = [ 103 // visibilityState, focused, url, type, frameType 104 [undefined, undefined, controlled_script_url, 'sharedworker', 'none'], 105 ]; 106 const expected_with_include_uncontrolled = [ 107 // visibilityState, focused, url, type, frameType 108 [undefined, undefined, controlled_script_url, 'sharedworker', 'none'], 109 [undefined, undefined, uncontrolled_script_url, 'sharedworker', 'none'], 110 ]; 111 112 await test_matchall(service_worker, expected_without_include_uncontrolled, 113 { type: 'sharedworker' }); 114 await test_matchall(service_worker, expected_with_include_uncontrolled, 115 { includeUncontrolled: true, type: 'sharedworker' }); 116 }, 'Verify matchAll() with shared workers respect includeUncontrolled'); 117 </script>