Roll src/third_party/WebKit 06cb9e9:a978ee5 (svn 202558:202559)
[chromium-blink-merge.git] / ui / accessibility / ax_tree_serializer_unittest.cc
blobd22a663c57ed39303bb6223d9a3c72fabf751316
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 using BasicAXTreeSerializer = AXTreeSerializer<const AXNode*, AXNodeData>;
17 // The framework for these tests is that each test sets up |treedata0_|
18 // and |treedata1_| and then calls GetTreeSerializer, which creates a
19 // serializer for a tree that's initially in state |treedata0_|, but then
20 // changes to state |treedata1_|. This allows each test to check the
21 // updates created by AXTreeSerializer or unit-test its private
22 // member functions.
23 class AXTreeSerializerTest : public testing::Test {
24 public:
25 AXTreeSerializerTest() {}
26 ~AXTreeSerializerTest() override {}
28 protected:
29 void CreateTreeSerializer();
31 AXTreeUpdate<AXNodeData> treedata0_;
32 AXTreeUpdate<AXNodeData> treedata1_;
33 scoped_ptr<AXSerializableTree> tree0_;
34 scoped_ptr<AXSerializableTree> tree1_;
35 scoped_ptr<AXTreeSource<const AXNode*, AXNodeData> > tree0_source_;
36 scoped_ptr<AXTreeSource<const AXNode*, AXNodeData> > tree1_source_;
37 scoped_ptr<BasicAXTreeSerializer > serializer_;
39 private:
40 DISALLOW_COPY_AND_ASSIGN(AXTreeSerializerTest);
43 void AXTreeSerializerTest::CreateTreeSerializer() {
44 if (serializer_)
45 return;
47 tree0_.reset(new AXSerializableTree(treedata0_));
48 tree1_.reset(new AXSerializableTree(treedata1_));
50 // Serialize tree0 so that AXTreeSerializer thinks that its client
51 // is totally in sync.
52 tree0_source_.reset(tree0_->CreateTreeSource());
53 serializer_.reset(new BasicAXTreeSerializer(tree0_source_.get()));
54 AXTreeUpdate<AXNodeData> unused_update;
55 serializer_->SerializeChanges(tree0_->root(), &unused_update);
57 // Pretend that tree0_ turned into tree1_. The next call to
58 // AXTreeSerializer will force it to consider these changes to
59 // the tree and send them as part of the next update.
60 tree1_source_.reset(tree1_->CreateTreeSource());
61 serializer_->ChangeTreeSourceForTesting(tree1_source_.get());
64 // In this test, one child is added to the root. Only the root and
65 // new child should be added.
66 TEST_F(AXTreeSerializerTest, UpdateContainsOnlyChangedNodes) {
67 // (1 (2 3))
68 treedata0_.nodes.resize(3);
69 treedata0_.nodes[0].id = 1;
70 treedata0_.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
71 treedata0_.nodes[0].child_ids.push_back(2);
72 treedata0_.nodes[0].child_ids.push_back(3);
73 treedata0_.nodes[1].id = 2;
74 treedata0_.nodes[2].id = 3;
76 // (1 (4 2 3))
77 treedata1_.nodes.resize(4);
78 treedata1_.nodes[0].id = 1;
79 treedata1_.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
80 treedata1_.nodes[0].child_ids.push_back(4);
81 treedata1_.nodes[0].child_ids.push_back(2);
82 treedata1_.nodes[0].child_ids.push_back(3);
83 treedata1_.nodes[1].id = 2;
84 treedata1_.nodes[2].id = 3;
85 treedata1_.nodes[3].id = 4;
87 CreateTreeSerializer();
88 AXTreeUpdate<AXNodeData> update;
89 serializer_->SerializeChanges(tree1_->GetFromId(1), &update);
91 // The update should only touch nodes 1 and 4 - nodes 2 and 3 are unchanged
92 // and shouldn't be affected.
93 EXPECT_EQ(0, update.node_id_to_clear);
94 ASSERT_EQ(static_cast<size_t>(2), update.nodes.size());
95 EXPECT_EQ(1, update.nodes[0].id);
96 EXPECT_EQ(4, update.nodes[1].id);
99 // When the root changes, the whole tree is updated, even if some of it
100 // is unaffected.
101 TEST_F(AXTreeSerializerTest, NewRootUpdatesEntireTree) {
102 // (1 (2 (3 (4))))
103 treedata0_.nodes.resize(4);
104 treedata0_.nodes[0].id = 1;
105 treedata0_.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
106 treedata0_.nodes[0].child_ids.push_back(2);
107 treedata0_.nodes[1].id = 2;
108 treedata0_.nodes[1].child_ids.push_back(3);
109 treedata0_.nodes[2].id = 3;
110 treedata0_.nodes[2].child_ids.push_back(4);
111 treedata0_.nodes[3].id = 4;
113 // (5 (2 (3 (4))))
114 treedata1_.nodes.resize(4);
115 treedata1_.nodes[0].id = 5;
116 treedata1_.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
117 treedata1_.nodes[0].child_ids.push_back(2);
118 treedata1_.nodes[1].id = 2;
119 treedata1_.nodes[1].child_ids.push_back(3);
120 treedata1_.nodes[2].id = 3;
121 treedata1_.nodes[2].child_ids.push_back(4);
122 treedata1_.nodes[3].id = 4;
124 CreateTreeSerializer();
125 AXTreeUpdate<AXNodeData> update;
126 serializer_->SerializeChanges(tree1_->GetFromId(4), &update);
128 // The update should delete the subtree rooted at node id=1, and
129 // then include all four nodes in the update, even though the
130 // subtree rooted at id=2 didn't actually change.
131 EXPECT_EQ(1, update.node_id_to_clear);
132 ASSERT_EQ(static_cast<size_t>(4), update.nodes.size());
133 EXPECT_EQ(5, update.nodes[0].id);
134 EXPECT_EQ(2, update.nodes[1].id);
135 EXPECT_EQ(3, update.nodes[2].id);
136 EXPECT_EQ(4, update.nodes[3].id);
139 // When a node is reparented, the subtree including both the old parent
140 // and new parent of the reparented node must be deleted and recreated.
141 TEST_F(AXTreeSerializerTest, ReparentingUpdatesSubtree) {
142 // (1 (2 (3 (4) 5)))
143 treedata0_.nodes.resize(5);
144 treedata0_.nodes[0].id = 1;
145 treedata0_.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
146 treedata0_.nodes[0].child_ids.push_back(2);
147 treedata0_.nodes[1].id = 2;
148 treedata0_.nodes[1].child_ids.push_back(3);
149 treedata0_.nodes[1].child_ids.push_back(5);
150 treedata0_.nodes[2].id = 3;
151 treedata0_.nodes[2].child_ids.push_back(4);
152 treedata0_.nodes[3].id = 4;
153 treedata0_.nodes[4].id = 5;
155 // Node 5 has been reparented from being a child of node 2,
156 // to a child of node 4.
157 // (1 (2 (3 (4 (5)))))
158 treedata1_.nodes.resize(5);
159 treedata1_.nodes[0].id = 1;
160 treedata1_.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
161 treedata1_.nodes[0].child_ids.push_back(2);
162 treedata1_.nodes[1].id = 2;
163 treedata1_.nodes[1].child_ids.push_back(3);
164 treedata1_.nodes[2].id = 3;
165 treedata1_.nodes[2].child_ids.push_back(4);
166 treedata1_.nodes[3].id = 4;
167 treedata1_.nodes[3].child_ids.push_back(5);
168 treedata1_.nodes[4].id = 5;
170 CreateTreeSerializer();
171 AXTreeUpdate<AXNodeData> update;
172 serializer_->SerializeChanges(tree1_->GetFromId(4), &update);
174 // The update should delete the subtree rooted at node id=2, and
175 // then include nodes 2...5.
176 EXPECT_EQ(2, update.node_id_to_clear);
177 ASSERT_EQ(static_cast<size_t>(4), update.nodes.size());
178 EXPECT_EQ(2, update.nodes[0].id);
179 EXPECT_EQ(3, update.nodes[1].id);
180 EXPECT_EQ(4, update.nodes[2].id);
181 EXPECT_EQ(5, update.nodes[3].id);
184 // A variant of AXTreeSource that returns true for IsValid() for one
185 // particular id.
186 class AXTreeSourceWithInvalidId
187 : public AXTreeSource<const AXNode*, AXNodeData> {
188 public:
189 AXTreeSourceWithInvalidId(AXTree* tree, int invalid_id)
190 : tree_(tree),
191 invalid_id_(invalid_id) {}
192 ~AXTreeSourceWithInvalidId() override {}
194 // AXTreeSource implementation.
195 AXNode* GetRoot() const override { return tree_->root(); }
196 AXNode* GetFromId(int32 id) const override { return tree_->GetFromId(id); }
197 int32 GetId(const AXNode* node) const override { return node->id(); }
198 void GetChildren(const AXNode* node,
199 std::vector<const AXNode*>* out_children) const override {
200 for (int i = 0; i < node->child_count(); ++i)
201 out_children->push_back(node->ChildAtIndex(i));
203 AXNode* GetParent(const AXNode* node) const override {
204 return node->parent();
206 bool IsValid(const AXNode* node) const override {
207 return node != NULL && node->id() != invalid_id_;
209 bool IsEqual(const AXNode* node1, const AXNode* node2) const override {
210 return node1 == node2;
212 const AXNode* GetNull() const override { return NULL; }
213 void SerializeNode(const AXNode* node, AXNodeData* out_data) const override {
214 *out_data = node->data();
215 if (node->id() == invalid_id_)
216 out_data->id = -1;
219 private:
220 AXTree* tree_;
221 int invalid_id_;
223 DISALLOW_COPY_AND_ASSIGN(AXTreeSourceWithInvalidId);
226 // Test that the serializer skips invalid children.
227 TEST(AXTreeSerializerInvalidTest, InvalidChild) {
228 // (1 (2 3))
229 AXTreeUpdate<AXNodeData> treedata;
230 treedata.nodes.resize(3);
231 treedata.nodes[0].id = 1;
232 treedata.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
233 treedata.nodes[0].child_ids.push_back(2);
234 treedata.nodes[0].child_ids.push_back(3);
235 treedata.nodes[1].id = 2;
236 treedata.nodes[2].id = 3;
238 AXTree tree(treedata);
239 AXTreeSourceWithInvalidId source(&tree, 3);
241 BasicAXTreeSerializer serializer(&source);
242 AXTreeUpdate<AXNodeData> update;
243 serializer.SerializeChanges(tree.root(), &update);
245 ASSERT_EQ(2U, update.nodes.size());
246 EXPECT_EQ(1, update.nodes[0].id);
247 EXPECT_EQ(2, update.nodes[1].id);
250 // Test that we can set a maximum number of nodes to serialize.
251 TEST_F(AXTreeSerializerTest, MaximumSerializedNodeCount) {
252 // (1 (2 (3 4) 5 (6 7)))
253 treedata0_.nodes.resize(7);
254 treedata0_.nodes[0].id = 1;
255 treedata0_.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
256 treedata0_.nodes[0].child_ids.push_back(2);
257 treedata0_.nodes[0].child_ids.push_back(5);
258 treedata0_.nodes[1].id = 2;
259 treedata0_.nodes[1].child_ids.push_back(3);
260 treedata0_.nodes[1].child_ids.push_back(4);
261 treedata0_.nodes[2].id = 3;
262 treedata0_.nodes[3].id = 4;
263 treedata0_.nodes[4].id = 5;
264 treedata0_.nodes[4].child_ids.push_back(6);
265 treedata0_.nodes[4].child_ids.push_back(7);
266 treedata0_.nodes[5].id = 6;
267 treedata0_.nodes[6].id = 7;
269 tree0_.reset(new AXSerializableTree(treedata0_));
270 tree0_source_.reset(tree0_->CreateTreeSource());
271 serializer_.reset(new BasicAXTreeSerializer(tree0_source_.get()));
272 serializer_->set_max_node_count(4);
273 AXTreeUpdate<AXNodeData> update;
274 serializer_->SerializeChanges(tree0_->root(), &update);
275 // It actually serializes 5 nodes, not 4 - to be consistent.
276 // It skips the children of node 5.
277 ASSERT_EQ(static_cast<size_t>(5), update.nodes.size());
280 } // namespace ui