1 | // Copyright 2021 The AccessKit Authors. All rights reserved. |
2 | // Licensed under the Apache License, Version 2.0 (found in |
3 | // the LICENSE-APACHE file) or the MIT license (found in |
4 | // the LICENSE-MIT file), at your option. |
5 | |
6 | #![no_std ] |
7 | |
8 | extern crate alloc; |
9 | |
10 | pub(crate) mod tree; |
11 | pub use tree::{ChangeHandler as TreeChangeHandler, State as TreeState, Tree}; |
12 | |
13 | pub(crate) mod node; |
14 | pub use node::Node; |
15 | |
16 | pub(crate) mod filters; |
17 | pub use filters::{common_filter, common_filter_with_root_exception, FilterResult}; |
18 | |
19 | pub(crate) mod iterators; |
20 | |
21 | pub(crate) mod text; |
22 | pub use text::{ |
23 | AttributeValue as TextAttributeValue, Position as TextPosition, Range as TextRange, |
24 | WeakRange as WeakTextRange, |
25 | }; |
26 | |
27 | #[cfg (test)] |
28 | mod tests { |
29 | use accesskit::{Affine, Node, NodeId, Rect, Role, Tree, TreeUpdate, Vec2}; |
30 | use alloc::vec; |
31 | |
32 | use crate::FilterResult; |
33 | |
34 | pub const ROOT_ID: NodeId = NodeId(0); |
35 | pub const PARAGRAPH_0_ID: NodeId = NodeId(1); |
36 | pub const LABEL_0_0_IGNORED_ID: NodeId = NodeId(2); |
37 | pub const PARAGRAPH_1_IGNORED_ID: NodeId = NodeId(3); |
38 | pub const BUTTON_1_0_HIDDEN_ID: NodeId = NodeId(4); |
39 | pub const CONTAINER_1_0_0_HIDDEN_ID: NodeId = NodeId(5); |
40 | pub const LABEL_1_1_ID: NodeId = NodeId(6); |
41 | pub const BUTTON_1_2_HIDDEN_ID: NodeId = NodeId(7); |
42 | pub const CONTAINER_1_2_0_HIDDEN_ID: NodeId = NodeId(8); |
43 | pub const PARAGRAPH_2_ID: NodeId = NodeId(9); |
44 | pub const LABEL_2_0_ID: NodeId = NodeId(10); |
45 | pub const PARAGRAPH_3_IGNORED_ID: NodeId = NodeId(11); |
46 | pub const EMPTY_CONTAINER_3_0_IGNORED_ID: NodeId = NodeId(12); |
47 | pub const LINK_3_1_IGNORED_ID: NodeId = NodeId(13); |
48 | pub const LABEL_3_1_0_ID: NodeId = NodeId(14); |
49 | pub const BUTTON_3_2_ID: NodeId = NodeId(15); |
50 | pub const EMPTY_CONTAINER_3_3_IGNORED_ID: NodeId = NodeId(16); |
51 | |
52 | pub fn test_tree() -> crate::tree::Tree { |
53 | let root = { |
54 | let mut node = Node::new(Role::RootWebArea); |
55 | node.set_children(vec![ |
56 | PARAGRAPH_0_ID, |
57 | PARAGRAPH_1_IGNORED_ID, |
58 | PARAGRAPH_2_ID, |
59 | PARAGRAPH_3_IGNORED_ID, |
60 | ]); |
61 | node |
62 | }; |
63 | let paragraph_0 = { |
64 | let mut node = Node::new(Role::Paragraph); |
65 | node.set_children(vec![LABEL_0_0_IGNORED_ID]); |
66 | node |
67 | }; |
68 | let label_0_0_ignored = { |
69 | let mut node = Node::new(Role::Label); |
70 | node.set_value("label_0_0_ignored" ); |
71 | node |
72 | }; |
73 | let paragraph_1_ignored = { |
74 | let mut node = Node::new(Role::Paragraph); |
75 | node.set_transform(Affine::translate(Vec2::new(10.0, 40.0))); |
76 | node.set_bounds(Rect { |
77 | x0: 0.0, |
78 | y0: 0.0, |
79 | x1: 800.0, |
80 | y1: 40.0, |
81 | }); |
82 | node.set_children(vec![ |
83 | BUTTON_1_0_HIDDEN_ID, |
84 | LABEL_1_1_ID, |
85 | BUTTON_1_2_HIDDEN_ID, |
86 | ]); |
87 | node |
88 | }; |
89 | let button_1_0_hidden = { |
90 | let mut node = Node::new(Role::Button); |
91 | node.set_label("button_1_0_hidden" ); |
92 | node.set_hidden(); |
93 | node.set_children(vec![CONTAINER_1_0_0_HIDDEN_ID]); |
94 | node |
95 | }; |
96 | let container_1_0_0_hidden = { |
97 | let mut node = Node::new(Role::GenericContainer); |
98 | node.set_hidden(); |
99 | node |
100 | }; |
101 | let label_1_1 = { |
102 | let mut node = Node::new(Role::Label); |
103 | node.set_bounds(Rect { |
104 | x0: 10.0, |
105 | y0: 10.0, |
106 | x1: 90.0, |
107 | y1: 30.0, |
108 | }); |
109 | node.set_value("label_1_1" ); |
110 | node |
111 | }; |
112 | let button_1_2_hidden = { |
113 | let mut node = Node::new(Role::Button); |
114 | node.set_label("button_1_2_hidden" ); |
115 | node.set_hidden(); |
116 | node.set_children(vec![CONTAINER_1_2_0_HIDDEN_ID]); |
117 | node |
118 | }; |
119 | let container_1_2_0_hidden = { |
120 | let mut node = Node::new(Role::GenericContainer); |
121 | node.set_hidden(); |
122 | node |
123 | }; |
124 | let paragraph_2 = { |
125 | let mut node = Node::new(Role::Paragraph); |
126 | node.set_children(vec![LABEL_2_0_ID]); |
127 | node |
128 | }; |
129 | let label_2_0 = { |
130 | let mut node = Node::new(Role::Label); |
131 | node.set_label("label_2_0" ); |
132 | node |
133 | }; |
134 | let paragraph_3_ignored = { |
135 | let mut node = Node::new(Role::Paragraph); |
136 | node.set_children(vec![ |
137 | EMPTY_CONTAINER_3_0_IGNORED_ID, |
138 | LINK_3_1_IGNORED_ID, |
139 | BUTTON_3_2_ID, |
140 | EMPTY_CONTAINER_3_3_IGNORED_ID, |
141 | ]); |
142 | node |
143 | }; |
144 | let empty_container_3_0_ignored = Node::new(Role::GenericContainer); |
145 | let link_3_1_ignored = { |
146 | let mut node = Node::new(Role::Link); |
147 | node.set_children(vec![LABEL_3_1_0_ID]); |
148 | node.set_linked(); |
149 | node |
150 | }; |
151 | let label_3_1_0 = { |
152 | let mut node = Node::new(Role::Label); |
153 | node.set_value("label_3_1_0" ); |
154 | node |
155 | }; |
156 | let button_3_2 = { |
157 | let mut node = Node::new(Role::Button); |
158 | node.set_label("button_3_2" ); |
159 | node |
160 | }; |
161 | let empty_container_3_3_ignored = Node::new(Role::GenericContainer); |
162 | let initial_update = TreeUpdate { |
163 | nodes: vec![ |
164 | (ROOT_ID, root), |
165 | (PARAGRAPH_0_ID, paragraph_0), |
166 | (LABEL_0_0_IGNORED_ID, label_0_0_ignored), |
167 | (PARAGRAPH_1_IGNORED_ID, paragraph_1_ignored), |
168 | (BUTTON_1_0_HIDDEN_ID, button_1_0_hidden), |
169 | (CONTAINER_1_0_0_HIDDEN_ID, container_1_0_0_hidden), |
170 | (LABEL_1_1_ID, label_1_1), |
171 | (BUTTON_1_2_HIDDEN_ID, button_1_2_hidden), |
172 | (CONTAINER_1_2_0_HIDDEN_ID, container_1_2_0_hidden), |
173 | (PARAGRAPH_2_ID, paragraph_2), |
174 | (LABEL_2_0_ID, label_2_0), |
175 | (PARAGRAPH_3_IGNORED_ID, paragraph_3_ignored), |
176 | (EMPTY_CONTAINER_3_0_IGNORED_ID, empty_container_3_0_ignored), |
177 | (LINK_3_1_IGNORED_ID, link_3_1_ignored), |
178 | (LABEL_3_1_0_ID, label_3_1_0), |
179 | (BUTTON_3_2_ID, button_3_2), |
180 | (EMPTY_CONTAINER_3_3_IGNORED_ID, empty_container_3_3_ignored), |
181 | ], |
182 | tree: Some(Tree::new(ROOT_ID)), |
183 | focus: ROOT_ID, |
184 | }; |
185 | crate::tree::Tree::new(initial_update, false) |
186 | } |
187 | |
188 | pub fn test_tree_filter(node: &crate::Node) -> FilterResult { |
189 | let id = node.id(); |
190 | if node.is_hidden() { |
191 | FilterResult::ExcludeSubtree |
192 | } else if id == LABEL_0_0_IGNORED_ID |
193 | || id == PARAGRAPH_1_IGNORED_ID |
194 | || id == PARAGRAPH_3_IGNORED_ID |
195 | || id == EMPTY_CONTAINER_3_0_IGNORED_ID |
196 | || id == LINK_3_1_IGNORED_ID |
197 | || id == EMPTY_CONTAINER_3_3_IGNORED_ID |
198 | { |
199 | FilterResult::ExcludeNode |
200 | } else { |
201 | FilterResult::Include |
202 | } |
203 | } |
204 | } |
205 | |