Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / ui / accessibility / ax_tree_unittest.cc
bloba0b7705ee04148c94311d489180ede1f7ebcc16d
1 // Copyright 2013 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.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/strings/string_number_conversions.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/accessibility/ax_node.h"
9 #include "ui/accessibility/ax_serializable_tree.h"
10 #include "ui/accessibility/ax_tree.h"
11 #include "ui/accessibility/ax_tree_serializer.h"
13 namespace ui {
15 namespace {
17 class FakeAXTreeDelegate : public AXTreeDelegate {
18 public:
19 FakeAXTreeDelegate() : root_changed_(false) {}
21 void OnNodeWillBeDeleted(AXTree* tree, AXNode* node) override {
22 deleted_ids_.push_back(node->id());
25 void OnSubtreeWillBeDeleted(AXTree* tree, AXNode* node) override {
26 subtree_deleted_ids_.push_back(node->id());
29 void OnNodeCreated(AXTree* tree, AXNode* node) override {
30 created_ids_.push_back(node->id());
33 void OnNodeChanged(AXTree* tree, AXNode* node) override {
34 changed_ids_.push_back(node->id());
37 void OnAtomicUpdateFinished(AXTree* tree,
38 bool root_changed,
39 const std::vector<Change>& changes) override {
40 root_changed_ = root_changed;
42 for (size_t i = 0; i < changes.size(); ++i) {
43 int id = changes[i].node->id();
44 switch (changes[i].type) {
45 case NODE_CREATED:
46 node_creation_finished_ids_.push_back(id);
47 break;
48 case SUBTREE_CREATED:
49 subtree_creation_finished_ids_.push_back(id);
50 break;
51 case NODE_CHANGED:
52 change_finished_ids_.push_back(id);
53 break;
58 bool root_changed() const { return root_changed_; }
59 const std::vector<int32>& deleted_ids() { return deleted_ids_; }
60 const std::vector<int32>& subtree_deleted_ids() {
61 return subtree_deleted_ids_;
63 const std::vector<int32>& created_ids() { return created_ids_; }
64 const std::vector<int32>& node_creation_finished_ids() {
65 return node_creation_finished_ids_;
67 const std::vector<int32>& subtree_creation_finished_ids() {
68 return subtree_creation_finished_ids_;
70 const std::vector<int32>& change_finished_ids() {
71 return change_finished_ids_;
74 private:
75 bool root_changed_;
76 std::vector<int32> deleted_ids_;
77 std::vector<int32> subtree_deleted_ids_;
78 std::vector<int32> created_ids_;
79 std::vector<int32> changed_ids_;
80 std::vector<int32> node_creation_finished_ids_;
81 std::vector<int32> subtree_creation_finished_ids_;
82 std::vector<int32> change_finished_ids_;
85 } // namespace
87 TEST(AXTreeTest, SerializeSimpleAXTree) {
88 AXNodeData root;
89 root.id = 1;
90 root.role = AX_ROLE_ROOT_WEB_AREA;
91 root.state = (1 << AX_STATE_FOCUSABLE) | (1 << AX_STATE_FOCUSED);
92 root.location = gfx::Rect(0, 0, 800, 600);
93 root.child_ids.push_back(2);
94 root.child_ids.push_back(3);
96 AXNodeData button;
97 button.id = 2;
98 button.role = AX_ROLE_BUTTON;
99 button.state = 0;
100 button.location = gfx::Rect(20, 20, 200, 30);
102 AXNodeData checkbox;
103 checkbox.id = 3;
104 checkbox.role = AX_ROLE_CHECK_BOX;
105 checkbox.state = 0;
106 checkbox.location = gfx::Rect(20, 50, 200, 30);
108 AXTreeUpdate<AXNodeData> initial_state;
109 initial_state.nodes.push_back(root);
110 initial_state.nodes.push_back(button);
111 initial_state.nodes.push_back(checkbox);
112 AXSerializableTree src_tree(initial_state);
114 scoped_ptr<AXTreeSource<const AXNode*, AXNodeData> > tree_source(
115 src_tree.CreateTreeSource());
116 AXTreeSerializer<const AXNode*, AXNodeData> serializer(
117 tree_source.get());
118 AXTreeUpdate<AXNodeData> update;
119 serializer.SerializeChanges(src_tree.root(), &update);
121 AXTree dst_tree;
122 ASSERT_TRUE(dst_tree.Unserialize(update));
124 const AXNode* root_node = dst_tree.root();
125 ASSERT_TRUE(root_node != NULL);
126 EXPECT_EQ(root.id, root_node->id());
127 EXPECT_EQ(root.role, root_node->data().role);
129 ASSERT_EQ(2, root_node->child_count());
131 const AXNode* button_node = root_node->ChildAtIndex(0);
132 EXPECT_EQ(button.id, button_node->id());
133 EXPECT_EQ(button.role, button_node->data().role);
135 const AXNode* checkbox_node = root_node->ChildAtIndex(1);
136 EXPECT_EQ(checkbox.id, checkbox_node->id());
137 EXPECT_EQ(checkbox.role, checkbox_node->data().role);
139 EXPECT_EQ(
140 "id=1 rootWebArea FOCUSABLE FOCUSED (0, 0)-(800, 600) child_ids=2,3\n"
141 " id=2 button (20, 20)-(200, 30)\n"
142 " id=3 checkBox (20, 50)-(200, 30)\n",
143 dst_tree.ToString());
146 TEST(AXTreeTest, SerializeAXTreeUpdate) {
147 AXNodeData list;
148 list.id = 3;
149 list.role = AX_ROLE_LIST;
150 list.state = 0;
151 list.child_ids.push_back(4);
152 list.child_ids.push_back(5);
153 list.child_ids.push_back(6);
155 AXNodeData list_item_2;
156 list_item_2.id = 5;
157 list_item_2.role = AX_ROLE_LIST_ITEM;
158 list_item_2.state = 0;
160 AXNodeData list_item_3;
161 list_item_3.id = 6;
162 list_item_3.role = AX_ROLE_LIST_ITEM;
163 list_item_3.state = 0;
165 AXNodeData button;
166 button.id = 7;
167 button.role = AX_ROLE_BUTTON;
168 button.state = 0;
170 AXTreeUpdate<AXNodeData> update;
171 update.nodes.push_back(list);
172 update.nodes.push_back(list_item_2);
173 update.nodes.push_back(list_item_3);
174 update.nodes.push_back(button);
176 EXPECT_EQ(
177 "id=3 list (0, 0)-(0, 0) child_ids=4,5,6\n"
178 " id=5 listItem (0, 0)-(0, 0)\n"
179 " id=6 listItem (0, 0)-(0, 0)\n"
180 "id=7 button (0, 0)-(0, 0)\n",
181 update.ToString());
184 TEST(AXTreeTest, DeleteUnknownSubtreeFails) {
185 AXNodeData root;
186 root.id = 1;
187 root.role = AX_ROLE_ROOT_WEB_AREA;
189 AXTreeUpdate<AXNodeData> initial_state;
190 initial_state.nodes.push_back(root);
191 AXTree tree(initial_state);
193 // This should fail because we're asking it to delete
194 // a subtree rooted at id=2, which doesn't exist.
195 AXTreeUpdate<AXNodeData> update;
196 update.node_id_to_clear = 2;
197 update.nodes.resize(1);
198 update.nodes[0].id = 1;
199 update.nodes[0].id = AX_ROLE_ROOT_WEB_AREA;
200 EXPECT_FALSE(tree.Unserialize(update));
201 ASSERT_EQ("Bad node_id_to_clear: 2", tree.error());
204 TEST(AXTreeTest, LeaveOrphanedDeletedSubtreeFails) {
205 AXTreeUpdate<AXNodeData> initial_state;
206 initial_state.nodes.resize(3);
207 initial_state.nodes[0].id = 1;
208 initial_state.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
209 initial_state.nodes[0].child_ids.push_back(2);
210 initial_state.nodes[0].child_ids.push_back(3);
211 initial_state.nodes[1].id = 2;
212 initial_state.nodes[2].id = 3;
213 AXTree tree(initial_state);
215 // This should fail because we delete a subtree rooted at id=2
216 // but never update it.
217 AXTreeUpdate<AXNodeData> update;
218 update.node_id_to_clear = 2;
219 update.nodes.resize(1);
220 update.nodes[0].id = 3;
221 EXPECT_FALSE(tree.Unserialize(update));
222 ASSERT_EQ("Nodes left pending by the update: 2", tree.error());
225 TEST(AXTreeTest, LeaveOrphanedNewChildFails) {
226 AXTreeUpdate<AXNodeData> initial_state;
227 initial_state.nodes.resize(1);
228 initial_state.nodes[0].id = 1;
229 initial_state.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
230 AXTree tree(initial_state);
232 // This should fail because we add a new child to the root node
233 // but never update it.
234 AXTreeUpdate<AXNodeData> update;
235 update.nodes.resize(1);
236 update.nodes[0].id = 1;
237 update.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
238 update.nodes[0].child_ids.push_back(2);
239 EXPECT_FALSE(tree.Unserialize(update));
240 ASSERT_EQ("Nodes left pending by the update: 2", tree.error());
243 TEST(AXTreeTest, DuplicateChildIdFails) {
244 AXTreeUpdate<AXNodeData> initial_state;
245 initial_state.nodes.resize(1);
246 initial_state.nodes[0].id = 1;
247 initial_state.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
248 AXTree tree(initial_state);
250 // This should fail because a child id appears twice.
251 AXTreeUpdate<AXNodeData> update;
252 update.nodes.resize(2);
253 update.nodes[0].id = 1;
254 update.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
255 update.nodes[0].child_ids.push_back(2);
256 update.nodes[0].child_ids.push_back(2);
257 update.nodes[1].id = 2;
258 EXPECT_FALSE(tree.Unserialize(update));
259 ASSERT_EQ("Node 1 has duplicate child id 2", tree.error());
262 TEST(AXTreeTest, InvalidReparentingFails) {
263 AXTreeUpdate<AXNodeData> initial_state;
264 initial_state.nodes.resize(3);
265 initial_state.nodes[0].id = 1;
266 initial_state.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
267 initial_state.nodes[0].child_ids.push_back(2);
268 initial_state.nodes[1].id = 2;
269 initial_state.nodes[1].child_ids.push_back(3);
270 initial_state.nodes[2].id = 3;
272 AXTree tree(initial_state);
274 // This should fail because node 3 is reparented from node 2 to node 1
275 // without deleting node 1's subtree first.
276 AXTreeUpdate<AXNodeData> update;
277 update.nodes.resize(3);
278 update.nodes[0].id = 1;
279 update.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
280 update.nodes[0].child_ids.push_back(3);
281 update.nodes[0].child_ids.push_back(2);
282 update.nodes[1].id = 2;
283 update.nodes[2].id = 3;
284 EXPECT_FALSE(tree.Unserialize(update));
285 ASSERT_EQ("Node 3 reparented from 2 to 1", tree.error());
288 TEST(AXTreeTest, TwoRootsFails) {
289 AXTreeUpdate<AXNodeData> initial_state;
290 initial_state.nodes.resize(1);
291 initial_state.nodes[0].id = 1;
292 initial_state.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
293 AXTree tree(initial_state);
295 // This should fail because there are two new roots.
296 AXTreeUpdate<AXNodeData> update;
297 update.nodes.resize(2);
298 update.nodes[0].id = 2;
299 update.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
300 update.nodes[1].id = 3;
301 update.nodes[1].role = AX_ROLE_ROOT_WEB_AREA;
302 EXPECT_FALSE(tree.Unserialize(update));
303 ASSERT_EQ("Tree update contains two new roots", tree.error());
306 TEST(AXTreeTest, TreeDelegateIsCalled) {
307 AXTreeUpdate<AXNodeData> initial_state;
308 initial_state.nodes.resize(2);
309 initial_state.nodes[0].id = 1;
310 initial_state.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
311 initial_state.nodes[0].child_ids.push_back(2);
312 initial_state.nodes[1].id = 2;
314 AXTree tree(initial_state);
315 AXTreeUpdate<AXNodeData> update;
316 update.node_id_to_clear = 1;
317 update.nodes.resize(2);
318 update.nodes[0].id = 3;
319 update.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
320 update.nodes[0].child_ids.push_back(4);
321 update.nodes[1].id = 4;
323 FakeAXTreeDelegate fake_delegate;
324 tree.SetDelegate(&fake_delegate);
326 EXPECT_TRUE(tree.Unserialize(update));
328 ASSERT_EQ(2U, fake_delegate.deleted_ids().size());
329 EXPECT_EQ(1, fake_delegate.deleted_ids()[0]);
330 EXPECT_EQ(2, fake_delegate.deleted_ids()[1]);
332 ASSERT_EQ(1U, fake_delegate.subtree_deleted_ids().size());
333 EXPECT_EQ(1, fake_delegate.subtree_deleted_ids()[0]);
335 ASSERT_EQ(2U, fake_delegate.created_ids().size());
336 EXPECT_EQ(3, fake_delegate.created_ids()[0]);
337 EXPECT_EQ(4, fake_delegate.created_ids()[1]);
339 ASSERT_EQ(1U, fake_delegate.subtree_creation_finished_ids().size());
340 EXPECT_EQ(3, fake_delegate.subtree_creation_finished_ids()[0]);
342 ASSERT_EQ(1U, fake_delegate.node_creation_finished_ids().size());
343 EXPECT_EQ(4, fake_delegate.node_creation_finished_ids()[0]);
345 ASSERT_EQ(true, fake_delegate.root_changed());
347 tree.SetDelegate(NULL);
350 } // namespace ui