| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 suite('cr-dialog', function() { | 5 suite('cr-dialog', function() { |
| 6 function pressEnter(element) { |
| 7 MockInteractions.keyEventOn(element, 'keypress', 13, undefined, 'Enter'); |
| 8 } |
| 9 |
| 6 setup(function() { | 10 setup(function() { |
| 7 PolymerTest.clearBody(); | 11 PolymerTest.clearBody(); |
| 8 }); | 12 }); |
| 9 | 13 |
| 10 test('focuses title on show', function() { | 14 test('focuses title on show', function() { |
| 11 document.body.innerHTML = ` | 15 document.body.innerHTML = ` |
| 12 <dialog is="cr-dialog"> | 16 <dialog is="cr-dialog"> |
| 13 <div class="title">title</div> | 17 <div class="title">title</div> |
| 14 <div class="body"><button>button</button></div> | 18 <div class="body"><button>button</button></div> |
| 15 </dialog>`; | 19 </dialog>`; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 37 </dialog>`; | 41 </dialog>`; |
| 38 | 42 |
| 39 var dialog = document.body.querySelector('dialog'); | 43 var dialog = document.body.querySelector('dialog'); |
| 40 var actionButton = document.body.querySelector('.action-button'); | 44 var actionButton = document.body.querySelector('.action-button'); |
| 41 | 45 |
| 42 dialog.showModal(); | 46 dialog.showModal(); |
| 43 | 47 |
| 44 // MockInteractions triggers event listeners synchronously. | 48 // MockInteractions triggers event listeners synchronously. |
| 45 var clickedCounter = 0; | 49 var clickedCounter = 0; |
| 46 actionButton.addEventListener('click', function() { | 50 actionButton.addEventListener('click', function() { |
| 47 clickedCounter += 1; | 51 clickedCounter++; |
| 48 }); | 52 }); |
| 49 | 53 |
| 50 // Enter keys on other buttons should be ignored. | 54 // Enter keys on other buttons should be ignored. |
| 51 clickedCounter = 0; | 55 clickedCounter = 0; |
| 52 var otherButton = document.body.querySelector('#other-button'); | 56 var otherButton = document.body.querySelector('#other-button'); |
| 53 assertTrue(!!otherButton); | 57 assertTrue(!!otherButton); |
| 54 MockInteractions.keyEventOn( | 58 pressEnter(otherButton); |
| 55 otherButton, 'keypress', 13, undefined, 'Enter'); | |
| 56 assertEquals(0, clickedCounter); | 59 assertEquals(0, clickedCounter); |
| 57 | 60 |
| 58 // Enter key on the action button should only fire the click handler once. | 61 // Enter key on the action button should only fire the click handler once. |
| 59 MockInteractions.tap(actionButton, 'keypress', 13, undefined, 'Enter'); | 62 MockInteractions.tap(actionButton, 'keypress', 13, undefined, 'Enter'); |
| 60 assertEquals(1, clickedCounter); | 63 assertEquals(1, clickedCounter); |
| 61 }); | 64 }); |
| 62 | 65 |
| 63 test('enter keys find the first non-hidden non-disabled button', function() { | 66 test('enter keys find the first non-hidden non-disabled button', function() { |
| 64 document.body.innerHTML = ` | 67 document.body.innerHTML = ` |
| 65 <dialog is="cr-dialog"> | 68 <dialog is="cr-dialog"> |
| (...skipping 13 matching lines...) Expand all Loading... |
| 79 | 82 |
| 80 // MockInteractions triggers event listeners synchronously. | 83 // MockInteractions triggers event listeners synchronously. |
| 81 hiddenButton.addEventListener('click', function() { | 84 hiddenButton.addEventListener('click', function() { |
| 82 assertNotReached('Hidden button received a click.'); | 85 assertNotReached('Hidden button received a click.'); |
| 83 }) | 86 }) |
| 84 var clicked = false; | 87 var clicked = false; |
| 85 actionButton.addEventListener('click', function() { | 88 actionButton.addEventListener('click', function() { |
| 86 clicked = true; | 89 clicked = true; |
| 87 }); | 90 }); |
| 88 | 91 |
| 89 MockInteractions.keyEventOn(dialog, 'keypress', 13, undefined, 'Enter'); | 92 pressEnter(dialog); |
| 90 assertTrue(clicked); | 93 assertTrue(clicked); |
| 91 }); | 94 }); |
| 92 | 95 |
| 96 test('enter keys from paper-inputs (only) are processed', function() { |
| 97 document.body.innerHTML = ` |
| 98 <dialog is="cr-dialog"> |
| 99 <div class="title">title</div> |
| 100 <div class="body"> |
| 101 <paper-input></paper-input> |
| 102 <foobar></foobar> |
| 103 <button class="action-button">active</button> |
| 104 </div> |
| 105 </dialog>`; |
| 106 |
| 107 var dialog = document.body.querySelector('dialog'); |
| 108 |
| 109 var inputElement = document.body.querySelector('paper-input'); |
| 110 var otherElement = document.body.querySelector('foobar'); |
| 111 var actionButton = document.body.querySelector('.action-button'); |
| 112 assertTrue(!!inputElement); |
| 113 assertTrue(!!otherElement); |
| 114 assertTrue(!!actionButton); |
| 115 |
| 116 // MockInteractions triggers event listeners synchronously. |
| 117 var clickedCounter = 0; |
| 118 actionButton.addEventListener('click', function() { |
| 119 clickedCounter++; |
| 120 }); |
| 121 |
| 122 pressEnter(otherElement); |
| 123 assertEquals(0, clickedCounter); |
| 124 |
| 125 pressEnter(inputElement); |
| 126 assertEquals(1, clickedCounter); |
| 127 }); |
| 128 |
| 93 test('focuses [autofocus] instead of title when present', function() { | 129 test('focuses [autofocus] instead of title when present', function() { |
| 94 document.body.innerHTML = ` | 130 document.body.innerHTML = ` |
| 95 <dialog is="cr-dialog"> | 131 <dialog is="cr-dialog"> |
| 96 <div class="title">title</div> | 132 <div class="title">title</div> |
| 97 <div class="body"><button autofocus>button</button></div> | 133 <div class="body"><button autofocus>button</button></div> |
| 98 </dialog>`; | 134 </dialog>`; |
| 99 | 135 |
| 100 var dialog = document.body.querySelector('dialog'); | 136 var dialog = document.body.querySelector('dialog'); |
| 101 var button = document.body.querySelector('button'); | 137 var button = document.body.querySelector('button'); |
| 102 | 138 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 } | 172 } |
| 137 }); | 173 }); |
| 138 observer.observe(bodyContainer, {attributes: true}); | 174 observer.observe(bodyContainer, {attributes: true}); |
| 139 | 175 |
| 140 // Height is normally set via CSS, but mixin doesn't work with innerHTML. | 176 // Height is normally set via CSS, but mixin doesn't work with innerHTML. |
| 141 bodyContainer.style.height = '1px'; | 177 bodyContainer.style.height = '1px'; |
| 142 bodyContainer.scrollTop = 100; | 178 bodyContainer.scrollTop = 100; |
| 143 dialog.showModal(); // Attach the dialog for the first time here. | 179 dialog.showModal(); // Attach the dialog for the first time here. |
| 144 }); | 180 }); |
| 145 }); | 181 }); |
| OLD | NEW |