| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 var group; | 5 var group; |
| 6 var h1; | 6 var h1; |
| 7 var p1; | 7 var p1; |
| 8 var link; | 8 var link; |
| 9 var main; | 9 var main; |
| 10 var p2; | 10 var p2; |
| 11 var p3; | 11 var p3; |
| 12 var okButton; | 12 var okButton; |
| 13 var cancelButton; | 13 var cancelButton; |
| 14 | 14 |
| 15 function initializeNodes(rootNode) { | 15 function initializeNodes(rootNode) { |
| 16 group = rootNode.firstChild; | 16 group = rootNode.firstChild; |
| 17 assertEq(RoleType.group, group.role); | 17 assertEq(RoleType.GROUP, group.role); |
| 18 | 18 |
| 19 h1 = group.firstChild; | 19 h1 = group.firstChild; |
| 20 assertEq(RoleType.heading, h1.role); | 20 assertEq(RoleType.HEADING, h1.role); |
| 21 assertEq(1, h1.hierarchicalLevel); | 21 assertEq(1, h1.hierarchicalLevel); |
| 22 | 22 |
| 23 p1 = group.lastChild; | 23 p1 = group.lastChild; |
| 24 assertEq(RoleType.paragraph, p1.role); | 24 assertEq(RoleType.PARAGRAPH, p1.role); |
| 25 | 25 |
| 26 link = p1.children[1]; | 26 link = p1.children[1]; |
| 27 assertEq(RoleType.link, link.role); | 27 assertEq(RoleType.LINK, link.role); |
| 28 | 28 |
| 29 main = rootNode.children[1]; | 29 main = rootNode.children[1]; |
| 30 assertEq(RoleType.main, main.role); | 30 assertEq(RoleType.MAIN, main.role); |
| 31 | 31 |
| 32 p2 = main.firstChild; | 32 p2 = main.firstChild; |
| 33 assertEq(RoleType.paragraph, p2.role); | 33 assertEq(RoleType.PARAGRAPH, p2.role); |
| 34 | 34 |
| 35 p3 = main.lastChild; | 35 p3 = main.lastChild; |
| 36 assertEq(RoleType.paragraph, p3.role); | 36 assertEq(RoleType.PARAGRAPH, p3.role); |
| 37 | 37 |
| 38 okButton = rootNode.children[2]; | 38 okButton = rootNode.children[2]; |
| 39 assertEq(RoleType.button, okButton.role); | 39 assertEq(RoleType.BUTTON, okButton.role); |
| 40 assertEq('Ok', okButton.name); | 40 assertEq('Ok', okButton.name); |
| 41 assertTrue(StateType.disabled in okButton.state); | 41 assertTrue(StateType.DISABLED in okButton.state); |
| 42 assertTrue(okButton.state.disabled); | 42 assertTrue(okButton.state.disabled); |
| 43 | 43 |
| 44 cancelButton = rootNode.children[3]; | 44 cancelButton = rootNode.children[3]; |
| 45 assertEq(RoleType.button, cancelButton.role); | 45 assertEq(RoleType.BUTTON, cancelButton.role); |
| 46 assertEq('Cancel', cancelButton.name); | 46 assertEq('Cancel', cancelButton.name); |
| 47 assertFalse(StateType.disabled in cancelButton.state); | 47 assertFalse(StateType.DISABLED in cancelButton.state); |
| 48 } | 48 } |
| 49 | 49 |
| 50 var allTests = [ | 50 var allTests = [ |
| 51 function testFindByRole() { | 51 function testFindByRole() { |
| 52 initializeNodes(rootNode); | 52 initializeNodes(rootNode); |
| 53 | 53 |
| 54 // Should find the only instance of this role. | 54 // Should find the only instance of this role. |
| 55 assertEq(h1, rootNode.find({ role: RoleType.heading})); | 55 assertEq(h1, rootNode.find({role: RoleType.HEADING})); |
| 56 assertEq([h1], rootNode.findAll({ role: RoleType.heading})); | 56 assertEq([h1], rootNode.findAll({role: RoleType.HEADING})); |
| 57 | 57 |
| 58 // find should find first instance only. | 58 // find should find first instance only. |
| 59 assertEq(okButton, rootNode.find({ role: RoleType.button })); | 59 assertEq(okButton, rootNode.find({role: RoleType.BUTTON})); |
| 60 assertEq(p1, rootNode.find({ role: RoleType.paragraph })); | 60 assertEq(p1, rootNode.find({role: RoleType.PARAGRAPH})); |
| 61 | 61 |
| 62 // findAll should find all instances. | 62 // findAll should find all instances. |
| 63 assertEq([okButton, cancelButton], | 63 assertEq([okButton, cancelButton], |
| 64 rootNode.findAll({ role: RoleType.button })); | 64 rootNode.findAll({role: RoleType.BUTTON})); |
| 65 assertEq([p1, p2, p3], rootNode.findAll({ role: RoleType.paragraph })); | 65 assertEq([p1, p2, p3], rootNode.findAll({role: RoleType.PARAGRAPH})); |
| 66 | 66 |
| 67 // No instances: find should return null; findAll should return empty array. | 67 // No instances: find should return null; findAll should return empty array. |
| 68 assertEq(null, rootNode.find({ role: RoleType.checkbox })); | 68 assertEq(null, rootNode.find({role: RoleType.CHECKBOX})); |
| 69 assertEq([], rootNode.findAll({ role: RoleType.checkbox })); | 69 assertEq([], rootNode.findAll({role: RoleType.CHECKBOX})); |
| 70 | 70 |
| 71 // Calling from node should search only its subtree. | 71 // Calling from node should search only its subtree. |
| 72 assertEq(p1, group.find({ role: RoleType.paragraph })); | 72 assertEq(p1, group.find({role: RoleType.PARAGRAPH})); |
| 73 assertEq(p2, main.find({ role: RoleType.paragraph })); | 73 assertEq(p2, main.find({role: RoleType.PARAGRAPH})); |
| 74 assertEq([p2, p3], main.findAll({ role: RoleType.paragraph })); | 74 assertEq([p2, p3], main.findAll({role: RoleType.PARAGRAPH})); |
| 75 | 75 |
| 76 chrome.test.succeed(); | 76 chrome.test.succeed(); |
| 77 }, | 77 }, |
| 78 | 78 |
| 79 function testFindByStates() { | 79 function testFindByStates() { |
| 80 initializeNodes(rootNode); | 80 initializeNodes(rootNode); |
| 81 | 81 |
| 82 // Find all focusable elements (disabled button is not focusable). | 82 // Find all focusable elements (disabled button is not focusable). |
| 83 assertEq(link, rootNode.find({ state: { focusable: true }})); | 83 assertEq(link, rootNode.find({state: {focusable: true}})); |
| 84 assertEq([link, cancelButton], | 84 assertEq([link, cancelButton], |
| 85 rootNode.findAll({ state: { focusable: true }})); | 85 rootNode.findAll({state: {focusable: true}})); |
| 86 | 86 |
| 87 // Find disabled buttons. | 87 // Find disabled buttons. |
| 88 assertEq(okButton, rootNode.find({ role: RoleType.button, | 88 assertEq(okButton, rootNode.find({role: RoleType.BUTTON, |
| 89 state: { disabled: true }})); | 89 state: {disabled: true}})); |
| 90 assertEq([okButton], rootNode.findAll({ role: RoleType.button, | 90 assertEq([okButton], rootNode.findAll({role: RoleType.BUTTON, |
| 91 state: { disabled: true }})); | 91 state: {disabled: true}})); |
| 92 | 92 |
| 93 // Find enabled buttons. | 93 // Find enabled buttons. |
| 94 assertEq(cancelButton, rootNode.find({ role: RoleType.button, | 94 assertEq(cancelButton, rootNode.find({role: RoleType.BUTTON, |
| 95 state: { disabled: false }})); | 95 state: {disabled: false}})); |
| 96 assertEq([cancelButton], rootNode.findAll({ role: RoleType.button, | 96 assertEq([cancelButton], rootNode.findAll({role: RoleType.BUTTON, |
| 97 state: { disabled: false }})); | 97 state: {disabled: false}})); |
| 98 chrome.test.succeed(); | 98 chrome.test.succeed(); |
| 99 }, | 99 }, |
| 100 | 100 |
| 101 function testFindByAttribute() { | 101 function testFindByAttribute() { |
| 102 initializeNodes(rootNode); | 102 initializeNodes(rootNode); |
| 103 | 103 |
| 104 // Find by name attribute. | 104 // Find by name attribute. |
| 105 assertEq(okButton, rootNode.find({ attributes: { name: 'Ok' }})); | 105 assertEq(okButton, rootNode.find({attributes: {name: 'Ok'}})); |
| 106 assertEq(cancelButton, rootNode.find({ attributes: { name: 'Cancel' }})); | 106 assertEq(cancelButton, rootNode.find({attributes: {name: 'Cancel'}})); |
| 107 | 107 |
| 108 // String attributes must be exact match unless a regex is used. | 108 // String attributes must be exact match unless a regex is used. |
| 109 assertEq(null, rootNode.find({ attributes: { name: 'Canc' }})); | 109 assertEq(null, rootNode.find({attributes: {name: 'Canc'}})); |
| 110 assertEq(null, rootNode.find({ attributes: { name: 'ok' }})); | 110 assertEq(null, rootNode.find({attributes: {name: 'ok'}})); |
| 111 | 111 |
| 112 // Find by value attribute - regexp. | 112 // Find by value attribute - regexp. |
| 113 var query = { attributes: { name: /relationship/ }}; | 113 var query = {attributes: {name: /relationship/}}; |
| 114 assertEq(p2, rootNode.find(query).parent); | 114 assertEq(p2, rootNode.find(query).parent); |
| 115 | 115 |
| 116 // Find by role and hierarchicalLevel attribute. | 116 // Find by role and hierarchicalLevel attribute. |
| 117 assertEq(h1, rootNode.find({ role: RoleType.heading, | 117 assertEq(h1, rootNode.find({role: RoleType.HEADING, |
| 118 attributes: { hierarchicalLevel: 1 }})); | 118 attributes: {hierarchicalLevel: 1}})); |
| 119 assertEq([], rootNode.findAll({ role: RoleType.heading, | 119 assertEq([], rootNode.findAll({role: RoleType.HEADING, |
| 120 attributes: { hierarchicalLevel: 2 }})); | 120 attributes: {hierarchicalLevel: 2}})); |
| 121 | 121 |
| 122 // Searching for an attribute which no element has fails. | 122 // Searching for an attribute which no element has fails. |
| 123 assertEq(null, rootNode.find({ attributes: { charisma: 12 } })); | 123 assertEq(null, rootNode.find({attributes: {charisma: 12}})); |
| 124 | 124 |
| 125 // Searching for an attribute value of the wrong type fails (even if the | 125 // Searching for an attribute value of the wrong type fails (even if the |
| 126 // value is equivalent). | 126 // value is equivalent). |
| 127 assertEq(null, rootNode.find({ role: RoleType.heading, | 127 assertEq(null, rootNode.find({role: RoleType.HEADING, |
| 128 attributes: { hierarchicalLevel: true }} )); | 128 attributes: {hierarchicalLevel: true}} )); |
| 129 | 129 |
| 130 chrome.test.succeed(); | 130 chrome.test.succeed(); |
| 131 }, | 131 }, |
| 132 | 132 |
| 133 function testMatches() { | 133 function testMatches() { |
| 134 initializeNodes(rootNode); | 134 initializeNodes(rootNode); |
| 135 assertTrue(h1.matches({ role: RoleType.heading }), | 135 assertTrue(h1.matches({role: RoleType.HEADING}), |
| 136 'h1 should match RoleType.heading'); | 136 'h1 should match RoleType.HEADING'); |
| 137 assertTrue(h1.matches({ role: RoleType.heading, | 137 assertTrue(h1.matches({role: RoleType.HEADING, |
| 138 attributes: { hierarchicalLevel: 1 } }), | 138 attributes: {hierarchicalLevel: 1}}), |
| 139 'h1 should match RoleType.heading and hierarchicalLevel: 1'); | 139 'h1 should match RoleType.HEADING and hierarchicalLevel: 1'); |
| 140 assertFalse(h1.matches({ role: RoleType.heading, | 140 assertFalse(h1.matches({role: RoleType.HEADING, |
| 141 state: { focusable: true }, | 141 state: {focusable: true}, |
| 142 attributes: { hierarchicalLevel: 1 } }), | 142 attributes: {hierarchicalLevel: 1}}), |
| 143 'h1 should not match focusable: true'); | 143 'h1 should not match focusable: true'); |
| 144 assertTrue(h1.matches({ role: RoleType.heading, | 144 assertTrue(h1.matches({role: RoleType.HEADING, |
| 145 state: { focusable: false }, | 145 state: {focusable: false}, |
| 146 attributes: { hierarchicalLevel: 1 } }), | 146 attributes: {hierarchicalLevel: 1}}), |
| 147 'h1 should match focusable: false'); | 147 'h1 should match focusable: false'); |
| 148 | 148 |
| 149 var p2StaticText = p2.firstChild; | 149 var p2StaticText = p2.firstChild; |
| 150 assertTrue( | 150 assertTrue( |
| 151 p2StaticText.matches({ role: RoleType.staticText, | 151 p2StaticText.matches({role: RoleType.STATIC_TEXT, |
| 152 attributes: { name: /relationship/ } }), | 152 attributes: {name: /relationship/}}), |
| 153 'p2StaticText should match name: /relationship/ (regex match)'); | 153 'p2StaticText should match name: /relationship/ (regex match)'); |
| 154 assertFalse( | 154 assertFalse( |
| 155 p2StaticText.matches({ role: RoleType.staticText, | 155 p2StaticText.matches({role: RoleType.STATIC_TEXT, |
| 156 attributes: { name: 'relationship' } }), | 156 attributes: {name: 'relationship'}}), |
| 157 'p2 should not match name: \'relationship'); | 157 'p2 should not match name: \'relationship'); |
| 158 | 158 |
| 159 chrome.test.succeed(); | 159 chrome.test.succeed(); |
| 160 } | 160 } |
| 161 ]; | 161 ]; |
| 162 | 162 |
| 163 setUpAndRunTests(allTests, 'complex.html'); | 163 setUpAndRunTests(allTests, 'complex.html'); |
| OLD | NEW |