1 // Copyright 2014 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"
12 #include "ui/accessibility/tree_generator.h"
17 // A function to turn a tree into a string, capturing only the node ids
18 // and their relationship to one another.
20 // The string format is kind of like an S-expression, with each expression
21 // being either a node id, or a node id followed by a subexpression
22 // representing its children.
26 // (1) is a tree with a single node with id 1.
27 // (1 (2 3)) is a tree with 1 as the root, and 2 and 3 as its children.
28 // (1 (2 (3))) has 1 as the root, 2 as its child, and then 3 as the child of 2.
29 void TreeToStringHelper(const AXNode
* node
, std::string
* out_result
) {
30 *out_result
+= base::IntToString(node
->id());
31 if (node
->child_count() != 0) {
33 for (int i
= 0; i
< node
->child_count(); ++i
) {
36 TreeToStringHelper(node
->ChildAtIndex(i
), out_result
);
42 std::string
TreeToString(const AXTree
& tree
) {
44 TreeToStringHelper(tree
.root(), &result
);
45 return "(" + result
+ ")";
48 } // anonymous namespace
50 // Test the TreeGenerator class by building all possible trees with
51 // 3 nodes and the ids [1...3], with no permutations of ids.
52 TEST(AXGeneratedTreeTest
, TestTreeGeneratorNoPermutations
) {
54 TreeGenerator
generator(tree_size
, false);
55 const char* EXPECTED_TREES
[] = {
62 int n
= generator
.UniqueTreeCount();
63 ASSERT_EQ(static_cast<int>(arraysize(EXPECTED_TREES
)), n
);
65 for (int i
= 0; i
< n
; ++i
) {
67 generator
.BuildUniqueTree(i
, &tree
);
68 std::string str
= TreeToString(tree
);
69 EXPECT_EQ(EXPECTED_TREES
[i
], str
);
73 // Test the TreeGenerator class by building all possible trees with
74 // 3 nodes and the ids [1...3] permuted in any order.
75 TEST(AXGeneratedTreeTest
, TestTreeGeneratorWithPermutations
) {
77 TreeGenerator
generator(tree_size
, true);
78 const char* EXPECTED_TREES
[] = {
96 int n
= generator
.UniqueTreeCount();
97 ASSERT_EQ(static_cast<int>(arraysize(EXPECTED_TREES
)), n
);
99 for (int i
= 0; i
< n
; i
++) {
101 generator
.BuildUniqueTree(i
, &tree
);
102 std::string str
= TreeToString(tree
);
103 EXPECT_EQ(EXPECTED_TREES
[i
], str
);
107 // Test mutating every possible tree with <n> nodes to every other possible
108 // tree with <n> nodes, where <n> is 4 in release mode and 3 in debug mode
109 // (for speed). For each possible combination of trees, we also vary which
110 // node we serialize first.
112 // For every possible scenario, we check that the AXTreeUpdate is valid,
113 // that the destination tree can unserialize it and create a valid tree,
114 // and that after updating all nodes the resulting tree now matches the
116 TEST(AXGeneratedTreeTest
, SerializeGeneratedTrees
) {
117 // Do a more exhaustive test in release mode. If you're modifying
118 // the algorithm you may want to try even larger tree sizes if you
119 // can afford the time.
121 int max_tree_size
= 4;
123 LOG(WARNING
) << "Debug build, only testing trees with 3 nodes and not 4.";
124 int max_tree_size
= 3;
127 TreeGenerator
generator0(max_tree_size
, false);
128 int n0
= generator0
.UniqueTreeCount();
130 TreeGenerator
generator1(max_tree_size
, true);
131 int n1
= generator1
.UniqueTreeCount();
133 for (int i
= 0; i
< n0
; i
++) {
134 // Build the first tree, tree0.
135 AXSerializableTree tree0
;
136 generator0
.BuildUniqueTree(i
, &tree0
);
137 SCOPED_TRACE("tree0 is " + TreeToString(tree0
));
139 for (int j
= 0; j
< n1
; j
++) {
140 // Build the second tree, tree1.
141 AXSerializableTree tree1
;
142 generator1
.BuildUniqueTree(j
, &tree1
);
143 SCOPED_TRACE("tree1 is " + TreeToString(tree1
));
145 int tree_size
= tree1
.size();
147 // Now iterate over which node to update first, |k|.
148 for (int k
= 0; k
< tree_size
; k
++) {
149 SCOPED_TRACE("i=" + base::IntToString(i
) +
150 " j=" + base::IntToString(j
) +
151 " k=" + base::IntToString(k
));
153 // Start by serializing tree0 and unserializing it into a new
154 // empty tree |dst_tree|.
155 scoped_ptr
<AXTreeSource
<const AXNode
*, AXNodeData
> > tree0_source(
156 tree0
.CreateTreeSource());
157 AXTreeSerializer
<const AXNode
*, AXNodeData
> serializer(
159 AXTreeUpdate
<AXNodeData
> update0
;
160 serializer
.SerializeChanges(tree0
.root(), &update0
);
163 ASSERT_TRUE(dst_tree
.Unserialize(update0
));
165 // At this point, |dst_tree| should now be identical to |tree0|.
166 EXPECT_EQ(TreeToString(tree0
), TreeToString(dst_tree
));
168 // Next, pretend that tree0 turned into tree1, and serialize
169 // a sequence of updates to |dst_tree| to match.
170 scoped_ptr
<AXTreeSource
<const AXNode
*, AXNodeData
> > tree1_source(
171 tree1
.CreateTreeSource());
172 serializer
.ChangeTreeSourceForTesting(tree1_source
.get());
174 for (int k_index
= 0; k_index
< tree_size
; ++k_index
) {
175 int id
= 1 + (k
+ k_index
) % tree_size
;
176 AXTreeUpdate
<AXNodeData
> update
;
177 serializer
.SerializeChanges(tree1
.GetFromId(id
), &update
);
178 ASSERT_TRUE(dst_tree
.Unserialize(update
));
181 // After the sequence of updates, |dst_tree| should now be
182 // identical to |tree1|.
183 EXPECT_EQ(TreeToString(tree1
), TreeToString(dst_tree
));