Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(824)

Side by Side Diff: ui/accessibility/tree_generator.cc

Issue 967583002: Improve AXTree unit tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed nit and rebased Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/accessibility/tree_generator.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "ui/accessibility/tree_generator.h" 5 #include "ui/accessibility/tree_generator.h"
6 6
7 #include "ui/accessibility/ax_serializable_tree.h" 7 #include "ui/accessibility/ax_serializable_tree.h"
8 #include "ui/accessibility/ax_tree.h" 8 #include "ui/accessibility/ax_tree.h"
9 9
10 namespace ui { 10 namespace ui {
11 11
12 TreeGenerator::TreeGenerator(int node_count) 12 static int UniqueTreeCountForNodeCount(int node_count,
13 : node_count_(node_count), unique_tree_count_(1) { 13 bool permutations) {
14 int unique_tree_count = 1;
15
14 // (n-1)! for the possible trees. 16 // (n-1)! for the possible trees.
15 for (int i = 2; i < node_count_; i++) 17 for (int i = 2; i < node_count; i++)
David Tseng 2015/03/18 16:15:01 nit: ++i (consistency)
16 unique_tree_count_ *= i; 18 unique_tree_count *= i;
17 // n! for the permutations of ids. 19
18 for (int i = 2; i <= node_count_; i++) 20 if (permutations) {
19 unique_tree_count_ *= i; 21 // n! for the permutations of ids.
20 }; 22 for (int i = 2; i <= node_count; i++)
David Tseng 2015/03/18 16:15:01 nit: You could just save the result of (n-1)! and
23 unique_tree_count *= i;
24 }
25
26 return unique_tree_count;
27 }
28
29 TreeGenerator::TreeGenerator(int max_node_count, bool permutations)
David Tseng 2015/03/18 16:15:01 nit: use_permutation?
30 : max_node_count_(max_node_count),
31 permutations_(permutations),
32 total_unique_tree_count_(0) {
33 unique_tree_count_by_size_.push_back(0);
34 for (int i = 1; i <= max_node_count; ++i) {
35 int unique_tree_count = UniqueTreeCountForNodeCount(i, permutations);
36 unique_tree_count_by_size_.push_back(unique_tree_count);
37 total_unique_tree_count_ += unique_tree_count;
38 }
39 }
40
41 TreeGenerator::~TreeGenerator() {
42 }
21 43
22 int TreeGenerator::UniqueTreeCount() const { 44 int TreeGenerator::UniqueTreeCount() const {
23 return unique_tree_count_; 45 return total_unique_tree_count_;
24 }; 46 }
25 47
26 void TreeGenerator::BuildUniqueTree(int tree_index, AXTree* out_tree) const { 48 void TreeGenerator::BuildUniqueTree(int tree_index, AXTree* out_tree) const {
49 CHECK_LT(tree_index, total_unique_tree_count_);
50
51 int unique_tree_count_so_far = 0;
52 for (int node_count = 1; node_count <= max_node_count_; ++node_count) {
53 int unique_tree_count = unique_tree_count_by_size_[node_count];
54 if (tree_index - unique_tree_count_so_far < unique_tree_count) {
55 BuildUniqueTreeWithSize(node_count,
56 tree_index - unique_tree_count_so_far,
57 out_tree);
58 return;
59 }
60 unique_tree_count_so_far += unique_tree_count;
61 }
62 }
63
64 void TreeGenerator::BuildUniqueTreeWithSize(
65 int node_count, int tree_index, AXTree* out_tree) const {
27 std::vector<int> indices; 66 std::vector<int> indices;
28 std::vector<int> permuted; 67 std::vector<int> permuted;
29 CHECK(tree_index <= unique_tree_count_); 68 int unique_tree_count = unique_tree_count_by_size_[node_count];
69 CHECK_LT(tree_index, unique_tree_count);
30 70
31 // Use the first few bits of |tree_index| to permute the indices. 71 if (permutations_) {
32 for (int i = 0; i < node_count_; i++) 72 // Use the first few bits of |tree_index| to permute the indices.
33 indices.push_back(i + 1); 73 for (int i = 0; i < node_count; i++)
34 for (int i = 0; i < node_count_; i++) { 74 indices.push_back(i + 1);
35 int p = (node_count_ - i); 75 for (int i = 0; i < node_count; i++) {
36 int index = tree_index % p; 76 int p = (node_count - i);
37 tree_index /= p; 77 int index = tree_index % p;
38 permuted.push_back(indices[index]); 78 tree_index /= p;
39 indices.erase(indices.begin() + index); 79 permuted.push_back(indices[index]);
80 indices.erase(indices.begin() + index);
81 }
82 } else {
83 for (int i = 0; i < node_count; i++)
84 permuted.push_back(i + 1);
40 } 85 }
41 86
42 // Build an AXTreeUpdate. The first two nodes of the tree always 87 // Build an AXTreeUpdate. The first two nodes of the tree always
43 // go in the same place. 88 // go in the same place.
44 AXTreeUpdate update; 89 AXTreeUpdate update;
45 update.nodes.resize(node_count_); 90 update.nodes.resize(node_count);
46 update.nodes[0].id = permuted[0]; 91 update.nodes[0].id = permuted[0];
47 update.nodes[0].role = AX_ROLE_ROOT_WEB_AREA; 92 update.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
48 update.nodes[0].state = AX_STATE_NONE; 93 update.nodes[0].state = AX_STATE_NONE;
49 update.nodes[0].child_ids.push_back(permuted[1]); 94 if (node_count > 1) {
50 update.nodes[1].id = permuted[1]; 95 update.nodes[0].child_ids.push_back(permuted[1]);
51 update.nodes[1].state = AX_STATE_NONE; 96 update.nodes[1].id = permuted[1];
97 update.nodes[1].state = AX_STATE_NONE;
98 }
52 99
53 // The remaining nodes are assigned based on their parent 100 // The remaining nodes are assigned based on their parent
54 // selected from the next bits from |tree_index|. 101 // selected from the next bits from |tree_index|.
55 for (int i = 2; i < node_count_; i++) { 102 for (int i = 2; i < node_count; i++) {
56 update.nodes[i].id = permuted[i]; 103 update.nodes[i].id = permuted[i];
57 update.nodes[i].state = AX_STATE_NONE; 104 update.nodes[i].state = AX_STATE_NONE;
58 int parent_index = (tree_index % i); 105 int parent_index = (tree_index % i);
59 tree_index /= i; 106 tree_index /= i;
60 update.nodes[parent_index].child_ids.push_back(permuted[i]); 107 update.nodes[parent_index].child_ids.push_back(permuted[i]);
61 } 108 }
62 109
63 // Unserialize the tree update into the destination tree. 110 // Unserialize the tree update into the destination tree.
64 CHECK(out_tree->Unserialize(update)) << out_tree->error(); 111 CHECK(out_tree->Unserialize(update)) << out_tree->error();
65 }; 112 };
66 113
67 } // namespace ui 114 } // namespace ui
OLDNEW
« no previous file with comments | « ui/accessibility/tree_generator.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698