OLD | NEW |
(Empty) | |
| 1 <html> |
| 2 <head> |
| 3 <script src="../../../resources/js-test.js"></script> |
| 4 </head> |
| 5 <body> |
| 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> |
| 10 <script> |
| 11 window.jsTestIsAsync = true; |
| 12 |
| 13 if (window.testRunner) { |
| 14 testRunner.setCloseRemainingWindowsWhenComplete(true); |
| 15 testRunner.setCanOpenWindows(); |
| 16 testRunner.setPopupBlockingEnabled(true); |
| 17 } |
| 18 |
| 19 var testSelf = true; |
| 20 var consumeInFrame = false; // where to consume user gesture: self or frame |
| 21 var needClickDiv = false; |
| 22 var message; |
| 23 |
| 24 function onMessageSelf(event) { |
| 25 message.count++; |
| 26 if (message.count < message.bounceTotal) |
| 27 window.postMessage({}, "*"); // stack postMessages at once |
| 28 |
| 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 co
nsumed |
| 32 |
| 33 if (message.count >= message.bounceTotal) { |
| 34 if (message.consumeCount == 1) |
| 35 testPassed("Stack postMessages on self window and the user gesture i
s only consumed once"); |
| 36 else |
| 37 testFailed("Stack postMessages on self window and the user gesture i
s consumed " + message.consumeCount + " times"); |
| 38 |
| 39 testSelf = false; |
| 40 window.onmessage = onMessageFrame; |
| 41 message = null; |
| 42 // Can not clickDiv() because current (consumed) user gesture token will
be used. |
| 43 needClickDiv = true; |
| 44 } |
| 45 } |
| 46 |
| 47 function onMessageFrame(event) { |
| 48 var msg = event.data; |
| 49 msg.count++; |
| 50 |
| 51 if (!msg.consumeInFrame && |
| 52 msg.count >= msg.consumeIndex && |
| 53 window.open("about:blank", Math.random())) |
| 54 msg.consumeCount++; |
| 55 |
| 56 if (msg.count < msg.bounceTotal) |
| 57 document.getElementById("subframe").contentWindow.postMessage(msg, "*"); |
| 58 else { |
| 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")); |
| 63 else |
| 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")); |
| 67 |
| 68 if (!consumeInFrame) { |
| 69 consumeInFrame = true; |
| 70 needClickDiv = true; |
| 71 } else |
| 72 finishJSTest(); |
| 73 } |
| 74 } |
| 75 |
| 76 function clickHandler() { |
| 77 if (testSelf) |
| 78 window.postMessage({}, "*"); |
| 79 else |
| 80 document.getElementById("subframe").contentWindow.postMessage({ |
| 81 count: 0, |
| 82 bounceTotal: 60, |
| 83 consumeIndex: 50, // Consume user gesture from the 50th bounce |
| 84 consumeCount: 0, |
| 85 consumeInFrame: consumeInFrame |
| 86 }, "*"); |
| 87 } |
| 88 |
| 89 function clickDiv() { |
| 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(); |
| 95 } |
| 96 } |
| 97 |
| 98 function startTest() { |
| 99 window.onmessage = onMessageSelf; |
| 100 message = { |
| 101 count: 0, |
| 102 bounceTotal: 60, |
| 103 consumeIndex: 50, |
| 104 consumeCount: 0 |
| 105 }; |
| 106 needClickDiv = true; |
| 107 |
| 108 window.setInterval(function() { |
| 109 if (needClickDiv) { |
| 110 needClickDiv = false; |
| 111 clickDiv(); |
| 112 } |
| 113 }, 20); |
| 114 } |
| 115 |
| 116 </script> |
| 117 </body> |
| 118 </html> |
OLD | NEW |