OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * Class containing predicates for the chrome automation API. Each predicate |
| 7 * can be run on one or more AutomationNodes and returns a boolean value. |
| 8 * |
| 9 * @constructor |
| 10 */ |
| 11 function AutomationPredicate() {}; |
| 12 |
| 13 /** |
| 14 * Returns true if |node| is a subtreeLeaf, meaning that |node| is either |
| 15 * interesting or a group (both defined below). |
| 16 * |
| 17 * @param {!chrome.automation.AutomationNode} node |
| 18 * @param {!chrome.automation.AutomationNode} scope |
| 19 * @return {boolean} |
| 20 */ |
| 21 AutomationPredicate.isSubtreeLeaf = function(node, scope) { |
| 22 return AutomationPredicate.isInteresting(node) |
| 23 || AutomationPredicate.isGroup(node, scope); |
| 24 }; |
| 25 |
| 26 /** |
| 27 * Returns true if |node| is a group, meaning that the node has more than one |
| 28 * interesting descendant, and that its interesting descendants exist in more |
| 29 * than one subtree of its immediate children. |
| 30 * |
| 31 * Additionally, for |node| to be a group, it cannot have the same bounding |
| 32 * box as its scope. |
| 33 * |
| 34 * @param {!chrome.automation.AutomationNode} node |
| 35 * @param {!chrome.automation.AutomationNode} scope |
| 36 * @return {boolean} |
| 37 */ |
| 38 AutomationPredicate.isGroup = function(node, scope) { |
| 39 if (node !== scope && AutomationPredicate.hasSameLocation_(node, scope)) |
| 40 return false; |
| 41 |
| 42 // Work around for client nested in client. No need to have user select both |
| 43 // clients for a window. Once locations for outer client updates correctly, |
| 44 // this won't be needed. |
| 45 if (node.role === chrome.automation.RoleType.CLIENT |
| 46 && node.role === scope.role && node !== scope) |
| 47 return false; |
| 48 |
| 49 let interestingBranches = 0; |
| 50 for (let child of node.children) { |
| 51 if (AutomationPredicate.isInterestingSubtree(child)) |
| 52 interestingBranches += 1; |
| 53 if (interestingBranches > 1) |
| 54 return true; |
| 55 } |
| 56 return false; |
| 57 }; |
| 58 |
| 59 /** |
| 60 * Returns true if the two nodes have the same location. |
| 61 * |
| 62 * @param {!chrome.automation.AutomationNode} node1 |
| 63 * @param {!chrome.automation.AutomationNode} node2 |
| 64 * @return {boolean} |
| 65 */ |
| 66 AutomationPredicate.hasSameLocation_ = function(node1, node2) { |
| 67 let l1 = node1.location; |
| 68 let l2 = node2.location; |
| 69 return l1.left === l2.left && l1.top === l2.top && l1.width === l2.width |
| 70 && l1.height === l2.height; |
| 71 }; |
| 72 |
| 73 /** |
| 74 * Returns true if there is an interesting node in the subtree containing |
| 75 * |node| as its root (including |node| itself). |
| 76 * |
| 77 * @param {!chrome.automation.AutomationNode} node |
| 78 * @return {boolean} |
| 79 */ |
| 80 AutomationPredicate.isInterestingSubtree = function(node) { |
| 81 let children = node.children || []; |
| 82 return AutomationPredicate.isInteresting(node) |
| 83 || children.some(AutomationPredicate.isInterestingSubtree); |
| 84 }; |
| 85 |
| 86 /** |
| 87 * Returns true if |node| is interesting, meaning that a user can perform some |
| 88 * type of action on it. |
| 89 * |
| 90 * @param {!chrome.automation.AutomationNode} node |
| 91 * @return {boolean} |
| 92 */ |
| 93 AutomationPredicate.isInteresting = function(node) { |
| 94 let loc = node.location; |
| 95 let parent = node.parent; |
| 96 let root = node.root; |
| 97 let role = node.role; |
| 98 let state = node.state; |
| 99 |
| 100 // TODO(elichtenberg): Define shorthand for chrome.automation.RoleType and |
| 101 // StateType. |
| 102 |
| 103 // Skip things that are offscreen |
| 104 if (state[chrome.automation.StateType.OFFSCREEN] |
| 105 || loc.top < 0 || loc.left < 0) |
| 106 return false; |
| 107 |
| 108 // Should just leave these as groups |
| 109 if (role === chrome.automation.RoleType.WEB_VIEW |
| 110 || role === chrome.automation.RoleType.ROOT_WEB_AREA) |
| 111 return false; |
| 112 |
| 113 if (parent) { |
| 114 // crbug.com/710559 |
| 115 // Work around for browser tabs |
| 116 if (role === chrome.automation.RoleType.TAB |
| 117 && parent.role === chrome.automation.RoleType.TAB_LIST |
| 118 && root.role === chrome.automation.RoleType.DESKTOP) |
| 119 return true; |
| 120 } |
| 121 |
| 122 // The general rule that applies to everything. |
| 123 return state[chrome.automation.StateType.FOCUSABLE] === true; |
| 124 } |
OLD | NEW |