tor-browser

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

test_useractivation_transient_consuming.html (6912B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <title>User activation test: consume 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 <iframe></iframe>
     11 <iframe></iframe>
     12 <script>
     13 
     14 SimpleTest.requestFlakyTimeout("Timeouts are needed to test transient user_activation");
     15 
     16 let timeout = SpecialPowers.getIntPref("dom.user_activation.transient.timeout") + 1000;
     17 let [iframe0, iframe1] = document.querySelectorAll("iframe");
     18 
     19 function waitForEvent(aTarget, aEvent, aCallback) {
     20  return new Promise((aResolve) => {
     21    aTarget.addEventListener(aEvent, function listener(event) {
     22      aCallback(event);
     23      aResolve();
     24    }, { once: true });
     25  });
     26 }
     27 
     28 function doCheck(aDocument, aName, aHasBeenUserGestureActivated,
     29                 aHasValidTransientUserGestureActivation,
     30                 aLastUserGestureTimeStamp) {
     31  is(SpecialPowers.wrap(aDocument).hasBeenUserGestureActivated,
     32     aHasBeenUserGestureActivated,
     33     `check has-been-user-activated on the ${aName}`);
     34  is(SpecialPowers.wrap(aDocument).hasValidTransientUserGestureActivation,
     35     aHasValidTransientUserGestureActivation,
     36     `check has-valid-transient-user-activation on the ${aName}`);
     37  is(SpecialPowers.wrap(aDocument).lastUserGestureTimeStamp,
     38     aLastUserGestureTimeStamp,
     39     `check last-user-gesture-timestamp on the ${aName}`);
     40 }
     41 
     42 add_task(async function checkInitialStatus() {
     43  doCheck(document, "top-level document", false, false, 0);
     44  ok(!SpecialPowers.wrap(document).consumeTransientUserGestureActivation(),
     45     "consume transient-user-activation on top-level document");
     46 
     47  doCheck(frames[0].document, "first iframe", false, false, 0);
     48  ok(!SpecialPowers.wrap(frames[0].document).consumeTransientUserGestureActivation(),
     49     "consume transient-user-activation on first iframe");
     50 
     51  doCheck(frames[1].document, "second iframe", false, false, 0);
     52  ok(!SpecialPowers.wrap(frames[1].document).consumeTransientUserGestureActivation(),
     53     "consume transient-user-activation on second iframe");
     54 });
     55 
     56 add_task(async function consumeTransientUserActivation() {
     57  // Trigger user activation on the first iframe.
     58  SpecialPowers.wrap(frames[0].document).notifyUserGestureActivation();
     59  let lastTimeStampTop = SpecialPowers.wrap(document).lastUserGestureTimeStamp;
     60  let lastTimeStampFirst = SpecialPowers.wrap(frames[0].document).lastUserGestureTimeStamp;
     61 
     62  // Try to consume transient user activation.
     63  ok(!SpecialPowers.wrap(frames[1].document).consumeTransientUserGestureActivation(),
     64     "consume transient-user-activation on second iframe");
     65  ok(SpecialPowers.wrap(frames[0].document).consumeTransientUserGestureActivation(),
     66     "consume transient-user-activation on first iframe");
     67  // Consuming a transient-user-activation should affect all tree.
     68  ok(!SpecialPowers.wrap(document).consumeTransientUserGestureActivation(),
     69     "consume transient-user-activation on top-level document");
     70 
     71  // Check has-valid-transient-user-activation and should not affect
     72  // has-been-user-activated
     73  doCheck(document, "top-level document", true, false, lastTimeStampTop);
     74  doCheck(frames[0].document, "first iframe", true, false, lastTimeStampFirst);
     75  doCheck(frames[1].document, "second iframe", false, false, 0);
     76 });
     77 
     78 add_task(async function consumeTransientUserActivationTimeout() {
     79  // Trigger user activation on the first iframe.
     80  SpecialPowers.wrap(frames[0].document).notifyUserGestureActivation();
     81 
     82  // Should not able to consume successfully after timeout.
     83  await new Promise((aResolve) => {
     84    setTimeout(() => {
     85      ok(!SpecialPowers.wrap(document).consumeTransientUserGestureActivation(),
     86         "consume transient-user-activation on top-level document");
     87      ok(!SpecialPowers.wrap(frames[0].document).consumeTransientUserGestureActivation(),
     88         "consume transient-user-activation on first iframe");
     89      ok(!SpecialPowers.wrap(frames[1].document).consumeTransientUserGestureActivation(),
     90         "consume transient-user-activation on second iframe");
     91      aResolve();
     92    }, timeout);
     93  });
     94 
     95  // Trigger user activation again.
     96  SpecialPowers.wrap(frames[0].document).notifyUserGestureActivation();
     97 
     98  // Try to consume transient user activation.
     99  ok(!SpecialPowers.wrap(frames[1].document).consumeTransientUserGestureActivation(),
    100     "consume transient-user-activation on second iframe");
    101  ok(SpecialPowers.wrap(frames[0].document).consumeTransientUserGestureActivation(),
    102     "consume transient-user-activation on first iframe");
    103  // Consuming a transient-user-activation should affect all tree.
    104  ok(!SpecialPowers.wrap(document).consumeTransientUserGestureActivation(),
    105     "consume transient-user-activation on top-level document");
    106 });
    107 
    108 add_task(async function iframeNavigation() {
    109  // Trigger user activation on the first iframe.
    110  SpecialPowers.wrap(frames[0].document).notifyUserGestureActivation();
    111 
    112  // Navigate away from current page.
    113  frames[0].frameElement.src = "file_empty.html";
    114  await waitForEvent(frames[0].frameElement, "load", () => {});
    115 
    116  // Try to consume transient user activation.
    117  ok(!SpecialPowers.wrap(frames[1].document).consumeTransientUserGestureActivation(),
    118     "consume transient-user-activation on second iframe");
    119  ok(!SpecialPowers.wrap(frames[0].document).consumeTransientUserGestureActivation(),
    120     "consume transient-user-activation on first iframe");
    121  ok(SpecialPowers.wrap(document).consumeTransientUserGestureActivation(),
    122     "consume transient-user-activation on top-level document");
    123 });
    124 
    125 add_task(async function triggerUserActivationOnCrossOriginFrame() {
    126  // Reset the activation flag.
    127  SpecialPowers.wrap(document).clearUserGestureActivation();
    128 
    129  // load cross-origin test page on iframe.
    130  frames[0].frameElement.src = "https://example.com/tests/dom/base/test/useractivation/file_iframe_consume_user_activation.html";
    131  await waitForEvent(window, "message", (event) => {
    132    if (event.data === "done") {
    133      ok(!SpecialPowers.wrap(document).consumeTransientUserGestureActivation(),
    134         "consume transient-user-activation on top-level document");
    135      ok(!SpecialPowers.wrap(document).hasValidTransientUserGestureActivation,
    136         "check has-valid-transient-user-activation on the top-level document");
    137      ok(!SpecialPowers.wrap(frames[1].document).hasValidTransientUserGestureActivation,
    138         "check has-valid-transient-user-activation on the second iframe");
    139    } else {
    140      ok(false, "receive unexpected message: " + event.data);
    141    }
    142  });
    143 });
    144 
    145 add_task(async function endTests() {
    146  // Reset the activation flag in order not to interfere following test in the
    147  // verify mode which would run the test using same document couple times.
    148  SpecialPowers.wrap(document).clearUserGestureActivation();
    149 });
    150 
    151 </script>
    152 </body>