client-navigate.https.html (4267B)
1 <!doctype html> 2 <meta charset=utf-8> 3 <meta name="timeout" content="long"> 4 <title>Service Worker: WindowClient.navigate</title> 5 <script src=/resources/testharness.js></script> 6 <script src=/resources/testharnessreport.js></script> 7 <script src="/common/get-host-info.sub.js"></script> 8 <script src="resources/test-helpers.sub.js"></script> 9 <script> 10 function wait_for_message(msg) { 11 return new Promise(function(resolve, reject) { 12 var get_message_data = function get_message_data(e) { 13 window.removeEventListener("message", get_message_data); 14 resolve(e.data); 15 } 16 window.addEventListener("message", get_message_data, false); 17 }); 18 } 19 20 function run_test(controller, clientId, test) { 21 return new Promise(function(resolve, reject) { 22 var channel = new MessageChannel(); 23 channel.port1.onmessage = function(e) { 24 resolve(e.data); 25 }; 26 var message = { 27 port: channel.port2, 28 test: test, 29 clientId: clientId, 30 }; 31 controller.postMessage( 32 message, [channel.port2]); 33 }); 34 } 35 36 async function with_controlled_iframe_and_url(t, name, f) { 37 const SCRIPT = "resources/client-navigate-worker.js"; 38 const SCOPE = "resources/client-navigate-frame.html"; 39 40 // Register service worker and wait for it to become activated 41 const registration = await service_worker_unregister_and_register(t, SCRIPT, SCOPE); 42 t.add_cleanup(() => registration.unregister()); 43 const worker = registration.installing; 44 await wait_for_state(t, worker, 'activated'); 45 46 // Create child iframe and make sure we register a listener for the message 47 // it sends before it's created 48 const client_id_promise = wait_for_message(); 49 const iframe = await with_iframe(SCOPE); 50 t.add_cleanup(() => iframe.remove()); 51 const { id } = await client_id_promise; 52 53 // Run the test in the service worker and fetch it 54 const { result, url } = await run_test(worker, id, name); 55 fetch_tests_from_worker(worker); 56 assert_equals(result, name); 57 58 // Hand over the iframe and URL from the service worker to the callback 59 await f(iframe, url); 60 } 61 62 promise_test(function(t) { 63 return with_controlled_iframe_and_url(t, 'test_client_navigate_success', async (iframe, url) => { 64 assert_equals( 65 url, new URL("resources/client-navigated-frame.html", 66 location).toString()); 67 assert_equals( 68 iframe.contentWindow.location.href, 69 new URL("resources/client-navigated-frame.html", 70 location).toString()); 71 }); 72 }, "Frame location should update on successful navigation"); 73 74 promise_test(function(t) { 75 return with_controlled_iframe_and_url(t, 'test_client_navigate_redirect', async (iframe, url) => { 76 assert_equals(url, ""); 77 assert_throws_dom("SecurityError", function() { return iframe.contentWindow.location.href }); 78 }); 79 }, "Frame location should not be accessible after redirect"); 80 81 promise_test(function(t) { 82 return with_controlled_iframe_and_url(t, 'test_client_navigate_cross_origin', async (iframe, url) => { 83 assert_equals(url, ""); 84 assert_throws_dom("SecurityError", function() { return iframe.contentWindow.location.href }); 85 }); 86 }, "Frame location should not be accessible after cross-origin navigation"); 87 88 promise_test(function(t) { 89 return with_controlled_iframe_and_url(t, 'test_client_navigate_about_blank', async (iframe, url) => { 90 assert_equals( 91 iframe.contentWindow.location.href, 92 new URL("resources/client-navigate-frame.html", 93 location).toString()); 94 iframe.contentWindow.document.body.style = "background-color: green" 95 }); 96 }, "Frame location should not update on failed about:blank navigation"); 97 98 promise_test(function(t) { 99 return with_controlled_iframe_and_url(t, 'test_client_navigate_mixed_content', async (iframe, url) => { 100 assert_equals( 101 iframe.contentWindow.location.href, 102 new URL("resources/client-navigate-frame.html", 103 location).toString()); 104 iframe.contentWindow.document.body.style = "background-color: green" 105 }); 106 }, "Frame location should not update on failed mixed-content navigation"); 107 </script>