OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "base/strings/string_number_conversions.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "ui/accessibility/ax_node.h" | 8 #include "ui/accessibility/ax_node.h" |
9 #include "ui/accessibility/ax_serializable_tree.h" | 9 #include "ui/accessibility/ax_serializable_tree.h" |
10 #include "ui/accessibility/ax_tree.h" | 10 #include "ui/accessibility/ax_tree.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 | 41 |
42 std::string TreeToString(const AXTree& tree) { | 42 std::string TreeToString(const AXTree& tree) { |
43 std::string result; | 43 std::string result; |
44 TreeToStringHelper(tree.root(), &result); | 44 TreeToStringHelper(tree.root(), &result); |
45 return "(" + result + ")"; | 45 return "(" + result + ")"; |
46 } | 46 } |
47 | 47 |
48 } // anonymous namespace | 48 } // anonymous namespace |
49 | 49 |
50 // Test the TreeGenerator class by building all possible trees with | 50 // Test the TreeGenerator class by building all possible trees with |
51 // 3 nodes and the ids [1...3]. | 51 // 3 nodes and the ids [1...3], with no permutations of ids. |
52 TEST(AXGeneratedTreeTest, TestTreeGenerator) { | 52 TEST(AXGeneratedTreeTest, TestTreeGeneratorNoPermutations) { |
53 int tree_size = 3; | 53 int tree_size = 3; |
54 TreeGenerator generator(tree_size); | 54 TreeGenerator generator(tree_size, false); |
55 const char* EXPECTED_TREES[] = { | 55 const char* EXPECTED_TREES[] = { |
| 56 "(1)", |
| 57 "(1 (2))", |
| 58 "(1 (2 3))", |
| 59 "(1 (2 (3)))", |
| 60 }; |
| 61 |
| 62 int n = generator.UniqueTreeCount(); |
| 63 ASSERT_EQ(static_cast<int>(arraysize(EXPECTED_TREES)), n); |
| 64 |
| 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); |
| 70 } |
| 71 } |
| 72 |
| 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))", |
56 "(1 (2 3))", | 82 "(1 (2 3))", |
57 "(2 (1 3))", | 83 "(2 (1 3))", |
58 "(3 (1 2))", | 84 "(3 (1 2))", |
59 "(1 (3 2))", | 85 "(1 (3 2))", |
60 "(2 (3 1))", | 86 "(2 (3 1))", |
61 "(3 (2 1))", | 87 "(3 (2 1))", |
62 "(1 (2 (3)))", | 88 "(1 (2 (3)))", |
63 "(2 (1 (3)))", | 89 "(2 (1 (3)))", |
64 "(3 (1 (2)))", | 90 "(3 (1 (2)))", |
65 "(1 (3 (2)))", | 91 "(1 (3 (2)))", |
(...skipping 19 matching lines...) Expand all Loading... |
85 // | 111 // |
86 // For every possible scenario, we check that the AXTreeUpdate is valid, | 112 // For every possible scenario, we check that the AXTreeUpdate is valid, |
87 // that the destination tree can unserialize it and create a valid tree, | 113 // that the destination tree can unserialize it and create a valid tree, |
88 // and that after updating all nodes the resulting tree now matches the | 114 // and that after updating all nodes the resulting tree now matches the |
89 // intended tree. | 115 // intended tree. |
90 TEST(AXGeneratedTreeTest, SerializeGeneratedTrees) { | 116 TEST(AXGeneratedTreeTest, SerializeGeneratedTrees) { |
91 // Do a more exhaustive test in release mode. If you're modifying | 117 // Do a more exhaustive test in release mode. If you're modifying |
92 // the algorithm you may want to try even larger tree sizes if you | 118 // the algorithm you may want to try even larger tree sizes if you |
93 // can afford the time. | 119 // can afford the time. |
94 #ifdef NDEBUG | 120 #ifdef NDEBUG |
95 int tree_size = 4; | 121 int max_tree_size = 4; |
96 #else | 122 #else |
97 LOG(WARNING) << "Debug build, only testing trees with 3 nodes and not 4."; | 123 LOG(WARNING) << "Debug build, only testing trees with 3 nodes and not 4."; |
98 int tree_size = 3; | 124 int max_tree_size = 3; |
99 #endif | 125 #endif |
100 | 126 |
101 TreeGenerator generator(tree_size); | 127 TreeGenerator generator0(max_tree_size, false); |
102 int n = generator.UniqueTreeCount(); | 128 int n0 = generator0.UniqueTreeCount(); |
103 | 129 |
104 for (int i = 0; i < n; i++) { | 130 TreeGenerator generator1(max_tree_size, true); |
| 131 int n1 = generator1.UniqueTreeCount(); |
| 132 |
| 133 for (int i = 0; i < n0; i++) { |
105 // Build the first tree, tree0. | 134 // Build the first tree, tree0. |
106 AXSerializableTree tree0; | 135 AXSerializableTree tree0; |
107 generator.BuildUniqueTree(i, &tree0); | 136 generator0.BuildUniqueTree(i, &tree0); |
108 SCOPED_TRACE("tree0 is " + TreeToString(tree0)); | 137 SCOPED_TRACE("tree0 is " + TreeToString(tree0)); |
109 | 138 |
110 for (int j = 0; j < n; j++) { | 139 for (int j = 0; j < n1; j++) { |
111 // Build the second tree, tree1. | 140 // Build the second tree, tree1. |
112 AXSerializableTree tree1; | 141 AXSerializableTree tree1; |
113 generator.BuildUniqueTree(j, &tree1); | 142 generator1.BuildUniqueTree(j, &tree1); |
114 SCOPED_TRACE("tree1 is " + TreeToString(tree0)); | 143 SCOPED_TRACE("tree1 is " + TreeToString(tree1)); |
| 144 |
| 145 int tree_size = tree1.size(); |
115 | 146 |
116 // Now iterate over which node to update first, |k|. | 147 // Now iterate over which node to update first, |k|. |
117 for (int k = 0; k < tree_size; k++) { | 148 for (int k = 0; k < tree_size; k++) { |
118 SCOPED_TRACE("i=" + base::IntToString(i) + | 149 SCOPED_TRACE("i=" + base::IntToString(i) + |
119 " j=" + base::IntToString(j) + | 150 " j=" + base::IntToString(j) + |
120 " k=" + base::IntToString(k)); | 151 " k=" + base::IntToString(k)); |
121 | 152 |
122 // Start by serializing tree0 and unserializing it into a new | 153 // Start by serializing tree0 and unserializing it into a new |
123 // empty tree |dst_tree|. | 154 // empty tree |dst_tree|. |
124 scoped_ptr<AXTreeSource<const AXNode*> > tree0_source( | 155 scoped_ptr<AXTreeSource<const AXNode*> > tree0_source( |
(...skipping 23 matching lines...) Expand all Loading... |
148 | 179 |
149 // After the sequence of updates, |dst_tree| should now be | 180 // After the sequence of updates, |dst_tree| should now be |
150 // identical to |tree1|. | 181 // identical to |tree1|. |
151 EXPECT_EQ(TreeToString(tree1), TreeToString(dst_tree)); | 182 EXPECT_EQ(TreeToString(tree1), TreeToString(dst_tree)); |
152 } | 183 } |
153 } | 184 } |
154 } | 185 } |
155 } | 186 } |
156 | 187 |
157 } // namespace ui | 188 } // namespace ui |
OLD | NEW |