focus-event-after-switching-iframes.sub.html (3800B)
1 <!doctype html> 2 <meta charset=utf-8> 3 <title>Test focus and blur event after switching focus from an iframe to another iframe</title> 4 <script src=/resources/testharness.js></script> 5 <script src=/resources/testharnessreport.js></script> 6 <script src="/resources/testdriver.js"></script> 7 <script src="/resources/testdriver-actions.js"></script> 8 <script src="/resources/testdriver-vendor.js"></script> 9 <body></body> 10 <script> 11 function waitForBothIFramesLoaded() { 12 let count = 0; 13 return new Promise(resolve => { 14 window.addEventListener("message", function handler(e) { 15 if (e.data == "ready") { 16 count += 1; 17 } 18 if (count == 2) { 19 window.removeEventListener("message", handler); 20 resolve(); 21 } 22 }); 23 }); 24 } 25 26 function waitForIframeFocus(w) { 27 return new Promise(resolve => { 28 window.addEventListener("message", function handler(e) { 29 if (e.data == "focus" && 30 e.source == w) { 31 window.removeEventListener("message", handler); 32 resolve(); 33 } 34 }); 35 }); 36 } 37 38 function clickFocusIFrame(w) { 39 w.postMessage("focus", "*"); 40 } 41 42 // This will send message to the inner frame to ask it 43 // send the log they collect back. 44 async function getLog(w) { 45 w.postMessage("getlog", "*"); 46 let log = await new Promise( r=> { 47 window.addEventListener("message", function handler(e) { 48 window.removeEventListener("message", handler); 49 r(e.data); 50 }); 51 }); 52 return log; 53 } 54 55 async function runTest(t, urlForFrame1, urlForFrame2) { 56 const iframe1 = document.createElement("iframe"); 57 iframe1.src = urlForFrame1; 58 const iframe2 = document.createElement("iframe"); 59 iframe2.src = urlForFrame2; 60 document.body.appendChild(iframe1); 61 document.body.appendChild(iframe2); 62 63 t.add_cleanup(() => { iframe1.remove(); iframe2.remove() }); 64 65 await waitForBothIFramesLoaded(); 66 iframe1.focus(); 67 await waitForIframeFocus(iframe1.contentWindow); 68 // Ask the iframe2 to be focused by using test_driver to simulate 69 // a click. 70 clickFocusIFrame(iframe2.contentWindow); 71 await waitForIframeFocus(iframe2.contentWindow); 72 assert_equals(await getLog(iframe1.contentWindow), 'innerlog:windowfocus,windowblur,'); 73 } 74 75 // Both inner frames are different sites. 76 promise_test(async t => { 77 await runTest( 78 t, 79 "http://{{hosts[alt][www]}}:{{ports[http][0]}}/focus/support/focus-event-after-switching-iframes-inner.html", 80 "http://{{hosts[alt][www]}}:{{ports[http][0]}}/focus/support/focus-event-after-switching-iframes-inner.html"); 81 }, "Check focus and blur events after switching from a different site iframe to a different site iframe"); 82 83 // The one that expects to focus and blur is same site, the other one is different site. 84 promise_test(async t => { 85 await runTest( 86 t, 87 "/focus/support/focus-event-after-switching-iframes-inner.html", 88 "http://{{hosts[alt][www]}}:{{ports[http][0]}}/focus/support/focus-event-after-switching-iframes-inner.html"); 89 }, "Check focus and blur events after switching from a same site iframe to a different site iframe"); 90 91 // Both inner frames are same site 92 promise_test(async t => { 93 await runTest( 94 t, 95 "/focus/support/focus-event-after-switching-iframes-inner.html", 96 "/focus/support/focus-event-after-switching-iframes-inner.html"); 97 }, "Check focus and blur events after switching from a same site iframe to a same site iframe"); 98 99 // The one that expects to focus and blur is different site, the other one is same site 100 promise_test(async t => { 101 await runTest( 102 t, 103 "http://{{hosts[alt][www]}}:{{ports[http][0]}}/focus/support/focus-event-after-switching-iframes-inner.html", 104 "/focus/support/focus-event-after-switching-iframes-inner.html"); 105 }, "Check focus and blur events after switching from a different site iframe to a same site iframe"); 106 </script>