1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/accessibility/ax_node.h"
#include "ui/accessibility/ax_serializable_tree.h"
#include "ui/accessibility/ax_tree.h"
#include "ui/accessibility/ax_tree_serializer.h"
namespace ui {
// The framework for these tests is that each test sets up |treedata0_|
// and |treedata1_| and then calls GetTreeSerializer, which creates a
// serializer for a tree that's initially in state |treedata0_|, but then
// changes to state |treedata1_|. This allows each test to check the
// updates created by AXTreeSerializer or unit-test its private
// member functions.
class AXTreeSerializerTest : public testing::Test {
public:
AXTreeSerializerTest() {}
virtual ~AXTreeSerializerTest() {}
protected:
void CreateTreeSerializer();
AXTreeUpdate treedata0_;
AXTreeUpdate treedata1_;
scoped_ptr<AXSerializableTree> tree0_;
scoped_ptr<AXSerializableTree> tree1_;
scoped_ptr<AXTreeSource<AXNode> > tree0_source_;
scoped_ptr<AXTreeSource<AXNode> > tree1_source_;
scoped_ptr<AXTreeSerializer<AXNode> > serializer_;
private:
DISALLOW_COPY_AND_ASSIGN(AXTreeSerializerTest);
};
void AXTreeSerializerTest::CreateTreeSerializer() {
if (serializer_)
return;
tree0_.reset(new AXSerializableTree(treedata0_));
tree1_.reset(new AXSerializableTree(treedata1_));
// Serialize tree0 so that AXTreeSerializer thinks that its client
// is totally in sync.
tree0_source_.reset(tree0_->CreateTreeSource());
serializer_.reset(new AXTreeSerializer<AXNode>(tree0_source_.get()));
AXTreeUpdate unused_update;
serializer_->SerializeChanges(tree0_->GetRoot(), &unused_update);
// Pretend that tree0_ turned into tree1_. The next call to
// AXTreeSerializer will force it to consider these changes to
// the tree and send them as part of the next update.
tree1_source_.reset(tree1_->CreateTreeSource());
serializer_->ChangeTreeSourceForTesting(tree1_source_.get());
}
// In this test, one child is added to the root. Only the root and
// new child should be added.
TEST_F(AXTreeSerializerTest, UpdateContainsOnlyChangedNodes) {
// (1 (2 3))
treedata0_.nodes.resize(3);
treedata0_.nodes[0].id = 1;
treedata0_.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
treedata0_.nodes[0].child_ids.push_back(2);
treedata0_.nodes[0].child_ids.push_back(3);
treedata0_.nodes[1].id = 2;
treedata0_.nodes[2].id = 3;
// (1 (4 2 3))
treedata1_.nodes.resize(4);
treedata1_.nodes[0].id = 1;
treedata1_.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
treedata1_.nodes[0].child_ids.push_back(4);
treedata1_.nodes[0].child_ids.push_back(2);
treedata1_.nodes[0].child_ids.push_back(3);
treedata1_.nodes[1].id = 2;
treedata1_.nodes[2].id = 3;
treedata1_.nodes[3].id = 4;
CreateTreeSerializer();
AXTreeUpdate update;
serializer_->SerializeChanges(tree1_->GetFromId(1), &update);
// The update should only touch nodes 1 and 4 - nodes 2 and 3 are unchanged
// and shouldn't be affected.
EXPECT_EQ(0, update.node_id_to_clear);
ASSERT_EQ(static_cast<size_t>(2), update.nodes.size());
EXPECT_EQ(1, update.nodes[0].id);
EXPECT_EQ(4, update.nodes[1].id);
}
// When the root changes, the whole tree is updated, even if some of it
// is unaffected.
TEST_F(AXTreeSerializerTest, NewRootUpdatesEntireTree) {
// (1 (2 (3 (4))))
treedata0_.nodes.resize(4);
treedata0_.nodes[0].id = 1;
treedata0_.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
treedata0_.nodes[0].child_ids.push_back(2);
treedata0_.nodes[1].id = 2;
treedata0_.nodes[1].child_ids.push_back(3);
treedata0_.nodes[2].id = 3;
treedata0_.nodes[2].child_ids.push_back(4);
treedata0_.nodes[3].id = 4;
// (5 (2 (3 (4))))
treedata1_.nodes.resize(4);
treedata1_.nodes[0].id = 5;
treedata1_.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
treedata1_.nodes[0].child_ids.push_back(2);
treedata1_.nodes[1].id = 2;
treedata1_.nodes[1].child_ids.push_back(3);
treedata1_.nodes[2].id = 3;
treedata1_.nodes[2].child_ids.push_back(4);
treedata1_.nodes[3].id = 4;
CreateTreeSerializer();
AXTreeUpdate update;
serializer_->SerializeChanges(tree1_->GetFromId(4), &update);
// The update should delete the subtree rooted at node id=1, and
// then include all four nodes in the update, even though the
// subtree rooted at id=2 didn't actually change.
EXPECT_EQ(1, update.node_id_to_clear);
ASSERT_EQ(static_cast<size_t>(4), update.nodes.size());
EXPECT_EQ(5, update.nodes[0].id);
EXPECT_EQ(2, update.nodes[1].id);
EXPECT_EQ(3, update.nodes[2].id);
EXPECT_EQ(4, update.nodes[3].id);
}
// When a node is reparented, the subtree including both the old parent
// and new parent of the reparented node must be deleted and recreated.
TEST_F(AXTreeSerializerTest, ReparentingUpdatesSubtree) {
// (1 (2 (3 (4) 5)))
treedata0_.nodes.resize(5);
treedata0_.nodes[0].id = 1;
treedata0_.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
treedata0_.nodes[0].child_ids.push_back(2);
treedata0_.nodes[1].id = 2;
treedata0_.nodes[1].child_ids.push_back(3);
treedata0_.nodes[1].child_ids.push_back(5);
treedata0_.nodes[2].id = 3;
treedata0_.nodes[2].child_ids.push_back(4);
treedata0_.nodes[3].id = 4;
treedata0_.nodes[4].id = 5;
// Node 5 has been reparented from being a child of node 2,
// to a child of node 4.
// (1 (2 (3 (4 (5)))))
treedata1_.nodes.resize(5);
treedata1_.nodes[0].id = 1;
treedata1_.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
treedata1_.nodes[0].child_ids.push_back(2);
treedata1_.nodes[1].id = 2;
treedata1_.nodes[1].child_ids.push_back(3);
treedata1_.nodes[2].id = 3;
treedata1_.nodes[2].child_ids.push_back(4);
treedata1_.nodes[3].id = 4;
treedata1_.nodes[3].child_ids.push_back(5);
treedata1_.nodes[4].id = 5;
CreateTreeSerializer();
AXTreeUpdate update;
serializer_->SerializeChanges(tree1_->GetFromId(4), &update);
// The update should delete the subtree rooted at node id=2, and
// then include nodes 2...5.
EXPECT_EQ(2, update.node_id_to_clear);
ASSERT_EQ(static_cast<size_t>(4), update.nodes.size());
EXPECT_EQ(2, update.nodes[0].id);
EXPECT_EQ(3, update.nodes[1].id);
EXPECT_EQ(4, update.nodes[2].id);
EXPECT_EQ(5, update.nodes[3].id);
}
} // namespace ui
|