1<!doctype html> 2<!-- 3@license 4Copyright (c) 2015 The Polymer Project Authors. All rights reserved. 5This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt 6The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt 7The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt 8Code distributed by Google as part of the polymer project is also 9subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt 10--> 11<html> 12 13<head> 14 15 <title>iron-focusables-helper tests</title> 16 17 <meta charset="utf-8"> 18 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 19 <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=yes"> 20 21 <script src="../../webcomponentsjs/webcomponents-lite.js"></script> 22 23 <script src="../../web-component-tester/browser.js"></script> 24 <link rel="import" href="../../iron-test-helpers/iron-test-helpers.html"> 25 <link rel="import" href="../iron-focusables-helper.html"> 26 <link rel="import" href="test-buttons.html"> 27 <link rel="import" href="test-buttons-wrapper.html"> 28 29 <style is="custom-style"> 30 .hidden { 31 visibility: hidden; 32 } 33 34 .no-display { 35 display: none; 36 } 37 </style> 38</head> 39 40<body> 41 42 <test-fixture id="basic"> 43 <template> 44 <div> 45 <h2>Focusables (no tabindex)</h2> 46 <div> 47 <input class="focusable1" placeholder="1 (nested)"> 48 </div> 49 <a href="#" class="focusable2">2</a> 50 <button disabled> disabled button</button> 51 <input disabled tabindex="0" value="disabled input with tabindex"> 52 <div tabindex="-1">not focusable</div> 53 <div contenteditable class="focusable3">3</div> 54 </div> 55 </template> 56 </test-fixture> 57 58 <test-fixture id="tabindex"> 59 <template> 60 <div> 61 <h2>Focusables (with tabindex)</h2> 62 <div tabindex="0" class="focusable7">7</div> 63 <div tabindex="0" class="focusable8">8</div> 64 <div tabindex="0" class="focusable9">9</div> 65 <div tabindex="0" class="focusable10">10</div> 66 <div tabindex="0" class="focusable11">11</div> 67 <div tabindex="0" class="focusable12">12</div> 68 <div tabindex="-1">not focusable</div> 69 <div tabindex="3" class="focusable3">3</div> 70 <div tabindex="4" class="focusable4">4</div> 71 <div tabindex="5" class="focusable5">5</div> 72 <div> 73 <div tabindex="1" class="focusable1">1 (nested)</div> 74 <div tabindex="6" class="focusable6">6 (nested)</div> 75 </div> 76 <div tabindex="2" class="focusable2">2</div> 77 </div> 78 </template> 79 </test-fixture> 80 81 <test-fixture id="shadow"> 82 <template> 83 <test-buttons> 84 <h2>focusables in ShadowDOM</h2> 85 <input placeholder="type something.."> 86 </test-buttons> 87 </template> 88 </test-fixture> 89 90 <test-fixture id="composed"> 91 <template> 92 <test-buttons-wrapper> 93 <input placeholder="type something.."> 94 </test-buttons-wrapper> 95 </template> 96 </test-fixture> 97 98 <script> 99 suite('getTabbableNodes', function() { 100 101 test('returns tabbable nodes', function() { 102 var node = fixture('basic'); 103 var focusableNodes = Polymer.IronFocusablesHelper.getTabbableNodes(node); 104 assert.equal(focusableNodes.length, 3, '3 nodes are focusable'); 105 assert.equal(focusableNodes[0], Polymer.dom(node).querySelector('.focusable1')); 106 assert.equal(focusableNodes[1], Polymer.dom(node).querySelector('.focusable2')); 107 assert.equal(focusableNodes[2], Polymer.dom(node).querySelector('.focusable3')); 108 }); 109 110 test('includes the root if it has a valid tabindex', function() { 111 var node = fixture('basic'); 112 node.setAttribute('tabindex', '0'); 113 var focusableNodes = Polymer.IronFocusablesHelper.getTabbableNodes(node); 114 assert.equal(focusableNodes.length, 4, '4 focusable nodes'); 115 assert.notEqual(focusableNodes.indexOf(node), -1, 'root is included'); 116 }); 117 118 test('excludes visibility: hidden elements', function() { 119 var node = fixture('basic'); 120 var focusable = Polymer.dom(node).querySelector('.focusable1'); 121 focusable.classList.add('hidden'); 122 var focusableNodes = Polymer.IronFocusablesHelper.getTabbableNodes(node); 123 assert.equal(focusableNodes.length, 2, '2 focusable nodes'); 124 assert.equal(focusableNodes.indexOf(focusable), -1, 'hidden element is not included'); 125 }); 126 127 test('excludes display: none elements', function() { 128 var node = fixture('basic'); 129 var focusable = Polymer.dom(node).querySelector('.focusable1'); 130 focusable.classList.add('no-display'); 131 var focusableNodes = Polymer.IronFocusablesHelper.getTabbableNodes(node); 132 assert.equal(focusableNodes.length, 2, '2 focusable nodes'); 133 assert.equal(focusableNodes.indexOf(focusable), -1, 'hidden element is not included'); 134 }); 135 136 test('respects the tabindex order', function() { 137 var node = fixture('tabindex'); 138 var focusableNodes = Polymer.IronFocusablesHelper.getTabbableNodes(node); 139 assert.equal(focusableNodes.length, 12, '12 nodes are focusable'); 140 for (var i = 0; i < 12; i++) { 141 assert.equal(focusableNodes[i], Polymer.dom(node).querySelector('.focusable' + (i + 1))); 142 } 143 }); 144 145 test('includes tabbable elements in the shadow dom', function() { 146 var node = fixture('shadow'); 147 var focusableNodes = Polymer.IronFocusablesHelper.getTabbableNodes(node); 148 assert.equal(focusableNodes.length, 4, '4 nodes are focusable'); 149 assert.equal(focusableNodes[0], node.$.button0); 150 assert.equal(focusableNodes[1], node.$.button1); 151 assert.equal(focusableNodes[2], Polymer.dom(node).querySelector('input')); 152 assert.equal(focusableNodes[3], node.$.button2); 153 }); 154 155 test('handles composition', function() { 156 var node = fixture('composed'); 157 var focusableNodes = Polymer.IronFocusablesHelper.getTabbableNodes(node); 158 assert.equal(focusableNodes.length, 6, '6 nodes are focusable'); 159 assert.equal(focusableNodes[0], node.$.select); 160 assert.equal(focusableNodes[1], node.$.wrapped.$.button0); 161 assert.equal(focusableNodes[2], node.$.wrapped.$.button1); 162 assert.equal(focusableNodes[3], Polymer.dom(node).querySelector('input')); 163 assert.equal(focusableNodes[4], node.$.wrapped.$.button2); 164 assert.equal(focusableNodes[5], node.$.focusableDiv); 165 }); 166 167 test('handles distributed nodes', function() { 168 var node = fixture('composed'); 169 var wrapped = node.$.wrapped; 170 var focusableNodes = Polymer.IronFocusablesHelper.getTabbableNodes(wrapped); 171 assert.equal(focusableNodes.length, 4, '4 nodes are focusable'); 172 assert.equal(focusableNodes[0], wrapped.$.button0); 173 assert.equal(focusableNodes[1], wrapped.$.button1); 174 assert.equal(focusableNodes[2], Polymer.dom(node).querySelector('input')); 175 assert.equal(focusableNodes[3], wrapped.$.button2); 176 }); 177 }); 178 </script> 179 180</body> 181 182</html> 183