3 <script src=
"../../../resources/js-test.js"></script>
6 <div id=
"description">Test that user gesture is kept when postMessage() to self or across frames.
</div>
7 <div id=
"div1" onclick=
"clickHandler()">Click me
</div>
8 <iframe id=
"subframe" src=
"resources/window-postmessage-user-gesture-frame.html" onload=
"startTest()"></iframe>
9 <div id=
"console"></div>
11 window
.jsTestIsAsync
= true;
13 if (window
.testRunner
) {
14 testRunner
.setCloseRemainingWindowsWhenComplete(true);
15 testRunner
.setCanOpenWindows();
16 testRunner
.setPopupBlockingEnabled(true);
20 var consumeInFrame
= false; // where to consume user gesture: self or frame
21 var needClickDiv
= false;
24 function onMessageSelf(event
) {
26 if (message
.count
< message
.bounceTotal
)
27 window
.postMessage({}, "*"); // stack postMessages at once
29 // Keep trying to consume user gesture after consumeIndex
30 if (message
.count
>= message
.consumeIndex
&& window
.open("about:blank", Math
.random()))
31 message
.consumeCount
++; // Log the times of which the user gesture is consumed
33 if (message
.count
>= message
.bounceTotal
) {
34 if (message
.consumeCount
== 1)
35 testPassed("Stack postMessages on self window and the user gesture is only consumed once");
37 testFailed("Stack postMessages on self window and the user gesture is consumed " + message
.consumeCount
+ " times");
40 window
.onmessage
= onMessageFrame
;
42 // Can not clickDiv() because current (consumed) user gesture token will be used.
47 function onMessageFrame(event
) {
51 if (!msg
.consumeInFrame
&&
52 msg
.count
>= msg
.consumeIndex
&&
53 window
.open("about:blank", Math
.random()))
56 if (msg
.count
< msg
.bounceTotal
)
57 document
.getElementById("subframe").contentWindow
.postMessage(msg
, "*");
59 if (msg
.consumeCount
== 1)
60 testPassed("Bounce postMessages between self and frame window and" +
61 " the user gesture is only consumed once " +
62 (consumeInFrame
? "in frame" : "in self window"));
64 testFailed("Bounce postMessages between self and frame window and" +
65 " the user gesture is consumed " + msg
.consumeCount
+ " times "+
66 (consumeInFrame
? "in frame" : "in self window"));
68 if (!consumeInFrame
) {
69 consumeInFrame
= true;
76 function clickHandler() {
78 window
.postMessage({}, "*");
80 document
.getElementById("subframe").contentWindow
.postMessage({
83 consumeIndex
: 50, // Consume user gesture from the 50th bounce
85 consumeInFrame
: consumeInFrame
90 var div1
= document
.getElementById("div1");
91 if (window
.eventSender
) {
92 eventSender
.mouseMoveTo(div1
.offsetLeft
+ 10, div1
.offsetTop
+ 5);
93 eventSender
.mouseDown();
94 eventSender
.mouseUp();
98 function startTest() {
99 window
.onmessage
= onMessageSelf
;
108 window
.setInterval(function() {
110 needClickDiv
= false;