blob: 87066229ce6099757c2206440d9ec8a9c8c7a66f [file] [log] [blame]
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/accessibility/ax_tree.h"
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/accessibility/ax_enum_util.h"
#include "ui/accessibility/ax_node.h"
#include "ui/accessibility/ax_node_position.h"
#include "ui/accessibility/ax_serializable_tree.h"
#include "ui/accessibility/ax_tree_observer.h"
#include "ui/accessibility/ax_tree_serializer.h"
#include "ui/gfx/transform.h"
// Helper macro for testing selection values and maintain
// correct stack tracing and failure causality.
#define TEST_SELECTION(tree_update, tree, input, expected) \
{ \
tree_update.has_tree_data = true; \
tree_update.tree_data.sel_anchor_object_id = input.anchor_id; \
tree_update.tree_data.sel_anchor_offset = input.anchor_offset; \
tree_update.tree_data.sel_focus_object_id = input.focus_id; \
tree_update.tree_data.sel_focus_offset = input.focus_offset; \
EXPECT_TRUE(tree.Unserialize(tree_update)); \
ui::AXTree::Selection actual = tree.GetUnignoredSelection(); \
EXPECT_EQ(expected.anchor_id, actual.anchor_object_id); \
EXPECT_EQ(expected.anchor_offset, actual.anchor_offset); \
EXPECT_EQ(expected.focus_id, actual.focus_object_id); \
EXPECT_EQ(expected.focus_offset, actual.focus_offset); \
}
namespace ui {
namespace {
std::string IntVectorToString(const std::vector<int>& items) {
std::string str;
for (size_t i = 0; i < items.size(); ++i) {
if (i > 0)
str += ",";
str += base::NumberToString(items[i]);
}
return str;
}
std::string GetBoundsAsString(const AXTree& tree, int32_t id) {
AXNode* node = tree.GetFromId(id);
gfx::RectF bounds = tree.GetTreeBounds(node);
return base::StringPrintf("(%.0f, %.0f) size (%.0f x %.0f)", bounds.x(),
bounds.y(), bounds.width(), bounds.height());
}
std::string GetUnclippedBoundsAsString(const AXTree& tree, int32_t id) {
AXNode* node = tree.GetFromId(id);
gfx::RectF bounds = tree.GetTreeBounds(node, nullptr, false);
return base::StringPrintf("(%.0f, %.0f) size (%.0f x %.0f)", bounds.x(),
bounds.y(), bounds.width(), bounds.height());
}
bool IsNodeOffscreen(const AXTree& tree, int32_t id) {
AXNode* node = tree.GetFromId(id);
bool result = false;
tree.GetTreeBounds(node, &result);
return result;
}
class TestAXTreeObserver : public AXTreeObserver {
public:
TestAXTreeObserver(AXTree* tree)
: tree_(tree), tree_data_changed_(false), root_changed_(false) {
tree_->AddObserver(this);
}
~TestAXTreeObserver() final { tree_->RemoveObserver(this); }
void OnNodeDataWillChange(AXTree* tree,
const AXNodeData& old_node_data,
const AXNodeData& new_node_data) override {}
void OnNodeDataChanged(AXTree* tree,
const AXNodeData& old_node_data,
const AXNodeData& new_node_data) override {}
void OnTreeDataChanged(AXTree* tree,
const ui::AXTreeData& old_data,
const ui::AXTreeData& new_data) override {
tree_data_changed_ = true;
}
base::Optional<AXNode::AXID> unignored_parent_id_before_node_deleted;
void OnNodeWillBeDeleted(AXTree* tree, AXNode* node) override {
deleted_ids_.push_back(node->id());
if (unignored_parent_id_before_node_deleted) {
ASSERT_NE(nullptr, node->GetUnignoredParent());
ASSERT_EQ(*unignored_parent_id_before_node_deleted,
node->GetUnignoredParent()->id());
}
}
void OnSubtreeWillBeDeleted(AXTree* tree, AXNode* node) override {
subtree_deleted_ids_.push_back(node->id());
}
void OnNodeWillBeReparented(AXTree* tree, AXNode* node) override {
node_will_be_reparented_ids_.push_back(node->id());
}
void OnSubtreeWillBeReparented(AXTree* tree, AXNode* node) override {
subtree_will_be_reparented_ids_.push_back(node->id());
}
void OnNodeCreated(AXTree* tree, AXNode* node) override {
created_ids_.push_back(node->id());
}
void OnNodeReparented(AXTree* tree, AXNode* node) override {
node_reparented_ids_.push_back(node->id());
}
void OnNodeChanged(AXTree* tree, AXNode* node) override {
changed_ids_.push_back(node->id());
}
void OnAtomicUpdateFinished(AXTree* tree,
bool root_changed,
const std::vector<Change>& changes) override {
root_changed_ = root_changed;
for (size_t i = 0; i < changes.size(); ++i) {
int id = changes[i].node->id();
switch (changes[i].type) {
case NODE_CREATED:
node_creation_finished_ids_.push_back(id);
break;
case SUBTREE_CREATED:
subtree_creation_finished_ids_.push_back(id);
break;
case NODE_REPARENTED:
node_reparented_finished_ids_.push_back(id);
break;
case SUBTREE_REPARENTED:
subtree_reparented_finished_ids_.push_back(id);
break;
case NODE_CHANGED:
change_finished_ids_.push_back(id);
break;
}
}
}
void OnRoleChanged(AXTree* tree,
AXNode* node,
ax::mojom::Role old_role,
ax::mojom::Role new_role) override {
attribute_change_log_.push_back(base::StringPrintf(
"Role changed from %s to %s", ToString(old_role), ToString(new_role)));
}
void OnStateChanged(AXTree* tree,
AXNode* node,
ax::mojom::State state,
bool new_value) override {
attribute_change_log_.push_back(base::StringPrintf(
"%s changed to %s", ToString(state), new_value ? "true" : "false"));
}
void OnStringAttributeChanged(AXTree* tree,
AXNode* node,
ax::mojom::StringAttribute attr,
const std::string& old_value,
const std::string& new_value) override {
attribute_change_log_.push_back(
base::StringPrintf("%s changed from %s to %s", ToString(attr),
old_value.c_str(), new_value.c_str()));
}
void OnIntAttributeChanged(AXTree* tree,
AXNode* node,
ax::mojom::IntAttribute attr,
int32_t old_value,
int32_t new_value) override {
attribute_change_log_.push_back(base::StringPrintf(
"%s changed from %d to %d", ToString(attr), old_value, new_value));
}
void OnFloatAttributeChanged(AXTree* tree,
AXNode* node,
ax::mojom::FloatAttribute attr,
float old_value,
float new_value) override {
attribute_change_log_.push_back(
base::StringPrintf("%s changed from %s to %s", ToString(attr),
base::NumberToString(old_value).c_str(),
base::NumberToString(new_value).c_str()));
}
void OnBoolAttributeChanged(AXTree* tree,
AXNode* node,
ax::mojom::BoolAttribute attr,
bool new_value) override {
attribute_change_log_.push_back(base::StringPrintf(
"%s changed to %s", ToString(attr), new_value ? "true" : "false"));
}
void OnIntListAttributeChanged(
AXTree* tree,
AXNode* node,
ax::mojom::IntListAttribute attr,
const std::vector<int32_t>& old_value,
const std::vector<int32_t>& new_value) override {
attribute_change_log_.push_back(
base::StringPrintf("%s changed from %s to %s", ToString(attr),
IntVectorToString(old_value).c_str(),
IntVectorToString(new_value).c_str()));
}
bool tree_data_changed() const { return tree_data_changed_; }
bool root_changed() const { return root_changed_; }
const std::vector<int32_t>& deleted_ids() { return deleted_ids_; }
const std::vector<int32_t>& subtree_deleted_ids() {
return subtree_deleted_ids_;
}
const std::vector<int32_t>& created_ids() { return created_ids_; }
const std::vector<int32_t>& node_creation_finished_ids() {
return node_creation_finished_ids_;
}
const std::vector<int32_t>& subtree_creation_finished_ids() {
return subtree_creation_finished_ids_;
}
const std::vector<int32_t>& node_reparented_finished_ids() {
return node_reparented_finished_ids_;
}
const std::vector<int32_t>& subtree_will_be_reparented_ids() {
return subtree_will_be_reparented_ids_;
}
const std::vector<int32_t>& node_will_be_reparented_ids() {
return node_will_be_reparented_ids_;
}
const std::vector<int32_t>& node_reparented_ids() {
return node_reparented_ids_;
}
const std::vector<int32_t>& subtree_reparented_finished_ids() {
return subtree_reparented_finished_ids_;
}
const std::vector<int32_t>& change_finished_ids() {
return change_finished_ids_;
}
const std::vector<std::string>& attribute_change_log() {
return attribute_change_log_;
}
private:
AXTree* tree_;
bool tree_data_changed_;
bool root_changed_;
std::vector<int32_t> deleted_ids_;
std::vector<int32_t> subtree_deleted_ids_;
std::vector<int32_t> created_ids_;
std::vector<int32_t> changed_ids_;
std::vector<int32_t> subtree_will_be_reparented_ids_;
std::vector<int32_t> node_will_be_reparented_ids_;
std::vector<int32_t> node_creation_finished_ids_;
std::vector<int32_t> subtree_creation_finished_ids_;
std::vector<int32_t> node_reparented_ids_;
std::vector<int32_t> node_reparented_finished_ids_;
std::vector<int32_t> subtree_reparented_finished_ids_;
std::vector<int32_t> change_finished_ids_;
std::vector<std::string> attribute_change_log_;
};
} // namespace
// A macro for testing that a base::Optional has both a value and that its value
// is set to a particular expectation.
#define EXPECT_OPTIONAL_EQ(expected, actual) \
EXPECT_TRUE(actual.has_value()); \
if (actual) { \
EXPECT_EQ(expected, actual.value()); \
}
TEST(AXTreeTest, SerializeSimpleAXTree) {
AXNodeData root;
root.id = 1;
root.role = ax::mojom::Role::kDialog;
root.AddState(ax::mojom::State::kFocusable);
root.relative_bounds.bounds = gfx::RectF(0, 0, 800, 600);
root.child_ids.push_back(2);
root.child_ids.push_back(3);
AXNodeData button;
button.id = 2;
button.role = ax::mojom::Role::kButton;
button.relative_bounds.bounds = gfx::RectF(20, 20, 200, 30);
AXNodeData checkbox;
checkbox.id = 3;
checkbox.role = ax::mojom::Role::kCheckBox;
checkbox.relative_bounds.bounds = gfx::RectF(20, 50, 200, 30);
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.push_back(root);
initial_state.nodes.push_back(button);
initial_state.nodes.push_back(checkbox);
initial_state.has_tree_data = true;
initial_state.tree_data.title = "Title";
AXSerializableTree src_tree(initial_state);
std::unique_ptr<AXTreeSource<const AXNode*, AXNodeData, AXTreeData>>
tree_source(src_tree.CreateTreeSource());
AXTreeSerializer<const AXNode*, AXNodeData, AXTreeData> serializer(
tree_source.get());
AXTreeUpdate update;
serializer.SerializeChanges(src_tree.root(), &update);
AXTree dst_tree;
ASSERT_TRUE(dst_tree.Unserialize(update));
const AXNode* root_node = dst_tree.root();
ASSERT_TRUE(root_node != nullptr);
EXPECT_EQ(root.id, root_node->id());
EXPECT_EQ(root.role, root_node->data().role);
ASSERT_EQ(2u, root_node->children().size());
const AXNode* button_node = root_node->children()[0];
EXPECT_EQ(button.id, button_node->id());
EXPECT_EQ(button.role, button_node->data().role);
const AXNode* checkbox_node = root_node->children()[1];
EXPECT_EQ(checkbox.id, checkbox_node->id());
EXPECT_EQ(checkbox.role, checkbox_node->data().role);
EXPECT_EQ(
"AXTree title=Title\n"
"id=1 dialog FOCUSABLE (0, 0)-(800, 600) actions= child_ids=2,3\n"
" id=2 button (20, 20)-(200, 30) actions=\n"
" id=3 checkBox (20, 50)-(200, 30) actions=\n",
dst_tree.ToString());
}
TEST(AXTreeTest, SerializeAXTreeUpdate) {
AXNodeData list;
list.id = 3;
list.role = ax::mojom::Role::kList;
list.child_ids.push_back(4);
list.child_ids.push_back(5);
list.child_ids.push_back(6);
AXNodeData list_item_2;
list_item_2.id = 5;
list_item_2.role = ax::mojom::Role::kListItem;
AXNodeData list_item_3;
list_item_3.id = 6;
list_item_3.role = ax::mojom::Role::kListItem;
AXNodeData button;
button.id = 7;
button.role = ax::mojom::Role::kButton;
AXTreeUpdate update;
update.root_id = 3;
update.nodes.push_back(list);
update.nodes.push_back(list_item_2);
update.nodes.push_back(list_item_3);
update.nodes.push_back(button);
EXPECT_EQ(
"AXTreeUpdate: root id 3\n"
"id=3 list (0, 0)-(0, 0) actions= child_ids=4,5,6\n"
" id=5 listItem (0, 0)-(0, 0) actions=\n"
" id=6 listItem (0, 0)-(0, 0) actions=\n"
"id=7 button (0, 0)-(0, 0) actions=\n",
update.ToString());
}
TEST(AXTreeTest, LeaveOrphanedDeletedSubtreeFails) {
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(3);
initial_state.nodes[0].id = 1;
initial_state.nodes[0].child_ids.push_back(2);
initial_state.nodes[0].child_ids.push_back(3);
initial_state.nodes[1].id = 2;
initial_state.nodes[2].id = 3;
AXTree tree(initial_state);
// This should fail because we delete a subtree rooted at id=2
// but never update it.
AXTreeUpdate update;
update.node_id_to_clear = 2;
update.nodes.resize(1);
update.nodes[0].id = 3;
EXPECT_FALSE(tree.Unserialize(update));
ASSERT_EQ("Nodes left pending by the update: 2", tree.error());
}
TEST(AXTreeTest, LeaveOrphanedNewChildFails) {
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(1);
initial_state.nodes[0].id = 1;
AXTree tree(initial_state);
// This should fail because we add a new child to the root node
// but never update it.
AXTreeUpdate update;
update.nodes.resize(1);
update.nodes[0].id = 1;
update.nodes[0].child_ids.push_back(2);
EXPECT_FALSE(tree.Unserialize(update));
ASSERT_EQ("Nodes left pending by the update: 2", tree.error());
}
TEST(AXTreeTest, DuplicateChildIdFails) {
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(1);
initial_state.nodes[0].id = 1;
AXTree tree(initial_state);
// This should fail because a child id appears twice.
AXTreeUpdate update;
update.nodes.resize(2);
update.nodes[0].id = 1;
update.nodes[0].child_ids.push_back(2);
update.nodes[0].child_ids.push_back(2);
update.nodes[1].id = 2;
EXPECT_FALSE(tree.Unserialize(update));
ASSERT_EQ("Node 1 has duplicate child id 2", tree.error());
}
TEST(AXTreeTest, InvalidReparentingFails) {
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(3);
initial_state.nodes[0].id = 1;
initial_state.nodes[0].child_ids.push_back(2);
initial_state.nodes[1].id = 2;
initial_state.nodes[1].child_ids.push_back(3);
initial_state.nodes[2].id = 3;
AXTree tree(initial_state);
// This should fail because node 3 is reparented from node 2 to node 1
// without deleting node 1's subtree first.
AXTreeUpdate update;
update.nodes.resize(3);
update.nodes[0].id = 1;
update.nodes[0].child_ids.push_back(3);
update.nodes[0].child_ids.push_back(2);
update.nodes[1].id = 2;
update.nodes[2].id = 3;
EXPECT_FALSE(tree.Unserialize(update));
ASSERT_EQ("Node 3 is not marked for destruction, would be reparented to 1",
tree.error());
}
TEST(AXTreeTest, NoReparentingOfRootIfNoNewRoot) {
AXNodeData root;
root.id = 1;
AXNodeData child1;
child1.id = 2;
AXNodeData child2;
child2.id = 3;
root.child_ids = {child1.id};
child1.child_ids = {child2.id};
AXTreeUpdate initial_state;
initial_state.root_id = root.id;
initial_state.nodes = {root, child1, child2};
AXTree tree(initial_state);
// Update the root but don't change it by reparenting |child2| to be a child
// of the root.
root.child_ids = {child1.id, child2.id};
child1.child_ids = {};
AXTreeUpdate update;
update.root_id = root.id;
update.node_id_to_clear = root.id;
update.nodes = {root, child1, child2};
TestAXTreeObserver test_observer(&tree);
ASSERT_TRUE(tree.Unserialize(update));
EXPECT_EQ(0U, test_observer.deleted_ids().size());
EXPECT_EQ(0U, test_observer.subtree_deleted_ids().size());
EXPECT_EQ(0U, test_observer.created_ids().size());
EXPECT_EQ(0U, test_observer.node_creation_finished_ids().size());
EXPECT_EQ(0U, test_observer.subtree_creation_finished_ids().size());
EXPECT_EQ(0U, test_observer.node_reparented_finished_ids().size());
ASSERT_EQ(2U, test_observer.subtree_reparented_finished_ids().size());
EXPECT_EQ(child1.id, test_observer.subtree_reparented_finished_ids()[0]);
EXPECT_EQ(child2.id, test_observer.subtree_reparented_finished_ids()[1]);
ASSERT_EQ(1U, test_observer.change_finished_ids().size());
EXPECT_EQ(root.id, test_observer.change_finished_ids()[0]);
EXPECT_FALSE(test_observer.root_changed());
EXPECT_FALSE(test_observer.tree_data_changed());
}
TEST(AXTreeTest, NoReparentingIfOnlyRemovedAndChangedNotReAdded) {
AXNodeData root;
root.id = 1;
AXNodeData child1;
child1.id = 2;
AXNodeData child2;
child2.id = 3;
root.child_ids = {child1.id};
child1.child_ids = {child2.id};
AXTreeUpdate initial_state;
initial_state.root_id = root.id;
initial_state.nodes = {root, child1, child2};
AXTree tree(initial_state);
// Change existing attributes.
AXTreeUpdate update;
update.nodes.resize(2);
update.nodes[0].id = 2;
update.nodes[0].AddIntAttribute(ax::mojom::IntAttribute::kActivedescendantId,
3);
update.nodes[1].id = 1;
TestAXTreeObserver test_observer(&tree);
EXPECT_TRUE(tree.Unserialize(update)) << tree.error();
EXPECT_EQ(2U, test_observer.deleted_ids().size());
EXPECT_EQ(2U, test_observer.subtree_deleted_ids().size());
EXPECT_EQ(0U, test_observer.created_ids().size());
EXPECT_EQ(0U, test_observer.node_creation_finished_ids().size());
EXPECT_EQ(0U, test_observer.subtree_creation_finished_ids().size());
EXPECT_EQ(0U, test_observer.node_will_be_reparented_ids().size());
EXPECT_EQ(0U, test_observer.subtree_will_be_reparented_ids().size());
EXPECT_EQ(0U, test_observer.node_reparented_ids().size());
EXPECT_EQ(0U, test_observer.node_reparented_finished_ids().size());
ASSERT_EQ(0U, test_observer.subtree_reparented_finished_ids().size());
EXPECT_FALSE(test_observer.root_changed());
EXPECT_FALSE(test_observer.tree_data_changed());
}
TEST(AXTreeTest, ReparentRootIfRootChanged) {
AXNodeData root;
root.id = 1;
AXNodeData child1;
child1.id = 2;
AXNodeData child2;
child2.id = 3;
root.child_ids = {child1.id};
child1.child_ids = {child2.id};
AXTreeUpdate initial_state;
initial_state.root_id = root.id;
initial_state.nodes = {root, child1, child2};
AXTree tree(initial_state);
// Create a new root and reparent |child2| to be a child of the new root.
AXNodeData root2;
root2.id = 4;
root2.child_ids = {child1.id, child2.id};
child1.child_ids = {};
AXTreeUpdate update;
update.root_id = root2.id;
update.node_id_to_clear = root.id;
update.nodes = {root2, child1, child2};
TestAXTreeObserver test_observer(&tree);
ASSERT_TRUE(tree.Unserialize(update));
ASSERT_EQ(1U, test_observer.deleted_ids().size());
EXPECT_EQ(root.id, test_observer.deleted_ids()[0]);
ASSERT_EQ(1U, test_observer.subtree_deleted_ids().size());
EXPECT_EQ(root.id, test_observer.subtree_deleted_ids()[0]);
ASSERT_EQ(1U, test_observer.created_ids().size());
EXPECT_EQ(root2.id, test_observer.created_ids()[0]);
EXPECT_EQ(0U, test_observer.node_creation_finished_ids().size());
ASSERT_EQ(1U, test_observer.subtree_creation_finished_ids().size());
EXPECT_EQ(root2.id, test_observer.subtree_creation_finished_ids()[0]);
ASSERT_EQ(2U, test_observer.node_reparented_finished_ids().size());
EXPECT_EQ(child1.id, test_observer.node_reparented_finished_ids()[0]);
EXPECT_EQ(child2.id, test_observer.node_reparented_finished_ids()[1]);
EXPECT_EQ(0U, test_observer.subtree_reparented_finished_ids().size());
EXPECT_EQ(0U, test_observer.change_finished_ids().size());
EXPECT_TRUE(test_observer.root_changed());
EXPECT_FALSE(test_observer.tree_data_changed());
}
TEST(AXTreeTest, ImplicitChildrenDelete) {
// This test covers the case where an AXTreeUpdate includes a node without
// mentioning that node's children, this should cause a delete of those child
// nodes.
// Setup initial tree state
// Tree:
// 1
// 2 3
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(3);
initial_state.nodes[0].id = 1;
initial_state.nodes[0].child_ids.resize(2);
initial_state.nodes[0].child_ids[0] = 2;
initial_state.nodes[0].child_ids[1] = 3;
initial_state.nodes[1].id = 2;
initial_state.nodes[2].id = 3;
AXTree tree(initial_state);
EXPECT_NE(tree.GetFromId(1), nullptr);
EXPECT_NE(tree.GetFromId(2), nullptr);
EXPECT_NE(tree.GetFromId(3), nullptr);
// Perform a no-op update of node 1 but omit any mention of its children. This
// should delete all of the node's children.
AXTreeUpdate update;
update.nodes.resize(1);
update.nodes[0].id = 1;
ASSERT_TRUE(tree.Unserialize(update));
// Check that nodes 2 and 3 have been deleted.
EXPECT_NE(tree.GetFromId(1), nullptr);
EXPECT_EQ(tree.GetFromId(2), nullptr);
EXPECT_EQ(tree.GetFromId(3), nullptr);
}
TEST(AXTreeTest, ImplicitAttributeDelete) {
// This test covers the case where an AXTreeUpdate includes a node without
// mentioning one of that node's attributes, this should cause a delete of any
// unmentioned attribute that was previously set on the node.
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(1);
initial_state.nodes[0].id = 1;
initial_state.nodes[0].SetName("Node 1 name");
AXTree tree(initial_state);
EXPECT_NE(tree.GetFromId(1), nullptr);
EXPECT_EQ(
tree.GetFromId(1)->GetStringAttribute(ax::mojom::StringAttribute::kName),
"Node 1 name");
// Perform a no-op update of node 1 but omit any mention of the name
// attribute. This should delete the name attribute.
AXTreeUpdate update;
update.nodes.resize(1);
update.nodes[0].id = 1;
ASSERT_TRUE(tree.Unserialize(update));
// Check that the name attribute is no longer present.
EXPECT_NE(tree.GetFromId(1), nullptr);
EXPECT_FALSE(
tree.GetFromId(1)->HasStringAttribute(ax::mojom::StringAttribute::kName));
}
TEST(AXTreeTest, TreeObserverIsCalled) {
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(2);
initial_state.nodes[0].id = 1;
initial_state.nodes[0].child_ids.push_back(2);
initial_state.nodes[1].id = 2;
AXTree tree(initial_state);
AXTreeUpdate update;
update.root_id = 3;
update.node_id_to_clear = 1;
update.nodes.resize(2);
update.nodes[0].id = 3;
update.nodes[0].child_ids.push_back(4);
update.nodes[1].id = 4;
TestAXTreeObserver test_observer(&tree);
ASSERT_TRUE(tree.Unserialize(update));
ASSERT_EQ(2U, test_observer.deleted_ids().size());
EXPECT_EQ(1, test_observer.deleted_ids()[0]);
EXPECT_EQ(2, test_observer.deleted_ids()[1]);
ASSERT_EQ(1U, test_observer.subtree_deleted_ids().size());
EXPECT_EQ(1, test_observer.subtree_deleted_ids()[0]);
ASSERT_EQ(2U, test_observer.created_ids().size());
EXPECT_EQ(3, test_observer.created_ids()[0]);
EXPECT_EQ(4, test_observer.created_ids()[1]);
ASSERT_EQ(1U, test_observer.subtree_creation_finished_ids().size());
EXPECT_EQ(3, test_observer.subtree_creation_finished_ids()[0]);
ASSERT_EQ(1U, test_observer.node_creation_finished_ids().size());
EXPECT_EQ(4, test_observer.node_creation_finished_ids()[0]);
ASSERT_TRUE(test_observer.root_changed());
}
TEST(AXTreeTest, TreeObserverIsCalledForTreeDataChanges) {
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(1);
initial_state.nodes[0].id = 1;
initial_state.has_tree_data = true;
initial_state.tree_data.title = "Initial";
AXTree tree(initial_state);
TestAXTreeObserver test_observer(&tree);
// An empty update shouldn't change tree data.
AXTreeUpdate empty_update;
EXPECT_TRUE(tree.Unserialize(empty_update));
EXPECT_FALSE(test_observer.tree_data_changed());
EXPECT_EQ("Initial", tree.data().title);
// An update with tree data shouldn't change tree data if
// |has_tree_data| isn't set.
AXTreeUpdate ignored_tree_data_update;
ignored_tree_data_update.tree_data.title = "Ignore Me";
EXPECT_TRUE(tree.Unserialize(ignored_tree_data_update));
EXPECT_FALSE(test_observer.tree_data_changed());
EXPECT_EQ("Initial", tree.data().title);
// An update with |has_tree_data| set should update the tree data.
AXTreeUpdate tree_data_update;
tree_data_update.has_tree_data = true;
tree_data_update.tree_data.title = "New Title";
EXPECT_TRUE(tree.Unserialize(tree_data_update));
EXPECT_TRUE(test_observer.tree_data_changed());
EXPECT_EQ("New Title", tree.data().title);
}
TEST(AXTreeTest, ReparentingDoesNotTriggerNodeCreated) {
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(3);
initial_state.nodes[0].id = 1;
initial_state.nodes[0].child_ids.push_back(2);
initial_state.nodes[1].id = 2;
initial_state.nodes[1].child_ids.push_back(3);
initial_state.nodes[2].id = 3;
AXTree tree(initial_state);
TestAXTreeObserver test_observer(&tree);
AXTreeUpdate update;
update.nodes.resize(2);
update.node_id_to_clear = 2;
update.root_id = 1;
update.nodes[0].id = 1;
update.nodes[0].child_ids.push_back(3);
update.nodes[1].id = 3;
EXPECT_TRUE(tree.Unserialize(update)) << tree.error();
std::vector<int> created = test_observer.node_creation_finished_ids();
std::vector<int> subtree_reparented =
test_observer.subtree_reparented_finished_ids();
std::vector<int> node_reparented =
test_observer.node_reparented_finished_ids();
ASSERT_FALSE(base::Contains(created, 3));
ASSERT_TRUE(base::Contains(subtree_reparented, 3));
ASSERT_FALSE(base::Contains(node_reparented, 3));
}
TEST(AXTreeTest, MultipleIgnoredChangesDoesNotBreakCache) {
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(3);
initial_state.nodes[0].id = 1;
initial_state.nodes[0].child_ids.push_back(2);
initial_state.nodes[1].id = 2;
initial_state.nodes[1].AddState(ax::mojom::State::kIgnored);
initial_state.nodes[1].child_ids.push_back(3);
initial_state.nodes[2].id = 3;
AXTree tree(initial_state);
TestAXTreeObserver test_observer(&tree);
EXPECT_EQ(1u, tree.GetFromId(2)->GetUnignoredChildCount());
AXTreeUpdate update;
update.nodes.resize(2);
update.nodes[0].id = 3;
update.nodes[0].AddState(ax::mojom::State::kIgnored);
update.nodes[1].id = 2;
update.nodes[1].child_ids.push_back(3);
EXPECT_TRUE(tree.Unserialize(update)) << tree.error();
EXPECT_EQ(0u, tree.GetFromId(2)->GetUnignoredChildCount());
EXPECT_FALSE(tree.GetFromId(2)->data().HasState(ax::mojom::State::kIgnored));
EXPECT_TRUE(tree.GetFromId(3)->data().HasState(ax::mojom::State::kIgnored));
}
TEST(AXTreeTest, TreeObserverIsNotCalledForReparenting) {
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(2);
initial_state.nodes[0].id = 1;
initial_state.nodes[0].child_ids.push_back(2);
initial_state.nodes[1].id = 2;
AXTree tree(initial_state);
AXTreeUpdate update;
update.node_id_to_clear = 1;
update.root_id = 2;
update.nodes.resize(2);
update.nodes[0].id = 2;
update.nodes[0].child_ids.push_back(4);
update.nodes[1].id = 4;
TestAXTreeObserver test_observer(&tree);
EXPECT_TRUE(tree.Unserialize(update));
ASSERT_EQ(1U, test_observer.deleted_ids().size());
EXPECT_EQ(1, test_observer.deleted_ids()[0]);
ASSERT_EQ(1U, test_observer.subtree_deleted_ids().size());
EXPECT_EQ(1, test_observer.subtree_deleted_ids()[0]);
ASSERT_EQ(1U, test_observer.created_ids().size());
EXPECT_EQ(4, test_observer.created_ids()[0]);
ASSERT_EQ(1U, test_observer.subtree_creation_finished_ids().size());
EXPECT_EQ(4, test_observer.subtree_creation_finished_ids()[0]);
ASSERT_EQ(1U, test_observer.subtree_reparented_finished_ids().size());
EXPECT_EQ(2, test_observer.subtree_reparented_finished_ids()[0]);
EXPECT_EQ(0U, test_observer.node_creation_finished_ids().size());
EXPECT_EQ(0U, test_observer.node_reparented_finished_ids().size());
ASSERT_TRUE(test_observer.root_changed());
}
// UAF caught by ax_tree_fuzzer
TEST(AXTreeTest, BogusAXTree) {
AXTreeUpdate initial_state;
AXNodeData node;
node.id = 0;
initial_state.nodes.push_back(node);
initial_state.nodes.push_back(node);
ui::AXTree tree;
tree.Unserialize(initial_state);
}
// UAF caught by ax_tree_fuzzer
TEST(AXTreeTest, BogusAXTree2) {
AXTreeUpdate initial_state;
AXNodeData node;
node.id = 0;
initial_state.nodes.push_back(node);
AXNodeData node2;
node2.id = 0;
node2.child_ids.push_back(0);
node2.child_ids.push_back(0);
initial_state.nodes.push_back(node2);
ui::AXTree tree;
tree.Unserialize(initial_state);
}
// UAF caught by ax_tree_fuzzer
TEST(AXTreeTest, BogusAXTree3) {
AXTreeUpdate initial_state;
AXNodeData node;
node.id = 0;
node.child_ids.push_back(1);
initial_state.nodes.push_back(node);
AXNodeData node2;
node2.id = 1;
node2.child_ids.push_back(1);
node2.child_ids.push_back(1);
initial_state.nodes.push_back(node2);
ui::AXTree tree;
tree.Unserialize(initial_state);
}
TEST(AXTreeTest, RoleAndStateChangeCallbacks) {
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(1);
initial_state.nodes[0].id = 1;
initial_state.nodes[0].role = ax::mojom::Role::kButton;
initial_state.nodes[0].SetCheckedState(ax::mojom::CheckedState::kTrue);
initial_state.nodes[0].AddState(ax::mojom::State::kFocusable);
AXTree tree(initial_state);
TestAXTreeObserver test_observer(&tree);
// Change the role and state.
AXTreeUpdate update;
update.root_id = 1;
update.nodes.resize(1);
update.nodes[0].id = 1;
update.nodes[0].role = ax::mojom::Role::kCheckBox;
update.nodes[0].SetCheckedState(ax::mojom::CheckedState::kFalse);
update.nodes[0].AddState(ax::mojom::State::kFocusable);
update.nodes[0].AddState(ax::mojom::State::kVisited);
EXPECT_TRUE(tree.Unserialize(update));
const std::vector<std::string>& change_log =
test_observer.attribute_change_log();
ASSERT_EQ(3U, change_log.size());
EXPECT_EQ("Role changed from button to checkBox", change_log[0]);
EXPECT_EQ("visited changed to true", change_log[1]);
EXPECT_EQ("checkedState changed from 2 to 1", change_log[2]);
}
TEST(AXTreeTest, AttributeChangeCallbacks) {
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(1);
initial_state.nodes[0].id = 1;
initial_state.nodes[0].AddStringAttribute(ax::mojom::StringAttribute::kName,
"N1");
initial_state.nodes[0].AddStringAttribute(
ax::mojom::StringAttribute::kDescription, "D1");
initial_state.nodes[0].AddBoolAttribute(ax::mojom::BoolAttribute::kLiveAtomic,
true);
initial_state.nodes[0].AddBoolAttribute(ax::mojom::BoolAttribute::kBusy,
false);
initial_state.nodes[0].AddFloatAttribute(
ax::mojom::FloatAttribute::kMinValueForRange, 1.0);
initial_state.nodes[0].AddFloatAttribute(
ax::mojom::FloatAttribute::kMaxValueForRange, 10.0);
initial_state.nodes[0].AddFloatAttribute(
ax::mojom::FloatAttribute::kStepValueForRange, 3.0);
initial_state.nodes[0].AddIntAttribute(ax::mojom::IntAttribute::kScrollX, 5);
initial_state.nodes[0].AddIntAttribute(ax::mojom::IntAttribute::kScrollXMin,
1);
AXTree tree(initial_state);
TestAXTreeObserver test_observer(&tree);
// Change existing attributes.
AXTreeUpdate update0;
update0.root_id = 1;
update0.nodes.resize(1);
update0.nodes[0].id = 1;
update0.nodes[0].AddStringAttribute(ax::mojom::StringAttribute::kName, "N2");
update0.nodes[0].AddStringAttribute(ax::mojom::StringAttribute::kDescription,
"D2");
update0.nodes[0].AddBoolAttribute(ax::mojom::BoolAttribute::kLiveAtomic,
false);
update0.nodes[0].AddBoolAttribute(ax::mojom::BoolAttribute::kBusy, true);
update0.nodes[0].AddFloatAttribute(
ax::mojom::FloatAttribute::kMinValueForRange, 2.0);
update0.nodes[0].AddFloatAttribute(
ax::mojom::FloatAttribute::kMaxValueForRange, 9.0);
update0.nodes[0].AddFloatAttribute(
ax::mojom::FloatAttribute::kStepValueForRange, 0.5);
update0.nodes[0].AddIntAttribute(ax::mojom::IntAttribute::kScrollX, 6);
update0.nodes[0].AddIntAttribute(ax::mojom::IntAttribute::kScrollXMin, 2);
EXPECT_TRUE(tree.Unserialize(update0));
const std::vector<std::string>& change_log =
test_observer.attribute_change_log();
ASSERT_EQ(9U, change_log.size());
EXPECT_EQ("name changed from N1 to N2", change_log[0]);
EXPECT_EQ("description changed from D1 to D2", change_log[1]);
EXPECT_EQ("liveAtomic changed to false", change_log[2]);
EXPECT_EQ("busy changed to true", change_log[3]);
EXPECT_EQ("minValueForRange changed from 1 to 2", change_log[4]);
EXPECT_EQ("maxValueForRange changed from 10 to 9", change_log[5]);
EXPECT_EQ("stepValueForRange changed from 3 to .5", change_log[6]);
EXPECT_EQ("scrollX changed from 5 to 6", change_log[7]);
EXPECT_EQ("scrollXMin changed from 1 to 2", change_log[8]);
TestAXTreeObserver test_observer2(&tree);
// Add and remove attributes.
AXTreeUpdate update1;
update1.root_id = 1;
update1.nodes.resize(1);
update1.nodes[0].id = 1;
update1.nodes[0].AddStringAttribute(ax::mojom::StringAttribute::kDescription,
"D3");
update1.nodes[0].AddStringAttribute(ax::mojom::StringAttribute::kValue, "V3");
update1.nodes[0].AddBoolAttribute(ax::mojom::BoolAttribute::kModal, true);
update1.nodes[0].AddFloatAttribute(ax::mojom::FloatAttribute::kValueForRange,
5.0);
update1.nodes[0].AddFloatAttribute(
ax::mojom::FloatAttribute::kMaxValueForRange, 9.0);
update1.nodes[0].AddIntAttribute(ax::mojom::IntAttribute::kScrollX, 7);
update1.nodes[0].AddIntAttribute(ax::mojom::IntAttribute::kScrollXMax, 10);
EXPECT_TRUE(tree.Unserialize(update1));
const std::vector<std::string>& change_log2 =
test_observer2.attribute_change_log();
ASSERT_EQ(11U, change_log2.size());
EXPECT_EQ("name changed from N2 to ", change_log2[0]);
EXPECT_EQ("description changed from D2 to D3", change_log2[1]);
EXPECT_EQ("value changed from to V3", change_log2[2]);
EXPECT_EQ("busy changed to false", change_log2[3]);
EXPECT_EQ("modal changed to true", change_log2[4]);
EXPECT_EQ("minValueForRange changed from 2 to 0", change_log2[5]);
EXPECT_EQ("stepValueForRange changed from 3 to .5", change_log[6]);
EXPECT_EQ("valueForRange changed from 0 to 5", change_log2[7]);
EXPECT_EQ("scrollXMin changed from 2 to 0", change_log2[8]);
EXPECT_EQ("scrollX changed from 6 to 7", change_log2[9]);
EXPECT_EQ("scrollXMax changed from 0 to 10", change_log2[10]);
}
TEST(AXTreeTest, IntListChangeCallbacks) {
std::vector<int32_t> one;
one.push_back(1);
std::vector<int32_t> two;
two.push_back(2);
two.push_back(2);
std::vector<int32_t> three;
three.push_back(3);
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(1);
initial_state.nodes[0].id = 1;
initial_state.nodes[0].AddIntListAttribute(
ax::mojom::IntListAttribute::kControlsIds, one);
initial_state.nodes[0].AddIntListAttribute(
ax::mojom::IntListAttribute::kRadioGroupIds, two);
AXTree tree(initial_state);
TestAXTreeObserver test_observer(&tree);
// Change existing attributes.
AXTreeUpdate update0;
update0.root_id = 1;
update0.nodes.resize(1);
update0.nodes[0].id = 1;
update0.nodes[0].AddIntListAttribute(
ax::mojom::IntListAttribute::kControlsIds, two);
update0.nodes[0].AddIntListAttribute(
ax::mojom::IntListAttribute::kRadioGroupIds, three);
EXPECT_TRUE(tree.Unserialize(update0));
const std::vector<std::string>& change_log =
test_observer.attribute_change_log();
ASSERT_EQ(2U, change_log.size());
EXPECT_EQ("controlsIds changed from 1 to 2,2", change_log[0]);
EXPECT_EQ("radioGroupIds changed from 2,2 to 3", change_log[1]);
TestAXTreeObserver test_observer2(&tree);
// Add and remove attributes.
AXTreeUpdate update1;
update1.root_id = 1;
update1.nodes.resize(1);
update1.nodes[0].id = 1;
update1.nodes[0].AddIntListAttribute(
ax::mojom::IntListAttribute::kRadioGroupIds, two);
update1.nodes[0].AddIntListAttribute(ax::mojom::IntListAttribute::kFlowtoIds,
three);
EXPECT_TRUE(tree.Unserialize(update1));
const std::vector<std::string>& change_log2 =
test_observer2.attribute_change_log();
ASSERT_EQ(3U, change_log2.size());
EXPECT_EQ("controlsIds changed from 2,2 to ", change_log2[0]);
EXPECT_EQ("radioGroupIds changed from 3 to 2,2", change_log2[1]);
EXPECT_EQ("flowtoIds changed from to 3", change_log2[2]);
}
// Create a very simple tree and make sure that we can get the bounds of
// any node.
TEST(AXTreeTest, GetBoundsBasic) {
AXTreeUpdate tree_update;
tree_update.root_id = 1;
tree_update.nodes.resize(2);
tree_update.nodes[0].id = 1;
tree_update.nodes[0].relative_bounds.bounds = gfx::RectF(0, 0, 800, 600);
tree_update.nodes[0].child_ids.push_back(2);
tree_update.nodes[1].id = 2;
tree_update.nodes[1].relative_bounds.bounds = gfx::RectF(100, 10, 400, 300);
AXTree tree(tree_update);
EXPECT_EQ("(0, 0) size (800 x 600)", GetBoundsAsString(tree, 1));
EXPECT_EQ("(100, 10) size (400 x 300)", GetBoundsAsString(tree, 2));
}
// If a node doesn't specify its location but at least one child does have
// a location, its computed bounds should be the union of all child bounds.
TEST(AXTreeTest, EmptyNodeBoundsIsUnionOfChildren) {
AXTreeUpdate tree_update;
tree_update.root_id = 1;
tree_update.nodes.resize(4);
tree_update.nodes[0].id = 1;
tree_update.nodes[0].relative_bounds.bounds = gfx::RectF(0, 0, 800, 600);
tree_update.nodes[0].child_ids.push_back(2);
tree_update.nodes[1].id = 2;
tree_update.nodes[1].relative_bounds.bounds =
gfx::RectF(); // Deliberately empty.
tree_update.nodes[1].child_ids.push_back(3);
tree_update.nodes[1].child_ids.push_back(4);
tree_update.nodes[2].id = 3;
tree_update.nodes[2].relative_bounds.bounds = gfx::RectF(100, 10, 400, 20);
tree_update.nodes[3].id = 4;
tree_update.nodes[3].relative_bounds.bounds = gfx::RectF(200, 30, 400, 20);
AXTree tree(tree_update);
EXPECT_EQ("(100, 10) size (500 x 40)", GetBoundsAsString(tree, 2));
}
// If a node doesn't specify its location but at least one child does have
// a location, it will be offscreen if all of its children are offscreen.
TEST(AXTreeTest, EmptyNodeNotOffscreenEvenIfAllChildrenOffscreen) {
AXTreeUpdate tree_update;
tree_update.root_id = 1;
tree_update.nodes.resize(4);
tree_update.nodes[0].id = 1;
tree_update.nodes[0].relative_bounds.bounds = gfx::RectF(0, 0, 800, 600);
tree_update.nodes[0].role = ax::mojom::Role::kRootWebArea;
tree_update.nodes[0].AddBoolAttribute(
ax::mojom::BoolAttribute::kClipsChildren, true);
tree_update.nodes[0].child_ids.push_back(2);
tree_update.nodes[1].id = 2;
tree_update.nodes[1].relative_bounds.bounds =
gfx::RectF(); // Deliberately empty.
tree_update.nodes[1].child_ids.push_back(3);
tree_update.nodes[1].child_ids.push_back(4);
// Both children are offscreen
tree_update.nodes[2].id = 3;
tree_update.nodes[2].relative_bounds.bounds = gfx::RectF(900, 10, 400, 20);
tree_update.nodes[3].id = 4;
tree_update.nodes[3].relative_bounds.bounds = gfx::RectF(1000, 30, 400, 20);
AXTree tree(tree_update);
EXPECT_FALSE(IsNodeOffscreen(tree, 2));
EXPECT_TRUE(IsNodeOffscreen(tree, 3));
EXPECT_TRUE(IsNodeOffscreen(tree, 4));
}
// Test that getting the bounds of a node works when there's a transform.
TEST(AXTreeTest, GetBoundsWithTransform) {
AXTreeUpdate tree_update;
tree_update.root_id = 1;
tree_update.nodes.resize(3);
tree_update.nodes[0].id = 1;
tree_update.nodes[0].relative_bounds.bounds = gfx::RectF(0, 0, 400, 300);
tree_update.nodes[0].relative_bounds.transform.reset(new gfx::Transform());
tree_update.nodes[0].relative_bounds.transform->Scale(2.0, 2.0);
tree_update.nodes[0].child_ids.push_back(2);
tree_update.nodes[0].child_ids.push_back(3);
tree_update.nodes[1].id = 2;
tree_update.nodes[1].relative_bounds.bounds = gfx::RectF(20, 10, 50, 5);
tree_update.nodes[2].id = 3;
tree_update.nodes[2].relative_bounds.bounds = gfx::RectF(20, 30, 50, 5);
tree_update.nodes[2].relative_bounds.transform.reset(new gfx::Transform());
tree_update.nodes[2].relative_bounds.transform->Scale(2.0, 2.0);
AXTree tree(tree_update);
EXPECT_EQ("(0, 0) size (800 x 600)", GetBoundsAsString(tree, 1));
EXPECT_EQ("(40, 20) size (100 x 10)", GetBoundsAsString(tree, 2));
EXPECT_EQ("(80, 120) size (200 x 20)", GetBoundsAsString(tree, 3));
}
// Test that getting the bounds of a node that's inside a container
// works correctly.
TEST(AXTreeTest, GetBoundsWithContainerId) {
AXTreeUpdate tree_update;
tree_update.root_id = 1;
tree_update.nodes.resize(4);
tree_update.nodes[0].id = 1;
tree_update.nodes[0].relative_bounds.bounds = gfx::RectF(0, 0, 800, 600);
tree_update.nodes[0].child_ids.push_back(2);
tree_update.nodes[1].id = 2;
tree_update.nodes[1].relative_bounds.bounds = gfx::RectF(100, 50, 600, 500);
tree_update.nodes[1].child_ids.push_back(3);
tree_update.nodes[1].child_ids.push_back(4);
tree_update.nodes[2].id = 3;
tree_update.nodes[2].relative_bounds.offset_container_id = 2;
tree_update.nodes[2].relative_bounds.bounds = gfx::RectF(20, 30, 50, 5);
tree_update.nodes[3].id = 4;
tree_update.nodes[3].relative_bounds.bounds = gfx::RectF(20, 30, 50, 5);
AXTree tree(tree_update);
EXPECT_EQ("(120, 80) size (50 x 5)", GetBoundsAsString(tree, 3));
EXPECT_EQ("(20, 30) size (50 x 5)", GetBoundsAsString(tree, 4));
}
// Test that getting the bounds of a node that's inside a scrolling container
// works correctly.
TEST(AXTreeTest, GetBoundsWithScrolling) {
AXTreeUpdate tree_update;
tree_update.root_id = 1;
tree_update.nodes.resize(3);
tree_update.nodes[0].id = 1;
tree_update.nodes[0].relative_bounds.bounds = gfx::RectF(0, 0, 800, 600);
tree_update.nodes[0].child_ids.push_back(2);
tree_update.nodes[1].id = 2;
tree_update.nodes[1].relative_bounds.bounds = gfx::RectF(100, 50, 600, 500);
tree_update.nodes[1].AddIntAttribute(ax::mojom::IntAttribute::kScrollX, 5);
tree_update.nodes[1].AddIntAttribute(ax::mojom::IntAttribute::kScrollY, 10);
tree_update.nodes[1].child_ids.push_back(3);
tree_update.nodes[2].id = 3;
tree_update.nodes[2].relative_bounds.offset_container_id = 2;
tree_update.nodes[2].relative_bounds.bounds = gfx::RectF(20, 30, 50, 5);
AXTree tree(tree_update);
EXPECT_EQ("(115, 70) size (50 x 5)", GetBoundsAsString(tree, 3));
}
TEST(AXTreeTest, GetBoundsEmptyBoundsInheritsFromParent) {
AXTreeUpdate tree_update;
tree_update.root_id = 1;
tree_update.nodes.resize(3);
tree_update.nodes[0].id = 1;
tree_update.nodes[0].relative_bounds.bounds = gfx::RectF(0, 0, 800, 600);
tree_update.nodes[1].AddBoolAttribute(
ax::mojom::BoolAttribute::kClipsChildren, true);
tree_update.nodes[0].child_ids.push_back(2);
tree_update.nodes[1].id = 2;
tree_update.nodes[1].relative_bounds.bounds = gfx::RectF(300, 200, 100, 100);
tree_update.nodes[1].child_ids.push_back(3);
tree_update.nodes[2].id = 3;
tree_update.nodes[2].relative_bounds.bounds = gfx::RectF();
AXTree tree(tree_update);
EXPECT_EQ("(0, 0) size (800 x 600)", GetBoundsAsString(tree, 1));
EXPECT_EQ("(300, 200) size (100 x 100)", GetBoundsAsString(tree, 2));
EXPECT_EQ("(300, 200) size (100 x 100)", GetBoundsAsString(tree, 3));
EXPECT_EQ("(0, 0) size (800 x 600)", GetUnclippedBoundsAsString(tree, 1));
EXPECT_EQ("(300, 200) size (100 x 100)", GetUnclippedBoundsAsString(tree, 2));
EXPECT_EQ("(300, 200) size (100 x 100)", GetUnclippedBoundsAsString(tree, 3));
EXPECT_FALSE(IsNodeOffscreen(tree, 1));
EXPECT_FALSE(IsNodeOffscreen(tree, 2));
EXPECT_TRUE(IsNodeOffscreen(tree, 3));
}
TEST(AXTreeTest, GetBoundsCropsChildToRoot) {
AXTreeUpdate tree_update;
tree_update.root_id = 1;
tree_update.nodes.resize(5);
tree_update.nodes[0].id = 1;
tree_update.nodes[0].relative_bounds.bounds = gfx::RectF(0, 0, 800, 600);
tree_update.nodes[0].AddBoolAttribute(
ax::mojom::BoolAttribute::kClipsChildren, true);
tree_update.nodes[0].child_ids.push_back(2);
tree_update.nodes[0].child_ids.push_back(3);
tree_update.nodes[0].child_ids.push_back(4);
tree_update.nodes[0].child_ids.push_back(5);
// Cropped in the top left
tree_update.nodes[1].id = 2;
tree_update.nodes[1].relative_bounds.bounds =
gfx::RectF(-100, -100, 150, 150);
// Cropped in the bottom right
tree_update.nodes[2].id = 3;
tree_update.nodes[2].relative_bounds.bounds = gfx::RectF(700, 500, 150, 150);
// Offscreen on the top
tree_update.nodes[3].id = 4;
tree_update.nodes[3].relative_bounds.bounds = gfx::RectF(50, -200, 150, 150);
// Offscreen on the bottom
tree_update.nodes[4].id = 5;
tree_update.nodes[4].relative_bounds.bounds = gfx::RectF(50, 700, 150, 150);
AXTree tree(tree_update);
EXPECT_EQ("(0, 0) size (50 x 50)", GetBoundsAsString(tree, 2));
EXPECT_EQ("(700, 500) size (100 x 100)", GetBoundsAsString(tree, 3));
EXPECT_EQ("(50, 0) size (150 x 1)", GetBoundsAsString(tree, 4));
EXPECT_EQ("(50, 599) size (150 x 1)", GetBoundsAsString(tree, 5));
// Check the unclipped bounds are as expected.
EXPECT_EQ("(-100, -100) size (150 x 150)",
GetUnclippedBoundsAsString(tree, 2));
EXPECT_EQ("(700, 500) size (150 x 150)", GetUnclippedBoundsAsString(tree, 3));
EXPECT_EQ("(50, -200) size (150 x 150)", GetUnclippedBoundsAsString(tree, 4));
EXPECT_EQ("(50, 700) size (150 x 150)", GetUnclippedBoundsAsString(tree, 5));
}
TEST(AXTreeTest, GetBoundsSetsOffscreenIfClipsChildren) {
AXTreeUpdate tree_update;
tree_update.root_id = 1;
tree_update.nodes.resize(5);
tree_update.nodes[0].id = 1;
tree_update.nodes[0].relative_bounds.bounds = gfx::RectF(0, 0, 800, 600);
tree_update.nodes[0].AddBoolAttribute(
ax::mojom::BoolAttribute::kClipsChildren, true);
tree_update.nodes[0].child_ids.push_back(2);
tree_update.nodes[0].child_ids.push_back(3);
tree_update.nodes[1].id = 2;
tree_update.nodes[1].relative_bounds.bounds = gfx::RectF(0, 0, 200, 200);
tree_update.nodes[1].AddBoolAttribute(
ax::mojom::BoolAttribute::kClipsChildren, true);
tree_update.nodes[1].child_ids.push_back(4);
tree_update.nodes[2].id = 3;
tree_update.nodes[2].relative_bounds.bounds = gfx::RectF(0, 0, 200, 200);
tree_update.nodes[2].child_ids.push_back(5);
// Clipped by its parent
tree_update.nodes[3].id = 4;
tree_update.nodes[3].relative_bounds.bounds = gfx::RectF(250, 250, 100, 100);
tree_update.nodes[3].relative_bounds.offset_container_id = 2;
// Outside of its parent, but its parent does not clip children,
// so it should not be offscreen.
tree_update.nodes[4].id = 5;
tree_update.nodes[4].relative_bounds.bounds = gfx::RectF(250, 250, 100, 100);
tree_update.nodes[4].relative_bounds.offset_container_id = 3;
AXTree tree(tree_update);
EXPECT_TRUE(IsNodeOffscreen(tree, 4));
EXPECT_FALSE(IsNodeOffscreen(tree, 5));
}
TEST(AXTreeTest, GetBoundsUpdatesOffscreen) {
AXTreeUpdate tree_update;
tree_update.root_id = 1;
tree_update.nodes.resize(5);
tree_update.nodes[0].id = 1;
tree_update.nodes[0].relative_bounds.bounds = gfx::RectF(0, 0, 800, 600);
tree_update.nodes[0].role = ax::mojom::Role::kRootWebArea;
tree_update.nodes[0].AddBoolAttribute(
ax::mojom::BoolAttribute::kClipsChildren, true);
tree_update.nodes[0].child_ids.push_back(2);
tree_update.nodes[0].child_ids.push_back(3);
tree_update.nodes[0].child_ids.push_back(4);
tree_update.nodes[0].child_ids.push_back(5);
// Fully onscreen
tree_update.nodes[1].id = 2;
tree_update.nodes[1].relative_bounds.bounds = gfx::RectF(10, 10, 150, 150);
// Cropped in the bottom right
tree_update.nodes[2].id = 3;
tree_update.nodes[2].relative_bounds.bounds = gfx::RectF(700, 500, 150, 150);
// Offscreen on the top
tree_update.nodes[3].id = 4;
tree_update.nodes[3].relative_bounds.bounds = gfx::RectF(50, -200, 150, 150);
// Offscreen on the bottom
tree_update.nodes[4].id = 5;
tree_update.nodes[4].relative_bounds.bounds = gfx::RectF(50, 700, 150, 150);
AXTree tree(tree_update);
EXPECT_FALSE(IsNodeOffscreen(tree, 2));
EXPECT_FALSE(IsNodeOffscreen(tree, 3));
EXPECT_TRUE(IsNodeOffscreen(tree, 4));
EXPECT_TRUE(IsNodeOffscreen(tree, 5));
}
TEST(AXTreeTest, IntReverseRelations) {
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(4);
initial_state.nodes[0].id = 1;
initial_state.nodes[0].AddIntAttribute(
ax::mojom::IntAttribute::kActivedescendantId, 2);
initial_state.nodes[0].child_ids.push_back(2);
initial_state.nodes[0].child_ids.push_back(3);
initial_state.nodes[0].child_ids.push_back(4);
initial_state.nodes[1].id = 2;
initial_state.nodes[2].id = 3;
initial_state.nodes[2].AddIntAttribute(ax::mojom::IntAttribute::kMemberOfId,
1);
initial_state.nodes[3].id = 4;
initial_state.nodes[3].AddIntAttribute(ax::mojom::IntAttribute::kMemberOfId,
1);
AXTree tree(initial_state);
auto reverse_active_descendant =
tree.GetReverseRelations(ax::mojom::IntAttribute::kActivedescendantId, 2);
ASSERT_EQ(1U, reverse_active_descendant.size());
EXPECT_TRUE(base::Contains(reverse_active_descendant, 1));
reverse_active_descendant =
tree.GetReverseRelations(ax::mojom::IntAttribute::kActivedescendantId, 1);
ASSERT_EQ(0U, reverse_active_descendant.size());
auto reverse_errormessage =
tree.GetReverseRelations(ax::mojom::IntAttribute::kErrormessageId, 1);
ASSERT_EQ(0U, reverse_errormessage.size());
auto reverse_member_of =
tree.GetReverseRelations(ax::mojom::IntAttribute::kMemberOfId, 1);
ASSERT_EQ(2U, reverse_member_of.size());
EXPECT_TRUE(base::Contains(reverse_member_of, 3));
EXPECT_TRUE(base::Contains(reverse_member_of, 4));
AXTreeUpdate update = initial_state;
update.nodes.resize(5);
update.nodes[0].int_attributes.clear();
update.nodes[0].AddIntAttribute(ax::mojom::IntAttribute::kActivedescendantId,
5);
update.nodes[0].child_ids.push_back(5);
update.nodes[2].int_attributes.clear();
update.nodes[4].id = 5;
update.nodes[4].AddIntAttribute(ax::mojom::IntAttribute::kMemberOfId, 1);
EXPECT_TRUE(tree.Unserialize(update));
reverse_active_descendant =
tree.GetReverseRelations(ax::mojom::IntAttribute::kActivedescendantId, 2);
ASSERT_EQ(0U, reverse_active_descendant.size());
reverse_active_descendant =
tree.GetReverseRelations(ax::mojom::IntAttribute::kActivedescendantId, 5);
ASSERT_EQ(1U, reverse_active_descendant.size());
EXPECT_TRUE(base::Contains(reverse_active_descendant, 1));
reverse_member_of =
tree.GetReverseRelations(ax::mojom::IntAttribute::kMemberOfId, 1);
ASSERT_EQ(2U, reverse_member_of.size());
EXPECT_TRUE(base::Contains(reverse_member_of, 4));
EXPECT_TRUE(base::Contains(reverse_member_of, 5));
}
TEST(AXTreeTest, IntListReverseRelations) {
std::vector<int32_t> node_two;
node_two.push_back(2);
std::vector<int32_t> nodes_two_three;
nodes_two_three.push_back(2);
nodes_two_three.push_back(3);
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(3);
initial_state.nodes[0].id = 1;
initial_state.nodes[0].AddIntListAttribute(
ax::mojom::IntListAttribute::kLabelledbyIds, node_two);
initial_state.nodes[0].child_ids.push_back(2);
initial_state.nodes[0].child_ids.push_back(3);
initial_state.nodes[1].id = 2;
initial_state.nodes[2].id = 3;
AXTree tree(initial_state);
auto reverse_labelled_by =
tree.GetReverseRelations(ax::mojom::IntListAttribute::kLabelledbyIds, 2);
ASSERT_EQ(1U, reverse_labelled_by.size());
EXPECT_TRUE(base::Contains(reverse_labelled_by, 1));
reverse_labelled_by =
tree.GetReverseRelations(ax::mojom::IntListAttribute::kLabelledbyIds, 3);
ASSERT_EQ(0U, reverse_labelled_by.size());
// Change existing attributes.
AXTreeUpdate update = initial_state;
update.nodes[0].intlist_attributes.clear();
update.nodes[0].AddIntListAttribute(
ax::mojom::IntListAttribute::kLabelledbyIds, nodes_two_three);
EXPECT_TRUE(tree.Unserialize(update));
reverse_labelled_by =
tree.GetReverseRelations(ax::mojom::IntListAttribute::kLabelledbyIds, 3);
ASSERT_EQ(1U, reverse_labelled_by.size());
EXPECT_TRUE(base::Contains(reverse_labelled_by, 1));
}
TEST(AXTreeTest, DeletingNodeUpdatesReverseRelations) {
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(3);
initial_state.nodes[0].id = 1;
initial_state.nodes[0].child_ids = {2, 3};
initial_state.nodes[1].id = 2;
initial_state.nodes[2].id = 3;
initial_state.nodes[2].AddIntAttribute(
ax::mojom::IntAttribute::kActivedescendantId, 2);
AXTree tree(initial_state);
auto reverse_active_descendant =
tree.GetReverseRelations(ax::mojom::IntAttribute::kActivedescendantId, 2);
ASSERT_EQ(1U, reverse_active_descendant.size());
EXPECT_TRUE(base::Contains(reverse_active_descendant, 3));
AXTreeUpdate update;
update.root_id = 1;
update.nodes.resize(1);
update.nodes[0].id = 1;
update.nodes[0].child_ids = {2};
EXPECT_TRUE(tree.Unserialize(update));
reverse_active_descendant =
tree.GetReverseRelations(ax::mojom::IntAttribute::kActivedescendantId, 2);
ASSERT_EQ(0U, reverse_active_descendant.size());
}
TEST(AXTreeTest, ReverseRelationsDoNotKeepGrowing) {
// The number of total entries in int_reverse_relations and
// intlist_reverse_relations should not keep growing as the tree
// changes.
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(2);
initial_state.nodes[0].id = 1;
initial_state.nodes[0].AddIntAttribute(
ax::mojom::IntAttribute::kActivedescendantId, 2);
initial_state.nodes[0].AddIntListAttribute(
ax::mojom::IntListAttribute::kLabelledbyIds, {2});
initial_state.nodes[0].child_ids.push_back(2);
initial_state.nodes[1].id = 2;
AXTree tree(initial_state);
for (int i = 0; i < 1000; ++i) {
AXTreeUpdate update;
update.root_id = 1;
update.nodes.resize(2);
update.nodes[0].id = 1;
update.nodes[1].id = i + 3;
update.nodes[0].AddIntAttribute(
ax::mojom::IntAttribute::kActivedescendantId, update.nodes[1].id);
update.nodes[0].AddIntListAttribute(
ax::mojom::IntListAttribute::kLabelledbyIds, {update.nodes[1].id});
update.nodes[1].AddIntAttribute(ax::mojom::IntAttribute::kMemberOfId, 1);
update.nodes[0].child_ids.push_back(update.nodes[1].id);
EXPECT_TRUE(tree.Unserialize(update));
}
size_t map_key_count = 0;
size_t set_entry_count = 0;
for (auto& iter : tree.int_reverse_relations()) {
map_key_count += iter.second.size() + 1;
for (auto it2 = iter.second.begin(); it2 != iter.second.end(); ++it2) {
set_entry_count += it2->second.size();
}
}
// Note: 10 is arbitary, the idea here is just that we mutated the tree
// 1000 times, so if we have fewer than 10 entries in the maps / sets then
// the map isn't growing / leaking. Same below.
EXPECT_LT(map_key_count, 10U);
EXPECT_LT(set_entry_count, 10U);
map_key_count = 0;
set_entry_count = 0;
for (auto& iter : tree.intlist_reverse_relations()) {
map_key_count += iter.second.size() + 1;
for (auto it2 = iter.second.begin(); it2 != iter.second.end(); ++it2) {
set_entry_count += it2->second.size();
}
}
EXPECT_LT(map_key_count, 10U);
EXPECT_LT(set_entry_count, 10U);
}
TEST(AXTreeTest, SkipIgnoredNodes) {
AXTreeUpdate tree_update;
tree_update.root_id = 1;
tree_update.nodes.resize(5);
tree_update.nodes[0].id = 1;
tree_update.nodes[0].child_ids = {2, 3};
tree_update.nodes[1].id = 2;
tree_update.nodes[1].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[1].child_ids = {4, 5};
tree_update.nodes[2].id = 3;
tree_update.nodes[3].id = 4;
tree_update.nodes[4].id = 5;
AXTree tree(tree_update);
AXNode* root = tree.root();
ASSERT_EQ(2u, root->children().size());
ASSERT_EQ(2, root->children()[0]->id());
ASSERT_EQ(3, root->children()[1]->id());
EXPECT_EQ(3u, root->GetUnignoredChildCount());
EXPECT_EQ(4, root->GetUnignoredChildAtIndex(0)->id());
EXPECT_EQ(5, root->GetUnignoredChildAtIndex(1)->id());
EXPECT_EQ(3, root->GetUnignoredChildAtIndex(2)->id());
EXPECT_EQ(0u, root->GetUnignoredChildAtIndex(0)->GetUnignoredIndexInParent());
EXPECT_EQ(1u, root->GetUnignoredChildAtIndex(1)->GetUnignoredIndexInParent());
EXPECT_EQ(2u, root->GetUnignoredChildAtIndex(2)->GetUnignoredIndexInParent());
EXPECT_EQ(1, root->GetUnignoredChildAtIndex(0)->GetUnignoredParent()->id());
}
TEST(AXTreeTest, CachedUnignoredValues) {
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(5);
initial_state.nodes[0].id = 1;
initial_state.nodes[0].child_ids = {2, 3};
initial_state.nodes[1].id = 2;
initial_state.nodes[1].AddState(ax::mojom::State::kIgnored);
initial_state.nodes[1].child_ids = {4, 5};
initial_state.nodes[2].id = 3;
initial_state.nodes[3].id = 4;
initial_state.nodes[4].id = 5;
AXTree tree(initial_state);
AXNode* root = tree.root();
ASSERT_EQ(2u, root->children().size());
ASSERT_EQ(2, root->children()[0]->id());
ASSERT_EQ(3, root->children()[1]->id());
EXPECT_EQ(3u, root->GetUnignoredChildCount());
EXPECT_EQ(4, root->GetUnignoredChildAtIndex(0)->id());
EXPECT_EQ(5, root->GetUnignoredChildAtIndex(1)->id());
EXPECT_EQ(3, root->GetUnignoredChildAtIndex(2)->id());
EXPECT_EQ(0u, root->GetUnignoredChildAtIndex(0)->GetUnignoredIndexInParent());
EXPECT_EQ(1u, root->GetUnignoredChildAtIndex(1)->GetUnignoredIndexInParent());
EXPECT_EQ(2u, root->GetUnignoredChildAtIndex(2)->GetUnignoredIndexInParent());
EXPECT_EQ(1, root->GetUnignoredChildAtIndex(0)->GetUnignoredParent()->id());
// Ensure when a node goes from ignored to unignored, its children have their
// unignored_index_in_parent updated.
AXTreeUpdate update = initial_state;
update.nodes[1].RemoveState(ax::mojom::State::kIgnored);
EXPECT_TRUE(tree.Unserialize(update));
root = tree.root();
EXPECT_EQ(2u, root->GetUnignoredChildCount());
EXPECT_EQ(2, root->GetUnignoredChildAtIndex(0)->id());
EXPECT_EQ(0u, tree.GetFromId(4)->GetUnignoredIndexInParent());
EXPECT_EQ(1u, tree.GetFromId(5)->GetUnignoredIndexInParent());
// Ensure when a node goes from unignored to unignored, siblings are correctly
// updated.
AXTreeUpdate update2 = update;
update2.nodes[3].AddState(ax::mojom::State::kIgnored);
EXPECT_TRUE(tree.Unserialize(update2));
EXPECT_EQ(0u, tree.GetFromId(5)->GetUnignoredIndexInParent());
// Ensure siblings of a deleted node are updated.
AXTreeUpdate update3 = update2;
update3.nodes.resize(1);
update3.nodes[0].id = 1;
update3.nodes[0].child_ids = {3};
EXPECT_TRUE(tree.Unserialize(update3));
EXPECT_EQ(0u, tree.GetFromId(3)->GetUnignoredIndexInParent());
// Ensure new nodes are correctly updated.
AXTreeUpdate update4 = update3;
update4.nodes.resize(3);
update4.nodes[0].id = 1;
update4.nodes[0].child_ids = {3, 6};
update4.nodes[1].id = 6;
update4.nodes[1].child_ids = {7};
update4.nodes[2].id = 7;
EXPECT_TRUE(tree.Unserialize(update4));
EXPECT_EQ(0u, tree.GetFromId(3)->GetUnignoredIndexInParent());
EXPECT_EQ(1u, tree.GetFromId(6)->GetUnignoredIndexInParent());
EXPECT_EQ(0u, tree.GetFromId(7)->GetUnignoredIndexInParent());
// Ensure reparented nodes are correctly updated.
AXTreeUpdate update5 = update4;
update5.nodes.resize(2);
update5.node_id_to_clear = 6;
update5.nodes[0].id = 1;
update5.nodes[0].child_ids = {3, 7};
update5.nodes[1].id = 7;
update5.nodes[1].child_ids = {};
EXPECT_TRUE(tree.Unserialize(update5));
EXPECT_EQ(2u, tree.GetFromId(1)->GetUnignoredChildCount());
EXPECT_EQ(0u, tree.GetFromId(3)->GetUnignoredIndexInParent());
EXPECT_EQ(1u, tree.GetFromId(7)->GetUnignoredIndexInParent());
AXTreeUpdate update6;
update6.nodes.resize(1);
update6.nodes[0].id = 7;
update6.nodes[0].AddState(ax::mojom::State::kIgnored);
EXPECT_TRUE(tree.Unserialize(update6));
EXPECT_EQ(1u, tree.GetFromId(1)->GetUnignoredChildCount());
EXPECT_EQ(0u, tree.GetFromId(3)->GetUnignoredIndexInParent());
AXTreeUpdate update7 = update6;
update7.nodes.resize(2);
update7.nodes[0].id = 7;
update7.nodes[0].child_ids = {8};
update7.nodes[1].id = 8;
EXPECT_TRUE(tree.Unserialize(update7));
EXPECT_EQ(2u, tree.GetFromId(1)->GetUnignoredChildCount());
EXPECT_EQ(0u, tree.GetFromId(3)->GetUnignoredIndexInParent());
}
TEST(AXTreeTest, TestRecursionUnignoredChildCount) {
AXTreeUpdate tree_update;
tree_update.root_id = 1;
tree_update.nodes.resize(5);
tree_update.nodes[0].id = 1;
tree_update.nodes[0].child_ids = {2, 3};
tree_update.nodes[1].id = 2;
tree_update.nodes[1].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[1].child_ids = {4};
tree_update.nodes[2].id = 3;
tree_update.nodes[2].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[3].id = 4;
tree_update.nodes[3].child_ids = {5};
tree_update.nodes[3].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[4].id = 5;
AXTree tree(tree_update);
AXNode* root = tree.root();
EXPECT_EQ(2u, root->children().size());
EXPECT_EQ(1u, root->GetUnignoredChildCount());
EXPECT_EQ(5, root->GetUnignoredChildAtIndex(0)->id());
AXNode* unignored = tree.GetFromId(5);
EXPECT_EQ(0u, unignored->GetUnignoredChildCount());
}
TEST(AXTreeTest, NullUnignoredChildren) {
AXTreeUpdate tree_update;
tree_update.root_id = 1;
tree_update.nodes.resize(3);
tree_update.nodes[0].id = 1;
tree_update.nodes[0].child_ids = {2, 3};
tree_update.nodes[1].id = 2;
tree_update.nodes[1].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[2].id = 3;
tree_update.nodes[2].AddState(ax::mojom::State::kIgnored);
AXTree tree(tree_update);
AXNode* root = tree.root();
EXPECT_EQ(2u, root->children().size());
EXPECT_EQ(0u, root->GetUnignoredChildCount());
EXPECT_EQ(nullptr, root->GetUnignoredChildAtIndex(0));
EXPECT_EQ(nullptr, root->GetUnignoredChildAtIndex(1));
}
TEST(AXTreeTest, UnignoredChildIterator) {
AXTreeUpdate tree_update;
// (i) => node is ignored
// 1
// |__________
// | | |
// 2(i) 3 4
// |_______________________
// | | | |
// 5 6 7(i) 8(i)
// | | |________
// | | | |
// 9 10(i) 11(i) 12
// | |____
// | | |
// 13(i) 14 15
tree_update.root_id = 1;
tree_update.nodes.resize(15);
tree_update.nodes[0].id = 1;
tree_update.nodes[0].child_ids = {2, 3, 4};
tree_update.nodes[1].id = 2;
tree_update.nodes[1].child_ids = {5, 6, 7, 8};
tree_update.nodes[1].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[2].id = 3;
tree_update.nodes[3].id = 4;
tree_update.nodes[4].id = 5;
tree_update.nodes[4].child_ids = {9};
tree_update.nodes[5].id = 6;
tree_update.nodes[5].child_ids = {10};
tree_update.nodes[6].id = 7;
tree_update.nodes[6].child_ids = {11, 12};
tree_update.nodes[6].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[7].id = 8;
tree_update.nodes[7].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[8].id = 9;
tree_update.nodes[9].id = 10;
tree_update.nodes[9].child_ids = {13};
tree_update.nodes[9].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[10].id = 11;
tree_update.nodes[10].child_ids = {14, 15};
tree_update.nodes[10].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[11].id = 12;
tree_update.nodes[12].id = 13;
tree_update.nodes[12].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[13].id = 14;
tree_update.nodes[14].id = 15;
AXTree tree(tree_update);
AXNode* root = tree.root();
// Test traversal
// UnignoredChildren(root) = {5, 6, 14, 15, 12, 3, 4}
AXNode::UnignoredChildIterator unignored_iterator =
root->UnignoredChildrenBegin();
EXPECT_EQ(5, (*unignored_iterator).id());
EXPECT_EQ(6, (*++unignored_iterator).id());
EXPECT_EQ(14, (*++unignored_iterator).id());
EXPECT_EQ(15, (*++unignored_iterator).id());
EXPECT_EQ(14, (*--unignored_iterator).id());
EXPECT_EQ(6, (*--unignored_iterator).id());
EXPECT_EQ(14, (*++unignored_iterator).id());
EXPECT_EQ(15, (*++unignored_iterator).id());
EXPECT_EQ(12, (*++unignored_iterator).id());
EXPECT_EQ(3, (*++unignored_iterator).id());
EXPECT_EQ(4, (*++unignored_iterator).id());
EXPECT_EQ(root->UnignoredChildrenEnd(), ++unignored_iterator);
// test empty list
// UnignoredChildren(3) = {}
AXNode* node3 = tree.GetFromId(3);
unignored_iterator = node3->UnignoredChildrenBegin();
EXPECT_EQ(node3->UnignoredChildrenEnd(), unignored_iterator);
// empty list from ignored node with no children
// UnignoredChildren(8) = {}
AXNode* node8 = tree.GetFromId(8);
unignored_iterator = node8->UnignoredChildrenBegin();
EXPECT_EQ(node8->UnignoredChildrenEnd(), unignored_iterator);
// empty list from ignored node with unignored children
// UnignoredChildren(11) = {}
AXNode* node11 = tree.GetFromId(11);
unignored_iterator = node11->UnignoredChildrenBegin();
EXPECT_EQ(14, (*unignored_iterator).id());
// Two UnignoredChildIterators from the same parent at the same position
// should be equivalent, even in end position.
unignored_iterator = root->UnignoredChildrenBegin();
AXNode::UnignoredChildIterator unignored_iterator2 =
root->UnignoredChildrenBegin();
auto end = root->UnignoredChildrenEnd();
while (unignored_iterator != end) {
ASSERT_EQ(unignored_iterator, unignored_iterator2);
++unignored_iterator;
++unignored_iterator2;
}
ASSERT_EQ(unignored_iterator, unignored_iterator2);
}
TEST(AXTreeTest, UnignoredAccessors) {
AXTreeUpdate tree_update;
// (i) => node is ignored
// 1
// |__________
// | | |
// 2(i) 3 4
// |_______________________
// | | | |
// 5 6 7(i) 8(i)
// | | |________
// | | | |
// 9 10(i) 11(i) 12
// | |____
// | | |
// 13(i) 14 15
// | |
// 16 17(i)
tree_update.root_id = 1;
tree_update.nodes.resize(17);
tree_update.nodes[0].id = 1;
tree_update.nodes[0].child_ids = {2, 3, 4};
tree_update.nodes[1].id = 2;
tree_update.nodes[1].child_ids = {5, 6, 7, 8};
tree_update.nodes[1].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[2].id = 3;
tree_update.nodes[3].id = 4;
tree_update.nodes[4].id = 5;
tree_update.nodes[4].child_ids = {9};
tree_update.nodes[5].id = 6;
tree_update.nodes[5].child_ids = {10};
tree_update.nodes[6].id = 7;
tree_update.nodes[6].child_ids = {11, 12};
tree_update.nodes[6].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[7].id = 8;
tree_update.nodes[7].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[8].id = 9;
tree_update.nodes[9].id = 10;
tree_update.nodes[9].child_ids = {13};
tree_update.nodes[9].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[10].id = 11;
tree_update.nodes[10].child_ids = {14, 15};
tree_update.nodes[10].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[11].id = 12;
tree_update.nodes[12].id = 13;
tree_update.nodes[12].child_ids = {16};
tree_update.nodes[12].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[13].id = 14;
tree_update.nodes[13].child_ids = {17};
tree_update.nodes[14].id = 15;
tree_update.nodes[15].id = 16;
tree_update.nodes[16].id = 17;
tree_update.nodes[16].AddState(ax::mojom::State::kIgnored);
AXTree tree(tree_update);
EXPECT_EQ(4, tree.GetFromId(1)->GetLastUnignoredChild()->id());
EXPECT_EQ(12, tree.GetFromId(2)->GetLastUnignoredChild()->id());
EXPECT_EQ(nullptr, tree.GetFromId(3)->GetLastUnignoredChild());
EXPECT_EQ(nullptr, tree.GetFromId(4)->GetLastUnignoredChild());
EXPECT_EQ(9, tree.GetFromId(5)->GetLastUnignoredChild()->id());
EXPECT_EQ(16, tree.GetFromId(6)->GetLastUnignoredChild()->id());
EXPECT_EQ(12, tree.GetFromId(7)->GetLastUnignoredChild()->id());
EXPECT_EQ(nullptr, tree.GetFromId(8)->GetLastUnignoredChild());
EXPECT_EQ(nullptr, tree.GetFromId(9)->GetLastUnignoredChild());
EXPECT_EQ(16, tree.GetFromId(10)->GetLastUnignoredChild()->id());
EXPECT_EQ(15, tree.GetFromId(11)->GetLastUnignoredChild()->id());
EXPECT_EQ(nullptr, tree.GetFromId(12)->GetLastUnignoredChild());
EXPECT_EQ(16, tree.GetFromId(13)->GetLastUnignoredChild()->id());
EXPECT_EQ(nullptr, tree.GetFromId(14)->GetLastUnignoredChild());
EXPECT_EQ(nullptr, tree.GetFromId(15)->GetLastUnignoredChild());
EXPECT_EQ(nullptr, tree.GetFromId(16)->GetLastUnignoredChild());
EXPECT_EQ(nullptr, tree.GetFromId(17)->GetLastUnignoredChild());
}
TEST(AXTreeTest, UnignoredNextPreviousChild) {
AXTreeUpdate tree_update;
// (i) => node is ignored
// 1
// |__________
// | | |
// 2(i) 3 4
// |_______________________
// | | | |
// 5 6 7(i) 8(i)
// | | |________
// | | | |
// 9 10(i) 11(i) 12
// | |____
// | | |
// 13(i) 14 15
// |
// 16
tree_update.root_id = 1;
tree_update.nodes.resize(16);
tree_update.nodes[0].id = 1;
tree_update.nodes[0].child_ids = {2, 3, 4};
tree_update.nodes[1].id = 2;
tree_update.nodes[1].child_ids = {5, 6, 7, 8};
tree_update.nodes[1].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[2].id = 3;
tree_update.nodes[3].id = 4;
tree_update.nodes[4].id = 5;
tree_update.nodes[4].child_ids = {9};
tree_update.nodes[5].id = 6;
tree_update.nodes[5].child_ids = {10};
tree_update.nodes[6].id = 7;
tree_update.nodes[6].child_ids = {11, 12};
tree_update.nodes[6].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[7].id = 8;
tree_update.nodes[7].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[8].id = 9;
tree_update.nodes[9].id = 10;
tree_update.nodes[9].child_ids = {13};
tree_update.nodes[9].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[10].id = 11;
tree_update.nodes[10].child_ids = {14, 15};
tree_update.nodes[10].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[11].id = 12;
tree_update.nodes[12].id = 13;
tree_update.nodes[12].child_ids = {16};
tree_update.nodes[12].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[13].id = 14;
tree_update.nodes[14].id = 15;
tree_update.nodes[15].id = 16;
AXTree tree(tree_update);
EXPECT_EQ(nullptr, tree.GetFromId(1)->GetNextUnignoredSibling());
EXPECT_EQ(nullptr, tree.GetFromId(1)->GetPreviousUnignoredSibling());
EXPECT_EQ(tree.GetFromId(3), tree.GetFromId(2)->GetNextUnignoredSibling());
EXPECT_EQ(nullptr, tree.GetFromId(2)->GetPreviousUnignoredSibling());
EXPECT_EQ(tree.GetFromId(4), tree.GetFromId(3)->GetNextUnignoredSibling());
EXPECT_EQ(tree.GetFromId(12),
tree.GetFromId(3)->GetPreviousUnignoredSibling());
EXPECT_EQ(nullptr, tree.GetFromId(4)->GetNextUnignoredSibling());
EXPECT_EQ(tree.GetFromId(3),
tree.GetFromId(4)->GetPreviousUnignoredSibling());
EXPECT_EQ(tree.GetFromId(6), tree.GetFromId(5)->GetNextUnignoredSibling());
EXPECT_EQ(nullptr, tree.GetFromId(5)->GetPreviousUnignoredSibling());
EXPECT_EQ(tree.GetFromId(14), tree.GetFromId(6)->GetNextUnignoredSibling());
EXPECT_EQ(tree.GetFromId(5),
tree.GetFromId(6)->GetPreviousUnignoredSibling());
EXPECT_EQ(tree.GetFromId(3), tree.GetFromId(7)->GetNextUnignoredSibling());
EXPECT_EQ(tree.GetFromId(6),
tree.GetFromId(7)->GetPreviousUnignoredSibling());
EXPECT_EQ(tree.GetFromId(3), tree.GetFromId(8)->GetNextUnignoredSibling());
EXPECT_EQ(tree.GetFromId(12),
tree.GetFromId(8)->GetPreviousUnignoredSibling());
EXPECT_EQ(nullptr, tree.GetFromId(9)->GetNextUnignoredSibling());
EXPECT_EQ(nullptr, tree.GetFromId(9)->GetPreviousUnignoredSibling());
EXPECT_EQ(nullptr, tree.GetFromId(10)->GetNextUnignoredSibling());
EXPECT_EQ(nullptr, tree.GetFromId(10)->GetPreviousUnignoredSibling());
EXPECT_EQ(tree.GetFromId(12), tree.GetFromId(11)->GetNextUnignoredSibling());
EXPECT_EQ(tree.GetFromId(6),
tree.GetFromId(11)->GetPreviousUnignoredSibling());
EXPECT_EQ(tree.GetFromId(3), tree.GetFromId(12)->GetNextUnignoredSibling());
EXPECT_EQ(tree.GetFromId(15),
tree.GetFromId(12)->GetPreviousUnignoredSibling());
EXPECT_EQ(nullptr, tree.GetFromId(13)->GetNextUnignoredSibling());
EXPECT_EQ(nullptr, tree.GetFromId(13)->GetPreviousUnignoredSibling());
EXPECT_EQ(tree.GetFromId(15), tree.GetFromId(14)->GetNextUnignoredSibling());
EXPECT_EQ(tree.GetFromId(6),
tree.GetFromId(14)->GetPreviousUnignoredSibling());
EXPECT_EQ(tree.GetFromId(12), tree.GetFromId(15)->GetNextUnignoredSibling());
EXPECT_EQ(tree.GetFromId(14),
tree.GetFromId(15)->GetPreviousUnignoredSibling());
EXPECT_EQ(nullptr, tree.GetFromId(16)->GetNextUnignoredSibling());
EXPECT_EQ(nullptr, tree.GetFromId(16)->GetPreviousUnignoredSibling());
}
TEST(AXTreeTest, UnignoredSelection) {
AXTreeUpdate tree_update;
// (i) => node is ignored
// 1
// |__________
// | | |
// 2(i) 3 4
// |_______________________
// | | | |
// 5 6 7(i) 8(i)
// | | |________
// | | | |
// 9 10(i) 11(i) 12
// | |____
// | | |
// 13(i) 14 15
// |
// 16
// Unignored Tree (conceptual)
// 1
// |______________________
// | | | | | | |
// 5 6 14 15 12 3 4
// | |
// 9 16
tree_update.has_tree_data = true;
tree_update.tree_data.tree_id = ui::AXTreeID::CreateNewAXTreeID();
tree_update.root_id = 1;
tree_update.nodes.resize(16);
tree_update.nodes[0].id = 1;
tree_update.nodes[0].role = ax::mojom::Role::kGenericContainer;
tree_update.nodes[0].child_ids = {2, 3, 4};
tree_update.nodes[1].id = 2;
tree_update.nodes[1].child_ids = {5, 6, 7, 8};
tree_update.nodes[1].role = ax::mojom::Role::kGenericContainer;
tree_update.nodes[1].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[2].id = 3;
tree_update.nodes[2].role = ax::mojom::Role::kStaticText;
tree_update.nodes[2].AddStringAttribute(ax::mojom::StringAttribute::kName,
"text");
tree_update.nodes[3].id = 4;
tree_update.nodes[3].role = ax::mojom::Role::kStaticText;
tree_update.nodes[3].AddStringAttribute(ax::mojom::StringAttribute::kName,
"text");
tree_update.nodes[4].id = 5;
tree_update.nodes[4].role = ax::mojom::Role::kGenericContainer;
tree_update.nodes[4].child_ids = {9};
tree_update.nodes[5].id = 6;
tree_update.nodes[5].role = ax::mojom::Role::kGenericContainer;
tree_update.nodes[5].child_ids = {10};
tree_update.nodes[6].id = 7;
tree_update.nodes[6].child_ids = {11, 12};
tree_update.nodes[6].role = ax::mojom::Role::kGenericContainer;
tree_update.nodes[6].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[7].id = 8;
tree_update.nodes[7].role = ax::mojom::Role::kGenericContainer;
tree_update.nodes[7].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[8].id = 9;
tree_update.nodes[8].role = ax::mojom::Role::kStaticText;
tree_update.nodes[8].AddStringAttribute(ax::mojom::StringAttribute::kName,
"text");
tree_update.nodes[9].id = 10;
tree_update.nodes[9].child_ids = {13};
tree_update.nodes[9].role = ax::mojom::Role::kGenericContainer;
tree_update.nodes[9].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[10].id = 11;
tree_update.nodes[10].child_ids = {14, 15};
tree_update.nodes[10].role = ax::mojom::Role::kGenericContainer;
tree_update.nodes[10].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[11].id = 12;
tree_update.nodes[11].role = ax::mojom::Role::kStaticText;
tree_update.nodes[11].AddStringAttribute(ax::mojom::StringAttribute::kName,
"text");
tree_update.nodes[12].id = 13;
tree_update.nodes[12].child_ids = {16};
tree_update.nodes[12].role = ax::mojom::Role::kGenericContainer;
tree_update.nodes[12].AddState(ax::mojom::State::kIgnored);
tree_update.nodes[13].id = 14;
tree_update.nodes[13].role = ax::mojom::Role::kStaticText;
tree_update.nodes[13].AddStringAttribute(ax::mojom::StringAttribute::kName,
"text");
tree_update.nodes[14].id = 15;
tree_update.nodes[14].role = ax::mojom::Role::kStaticText;
tree_update.nodes[14].AddStringAttribute(ax::mojom::StringAttribute::kName,
"text");
tree_update.nodes[15].id = 16;
tree_update.nodes[15].role = ax::mojom::Role::kStaticText;
tree_update.nodes[15].AddStringAttribute(ax::mojom::StringAttribute::kName,
"text");
AXTree tree(tree_update);
AXNodePosition::SetTree(&tree);
AXTree::Selection unignored_selection = tree.GetUnignoredSelection();
EXPECT_EQ(-1, unignored_selection.anchor_object_id);
EXPECT_EQ(-1, unignored_selection.anchor_offset);
EXPECT_EQ(-1, unignored_selection.focus_object_id);
EXPECT_EQ(-1, unignored_selection.focus_offset);
struct SelectionData {
int32_t anchor_id;
int32_t anchor_offset;
int32_t focus_id;
int32_t focus_offset;
};
SelectionData input = {1, 0, 1, 0};
SelectionData expected = {9, 0, 9, 0};
TEST_SELECTION(tree_update, tree, input, expected);
input = {1, 0, 2, 2};
expected = {9, 0, 14, 0};
TEST_SELECTION(tree_update, tree, input, expected);
input = {2, 1, 5, 0};
expected = {16, 0, 5, 0};
TEST_SELECTION(tree_update, tree, input, expected);
input = {5, 0, 9, 0};
expected = {5, 0, 9, 0};
TEST_SELECTION(tree_update, tree, input, expected);
input = {9, 0, 6, 0};
expected = {9, 0, 16, 0};
TEST_SELECTION(tree_update, tree, input, expected);
input = {6, 0, 10, 0};
expected = {16, 0, 16, 0};
TEST_SELECTION(tree_update, tree, input, expected);
input = {10, 0, 13, 0};
expected = {16, 0, 16, 0};
TEST_SELECTION(tree_update, tree, input, expected);
input = {13, 0, 16, 0};
expected = {16, 0, 16, 0};
TEST_SELECTION(tree_update, tree, input, expected);
input = {16, 0, 7, 0};
expected = {16, 0, 14, 0};
TEST_SELECTION(tree_update, tree, input, expected);
input = {7, 0, 11, 0};
expected = {14, 0, 14, 0};
TEST_SELECTION(tree_update, tree, input, expected);
input = {11, 1, 14, 2};
expected = {15, 0, 14, 2};
TEST_SELECTION(tree_update, tree, input, expected);
input = {14, 2, 15, 3};
expected = {14, 2, 15, 3};
TEST_SELECTION(tree_update, tree, input, expected);
input = {15, 0, 12, 0};
expected = {15, 0, 12, 0};
TEST_SELECTION(tree_update, tree, input, expected);
input = {12, 0, 8, 0};
expected = {12, 0, 3, 0};
TEST_SELECTION(tree_update, tree, input, expected);
input = {8, 0, 3, 0};
expected = {12, 4, 3, 0};
TEST_SELECTION(tree_update, tree, input, expected);
input = {3, 0, 4, 0};
expected = {3, 0, 4, 0};
TEST_SELECTION(tree_update, tree, input, expected);
input = {4, 0, 4, 0};
expected = {4, 0, 4, 0};
TEST_SELECTION(tree_update, tree, input, expected);
AXNodePosition::SetTree(nullptr);
}
TEST(AXTreeTest, ChildTreeIds) {
ui::AXTreeID tree_id_1 = ui::AXTreeID::CreateNewAXTreeID();
ui::AXTreeID tree_id_2 = ui::AXTreeID::CreateNewAXTreeID();
ui::AXTreeID tree_id_3 = ui::AXTreeID::CreateNewAXTreeID();
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(4);
initial_state.nodes[0].id = 1;
initial_state.nodes[0].child_ids.push_back(2);
initial_state.nodes[0].child_ids.push_back(3);
initial_state.nodes[0].child_ids.push_back(4);
initial_state.nodes[1].id = 2;
initial_state.nodes[1].AddStringAttribute(
ax::mojom::StringAttribute::kChildTreeId, tree_id_2.ToString());
initial_state.nodes[2].id = 3;
initial_state.nodes[2].AddStringAttribute(
ax::mojom::StringAttribute::kChildTreeId, tree_id_3.ToString());
initial_state.nodes[3].id = 4;
initial_state.nodes[3].AddStringAttribute(
ax::mojom::StringAttribute::kChildTreeId, tree_id_3.ToString());
AXTree tree(initial_state);
auto child_tree_1_nodes = tree.GetNodeIdsForChildTreeId(tree_id_1);
EXPECT_EQ(0U, child_tree_1_nodes.size());
auto child_tree_2_nodes = tree.GetNodeIdsForChildTreeId(tree_id_2);
EXPECT_EQ(1U, child_tree_2_nodes.size());
EXPECT_TRUE(base::Contains(child_tree_2_nodes, 2));
auto child_tree_3_nodes = tree.GetNodeIdsForChildTreeId(tree_id_3);
EXPECT_EQ(2U, child_tree_3_nodes.size());
EXPECT_TRUE(base::Contains(child_tree_3_nodes, 3));
EXPECT_TRUE(base::Contains(child_tree_3_nodes, 4));
AXTreeUpdate update = initial_state;
update.nodes[2].string_attributes.clear();
update.nodes[2].AddStringAttribute(ax::mojom::StringAttribute::kChildTreeId,
tree_id_2.ToString());
update.nodes[3].string_attributes.clear();
EXPECT_TRUE(tree.Unserialize(update));
child_tree_2_nodes = tree.GetNodeIdsForChildTreeId(tree_id_2);
EXPECT_EQ(2U, child_tree_2_nodes.size());
EXPECT_TRUE(base::Contains(child_tree_2_nodes, 2));
EXPECT_TRUE(base::Contains(child_tree_2_nodes, 3));
child_tree_3_nodes = tree.GetNodeIdsForChildTreeId(tree_id_3);
EXPECT_EQ(0U, child_tree_3_nodes.size());
}
// Tests GetPosInSet and GetSetSize return the assigned int attribute values.
TEST(AXTreeTest, TestSetSizePosInSetAssigned) {
AXTreeUpdate tree_update;
tree_update.root_id = 1;
tree_update.nodes.resize(4);
tree_update.nodes[0].id = 1;
tree_update.nodes[0].role = ax::mojom::Role::kList;