tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

activeelement-after-calling-window-focus.sub.html (3619B)


      1 <!doctype html>
      2 <meta charset=utf-8>
      3 <title>activeElement after calling window.focus()</title>
      4 <meta name="timeout" content="long">
      5 <script src=/resources/testharness.js></script>
      6 <script src=/resources/testharnessreport.js></script>
      7 <script>
      8 function waitForEvent(target, event, checkFn) {
      9  return new Promise(resolve => {
     10    target.addEventListener(event, e => {
     11      if (checkFn && !checkFn(e)) {
     12        return;
     13      }
     14      resolve();
     15    }, { once: true });
     16  });
     17 }
     18 
     19 function start(w) {
     20  w.postMessage("start", "*");
     21 }
     22 
     23 function focusInnerInput(w) {
     24  w.postMessage("focusinnerinput", "*");
     25 }
     26 
     27 function focusInnerFrame(w) {
     28  w.postMessage("focusinner", "*");
     29 }
     30 
     31 function focusMiddleFrame(w) {
     32  w.postMessage("focusmiddle", "*");
     33 }
     34 
     35 function focusOuterFrame(w) {
     36  w.postMessage("focusouter", "*");
     37 }
     38 
     39 // This will send message to outer frame and also inner frame to ask them
     40 // send the log they collect back, the logs of outer and inner will be
     41 // concatenated.
     42 async function getLog(w) {
     43  let log = "";
     44  step_timeout(function() {
     45    w.postMessage("getlog", "*");
     46  }, 0);
     47  await waitForEvent(window, "message", (e) => {
     48    log = e.data;
     49    return true;
     50  });
     51  return log;
     52 }
     53 
     54 async function runTest(t, url) {
     55  let w = window.open(url);
     56  t.add_cleanup(() => { w.close(); });
     57  await waitForEvent(window, "message", e => e.data === "ready");
     58  start(w);
     59  // Calling input.focus() on inner iframe should move its document.activeElement to INPUT.
     60  focusInnerInput(w);
     61  assert_equals(await getLog(w), 'outerlog:windowblur,middlelog:innerlog:windowfocus,INPUT,');
     62  // Calling window.focus() on inner iframe should NOT reset its document.activeElement to BODY
     63  // because it's focused element is not an iframe.
     64  focusInnerFrame(w);
     65  assert_equals(await getLog(w), 'outerlog:windowblur,middlelog:innerlog:windowfocus,INPUT,INPUT,');
     66  // Calling window.focus() on middle iframe should reset its document.activeElement to BODY.
     67  focusMiddleFrame(w);
     68  assert_equals(await getLog(w), 'outerlog:windowblur,middlelog:windowfocus,BODY,innerlog:windowfocus,INPUT,INPUT,windowblur,');
     69  // Calling window.focus() on top-level frame should reset its document.activeElement to BODY.
     70  focusOuterFrame(w);
     71  assert_equals(await getLog(w), 'outerlog:windowblur,windowfocus,BODY,middlelog:windowfocus,BODY,windowblur,innerlog:windowfocus,INPUT,INPUT,windowblur,');
     72 }
     73 
     74 promise_test(async t => {
     75  await runTest(t, "https://{{hosts[][www]}}:{{ports[https][0]}}/focus/support/activeelement-after-calling-window-focus-outer-same.sub.html");
     76 }, "Tests for all frames are in same origin");
     77 
     78 promise_test(async t => {
     79  await runTest(t, "https://{{hosts[alt][www]}}:{{ports[https][0]}}/focus/support/activeelement-after-calling-window-focus-outer-same.sub.html");
     80 }, "Tests for middle frame and inner frame are in same origin and outer frame is in different origin");
     81 
     82 promise_test(async t => {
     83  await runTest(t, "https://{{hosts[alt][www]}}:{{ports[https][0]}}/focus/support/activeelement-after-calling-window-focus-outer-different.sub.html");
     84 }, "Tests for outer frame and middle frame are in same origin and inner frame is in different origin");
     85 
     86 promise_test(async t => {
     87  await runTest(t, "https://{{hosts[][www]}}:{{ports[https][0]}}/focus/support/activeelement-after-calling-window-focus-outer-different.sub.html");
     88 }, "Tests for outer frame and inner frame are in same origin and middle frame is in different origin");
     89 
     90 promise_test(async t => {
     91  await runTest(t, "support/activeelement-after-calling-window-focus-outer-different.sub.html");
     92 }, "Tests for all frames are in different origin");
     93 </script>