| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "ui/accessibility/tree_generator.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "ui/accessibility/ax_serializable_tree.h" | |
| 9 #include "ui/accessibility/ax_tree.h" | |
| 10 | |
| 11 namespace ui { | |
| 12 | |
| 13 TreeGenerator::TreeGenerator(int node_count) | |
| 14 : node_count_(node_count), unique_tree_count_(1) { | |
| 15 // (n-1)! for the possible trees. | |
| 16 for (int i = 2; i < node_count_; i++) | |
| 17 unique_tree_count_ *= i; | |
| 18 // n! for the permutations of ids. | |
| 19 for (int i = 2; i <= node_count_; i++) | |
| 20 unique_tree_count_ *= i; | |
| 21 }; | |
| 22 | |
| 23 int TreeGenerator::UniqueTreeCount() const { | |
| 24 return unique_tree_count_; | |
| 25 }; | |
| 26 | |
| 27 void TreeGenerator::BuildUniqueTree(int tree_index, AXTree* out_tree) const { | |
| 28 std::vector<int> indices; | |
| 29 std::vector<int> permuted; | |
| 30 CHECK(tree_index <= unique_tree_count_); | |
| 31 | |
| 32 // Use the first few bits of |tree_index| to permute the indices. | |
| 33 for (int i = 0; i < node_count_; i++) | |
| 34 indices.push_back(i + 1); | |
| 35 for (int i = 0; i < node_count_; i++) { | |
| 36 int p = (node_count_ - i); | |
| 37 int index = tree_index % p; | |
| 38 tree_index /= p; | |
| 39 permuted.push_back(indices[index]); | |
| 40 indices.erase(indices.begin() + index); | |
| 41 } | |
| 42 | |
| 43 // Build an AXTreeUpdate. The first two nodes of the tree always | |
| 44 // go in the same place. | |
| 45 AXTreeUpdate update; | |
| 46 update.nodes.resize(node_count_); | |
| 47 update.nodes[0].id = permuted[0]; | |
| 48 update.nodes[0].role = AX_ROLE_ROOT_WEB_AREA; | |
| 49 update.nodes[0].state = AX_STATE_NONE; | |
| 50 update.nodes[0].child_ids.push_back(permuted[1]); | |
| 51 update.nodes[1].id = permuted[1]; | |
| 52 update.nodes[1].state = AX_STATE_NONE; | |
| 53 | |
| 54 // The remaining nodes are assigned based on their parent | |
| 55 // selected from the next bits from |tree_index|. | |
| 56 for (int i = 2; i < node_count_; i++) { | |
| 57 update.nodes[i].id = permuted[i]; | |
| 58 update.nodes[i].state = AX_STATE_NONE; | |
| 59 int parent_index = (tree_index % i); | |
| 60 tree_index /= i; | |
| 61 update.nodes[parent_index].child_ids.push_back(permuted[i]); | |
| 62 } | |
| 63 | |
| 64 // Unserialize the tree update into the destination tree. | |
| 65 CHECK(out_tree->Unserialize(update)) << out_tree->error(); | |
| 66 }; | |
| 67 | |
| 68 } // namespace ui | |
| OLD | NEW |