Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(613)

Side by Side Diff: chrome/test/data/webui/cr_elements/cr_dialog_test.js

Issue 2792853003: WebUI: For cr-dialog, make Enter key ignore disabled or hidden buttons (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | ui/webui/resources/cr_elements/cr_dialog/cr_dialog.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 setup(function() { 6 setup(function() {
7 PolymerTest.clearBody(); 7 PolymerTest.clearBody();
8 }); 8 });
9 9
10 test('focuses title on show', function() { 10 test('focuses title on show', function() {
(...skipping 29 matching lines...) Expand all
40 var actionButton = document.body.querySelector('.action-button'); 40 var actionButton = document.body.querySelector('.action-button');
41 41
42 dialog.showModal(); 42 dialog.showModal();
43 43
44 // MockInteractions triggers event listeners synchronously. 44 // MockInteractions triggers event listeners synchronously.
45 var clickedCounter = 0; 45 var clickedCounter = 0;
46 actionButton.addEventListener('click', function() { 46 actionButton.addEventListener('click', function() {
47 clickedCounter += 1; 47 clickedCounter += 1;
48 }); 48 });
49 49
50 // Enter key should ignore disabled buttons. 50 // Enter key should ignore disabled buttons.
dpapad 2017/04/04 17:10:17 Are lines 50-57 now redundant, meaning that the ne
tommycli 2017/04/04 17:44:10 Done.
51 MockInteractions.keyEventOn(dialog, 'keypress', 13, undefined, 'Enter'); 51 MockInteractions.keyEventOn(dialog, 'keypress', 13, undefined, 'Enter');
52 assertEquals(0, clickedCounter); 52 assertEquals(0, clickedCounter);
53 53
54 // Enter key should trigger enabled buttons. 54 // Enter key should trigger enabled buttons.
55 actionButton.disabled = false; 55 actionButton.disabled = false;
56 MockInteractions.keyEventOn(dialog, 'keypress', 13, undefined, 'Enter'); 56 MockInteractions.keyEventOn(dialog, 'keypress', 13, undefined, 'Enter');
57 assertEquals(1, clickedCounter); 57 assertEquals(1, clickedCounter);
58 58
59 // Enter keys on other buttons should be ignored. 59 // Enter keys on other buttons should be ignored.
60 clickedCounter = 0; 60 clickedCounter = 0;
61 var otherButton = document.body.querySelector('#other-button'); 61 var otherButton = document.body.querySelector('#other-button');
62 assertTrue(!!otherButton); 62 assertTrue(!!otherButton);
63 MockInteractions.keyEventOn( 63 MockInteractions.keyEventOn(
64 otherButton, 'keypress', 13, undefined, 'Enter'); 64 otherButton, 'keypress', 13, undefined, 'Enter');
65 assertEquals(0, clickedCounter); 65 assertEquals(0, clickedCounter);
66 66
67 // Enter key on the action button should only fire the click handler once. 67 // Enter key on the action button should only fire the click handler once.
68 MockInteractions.tap(actionButton, 'keypress', 13, undefined, 'Enter'); 68 MockInteractions.tap(actionButton, 'keypress', 13, undefined, 'Enter');
69 assertEquals(1, clickedCounter); 69 assertEquals(1, clickedCounter);
70 }); 70 });
71 71
72 test('enter ignores hidden and disabled action buttons', function() {
73 document.body.innerHTML = `
74 <dialog is="cr-dialog">
75 <div class="title">title</div>
76 <div class="body">
77 <button id="hidden" class="action-button" hidden>hidden</button>
78 <button class="action-button" disabled>disabled</button>
79 <button class="action-button" disabled hidden>disabled hidden</button>
80 <button id="active" class="action-button">active</button>
81 </div>
82 </dialog>`;
83
84 var dialog = document.body.querySelector('dialog');
85 var hiddenButton = document.body.querySelector('#hidden');
86 var actionButton = document.body.querySelector('#active');
87 dialog.showModal();
88
89 // MockInteractions triggers event listeners synchronously.
90 hiddenButton.addEventListener('click', function() {
91 throw new Error('Hidden button received a click.');
dpapad 2017/04/04 17:10:17 Nit: Can we use assertNotReached() instead? We act
tommycli 2017/04/04 17:44:11 Done. And I can confirm this fails the test if I r
92 })
93 var clicked = false;
94 actionButton.addEventListener('click', function() {
95 clicked = true;
96 });
97
98 MockInteractions.keyEventOn(dialog, 'keypress', 13, undefined, 'Enter');
99 assertTrue(clicked);
100 });
101
72 test('focuses [autofocus] instead of title when present', function() { 102 test('focuses [autofocus] instead of title when present', function() {
73 document.body.innerHTML = ` 103 document.body.innerHTML = `
74 <dialog is="cr-dialog"> 104 <dialog is="cr-dialog">
75 <div class="title">title</div> 105 <div class="title">title</div>
76 <div class="body"><button autofocus>button</button></div> 106 <div class="body"><button autofocus>button</button></div>
77 </dialog>`; 107 </dialog>`;
78 108
79 var dialog = document.body.querySelector('dialog'); 109 var dialog = document.body.querySelector('dialog');
80 var button = document.body.querySelector('button'); 110 var button = document.body.querySelector('button');
81 111
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 } 145 }
116 }); 146 });
117 observer.observe(bodyContainer, {attributes: true}); 147 observer.observe(bodyContainer, {attributes: true});
118 148
119 // Height is normally set via CSS, but mixin doesn't work with innerHTML. 149 // Height is normally set via CSS, but mixin doesn't work with innerHTML.
120 bodyContainer.style.height = '1px'; 150 bodyContainer.style.height = '1px';
121 bodyContainer.scrollTop = 100; 151 bodyContainer.scrollTop = 100;
122 dialog.showModal(); // Attach the dialog for the first time here. 152 dialog.showModal(); // Attach the dialog for the first time here.
123 }); 153 });
124 }); 154 });
OLDNEW
« no previous file with comments | « no previous file | ui/webui/resources/cr_elements/cr_dialog/cr_dialog.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698