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"
17 class FakeAXTreeDelegate
: public AXTreeDelegate
{
19 FakeAXTreeDelegate() : root_changed_(false) {}
21 void OnNodeWillBeDeleted(AXNode
* node
) override
{
22 deleted_ids_
.push_back(node
->id());
25 void OnSubtreeWillBeDeleted(AXNode
* node
) override
{
26 subtree_deleted_ids_
.push_back(node
->id());
29 void OnNodeCreated(AXNode
* node
) override
{
30 created_ids_
.push_back(node
->id());
33 void OnNodeChanged(AXNode
* node
) override
{
34 changed_ids_
.push_back(node
->id());
37 void OnAtomicUpdateFinished(bool root_changed
,
38 const std::vector
<Change
>& changes
) override
{
39 root_changed_
= root_changed
;
41 for (size_t i
= 0; i
< changes
.size(); ++i
) {
42 int id
= changes
[i
].node
->id();
43 switch (changes
[i
].type
) {
45 node_creation_finished_ids_
.push_back(id
);
48 subtree_creation_finished_ids_
.push_back(id
);
51 change_finished_ids_
.push_back(id
);
57 bool root_changed() const { return root_changed_
; }
58 const std::vector
<int32
>& deleted_ids() { return deleted_ids_
; }
59 const std::vector
<int32
>& subtree_deleted_ids() {
60 return subtree_deleted_ids_
;
62 const std::vector
<int32
>& created_ids() { return created_ids_
; }
63 const std::vector
<int32
>& node_creation_finished_ids() {
64 return node_creation_finished_ids_
;
66 const std::vector
<int32
>& subtree_creation_finished_ids() {
67 return subtree_creation_finished_ids_
;
69 const std::vector
<int32
>& change_finished_ids() {
70 return change_finished_ids_
;
75 std::vector
<int32
> deleted_ids_
;
76 std::vector
<int32
> subtree_deleted_ids_
;
77 std::vector
<int32
> created_ids_
;
78 std::vector
<int32
> changed_ids_
;
79 std::vector
<int32
> node_creation_finished_ids_
;
80 std::vector
<int32
> subtree_creation_finished_ids_
;
81 std::vector
<int32
> change_finished_ids_
;
86 TEST(AXTreeTest
, SerializeSimpleAXTree
) {
89 root
.role
= AX_ROLE_ROOT_WEB_AREA
;
90 root
.state
= (1 << AX_STATE_FOCUSABLE
) | (1 << AX_STATE_FOCUSED
);
91 root
.location
= gfx::Rect(0, 0, 800, 600);
92 root
.child_ids
.push_back(2);
93 root
.child_ids
.push_back(3);
97 button
.role
= AX_ROLE_BUTTON
;
99 button
.location
= gfx::Rect(20, 20, 200, 30);
103 checkbox
.role
= AX_ROLE_CHECK_BOX
;
105 checkbox
.location
= gfx::Rect(20, 50, 200, 30);
107 AXTreeUpdate initial_state
;
108 initial_state
.nodes
.push_back(root
);
109 initial_state
.nodes
.push_back(button
);
110 initial_state
.nodes
.push_back(checkbox
);
111 AXSerializableTree
src_tree(initial_state
);
113 scoped_ptr
<AXTreeSource
<const AXNode
*> > tree_source(
114 src_tree
.CreateTreeSource());
115 AXTreeSerializer
<const AXNode
*> serializer(tree_source
.get());
117 serializer
.SerializeChanges(src_tree
.root(), &update
);
120 ASSERT_TRUE(dst_tree
.Unserialize(update
));
122 const AXNode
* root_node
= dst_tree
.root();
123 ASSERT_TRUE(root_node
!= NULL
);
124 EXPECT_EQ(root
.id
, root_node
->id());
125 EXPECT_EQ(root
.role
, root_node
->data().role
);
127 ASSERT_EQ(2, root_node
->child_count());
129 const AXNode
* button_node
= root_node
->ChildAtIndex(0);
130 EXPECT_EQ(button
.id
, button_node
->id());
131 EXPECT_EQ(button
.role
, button_node
->data().role
);
133 const AXNode
* checkbox_node
= root_node
->ChildAtIndex(1);
134 EXPECT_EQ(checkbox
.id
, checkbox_node
->id());
135 EXPECT_EQ(checkbox
.role
, checkbox_node
->data().role
);
138 "id=1 rootWebArea FOCUSABLE FOCUSED (0, 0)-(800, 600) child_ids=2,3\n"
139 " id=2 button (20, 20)-(200, 30)\n"
140 " id=3 checkBox (20, 50)-(200, 30)\n",
141 dst_tree
.ToString());
144 TEST(AXTreeTest
, SerializeAXTreeUpdate
) {
147 list
.role
= AX_ROLE_LIST
;
149 list
.child_ids
.push_back(4);
150 list
.child_ids
.push_back(5);
151 list
.child_ids
.push_back(6);
153 AXNodeData list_item_2
;
155 list_item_2
.role
= AX_ROLE_LIST_ITEM
;
156 list_item_2
.state
= 0;
158 AXNodeData list_item_3
;
160 list_item_3
.role
= AX_ROLE_LIST_ITEM
;
161 list_item_3
.state
= 0;
165 button
.role
= AX_ROLE_BUTTON
;
169 update
.nodes
.push_back(list
);
170 update
.nodes
.push_back(list_item_2
);
171 update
.nodes
.push_back(list_item_3
);
172 update
.nodes
.push_back(button
);
175 "id=3 list (0, 0)-(0, 0) child_ids=4,5,6\n"
176 " id=5 listItem (0, 0)-(0, 0)\n"
177 " id=6 listItem (0, 0)-(0, 0)\n"
178 "id=7 button (0, 0)-(0, 0)\n",
182 TEST(AXTreeTest
, DeleteUnknownSubtreeFails
) {
185 root
.role
= AX_ROLE_ROOT_WEB_AREA
;
187 AXTreeUpdate initial_state
;
188 initial_state
.nodes
.push_back(root
);
189 AXTree
tree(initial_state
);
191 // This should fail because we're asking it to delete
192 // a subtree rooted at id=2, which doesn't exist.
194 update
.node_id_to_clear
= 2;
195 update
.nodes
.resize(1);
196 update
.nodes
[0].id
= 1;
197 update
.nodes
[0].id
= AX_ROLE_ROOT_WEB_AREA
;
198 EXPECT_FALSE(tree
.Unserialize(update
));
199 ASSERT_EQ("Bad node_id_to_clear: 2", tree
.error());
202 TEST(AXTreeTest
, LeaveOrphanedDeletedSubtreeFails
) {
203 AXTreeUpdate initial_state
;
204 initial_state
.nodes
.resize(3);
205 initial_state
.nodes
[0].id
= 1;
206 initial_state
.nodes
[0].role
= AX_ROLE_ROOT_WEB_AREA
;
207 initial_state
.nodes
[0].child_ids
.push_back(2);
208 initial_state
.nodes
[0].child_ids
.push_back(3);
209 initial_state
.nodes
[1].id
= 2;
210 initial_state
.nodes
[2].id
= 3;
211 AXTree
tree(initial_state
);
213 // This should fail because we delete a subtree rooted at id=2
214 // but never update it.
216 update
.node_id_to_clear
= 2;
217 update
.nodes
.resize(1);
218 update
.nodes
[0].id
= 3;
219 EXPECT_FALSE(tree
.Unserialize(update
));
220 ASSERT_EQ("Nodes left pending by the update: 2", tree
.error());
223 TEST(AXTreeTest
, LeaveOrphanedNewChildFails
) {
224 AXTreeUpdate initial_state
;
225 initial_state
.nodes
.resize(1);
226 initial_state
.nodes
[0].id
= 1;
227 initial_state
.nodes
[0].role
= AX_ROLE_ROOT_WEB_AREA
;
228 AXTree
tree(initial_state
);
230 // This should fail because we add a new child to the root node
231 // but never update it.
233 update
.nodes
.resize(1);
234 update
.nodes
[0].id
= 1;
235 update
.nodes
[0].role
= AX_ROLE_ROOT_WEB_AREA
;
236 update
.nodes
[0].child_ids
.push_back(2);
237 EXPECT_FALSE(tree
.Unserialize(update
));
238 ASSERT_EQ("Nodes left pending by the update: 2", tree
.error());
241 TEST(AXTreeTest
, DuplicateChildIdFails
) {
242 AXTreeUpdate initial_state
;
243 initial_state
.nodes
.resize(1);
244 initial_state
.nodes
[0].id
= 1;
245 initial_state
.nodes
[0].role
= AX_ROLE_ROOT_WEB_AREA
;
246 AXTree
tree(initial_state
);
248 // This should fail because a child id appears twice.
250 update
.nodes
.resize(2);
251 update
.nodes
[0].id
= 1;
252 update
.nodes
[0].role
= AX_ROLE_ROOT_WEB_AREA
;
253 update
.nodes
[0].child_ids
.push_back(2);
254 update
.nodes
[0].child_ids
.push_back(2);
255 update
.nodes
[1].id
= 2;
256 EXPECT_FALSE(tree
.Unserialize(update
));
257 ASSERT_EQ("Node 1 has duplicate child id 2", tree
.error());
260 TEST(AXTreeTest
, InvalidReparentingFails
) {
261 AXTreeUpdate initial_state
;
262 initial_state
.nodes
.resize(3);
263 initial_state
.nodes
[0].id
= 1;
264 initial_state
.nodes
[0].role
= AX_ROLE_ROOT_WEB_AREA
;
265 initial_state
.nodes
[0].child_ids
.push_back(2);
266 initial_state
.nodes
[1].id
= 2;
267 initial_state
.nodes
[1].child_ids
.push_back(3);
268 initial_state
.nodes
[2].id
= 3;
270 AXTree
tree(initial_state
);
272 // This should fail because node 3 is reparented from node 2 to node 1
273 // without deleting node 1's subtree first.
275 update
.nodes
.resize(3);
276 update
.nodes
[0].id
= 1;
277 update
.nodes
[0].role
= AX_ROLE_ROOT_WEB_AREA
;
278 update
.nodes
[0].child_ids
.push_back(3);
279 update
.nodes
[0].child_ids
.push_back(2);
280 update
.nodes
[1].id
= 2;
281 update
.nodes
[2].id
= 3;
282 EXPECT_FALSE(tree
.Unserialize(update
));
283 ASSERT_EQ("Node 3 reparented from 2 to 1", tree
.error());
286 TEST(AXTreeTest
, TreeDelegateIsCalled
) {
287 AXTreeUpdate initial_state
;
288 initial_state
.nodes
.resize(2);
289 initial_state
.nodes
[0].id
= 1;
290 initial_state
.nodes
[0].role
= AX_ROLE_ROOT_WEB_AREA
;
291 initial_state
.nodes
[0].child_ids
.push_back(2);
292 initial_state
.nodes
[1].id
= 2;
294 AXTree
tree(initial_state
);
296 update
.node_id_to_clear
= 1;
297 update
.nodes
.resize(2);
298 update
.nodes
[0].id
= 3;
299 update
.nodes
[0].role
= AX_ROLE_ROOT_WEB_AREA
;
300 update
.nodes
[0].child_ids
.push_back(4);
301 update
.nodes
[1].id
= 4;
303 FakeAXTreeDelegate fake_delegate
;
304 tree
.SetDelegate(&fake_delegate
);
306 EXPECT_TRUE(tree
.Unserialize(update
));
308 ASSERT_EQ(2U, fake_delegate
.deleted_ids().size());
309 EXPECT_EQ(2, fake_delegate
.deleted_ids()[0]);
310 EXPECT_EQ(1, fake_delegate
.deleted_ids()[1]);
312 ASSERT_EQ(1U, fake_delegate
.subtree_deleted_ids().size());
313 EXPECT_EQ(1, fake_delegate
.subtree_deleted_ids()[0]);
315 ASSERT_EQ(2U, fake_delegate
.created_ids().size());
316 EXPECT_EQ(3, fake_delegate
.created_ids()[0]);
317 EXPECT_EQ(4, fake_delegate
.created_ids()[1]);
319 ASSERT_EQ(1U, fake_delegate
.subtree_creation_finished_ids().size());
320 EXPECT_EQ(3, fake_delegate
.subtree_creation_finished_ids()[0]);
322 ASSERT_EQ(1U, fake_delegate
.node_creation_finished_ids().size());
323 EXPECT_EQ(4, fake_delegate
.node_creation_finished_ids()[0]);
325 ASSERT_EQ(true, fake_delegate
.root_changed());
327 tree
.SetDelegate(NULL
);