This source file includes following definitions.
- TreeToStringHelper
- TreeToString
- unique_tree_count_
- UniqueTreeCount
- BuildUniqueTree
- TEST
- TEST
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/accessibility/ax_node.h"
#include "ui/accessibility/ax_serializable_tree.h"
#include "ui/accessibility/ax_tree.h"
#include "ui/accessibility/ax_tree_serializer.h"
namespace ui {
namespace {
void TreeToStringHelper(const AXNode* node, std::string* out_result) {
*out_result += base::IntToString(node->id());
if (node->child_count() != 0) {
*out_result += " (";
for (int i = 0; i < node->child_count(); ++i) {
if (i != 0)
*out_result += " ";
TreeToStringHelper(node->ChildAtIndex(i), out_result);
}
*out_result += ")";
}
}
std::string TreeToString(const AXTree& tree) {
std::string result;
TreeToStringHelper(tree.GetRoot(), &result);
return "(" + result + ")";
}
}
class TreeGenerator {
public:
TreeGenerator(int node_count)
: node_count_(node_count),
unique_tree_count_(1) {
for (int i = 2; i < node_count_; i++)
unique_tree_count_ *= i;
for (int i = 2; i <= node_count_; i++)
unique_tree_count_ *= i;
}
int UniqueTreeCount() {
return unique_tree_count_;
}
void BuildUniqueTree(int tree_index, AXTree* out_tree) {
std::vector<int> indices;
std::vector<int> permuted;
CHECK(tree_index <= unique_tree_count_);
for (int i = 0; i < node_count_; i++)
indices.push_back(i + 1);
for (int i = 0; i < node_count_; i++) {
int p = (node_count_ - i);
int index = tree_index % p;
tree_index /= p;
permuted.push_back(indices[index]);
indices.erase(indices.begin() + index);
}
AXTreeUpdate update;
update.nodes.resize(node_count_);
update.nodes[0].id = permuted[0];
update.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
update.nodes[0].child_ids.push_back(permuted[1]);
update.nodes[1].id = permuted[1];
for (int i = 2; i < node_count_; i++) {
update.nodes[i].id = permuted[i];
int parent_index = (tree_index % i);
tree_index /= i;
update.nodes[parent_index].child_ids.push_back(permuted[i]);
}
CHECK(out_tree->Unserialize(update));
}
private:
int node_count_;
int unique_tree_count_;
};
TEST(AXGeneratedTreeTest, TestTreeGenerator) {
int tree_size = 3;
TreeGenerator generator(tree_size);
const char* EXPECTED_TREES[] = {
"(1 (2 3))",
"(2 (1 3))",
"(3 (1 2))",
"(1 (3 2))",
"(2 (3 1))",
"(3 (2 1))",
"(1 (2 (3)))",
"(2 (1 (3)))",
"(3 (1 (2)))",
"(1 (3 (2)))",
"(2 (3 (1)))",
"(3 (2 (1)))",
};
int n = generator.UniqueTreeCount();
ASSERT_EQ(static_cast<int>(arraysize(EXPECTED_TREES)), n);
for (int i = 0; i < n; i++) {
AXTree tree;
generator.BuildUniqueTree(i, &tree);
std::string str = TreeToString(tree);
EXPECT_EQ(EXPECTED_TREES[i], str);
}
}
TEST(AXGeneratedTreeTest, SerializeGeneratedTrees) {
#ifdef NDEBUG
int tree_size = 4;
#else
LOG(WARNING) << "Debug build, only testing trees with 3 nodes and not 4.";
int tree_size = 3;
#endif
TreeGenerator generator(tree_size);
int n = generator.UniqueTreeCount();
for (int i = 0; i < n; i++) {
AXSerializableTree tree0;
generator.BuildUniqueTree(i, &tree0);
SCOPED_TRACE("tree0 is " + TreeToString(tree0));
for (int j = 0; j < n; j++) {
AXSerializableTree tree1;
generator.BuildUniqueTree(j, &tree1);
SCOPED_TRACE("tree1 is " + TreeToString(tree0));
for (int k = 0; k < tree_size; k++) {
SCOPED_TRACE("i=" + base::IntToString(i) +
" j=" + base::IntToString(j) +
" k=" + base::IntToString(k));
scoped_ptr<AXTreeSource<const AXNode*> > tree0_source(
tree0.CreateTreeSource());
AXTreeSerializer<const AXNode*> serializer(tree0_source.get());
AXTreeUpdate update0;
serializer.SerializeChanges(tree0.GetRoot(), &update0);
AXTree dst_tree;
ASSERT_TRUE(dst_tree.Unserialize(update0));
EXPECT_EQ(TreeToString(tree0), TreeToString(dst_tree));
scoped_ptr<AXTreeSource<const AXNode*> > tree1_source(
tree1.CreateTreeSource());
serializer.ChangeTreeSourceForTesting(tree1_source.get());
for (int k_index = 0; k_index < tree_size; ++k_index) {
int id = 1 + (k + k_index) % tree_size;
AXTreeUpdate update;
serializer.SerializeChanges(tree1.GetFromId(id), &update);
ASSERT_TRUE(dst_tree.Unserialize(update));
}
EXPECT_EQ(TreeToString(tree1), TreeToString(dst_tree));
}
}
}
}
}