Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(232)

Side by Side Diff: ui/accessibility/ax_tree_unittest.cc

Issue 90853002: Make tree serialization more robust and add more unit tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/memory/scoped_ptr.h" 5 #include "base/memory/scoped_ptr.h"
6 #include "base/strings/string_number_conversions.h"
6 #include "testing/gtest/include/gtest/gtest.h" 7 #include "testing/gtest/include/gtest/gtest.h"
7 #include "ui/accessibility/ax_node.h" 8 #include "ui/accessibility/ax_node.h"
8 #include "ui/accessibility/ax_serializable_tree.h" 9 #include "ui/accessibility/ax_serializable_tree.h"
9 #include "ui/accessibility/ax_tree.h" 10 #include "ui/accessibility/ax_tree.h"
10 #include "ui/accessibility/ax_tree_serializer.h" 11 #include "ui/accessibility/ax_tree_serializer.h"
11 12
12 namespace ui { 13 namespace ui {
13 14
14 TEST(AXTreeTest, TestSerialize) { 15 TEST(AXTreeTest, SerializeSimpleAXTree) {
15 AXNodeData root; 16 AXNodeData root;
16 root.id = 1; 17 root.id = 1;
17 root.role = AX_ROLE_ROOT_WEB_AREA; 18 root.role = AX_ROLE_ROOT_WEB_AREA;
18 root.child_ids.push_back(2); 19 root.child_ids.push_back(2);
19 root.child_ids.push_back(3); 20 root.child_ids.push_back(3);
20 21
21 AXNodeData button; 22 AXNodeData button;
22 button.id = 2; 23 button.id = 2;
23 button.role = AX_ROLE_BUTTON; 24 button.role = AX_ROLE_BUTTON;
24 button.state = 0; 25 button.state = 0;
(...skipping 26 matching lines...) Expand all
51 52
52 AXNode* button_node = root_node->ChildAtIndex(0); 53 AXNode* button_node = root_node->ChildAtIndex(0);
53 EXPECT_EQ(button.id, button_node->id()); 54 EXPECT_EQ(button.id, button_node->id());
54 EXPECT_EQ(button.role, button_node->data().role); 55 EXPECT_EQ(button.role, button_node->data().role);
55 56
56 AXNode* checkbox_node = root_node->ChildAtIndex(1); 57 AXNode* checkbox_node = root_node->ChildAtIndex(1);
57 EXPECT_EQ(checkbox.id, checkbox_node->id()); 58 EXPECT_EQ(checkbox.id, checkbox_node->id());
58 EXPECT_EQ(checkbox.role, checkbox_node->data().role); 59 EXPECT_EQ(checkbox.role, checkbox_node->data().role);
59 } 60 }
60 61
62 TEST(AXTreeTest, EmptyUpdateFails) {
aboxhall 2013/12/02 17:18:08 This test doesn't assert anything?
dmazzoni 2013/12/03 08:35:04 Good catch, thanks. I actually changed my mind abo
63 AXNodeData root;
64 root.id = 1;
65 root.role = AX_ROLE_ROOT_WEB_AREA;
66
67 AXTreeUpdate initial_state;
68 initial_state.nodes.push_back(root);
69 AXSerializableTree src_tree(initial_state);
70
71 AXTreeUpdate update;
72 update.node_id_to_clear = 2;
73 }
74
75 TEST(AXTreeTest, DeleteUnknownSubtreeFails) {
76 AXNodeData root;
77 root.id = 1;
78 root.role = AX_ROLE_ROOT_WEB_AREA;
79
80 AXTreeUpdate initial_state;
81 initial_state.nodes.push_back(root);
82 AXTree tree(initial_state);
83
84 // This should fail because we're asking it to delete
85 // a subtree rooted at id=2, which doesn't exist.
86 AXTreeUpdate update;
87 update.node_id_to_clear = 2;
88 update.nodes.resize(1);
89 update.nodes[0].id = 1;
90 update.nodes[0].id = AX_ROLE_ROOT_WEB_AREA;
91 EXPECT_FALSE(tree.Unserialize(update));
92 ASSERT_EQ("Bad node_id_to_clear: 2", tree.error());
93 }
94
95 TEST(AXTreeTest, LeaveOrphanedDeletedSubtreeFails) {
96 AXTreeUpdate initial_state;
97 initial_state.nodes.resize(3);
98 initial_state.nodes[0].id = 1;
99 initial_state.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
100 initial_state.nodes[0].child_ids.push_back(2);
101 initial_state.nodes[0].child_ids.push_back(3);
102 initial_state.nodes[1].id = 2;
103 initial_state.nodes[2].id = 3;
104 AXTree tree(initial_state);
105
106 // This should fail because we delete a subtree rooted at id=2
107 // but never update it.
108 AXTreeUpdate update;
109 update.node_id_to_clear = 2;
110 update.nodes.resize(1);
111 update.nodes[0].id = 3;
112 EXPECT_FALSE(tree.Unserialize(update));
113 ASSERT_EQ("Nodes left pending by the update: 2", tree.error());
114 }
115
116 TEST(AXTreeTest, LeaveOrphanedNewChildFails) {
117 AXTreeUpdate initial_state;
118 initial_state.nodes.resize(1);
119 initial_state.nodes[0].id = 1;
120 initial_state.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
121 AXTree tree(initial_state);
122
123 // This should fail because we add a new child to the root node
124 // but never update it.
125 AXTreeUpdate update;
126 update.nodes.resize(1);
127 update.nodes[0].id = 1;
128 update.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
129 update.nodes[0].child_ids.push_back(2);
130 EXPECT_FALSE(tree.Unserialize(update));
131 ASSERT_EQ("Nodes left pending by the update: 2", tree.error());
132 }
133
134 TEST(AXTreeTest, DuplicateChildIdFails) {
135 AXTreeUpdate initial_state;
136 initial_state.nodes.resize(1);
137 initial_state.nodes[0].id = 1;
138 initial_state.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
139 AXTree tree(initial_state);
140
141 // This should fail because a child id appears twice.
142 AXTreeUpdate update;
143 update.nodes.resize(2);
144 update.nodes[0].id = 1;
145 update.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
146 update.nodes[0].child_ids.push_back(2);
147 update.nodes[0].child_ids.push_back(2);
148 update.nodes[1].id = 2;
149 EXPECT_FALSE(tree.Unserialize(update));
150 ASSERT_EQ("Node 1 has duplicate child id 2", tree.error());
151 }
152
153 TEST(AXTreeTest, InvalidReparentingFails) {
154 AXTreeUpdate initial_state;
155 initial_state.nodes.resize(3);
156 initial_state.nodes[0].id = 1;
157 initial_state.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
158 initial_state.nodes[0].child_ids.push_back(2);
159 initial_state.nodes[1].id = 2;
160 initial_state.nodes[1].child_ids.push_back(3);
161 initial_state.nodes[2].id = 3;
162
163 AXTree tree(initial_state);
164
165 // This should fail because node 3 is reparented from node 2 to node 1
166 // without deleting node 1's subtree first.
167 AXTreeUpdate update;
168 update.nodes.resize(3);
169 update.nodes[0].id = 1;
170 update.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
171 update.nodes[0].child_ids.push_back(3);
172 update.nodes[0].child_ids.push_back(2);
173 update.nodes[1].id = 2;
174 update.nodes[2].id = 3;
175 EXPECT_FALSE(tree.Unserialize(update));
176 ASSERT_EQ("Node 3 reparented from 2 to 1", tree.error());
177 }
178
61 } // namespace ui 179 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698