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( ...@@ -658,7 +658,7 @@ gfx::Rect BrowserAccessibility::GetRootFrameHypertextRangeBoundsRect(
const BrowserAccessibility* child = it.get(); const BrowserAccessibility* child = it.get();
if (child->GetRole() != ax::mojom::Role::kInlineTextBox) { if (child->GetRole() != ax::mojom::Role::kInlineTextBox) {
DLOG(WARNING) << "BrowserAccessibility objects with role STATIC_TEXT " 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; continue;
} }
...@@ -1366,6 +1366,21 @@ const ui::AXUniqueId& BrowserAccessibility::GetUniqueId() const { ...@@ -1366,6 +1366,21 @@ const ui::AXUniqueId& BrowserAccessibility::GetUniqueId() const {
return unique_id_; 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( base::Optional<int> BrowserAccessibility::FindTextBoundary(
ui::AXTextBoundary boundary, ui::AXTextBoundary boundary,
int offset, int offset,
...@@ -1531,7 +1546,7 @@ BrowserAccessibility::PlatformChildIterator::PlatformChildIterator( ...@@ -1531,7 +1546,7 @@ BrowserAccessibility::PlatformChildIterator::PlatformChildIterator(
DCHECK(parent && parent->instance_active()); DCHECK(parent && parent->instance_active());
} }
BrowserAccessibility::PlatformChildIterator::~PlatformChildIterator() {} BrowserAccessibility::PlatformChildIterator::~PlatformChildIterator() = default;
bool BrowserAccessibility::PlatformChildIterator::operator==( bool BrowserAccessibility::PlatformChildIterator::operator==(
const ChildIterator& rhs) const { const ChildIterator& rhs) const {
...@@ -1580,6 +1595,7 @@ int BrowserAccessibility::PlatformChildIterator::GetIndexInParent() const { ...@@ -1580,6 +1595,7 @@ int BrowserAccessibility::PlatformChildIterator::GetIndexInParent() const {
return platform_iterator->GetIndexInParent(); return platform_iterator->GetIndexInParent();
} }
BrowserAccessibility& BrowserAccessibility::PlatformChildIterator::operator*() BrowserAccessibility& BrowserAccessibility::PlatformChildIterator::operator*()
const { const {
return *platform_iterator; return *platform_iterator;
...@@ -2204,11 +2220,6 @@ bool BrowserAccessibility::HasInvalidAttribute( ...@@ -2204,11 +2220,6 @@ bool BrowserAccessibility::HasInvalidAttribute(
}) != attributes.end(); }) != attributes.end();
} }
std::ostream& operator<<(std::ostream& stream,
const BrowserAccessibility& object) {
return stream << object.ToString();
}
static bool HasListAncestor(const BrowserAccessibility* node) { static bool HasListAncestor(const BrowserAccessibility* node) {
if (node == nullptr) if (node == nullptr)
return false; return false;
......
...@@ -169,9 +169,8 @@ class CONTENT_EXPORT BrowserAccessibility : public ui::AXPlatformNodeDelegate { ...@@ -169,9 +169,8 @@ class CONTENT_EXPORT BrowserAccessibility : public ui::AXPlatformNodeDelegate {
gfx::NativeViewAccessible GetNativeViewAccessible() const override; gfx::NativeViewAccessible GetNativeViewAccessible() const override;
BrowserAccessibility* get() const; BrowserAccessibility* get() const;
int GetIndexInParent() const override; int GetIndexInParent() const override;
BrowserAccessibility& operator*() const override;
BrowserAccessibility& operator*() const; BrowserAccessibility* operator->() const override;
BrowserAccessibility* operator->() const;
private: private:
const BrowserAccessibility* parent_; const BrowserAccessibility* parent_;
...@@ -588,6 +587,8 @@ class CONTENT_EXPORT BrowserAccessibility : public ui::AXPlatformNodeDelegate { ...@@ -588,6 +587,8 @@ class CONTENT_EXPORT BrowserAccessibility : public ui::AXPlatformNodeDelegate {
// errors are present. // errors are present.
ui::TextAttributeMap GetSpellingAndGrammarAttributes() const; ui::TextAttributeMap GetSpellingAndGrammarAttributes() const;
std::string SubtreeToStringHelper(size_t level) override;
private: private:
// Return the bounds after converting from this node's coordinate system // Return the bounds after converting from this node's coordinate system
// (which is relative to its nearest scrollable ancestor) to the coordinate // (which is relative to its nearest scrollable ancestor) to the coordinate
...@@ -654,9 +655,6 @@ class CONTENT_EXPORT BrowserAccessibility : public ui::AXPlatformNodeDelegate { ...@@ -654,9 +655,6 @@ class CONTENT_EXPORT BrowserAccessibility : public ui::AXPlatformNodeDelegate {
DISALLOW_COPY_AND_ASSIGN(BrowserAccessibility); DISALLOW_COPY_AND_ASSIGN(BrowserAccessibility);
}; };
CONTENT_EXPORT std::ostream& operator<<(std::ostream& stream,
const BrowserAccessibility& object);
} // namespace content } // namespace content
#endif // CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_ #endif // CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_
...@@ -62,6 +62,18 @@ int32_t AXPlatformNode::GetUniqueId() const { ...@@ -62,6 +62,18 @@ int32_t AXPlatformNode::GetUniqueId() const {
return GetDelegate() ? GetDelegate()->GetUniqueId().Get() : -1; 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 // static
void AXPlatformNode::AddAXModeObserver(AXModeObserver* observer) { void AXPlatformNode::AddAXModeObserver(AXModeObserver* observer) {
ax_mode_observers_.Get().AddObserver(observer); ax_mode_observers_.Get().AddObserver(observer);
......
...@@ -5,6 +5,9 @@ ...@@ -5,6 +5,9 @@
#ifndef UI_ACCESSIBILITY_PLATFORM_AX_PLATFORM_NODE_H_ #ifndef UI_ACCESSIBILITY_PLATFORM_AX_PLATFORM_NODE_H_
#define 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/callback.h"
#include "base/lazy_instance.h" #include "base/lazy_instance.h"
#include "base/observer_list.h" #include "base/observer_list.h"
...@@ -92,6 +95,15 @@ class AX_EXPORT AXPlatformNode { ...@@ -92,6 +95,15 @@ class AX_EXPORT AXPlatformNode {
// Return the unique ID // Return the unique ID
int32_t GetUniqueId() const; 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: protected:
AXPlatformNode(); AXPlatformNode();
virtual ~AXPlatformNode(); virtual ~AXPlatformNode();
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <new> #include <new>
#include <ostream>
#include <set> #include <set>
#include <string> #include <string>
#include <utility> #include <utility>
...@@ -33,7 +34,8 @@ ...@@ -33,7 +34,8 @@
namespace gfx { namespace gfx {
class Rect; class Rect;
}
} // namespace gfx
namespace ui { namespace ui {
...@@ -120,7 +122,7 @@ class AX_EXPORT AXPlatformNodeDelegate { ...@@ -120,7 +122,7 @@ class AX_EXPORT AXPlatformNodeDelegate {
class ChildIterator { class ChildIterator {
public: public:
virtual ~ChildIterator() {} virtual ~ChildIterator() = default;
virtual bool operator==(const ChildIterator& rhs) const = 0; virtual bool operator==(const ChildIterator& rhs) const = 0;
virtual bool operator!=(const ChildIterator& rhs) const = 0; virtual bool operator!=(const ChildIterator& rhs) const = 0;
virtual void operator++() = 0; virtual void operator++() = 0;
...@@ -129,6 +131,8 @@ class AX_EXPORT AXPlatformNodeDelegate { ...@@ -129,6 +131,8 @@ class AX_EXPORT AXPlatformNodeDelegate {
virtual void operator--(int) = 0; virtual void operator--(int) = 0;
virtual gfx::NativeViewAccessible GetNativeViewAccessible() const = 0; virtual gfx::NativeViewAccessible GetNativeViewAccessible() const = 0;
virtual int GetIndexInParent() const = 0; virtual int GetIndexInParent() const = 0;
virtual AXPlatformNodeDelegate& operator*() const = 0;
virtual AXPlatformNodeDelegate* operator->() const = 0;
}; };
virtual std::unique_ptr<AXPlatformNodeDelegate::ChildIterator> virtual std::unique_ptr<AXPlatformNodeDelegate::ChildIterator>
ChildrenBegin() = 0; ChildrenBegin() = 0;
...@@ -376,9 +380,23 @@ class AX_EXPORT AXPlatformNodeDelegate { ...@@ -376,9 +380,23 @@ class AX_EXPORT AXPlatformNodeDelegate {
// element. The default value should be false if not in testing mode. // element. The default value should be false if not in testing mode.
virtual bool ShouldIgnoreHoveredStateForTesting() = 0; 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: protected:
AXPlatformNodeDelegate() = default; AXPlatformNodeDelegate() = default;
virtual std::string SubtreeToStringHelper(size_t level) = 0;
private: private:
DISALLOW_COPY_AND_ASSIGN(AXPlatformNodeDelegate); DISALLOW_COPY_AND_ASSIGN(AXPlatformNodeDelegate);
}; };
......
...@@ -147,6 +147,21 @@ int AXPlatformNodeDelegateBase::ChildIteratorBase::GetIndexInParent() const { ...@@ -147,6 +147,21 @@ int AXPlatformNodeDelegateBase::ChildIteratorBase::GetIndexInParent() const {
return index_; 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> std::unique_ptr<AXPlatformNodeDelegate::ChildIterator>
AXPlatformNodeDelegateBase::ChildrenBegin() { AXPlatformNodeDelegateBase::ChildrenBegin() {
return std::make_unique<ChildIteratorBase>(this, 0); return std::make_unique<ChildIteratorBase>(this, 0);
...@@ -527,4 +542,25 @@ AXPlatformNodeDelegate* AXPlatformNodeDelegateBase::GetParentDelegate() { ...@@ -527,4 +542,25 @@ AXPlatformNodeDelegate* AXPlatformNodeDelegateBase::GetParentDelegate() {
return nullptr; 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 } // namespace ui
...@@ -71,7 +71,7 @@ class AX_EXPORT AXPlatformNodeDelegateBase : public AXPlatformNodeDelegate { ...@@ -71,7 +71,7 @@ class AX_EXPORT AXPlatformNodeDelegateBase : public AXPlatformNodeDelegate {
public: public:
ChildIteratorBase(AXPlatformNodeDelegateBase* parent, int index); ChildIteratorBase(AXPlatformNodeDelegateBase* parent, int index);
ChildIteratorBase(const ChildIteratorBase& it); ChildIteratorBase(const ChildIteratorBase& it);
~ChildIteratorBase() override {} ~ChildIteratorBase() override = default;
bool operator==(const ChildIterator& rhs) const override; bool operator==(const ChildIterator& rhs) const override;
bool operator!=(const ChildIterator& rhs) const override; bool operator!=(const ChildIterator& rhs) const override;
void operator++() override; void operator++() override;
...@@ -79,9 +79,9 @@ class AX_EXPORT AXPlatformNodeDelegateBase : public AXPlatformNodeDelegate { ...@@ -79,9 +79,9 @@ class AX_EXPORT AXPlatformNodeDelegateBase : public AXPlatformNodeDelegate {
void operator--() override; void operator--() override;
void operator--(int) override; void operator--(int) override;
gfx::NativeViewAccessible GetNativeViewAccessible() const override; gfx::NativeViewAccessible GetNativeViewAccessible() const override;
protected:
int GetIndexInParent() const override; int GetIndexInParent() const override;
AXPlatformNodeDelegate& operator*() const override;
AXPlatformNodeDelegate* operator->() const override;
private: private:
int index_; int index_;
...@@ -280,6 +280,8 @@ class AX_EXPORT AXPlatformNodeDelegateBase : public AXPlatformNodeDelegate { ...@@ -280,6 +280,8 @@ class AX_EXPORT AXPlatformNodeDelegateBase : public AXPlatformNodeDelegate {
bool ShouldIgnoreHoveredStateForTesting() override; bool ShouldIgnoreHoveredStateForTesting() override;
protected: protected:
std::string SubtreeToStringHelper(size_t level) override;
// Given a list of node ids, return the nodes in this delegate's tree to // Given a list of node ids, return the nodes in this delegate's tree to
// which they correspond. // which they correspond.
std::set<ui::AXPlatformNode*> GetNodesForNodeIds( 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