tor-browser

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

test_popup_blocker_postmessage.html (1822B)


      1 <!DOCTYPE html>
      2 <html>
      3 <head>
      4  <meta charset="utf-8">
      5  <title>Test for triggering popup by postMessage</title>
      6  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      7  <script src="/tests/SimpleTest/EventUtils.js"></script>
      8  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
      9 </head>
     10 <body>
     11 <button id="target">click me</button>
     12 <script>
     13 function sendMouseEvent(element, eventName, button) {
     14  synthesizeMouseAtCenter(element, {type: eventName, button});
     15 }
     16 
     17 add_setup(async function() {
     18  // Deny popup permission.
     19  const DENY_ACTION = SpecialPowers.Ci.nsIPermissionManager.DENY_ACTION;
     20  let xorigin = SimpleTest.getTestFileURL("").replace(location.hostname, 'mochi.xorigin-test');
     21  await SpecialPowers.pushPermissions([
     22    {'type': 'popup', 'allow': DENY_ACTION,
     23     'context': document},
     24    {'type': 'popup', 'allow': DENY_ACTION,
     25     'context': xorigin}
     26  ]);
     27 
     28  await new Promise(resolve => SimpleTest.waitForFocus(resolve));
     29 });
     30 
     31 const LEFT_BUTTON = 0;
     32 const MIDDLE_BUTTON = 1;
     33 const RIGHT_BUTTON = 2;
     34 let target = document.getElementById("target");
     35 
     36 let waits = [];
     37 target.addEventListener("mouseup", () => {
     38  waits.push(Promise.withResolvers());
     39  window.postMessage({ openPopup: waits.length - 1 }, "*");
     40 });
     41 
     42 window.addEventListener("message", (e) => {
     43  if (e.data.openPopup != null) {
     44    let w = window.open("");
     45    ok(w, "Should allow popup");
     46    if (w) {
     47      w.close();
     48    }
     49 
     50    let w2 = window.open("");
     51    ok(!w2, "Should block another popup");
     52    if (w2) {
     53      w2.close();
     54    }
     55 
     56    waits[e.data.openPopup].resolve();
     57  }
     58 });
     59 
     60 
     61 add_task(async function testMouseInitiated() {
     62  sendMouseEvent(target, "mousedown", LEFT_BUTTON);
     63  sendMouseEvent(target, "mouseup", LEFT_BUTTON);
     64 
     65  await Promise.all(waits.map(x => x.promise));
     66 });
     67 </script>
     68 </body>
     69 </html>