summaryrefslogtreecommitdiffstats
path: root/dom/base/test/useractivation/test_useractivation_transient.html
blob: d3148e5d1d76b811f5bb23234dea46a80b53d92e (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<!DOCTYPE HTML>
<html>
<head>
  <title>User activation test: transient flag</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>
<iframe></iframe>
<iframe></iframe>
<script>

SimpleTest.requestFlakyTimeout("Timeouts are needed to test transient user_activation");

let timeout = SpecialPowers.getIntPref("dom.user_activation.transient.timeout") + 1000;
let [iframe0, iframe1] = document.querySelectorAll("iframe");

function waitForEvent(aTarget, aEvent, aCallback) {
  return new Promise((aResolve) => {
    aTarget.addEventListener(aEvent, function listener(event) {
      aCallback(event);
      aResolve();
    }, { once: true });
  });
}

add_task(async function checkInitialStatus() {
  ok(!SpecialPowers.wrap(document).hasValidTransientUserGestureActivation,
     "check has-valid-transient-user-activation on top-level document");
  ok(!SpecialPowers.wrap(frames[0].document).hasValidTransientUserGestureActivation,
     "check has-valid-transient-user-activation on first iframe");
  ok(!SpecialPowers.wrap(frames[1].document).hasValidTransientUserGestureActivation,
     "check has-valid-transient-user-activation on second iframe");
});

add_task(async function triggerUserActivation() {
  // Trigger user activation on the first iframe.
  SpecialPowers.wrap(frames[0].document).notifyUserGestureActivation();

  // We should also propagate to all the ancestors.
  ok(SpecialPowers.wrap(document).hasValidTransientUserGestureActivation,
     "check has-valid-transient-user-activation on the top-level document");
  ok(SpecialPowers.wrap(frames[0].document).hasValidTransientUserGestureActivation,
     "check has-valid-transient-user-activation on the first iframe");
  ok(!SpecialPowers.wrap(frames[1].document).hasValidTransientUserGestureActivation,
     "check has-valid-transient-user-activation on the second iframe");
});

add_task(async function iframeNavigation() {
  frames[0].frameElement.src = "file_empty.html";
  await waitForEvent(frames[0].frameElement, "load", () => {});
  // We should reset the flag on iframe that navigates away from current page,
  // but the flag on its ancestor isn't changed.
  ok(SpecialPowers.wrap(document).hasValidTransientUserGestureActivation,
     "check has-valid-transient-user-activation on the top-level document");
  ok(!SpecialPowers.wrap(frames[0].document).hasValidTransientUserGestureActivation,
     "check has-valid-transient-user-activation on the first iframe");
  ok(!SpecialPowers.wrap(frames[1].document).hasValidTransientUserGestureActivation,
     "check has-valid-transient-user-activation on the second iframe");
});

add_task(async function triggerUserActivationTimeout() {
  // Trigger user activation on the first iframe.
  SpecialPowers.wrap(frames[0].document).notifyUserGestureActivation();

  // hasValidTransientUserGestureActivation should return false after timeout.
  await new Promise((aResolve) => {
    setTimeout(() => {
      ok(!SpecialPowers.wrap(document).hasValidTransientUserGestureActivation,
         "check has-valid-transient-user-activation on the top-level document");
      ok(!SpecialPowers.wrap(frames[0].document).hasValidTransientUserGestureActivation,
         "check has-valid-transient-user-activation on the first iframe");
      ok(!SpecialPowers.wrap(frames[1].document).hasValidTransientUserGestureActivation,
         "check has-valid-transient-user-activation on the second iframe");
      aResolve();
    }, timeout);
  });

  // Trigger user activation again.
  SpecialPowers.wrap(frames[0].document).notifyUserGestureActivation();
  ok(SpecialPowers.wrap(document).hasValidTransientUserGestureActivation,
     "check has-valid-transient-user-activation on the top-level document");
  ok(SpecialPowers.wrap(frames[0].document).hasValidTransientUserGestureActivation,
     "check has-valid-transient-user-activation on the first iframe");
  ok(!SpecialPowers.wrap(frames[1].document).hasValidTransientUserGestureActivation,
     "check has-valid-transient-user-activation on the second iframe");
});

add_task(async function triggerUserActivationOnCrossOriginFrame() {
  // Reset the activation flag.
  SpecialPowers.wrap(document).clearUserGestureActivation();

  // load cross-origin test page on iframe.
  frames[0].frameElement.src = "https://example.com/tests/dom/base/test/useractivation/file_iframe_user_activated.html";
  await waitForEvent(window, "message", (event) => {
    if (event.data === "done") {
      ok(SpecialPowers.wrap(document).hasValidTransientUserGestureActivation,
         "check has-valid-transient-user-activation on the top-level document");
      ok(!SpecialPowers.wrap(frames[1].document).hasValidTransientUserGestureActivation,
         "check has-valid-transient-user-activation on the second iframe");
    } else {
      ok(false, "receive unexpected message: " + event.data);
    }
  });

  // hasValidTransientUserGestureActivation should return false after timeout.
  await new Promise((aResolve) => {
    setTimeout(() => {
      ok(!SpecialPowers.wrap(document).hasValidTransientUserGestureActivation,
         "check has-valid-transient-user-activation on the top-level document");
      ok(!SpecialPowers.wrap(frames[1].document).hasValidTransientUserGestureActivation,
         "check has-valid-transient-user-activation on the second iframe");
      aResolve();
    }, timeout);
  });
});

add_task(async function propagateToSameOriginConnectedSubframe() {
  // Reset the activation flag.
  SpecialPowers.wrap(document).clearUserGestureActivation();

  // load cross-origin test page on iframe.
  iframe0.src = "https://example.com/tests/dom/base/test/useractivation/file_iframe_check_user_activation.html";
  await waitForEvent(window, "message", (event) => {
    if (event.data !== "loaded") {
      ok(false, "receive unexpected message: " + event.data);
    }
  });

  // Trigger user activation on top-level document.
  SpecialPowers.wrap(document).notifyUserGestureActivation();
  ok(SpecialPowers.wrap(document).hasValidTransientUserGestureActivation,
     "check has-valid-transient-user-activation on the top-level document");
  ok(SpecialPowers.wrap(iframe1.contentDocument).hasValidTransientUserGestureActivation,
     "check has-valid-transient-user-activation on the second iframe");

  iframe0.contentWindow.postMessage("get", "*");
  await waitForEvent(window, "message", (event) => {
    if (typeof event.data === "object") {
      ok(!event.data.isActivated, "check has-valid-transient-user-activation on the first iframe");
    } else {
      ok(false, "receive unexpected message: " + event.data);
    }
  });
});

add_task(async function endTests() {
  // Reset the activation flag in order not to interfere following test in the
  // verify mode which would run the test using same document couple times.
  SpecialPowers.wrap(document).clearUserGestureActivation();
});

</script>
</body>