Commit 46359a0a authored by Nektarios Paisios's avatar Nektarios Paisios Committed by Commit Bot

Added ToString and SubtreeToString methods to AXPlatformNodeDelegate

Also exposed similar functionality in AXPlatformNode and its subclasses.
A << operator was implemented as well.
This will help in debugging and testing.
R=aleventhal@chromium.org, dmazzoni@chromium.org

Change-Id: Ieda0e076826f422743efdb95eb747cfc63b7d81e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1927293
Commit-Queue: Nektarios Paisios <nektar@chromium.org>
Reviewed-by: default avatarAaron Leventhal <aleventhal@chromium.org>
Cr-Commit-Position: refs/heads/master@{#717764}
parent 06a37c93
......@@ -658,7 +658,7 @@ gfx::Rect BrowserAccessibility::GetRootFrameHypertextRangeBoundsRect(
const BrowserAccessibility* child = it.get();
if (child->GetRole() != ax::mojom::Role::kInlineTextBox) {
DLOG(WARNING) << "BrowserAccessibility objects with role STATIC_TEXT "
<< "should have children of role INLINE_TEXT_BOX.";
<< "should have children of role INLINE_TEXT_BOX.\n";
continue;
}
......@@ -1366,6 +1366,21 @@ const ui::AXUniqueId& BrowserAccessibility::GetUniqueId() const {
return unique_id_;
}
std::string BrowserAccessibility::SubtreeToStringHelper(size_t level) {
std::string result(level * 2, '+');
result += ToString();
result += '\n';
for (InternalChildIterator it = InternalChildrenBegin();
it != InternalChildrenEnd(); ++it) {
BrowserAccessibility* child = it.get();
DCHECK(child);
result += child->SubtreeToStringHelper(level + 1);
}
return result;
}
base::Optional<int> BrowserAccessibility::FindTextBoundary(
ui::AXTextBoundary boundary,
int offset,
......@@ -1531,7 +1546,7 @@ BrowserAccessibility::PlatformChildIterator::PlatformChildIterator(
DCHECK(parent && parent->instance_active());
}
BrowserAccessibility::PlatformChildIterator::~PlatformChildIterator() {}
BrowserAccessibility::PlatformChildIterator::~PlatformChildIterator() = default;
bool BrowserAccessibility::PlatformChildIterator::operator==(
const ChildIterator& rhs) const {
......@@ -1580,6 +1595,7 @@ int BrowserAccessibility::PlatformChildIterator::GetIndexInParent() const {
return platform_iterator->GetIndexInParent();
}
BrowserAccessibility& BrowserAccessibility::PlatformChildIterator::operator*()
const {
return *platform_iterator;
......@@ -2204,11 +2220,6 @@ bool BrowserAccessibility::HasInvalidAttribute(
}) != attributes.end();
}
std::ostream& operator<<(std::ostream& stream,
const BrowserAccessibility& object) {
return stream << object.ToString();
}
static bool HasListAncestor(const BrowserAccessibility* node) {
if (node == nullptr)
return false;
......
......@@ -169,9 +169,8 @@ class CONTENT_EXPORT BrowserAccessibility : public ui::AXPlatformNodeDelegate {
gfx::NativeViewAccessible GetNativeViewAccessible() const override;
BrowserAccessibility* get() const;
int GetIndexInParent() const override;
BrowserAccessibility& operator*() const;
BrowserAccessibility* operator->() const;
BrowserAccessibility& operator*() const override;
BrowserAccessibility* operator->() const override;
private:
const BrowserAccessibility* parent_;
......@@ -588,6 +587,8 @@ class CONTENT_EXPORT BrowserAccessibility : public ui::AXPlatformNodeDelegate {
// errors are present.
ui::TextAttributeMap GetSpellingAndGrammarAttributes() const;
std::string SubtreeToStringHelper(size_t level) override;
private:
// Return the bounds after converting from this node's coordinate system
// (which is relative to its nearest scrollable ancestor) to the coordinate
......@@ -654,9 +655,6 @@ class CONTENT_EXPORT BrowserAccessibility : public ui::AXPlatformNodeDelegate {
DISALLOW_COPY_AND_ASSIGN(BrowserAccessibility);
};
CONTENT_EXPORT std::ostream& operator<<(std::ostream& stream,
const BrowserAccessibility& object);
} // namespace content
#endif // CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_
......@@ -62,6 +62,18 @@ int32_t AXPlatformNode::GetUniqueId() const {
return GetDelegate() ? GetDelegate()->GetUniqueId().Get() : -1;
}
std::string AXPlatformNode::ToString() {
return GetDelegate() ? GetDelegate()->ToString() : "No delegate";
}
std::string AXPlatformNode::SubtreeToString() {
return GetDelegate() ? GetDelegate()->SubtreeToString() : "No delegate";
}
std::ostream& operator<<(std::ostream& stream, AXPlatformNode& node) {
return stream << node.ToString();
}
// static
void AXPlatformNode::AddAXModeObserver(AXModeObserver* observer) {
ax_mode_observers_.Get().AddObserver(observer);
......
......@@ -5,6 +5,9 @@
#ifndef UI_ACCESSIBILITY_PLATFORM_AX_PLATFORM_NODE_H_
#define UI_ACCESSIBILITY_PLATFORM_AX_PLATFORM_NODE_H_
#include <ostream>
#include <string>
#include "base/callback.h"
#include "base/lazy_instance.h"
#include "base/observer_list.h"
......@@ -92,6 +95,15 @@ class AX_EXPORT AXPlatformNode {
// Return the unique ID
int32_t GetUniqueId() const;
// Creates a string representation of this node's data.
std::string ToString();
// Returns a string representation of the subtree of nodes rooted at this
// node.
std::string SubtreeToString();
friend std::ostream& operator<<(std::ostream& stream, AXPlatformNode& node);
protected:
AXPlatformNode();
virtual ~AXPlatformNode();
......
......@@ -10,6 +10,7 @@
#include <map>
#include <memory>
#include <new>
#include <ostream>
#include <set>
#include <string>
#include <utility>
......@@ -33,7 +34,8 @@
namespace gfx {
class Rect;
}
} // namespace gfx
namespace ui {
......@@ -120,7 +122,7 @@ class AX_EXPORT AXPlatformNodeDelegate {
class ChildIterator {
public:
virtual ~ChildIterator() {}
virtual ~ChildIterator() = default;
virtual bool operator==(const ChildIterator& rhs) const = 0;
virtual bool operator!=(const ChildIterator& rhs) const = 0;
virtual void operator++() = 0;
......@@ -129,6 +131,8 @@ class AX_EXPORT AXPlatformNodeDelegate {
virtual void operator--(int) = 0;
virtual gfx::NativeViewAccessible GetNativeViewAccessible() const = 0;
virtual int GetIndexInParent() const = 0;
virtual AXPlatformNodeDelegate& operator*() const = 0;
virtual AXPlatformNodeDelegate* operator->() const = 0;
};
virtual std::unique_ptr<AXPlatformNodeDelegate::ChildIterator>
ChildrenBegin() = 0;
......@@ -376,9 +380,23 @@ class AX_EXPORT AXPlatformNodeDelegate {
// element. The default value should be false if not in testing mode.
virtual bool ShouldIgnoreHoveredStateForTesting() = 0;
// Creates a string representation of this delegate's data.
std::string ToString() { return GetData().ToString(); }
// Returns a string representation of the subtree of delegates rooted at this
// delegate.
std::string SubtreeToString() { return SubtreeToStringHelper(0u); }
friend std::ostream& operator<<(std::ostream& stream,
AXPlatformNodeDelegate& delegate) {
return stream << delegate.ToString();
}
protected:
AXPlatformNodeDelegate() = default;
virtual std::string SubtreeToStringHelper(size_t level) = 0;
private:
DISALLOW_COPY_AND_ASSIGN(AXPlatformNodeDelegate);
};
......
......@@ -147,6 +147,21 @@ int AXPlatformNodeDelegateBase::ChildIteratorBase::GetIndexInParent() const {
return index_;
}
AXPlatformNodeDelegate& AXPlatformNodeDelegateBase::ChildIteratorBase::
operator*() const {
AXPlatformNode* platform_node =
AXPlatformNode::FromNativeViewAccessible(GetNativeViewAccessible());
DCHECK(platform_node && platform_node->GetDelegate());
return *(platform_node->GetDelegate());
}
AXPlatformNodeDelegate* AXPlatformNodeDelegateBase::ChildIteratorBase::
operator->() const {
AXPlatformNode* platform_node =
AXPlatformNode::FromNativeViewAccessible(GetNativeViewAccessible());
return platform_node ? platform_node->GetDelegate() : nullptr;
}
std::unique_ptr<AXPlatformNodeDelegate::ChildIterator>
AXPlatformNodeDelegateBase::ChildrenBegin() {
return std::make_unique<ChildIteratorBase>(this, 0);
......@@ -527,4 +542,25 @@ AXPlatformNodeDelegate* AXPlatformNodeDelegateBase::GetParentDelegate() {
return nullptr;
}
std::string AXPlatformNodeDelegateBase::SubtreeToStringHelper(size_t level) {
std::string result(level * 2, '+');
result += ToString();
result += '\n';
// We can't use ChildrenBegin() and ChildrenEnd() here, because they both
// return an std::unique_ptr<ChildIterator> which is an abstract class.
//
// TODO(accessibility): Refactor ChildIterator into a separate base
// (non-abstract) class.
auto iter_start = ChildIteratorBase(this, 0);
auto iter_end = ChildIteratorBase(this, GetChildCount());
for (auto iter = iter_start; iter != iter_end; ++iter) {
AXPlatformNodeDelegateBase& child =
static_cast<AXPlatformNodeDelegateBase&>(*iter);
result += child.SubtreeToStringHelper(level + 1);
}
return result;
}
} // namespace ui
......@@ -71,7 +71,7 @@ class AX_EXPORT AXPlatformNodeDelegateBase : public AXPlatformNodeDelegate {
public:
ChildIteratorBase(AXPlatformNodeDelegateBase* parent, int index);
ChildIteratorBase(const ChildIteratorBase& it);
~ChildIteratorBase() override {}
~ChildIteratorBase() override = default;
bool operator==(const ChildIterator& rhs) const override;
bool operator!=(const ChildIterator& rhs) const override;
void operator++() override;
......@@ -79,9 +79,9 @@ class AX_EXPORT AXPlatformNodeDelegateBase : public AXPlatformNodeDelegate {
void operator--() override;
void operator--(int) override;
gfx::NativeViewAccessible GetNativeViewAccessible() const override;
protected:
int GetIndexInParent() const override;
AXPlatformNodeDelegate& operator*() const override;
AXPlatformNodeDelegate* operator->() const override;
private:
int index_;
......@@ -280,6 +280,8 @@ class AX_EXPORT AXPlatformNodeDelegateBase : public AXPlatformNodeDelegate {
bool ShouldIgnoreHoveredStateForTesting() override;
protected:
std::string SubtreeToStringHelper(size_t level) override;
// Given a list of node ids, return the nodes in this delegate's tree to
// which they correspond.
std::set<ui::AXPlatformNode*> GetNodesForNodeIds(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment