tor-browser

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

test_useractivation_sandbox_transient.html (3192B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <title>User activation test: transient flag</title>
      5  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      6  <script src="/tests/SimpleTest/EventUtils.js"></script>
      7  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
      8 </head>
      9 <body>
     10 <script>
     11 SimpleTest.requestFlakyTimeout("Timeouts are needed to test transient user_activation");
     12 let timeout = SpecialPowers.getIntPref("dom.user_activation.transient.timeout") + 1000;
     13 
     14 function waitForEvent(aTarget, aEvent, aCallback) {
     15  return new Promise((aResolve) => {
     16    aTarget.addEventListener(aEvent, function listener(event) {
     17      aCallback(event);
     18      aResolve();
     19    }, { once: true });
     20  });
     21 }
     22 
     23 function checkPopupActivated(popup, activated) {
     24  let state = activated ? "valid" : "invalid";
     25  ok(activated === SpecialPowers.wrap(popup.document).hasValidTransientUserGestureActivation,
     26     `check has-${state}-transient-user-activation on top-level document`);
     27  ok(activated === SpecialPowers.wrap(popup.frames[0].document).hasValidTransientUserGestureActivation,
     28     `check has-${state}-transient-user-activation on iframe`);
     29 }
     30 
     31 add_task(async function triggerUserActivation() {
     32  let message = waitForEvent(window, "message", (e) => {
     33    if (e.data === "topNavigation") {
     34      ok(true, "Top navigation changed");
     35    } else {
     36      ok(false, "Unexpected message");
     37    }
     38  });
     39  let popup = window.open("./file_useractivation_sandbox_transient_popup.html", "_blank");
     40  await new Promise((r) => {
     41    popup.addEventListener("load", r);
     42  });
     43  checkPopupActivated(popup, false);
     44  // Create user gesture into iframe within popup just opened
     45  SpecialPowers.wrap(popup.frames[0].frameElement.contentDocument).notifyUserGestureActivation();
     46  checkPopupActivated(popup, true);
     47  // Notify popup to load into the frame
     48  popup.postMessage("triggerIframeLoad");
     49  await message;
     50  popup.close();
     51 });
     52 
     53 add_task(async function triggerUserActivationTimeout() {
     54  let message = waitForEvent(window, "message", (e) => {
     55    // Top navigation MUST be blocked
     56    if (e.data === "topNavigationBlocked") {
     57      ok(true, "Top navigation blocked");
     58    } else {
     59      ok(false, "Unexpected message");
     60    }
     61  });
     62  let popup = window.open("./file_useractivation_sandbox_transient_popup.html", "_blank");
     63  await new Promise((r) => {
     64    popup.addEventListener("load", r);
     65  });
     66  checkPopupActivated(popup, false);
     67  // Create user gesture into iframe within popup just opened
     68  SpecialPowers.wrap(popup.frames[0].frameElement.contentDocument).notifyUserGestureActivation();
     69  checkPopupActivated(popup, true);
     70  // Ensure we timeout user gesture
     71  await new Promise((aResolve) => {
     72    setTimeout(() => {
     73      checkPopupActivated(popup, false);
     74      aResolve();
     75    }, timeout);
     76  });
     77  // Notify popup to load into the frame
     78  popup.postMessage("triggerIframeLoad");
     79  await message;
     80  popup.close();
     81 });
     82 
     83 add_task(async function endTests() {
     84  // Reset the activation flag in order not to interfere following test in the
     85  // verify mode which would run the test using same document couple times.
     86  SpecialPowers.wrap(document).clearUserGestureActivation();
     87 });
     88 
     89 </script>
     90 </body>