Migrate TabUmaTest from ChromeShell to ChromePublic.
[chromium-blink-merge.git] / ui / accessibility / ax_tree_unittest.cc
blob40aeeffd0dd8e04d78f11bb5e20113f37de76bfa
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 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*> > tree_source(
115 src_tree.CreateTreeSource());
116 AXTreeSerializer<const AXNode*> serializer(tree_source.get());
117 AXTreeUpdate update;
118 serializer.SerializeChanges(src_tree.root(), &update);
120 AXTree dst_tree;
121 ASSERT_TRUE(dst_tree.Unserialize(update));
123 const AXNode* root_node = dst_tree.root();
124 ASSERT_TRUE(root_node != NULL);
125 EXPECT_EQ(root.id, root_node->id());
126 EXPECT_EQ(root.role, root_node->data().role);
128 ASSERT_EQ(2, root_node->child_count());
130 const AXNode* button_node = root_node->ChildAtIndex(0);
131 EXPECT_EQ(button.id, button_node->id());
132 EXPECT_EQ(button.role, button_node->data().role);
134 const AXNode* checkbox_node = root_node->ChildAtIndex(1);
135 EXPECT_EQ(checkbox.id, checkbox_node->id());
136 EXPECT_EQ(checkbox.role, checkbox_node->data().role);
138 EXPECT_EQ(
139 "id=1 rootWebArea FOCUSABLE FOCUSED (0, 0)-(800, 600) child_ids=2,3\n"
140 " id=2 button (20, 20)-(200, 30)\n"
141 " id=3 checkBox (20, 50)-(200, 30)\n",
142 dst_tree.ToString());
145 TEST(AXTreeTest, SerializeAXTreeUpdate) {
146 AXNodeData list;
147 list.id = 3;
148 list.role = AX_ROLE_LIST;
149 list.state = 0;
150 list.child_ids.push_back(4);
151 list.child_ids.push_back(5);
152 list.child_ids.push_back(6);
154 AXNodeData list_item_2;
155 list_item_2.id = 5;
156 list_item_2.role = AX_ROLE_LIST_ITEM;
157 list_item_2.state = 0;
159 AXNodeData list_item_3;
160 list_item_3.id = 6;
161 list_item_3.role = AX_ROLE_LIST_ITEM;
162 list_item_3.state = 0;
164 AXNodeData button;
165 button.id = 7;
166 button.role = AX_ROLE_BUTTON;
167 button.state = 0;
169 AXTreeUpdate update;
170 update.nodes.push_back(list);
171 update.nodes.push_back(list_item_2);
172 update.nodes.push_back(list_item_3);
173 update.nodes.push_back(button);
175 EXPECT_EQ(
176 "id=3 list (0, 0)-(0, 0) child_ids=4,5,6\n"
177 " id=5 listItem (0, 0)-(0, 0)\n"
178 " id=6 listItem (0, 0)-(0, 0)\n"
179 "id=7 button (0, 0)-(0, 0)\n",
180 update.ToString());
183 TEST(AXTreeTest, DeleteUnknownSubtreeFails) {
184 AXNodeData root;
185 root.id = 1;
186 root.role = AX_ROLE_ROOT_WEB_AREA;
188 AXTreeUpdate initial_state;
189 initial_state.nodes.push_back(root);
190 AXTree tree(initial_state);
192 // This should fail because we're asking it to delete
193 // a subtree rooted at id=2, which doesn't exist.
194 AXTreeUpdate update;
195 update.node_id_to_clear = 2;
196 update.nodes.resize(1);
197 update.nodes[0].id = 1;
198 update.nodes[0].id = AX_ROLE_ROOT_WEB_AREA;
199 EXPECT_FALSE(tree.Unserialize(update));
200 ASSERT_EQ("Bad node_id_to_clear: 2", tree.error());
203 TEST(AXTreeTest, LeaveOrphanedDeletedSubtreeFails) {
204 AXTreeUpdate initial_state;
205 initial_state.nodes.resize(3);
206 initial_state.nodes[0].id = 1;
207 initial_state.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
208 initial_state.nodes[0].child_ids.push_back(2);
209 initial_state.nodes[0].child_ids.push_back(3);
210 initial_state.nodes[1].id = 2;
211 initial_state.nodes[2].id = 3;
212 AXTree tree(initial_state);
214 // This should fail because we delete a subtree rooted at id=2
215 // but never update it.
216 AXTreeUpdate update;
217 update.node_id_to_clear = 2;
218 update.nodes.resize(1);
219 update.nodes[0].id = 3;
220 EXPECT_FALSE(tree.Unserialize(update));
221 ASSERT_EQ("Nodes left pending by the update: 2", tree.error());
224 TEST(AXTreeTest, LeaveOrphanedNewChildFails) {
225 AXTreeUpdate initial_state;
226 initial_state.nodes.resize(1);
227 initial_state.nodes[0].id = 1;
228 initial_state.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
229 AXTree tree(initial_state);
231 // This should fail because we add a new child to the root node
232 // but never update it.
233 AXTreeUpdate update;
234 update.nodes.resize(1);
235 update.nodes[0].id = 1;
236 update.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
237 update.nodes[0].child_ids.push_back(2);
238 EXPECT_FALSE(tree.Unserialize(update));
239 ASSERT_EQ("Nodes left pending by the update: 2", tree.error());
242 TEST(AXTreeTest, DuplicateChildIdFails) {
243 AXTreeUpdate initial_state;
244 initial_state.nodes.resize(1);
245 initial_state.nodes[0].id = 1;
246 initial_state.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
247 AXTree tree(initial_state);
249 // This should fail because a child id appears twice.
250 AXTreeUpdate update;
251 update.nodes.resize(2);
252 update.nodes[0].id = 1;
253 update.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
254 update.nodes[0].child_ids.push_back(2);
255 update.nodes[0].child_ids.push_back(2);
256 update.nodes[1].id = 2;
257 EXPECT_FALSE(tree.Unserialize(update));
258 ASSERT_EQ("Node 1 has duplicate child id 2", tree.error());
261 TEST(AXTreeTest, InvalidReparentingFails) {
262 AXTreeUpdate initial_state;
263 initial_state.nodes.resize(3);
264 initial_state.nodes[0].id = 1;
265 initial_state.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
266 initial_state.nodes[0].child_ids.push_back(2);
267 initial_state.nodes[1].id = 2;
268 initial_state.nodes[1].child_ids.push_back(3);
269 initial_state.nodes[2].id = 3;
271 AXTree tree(initial_state);
273 // This should fail because node 3 is reparented from node 2 to node 1
274 // without deleting node 1's subtree first.
275 AXTreeUpdate update;
276 update.nodes.resize(3);
277 update.nodes[0].id = 1;
278 update.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
279 update.nodes[0].child_ids.push_back(3);
280 update.nodes[0].child_ids.push_back(2);
281 update.nodes[1].id = 2;
282 update.nodes[2].id = 3;
283 EXPECT_FALSE(tree.Unserialize(update));
284 ASSERT_EQ("Node 3 reparented from 2 to 1", tree.error());
287 TEST(AXTreeTest, TwoRootsFails) {
288 AXTreeUpdate initial_state;
289 initial_state.nodes.resize(1);
290 initial_state.nodes[0].id = 1;
291 initial_state.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
292 AXTree tree(initial_state);
294 // This should fail because there are two new roots.
295 AXTreeUpdate update;
296 update.nodes.resize(2);
297 update.nodes[0].id = 2;
298 update.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
299 update.nodes[1].id = 3;
300 update.nodes[1].role = AX_ROLE_ROOT_WEB_AREA;
301 EXPECT_FALSE(tree.Unserialize(update));
302 ASSERT_EQ("Tree update contains two new roots", tree.error());
305 TEST(AXTreeTest, TreeDelegateIsCalled) {
306 AXTreeUpdate initial_state;
307 initial_state.nodes.resize(2);
308 initial_state.nodes[0].id = 1;
309 initial_state.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
310 initial_state.nodes[0].child_ids.push_back(2);
311 initial_state.nodes[1].id = 2;
313 AXTree tree(initial_state);
314 AXTreeUpdate update;
315 update.node_id_to_clear = 1;
316 update.nodes.resize(2);
317 update.nodes[0].id = 3;
318 update.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
319 update.nodes[0].child_ids.push_back(4);
320 update.nodes[1].id = 4;
322 FakeAXTreeDelegate fake_delegate;
323 tree.SetDelegate(&fake_delegate);
325 EXPECT_TRUE(tree.Unserialize(update));
327 ASSERT_EQ(2U, fake_delegate.deleted_ids().size());
328 EXPECT_EQ(2, fake_delegate.deleted_ids()[0]);
329 EXPECT_EQ(1, fake_delegate.deleted_ids()[1]);
331 ASSERT_EQ(1U, fake_delegate.subtree_deleted_ids().size());
332 EXPECT_EQ(1, fake_delegate.subtree_deleted_ids()[0]);
334 ASSERT_EQ(2U, fake_delegate.created_ids().size());
335 EXPECT_EQ(3, fake_delegate.created_ids()[0]);
336 EXPECT_EQ(4, fake_delegate.created_ids()[1]);
338 ASSERT_EQ(1U, fake_delegate.subtree_creation_finished_ids().size());
339 EXPECT_EQ(3, fake_delegate.subtree_creation_finished_ids()[0]);
341 ASSERT_EQ(1U, fake_delegate.node_creation_finished_ids().size());
342 EXPECT_EQ(4, fake_delegate.node_creation_finished_ids()[0]);
344 ASSERT_EQ(true, fake_delegate.root_changed());
346 tree.SetDelegate(NULL);
349 } // namespace ui