tor-browser

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

focus-without-user-activation-disabled-tentative.html (2300B)


      1 <!DOCTYPE html>
      2 <script src="/resources/testharness.js"></script>
      3 <script src="/resources/testharnessreport.js"></script>
      4 <script src="./resources/common.js"></script>
      5 <title> 'focus-without-user-activation' Policy : Correctly block automatic focus when policy disabled
      6 </title>
      7 <body>
      8 <script>
      9  "use strict"
     10  // Note: According to html spec: https://html.spec.whatwg.org/#attr-fe-autofocus,
     11  // topDocument's autofocus processed flag initially is false and is set to true
     12  // after flushing autofocus candidates, i.e. flush of autofocus candidates
     13  // only happens once per page load.
     14  // In order to test the behaviour with both focus-without-user-activation on and off:
     15  // two test files are necessary:
     16  // - focus-without-user-activation-disabled-tentative.html
     17  // - focus-without-user-activation-enabled-tentative.sub.html
     18 
     19  // Use same origin url here because when iframe document has cross origin
     20  // url, autofocus will be blocked by default with following console error:
     21  // "Blocked autofocusing on a form control in a cross-origin subframe."
     22  const url = "/feature-policy/experimental-features/resources/focus-without-user-activation-iframe-tentative.html";
     23 
     24  function subframe_focused(subframe, event_name, timeout) {
     25    return new Promise(resolve => {
     26      window.onmessage = m => resolve(m.data.focused);
     27      subframe.contentWindow.postMessage({
     28        event: event_name,
     29        timeout: timeout
     30      }, "*");
     31    });
     32  }
     33 
     34  promise_test( async (instance) => {
     35    const frame = createIframe(document.body, {
     36      sandbox: "allow-scripts allow-same-origin",
     37      allow: "focus-without-user-activation 'none'",
     38      src: url
     39    });
     40 
     41    await wait_for_load(frame);
     42    assert_false(await subframe_focused(frame, "autofocus", 400), "'autofocus' should not work.");
     43    window.focus(); // Reset focus state in subframe.
     44    assert_false(await subframe_focused(frame, "focus-input", 400), "'element.focus' should not work.");
     45    window.focus(); // Reset focus state in subframe.
     46    assert_false(await subframe_focused(frame, "focus-window", 400), "'window.focus' should not work.");
     47    window.focus(); // Reset focus state in subframe.
     48  }, "When the policy is disabled, 'autofocus' and scripted focus do not focus " +
     49     "the document.");
     50 </script>
     51 </body>