summaryrefslogtreecommitdiffstats
path: root/dom/base/test/useractivation/test_popup_blocker_async_callback.html
blob: dc5359653158b91809616ecb5c53099c387f6150 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Test for triggering popup by mouse events</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<div id="target" style="width: 50px; height: 50px; background: green"></div>
<script>

SimpleTest.requestFlakyTimeout("Need to test setTimeout");

function startTest(aTestAsyncFun, aAllowPopup = true) {
  return new Promise(aResolve => {
    let target = document.getElementById("target");
    target.addEventListener("click", (e) => {
      aTestAsyncFun(() => {
        let w = window.open("about:blank");
        is(!!w, aAllowPopup, `Should ${aAllowPopup ? "allow" : "block"} popup`);
        if (w) {
          w.close();
        }
        aResolve();
      });
    }, {once: true});
    synthesizeMouseAtCenter(target, {type: "mousedown"});
    synthesizeMouseAtCenter(target, {type: "mouseup"});
  });
}

add_setup(async function() {
  await SpecialPowers.pushPrefEnv({"set": [
    ["dom.disable_open_during_load", true],
    ["dom.serviceWorkers.enabled", true],
    ["dom.serviceWorkers.testing.enabled", true],
  ]});
});

[
  // setTimeout
  function testSetTimout(aCallback) {
    setTimeout(aCallback, 500);
  },
  // fetch
  function testFetch(aCallback) {
    fetch("../dummy.html").then(aCallback);
  },
  // requestStorageAccess
  function testRequestStorageAccess(aCallback) {
    document.requestStorageAccess().then(aCallback);
  },
  // serviceWorker.getRegistration
  function testGetServiceWorkerRegistration(aCallback) {
    navigator.serviceWorker.getRegistration("/app").then(aCallback);
  },
].forEach(testAsyncFun => {
  add_task(async function() {
    info(`start ${testAsyncFun.name}`);
    SpecialPowers.wrap(document).clearUserGestureActivation();
    await startTest(testAsyncFun);
    await new Promise(aResolve => SimpleTest.executeSoon(aResolve));
  });
});

// Test popup should be blocked if user transient is timeout
add_task(async function timeout() {
  info(`start user transient timeout`);
  await SpecialPowers.pushPrefEnv({"set": [
    ["dom.user_activation.transient.timeout", 1000],
  ]});
  SpecialPowers.wrap(document).clearUserGestureActivation();
  await startTest((aCallback) => {
    setTimeout(aCallback, 2000);
  }, false);
  await new Promise(aResolve => SimpleTest.executeSoon(aResolve));
});

</script>
</body>
</html>