| <!DOCTYPE html> |
| <html> |
| <head> |
| <style> |
| #bottom { |
| top: 100px; |
| left: 100px; |
| height: 300px; |
| width: 300px; |
| margin: 0; |
| background: cyan; |
| } |
| |
| #top { |
| top: 150px; |
| left: 150px; |
| height: 200px; |
| width: 200px; |
| margin: 0; |
| background: yellow; |
| } |
| </style> |
| <script src="../../../resources/js-test.js"></script> |
| </head> |
| <body> |
| <dialog id="bottom"> |
| <span></span> |
| <div>You can't Escape when this textbox has focus: <input id="swallow-input" type="text"></div> |
| <div>You can Escape even if this textbox has focus: <input id="normal-input" type="text"></div> |
| </dialog> |
| <dialog id="top"> |
| <span></span> |
| </dialog> |
| <script> |
| description('Tests canceling modal dialogs using the Escape key. ' + |
| 'To test manually, hit Escape once to see the topmost dialog turn green, ' + |
| 'then once again to close it. Repeat for the remaining dialog.'); |
| |
| function handleCancel(event) { |
| this.style.background = 'green'; |
| this.querySelector('span').textContent = 'I blocked the cancel! Try again to close me.'; |
| event.preventDefault(); |
| this.removeEventListener('cancel', handleCancel); |
| } |
| |
| function test() { |
| bottomDialog = document.getElementById('bottom'); |
| bottomDialog.addEventListener('cancel', handleCancel); |
| |
| topDialog = document.getElementById('top'); |
| topDialog.addEventListener('cancel', handleCancel); |
| |
| normalInput = document.getElementById('normal-input'); |
| swallowInput = document.getElementById('swallow-input'); |
| swallowInput.addEventListener('keydown', function(event) { |
| event.preventDefault(); |
| }); |
| |
| bottomDialog.showModal(); |
| topDialog.showModal(); |
| |
| if (!window.eventSender) |
| return; |
| |
| debug('Top dialog event listener should prevent closing.'); |
| eventSender.keyDown("escape"); |
| shouldBeTrue('topDialog.open'); |
| shouldBeTrue('bottomDialog.open'); |
| |
| debug('Top dialog should close.'); |
| eventSender.keyDown("escape"); |
| shouldBeFalse('topDialog.open'); |
| shouldBeTrue('bottomDialog.open'); |
| |
| debug('Input should swallow Escape mechanism.'); |
| swallowInput.focus(); |
| eventSender.keyDown("escape"); |
| eventSender.keyDown("escape"); |
| eventSender.keyDown("escape"); |
| shouldBeFalse('topDialog.open'); |
| shouldBeTrue('bottomDialog.open'); |
| |
| normalInput.focus(); |
| debug('Bottom dialog event listener should prevent closing.'); |
| eventSender.keyDown("escape"); |
| shouldBeFalse('topDialog.open'); |
| shouldBeTrue('bottomDialog.open'); |
| |
| debug('Bottom dialog should close.'); |
| eventSender.keyDown("escape"); |
| shouldBeFalse('topDialog.open'); |
| shouldBeFalse('bottomDialog.open'); |
| |
| debug('Pressing Escape now should do nothing.'); |
| eventSender.keyDown("escape"); |
| shouldBeFalse('topDialog.open'); |
| shouldBeFalse('bottomDialog.open'); |
| |
| bottomDialog.remove(); |
| topDialog.remove(); |
| } |
| |
| test(); |
| </script> |
| </body> |
| </html> |