Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / accessibility / ax_generated_tree_unittest.cc
blobcb1038c018ce17beb5ff8203b93ac89182cff150
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"
14 namespace ui {
15 namespace {
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.
24 // Examples:
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) {
32 *out_result += " (";
33 for (int i = 0; i < node->child_count(); ++i) {
34 if (i != 0)
35 *out_result += " ";
36 TreeToStringHelper(node->ChildAtIndex(i), out_result);
38 *out_result += ")";
42 std::string TreeToString(const AXTree& tree) {
43 std::string result;
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) {
53 int tree_size = 3;
54 TreeGenerator generator(tree_size, false);
55 const char* EXPECTED_TREES[] = {
56 "(1)",
57 "(1 (2))",
58 "(1 (2 3))",
59 "(1 (2 (3)))",
62 int n = generator.UniqueTreeCount();
63 ASSERT_EQ(static_cast<int>(arraysize(EXPECTED_TREES)), n);
65 for (int i = 0; i < n; ++i) {
66 AXTree tree;
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) {
76 int tree_size = 3;
77 TreeGenerator generator(tree_size, true);
78 const char* EXPECTED_TREES[] = {
79 "(1)",
80 "(1 (2))",
81 "(2 (1))",
82 "(1 (2 3))",
83 "(2 (1 3))",
84 "(3 (1 2))",
85 "(1 (3 2))",
86 "(2 (3 1))",
87 "(3 (2 1))",
88 "(1 (2 (3)))",
89 "(2 (1 (3)))",
90 "(3 (1 (2)))",
91 "(1 (3 (2)))",
92 "(2 (3 (1)))",
93 "(3 (2 (1)))",
96 int n = generator.UniqueTreeCount();
97 ASSERT_EQ(static_cast<int>(arraysize(EXPECTED_TREES)), n);
99 for (int i = 0; i < n; i++) {
100 AXTree tree;
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
115 // intended tree.
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.
120 #ifdef NDEBUG
121 int max_tree_size = 4;
122 #else
123 LOG(WARNING) << "Debug build, only testing trees with 3 nodes and not 4.";
124 int max_tree_size = 3;
125 #endif
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*> > tree0_source(
156 tree0.CreateTreeSource());
157 AXTreeSerializer<const AXNode*> serializer(tree0_source.get());
158 AXTreeUpdate update0;
159 serializer.SerializeChanges(tree0.root(), &update0);
161 AXTree dst_tree;
162 ASSERT_TRUE(dst_tree.Unserialize(update0));
164 // At this point, |dst_tree| should now be identical to |tree0|.
165 EXPECT_EQ(TreeToString(tree0), TreeToString(dst_tree));
167 // Next, pretend that tree0 turned into tree1, and serialize
168 // a sequence of updates to |dst_tree| to match.
169 scoped_ptr<AXTreeSource<const AXNode*> > tree1_source(
170 tree1.CreateTreeSource());
171 serializer.ChangeTreeSourceForTesting(tree1_source.get());
173 for (int k_index = 0; k_index < tree_size; ++k_index) {
174 int id = 1 + (k + k_index) % tree_size;
175 AXTreeUpdate update;
176 serializer.SerializeChanges(tree1.GetFromId(id), &update);
177 ASSERT_TRUE(dst_tree.Unserialize(update));
180 // After the sequence of updates, |dst_tree| should now be
181 // identical to |tree1|.
182 EXPECT_EQ(TreeToString(tree1), TreeToString(dst_tree));
188 } // namespace ui