Commit a75cf61b authored by Alexander Surkov's avatar Alexander Surkov Committed by Commit Bot

DumpAccTree testing: support TextMarker parameters

Bug: 1095619
Change-Id: I0ecd3773b2156adbdc65d6958b756094ee26dbc1
AX-Relnotes: n/a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2261396
Commit-Queue: Alexander Surkov <asurkov@igalia.com>
Reviewed-by: default avatarDominic Mazzoni <dmazzoni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#782781}
parent 52c0d88c
...@@ -66,6 +66,14 @@ const char kFailedToParseArgsError[] = "_const_ERROR:FAILED_TO_PARSE_ARGS"; ...@@ -66,6 +66,14 @@ const char kFailedToParseArgsError[] = "_const_ERROR:FAILED_TO_PARSE_ARGS";
<< " to UIElement: " << msg; \ << " to UIElement: " << msg; \
return nil; return nil;
#define TEXTMARKER_FAIL(propnode, msg) \
LOG(ERROR) << "Failed to parse " << propnode.original_property \
<< " to AXTextMarker: " << msg \
<< ". Expected format: {anchor, offset, affinity}, where anchor " \
"is :line_num, offset is integer, affinity is either down, " \
"up or none"; \
return nil;
} // namespace } // namespace
class AccessibilityTreeFormatterMac : public AccessibilityTreeFormatterBase { class AccessibilityTreeFormatterMac : public AccessibilityTreeFormatterBase {
...@@ -137,6 +145,7 @@ class AccessibilityTreeFormatterMac : public AccessibilityTreeFormatterBase { ...@@ -137,6 +145,7 @@ class AccessibilityTreeFormatterMac : public AccessibilityTreeFormatterBase {
gfx::NativeViewAccessible PropertyNodeToUIElement( gfx::NativeViewAccessible PropertyNodeToUIElement(
const PropertyNode&, const PropertyNode&,
const LineIndexesMap&) const; const LineIndexesMap&) const;
id PropertyNodeToTextMarker(const PropertyNode&, const LineIndexesMap&) const;
base::Value PopulateSize(const BrowserAccessibilityCocoa*) const; base::Value PopulateSize(const BrowserAccessibilityCocoa*) const;
base::Value PopulatePosition(const BrowserAccessibilityCocoa*) const; base::Value PopulatePosition(const BrowserAccessibilityCocoa*) const;
...@@ -150,6 +159,9 @@ class AccessibilityTreeFormatterMac : public AccessibilityTreeFormatterBase { ...@@ -150,6 +159,9 @@ class AccessibilityTreeFormatterMac : public AccessibilityTreeFormatterBase {
const LineIndexesMap& line_indexes_map) const; const LineIndexesMap& line_indexes_map) const;
std::string NodeToLineIndex(id, const LineIndexesMap&) const; std::string NodeToLineIndex(id, const LineIndexesMap&) const;
gfx::NativeViewAccessible LineIndexToNode(
const base::string16 line_index,
const LineIndexesMap& line_indexes_map) const;
base::string16 ProcessTreeForOutput( base::string16 ProcessTreeForOutput(
const base::DictionaryValue& node, const base::DictionaryValue& node,
...@@ -325,6 +337,8 @@ AccessibilityTreeFormatterMac::ParamByPropertyNode( ...@@ -325,6 +337,8 @@ AccessibilityTreeFormatterMac::ParamByPropertyNode(
param = PropertyNodeToRange(property_node); param = PropertyNodeToRange(property_node);
} else if (property_name == "AXIndexForChildUIElement") { // UIElement } else if (property_name == "AXIndexForChildUIElement") { // UIElement
param = PropertyNodeToUIElement(property_node, line_indexes_map); param = PropertyNodeToUIElement(property_node, line_indexes_map);
} else if (property_name == "AXIndexForTextMarker") { // TextMarker
param = PropertyNodeToTextMarker(property_node, line_indexes_map);
} }
return param; return param;
...@@ -404,17 +418,53 @@ AccessibilityTreeFormatterMac::PropertyNodeToUIElement( ...@@ -404,17 +418,53 @@ AccessibilityTreeFormatterMac::PropertyNodeToUIElement(
UIELEMENT_FAIL(propnode, "single argument is expected") UIELEMENT_FAIL(propnode, "single argument is expected")
} }
const auto& uielnode = propnode.parameters[0]; gfx::NativeViewAccessible uielement =
base::string16 line_index = uielnode.name_or_value; LineIndexToNode(propnode.parameters[0].name_or_value, line_indexes_map);
for (std::pair<const gfx::NativeViewAccessible, base::string16> item : if (!uielement) {
line_indexes_map) { UIELEMENT_FAIL(propnode, "no corresponding UIElement was found in the tree")
if (item.second == line_index) {
return item.first;
}
} }
return uielement;
}
UIELEMENT_FAIL(propnode, "no corresponding UIElement was found in the tree") id AccessibilityTreeFormatterMac::PropertyNodeToTextMarker(
return nil; const PropertyNode& propnode,
const LineIndexesMap& line_indexes_map) const {
if (propnode.parameters.size() != 1) {
TEXTMARKER_FAIL(propnode, "single argument is expected")
}
const auto& tmnode = propnode.parameters[0];
if (!tmnode.IsDict()) {
TEXTMARKER_FAIL(propnode, "dictionary is expected")
}
if (tmnode.parameters.size() != 3) {
TEXTMARKER_FAIL(propnode, "wrong number of dictionary elements")
}
BrowserAccessibilityCocoa* anchor_cocoa =
LineIndexToNode(tmnode.parameters[0].name_or_value, line_indexes_map);
if (!anchor_cocoa) {
TEXTMARKER_FAIL(propnode, "1st argument: wrong anchor")
}
base::Optional<int> offset = tmnode.parameters[1].AsInt();
if (!offset) {
TEXTMARKER_FAIL(propnode, "2nd argument: wrong offset")
}
ax::mojom::TextAffinity affinity;
const base::string16& affinity_str = tmnode.parameters[2].name_or_value;
if (affinity_str == base::UTF8ToUTF16("none")) {
affinity = ax::mojom::TextAffinity::kNone;
} else if (affinity_str == base::UTF8ToUTF16("down")) {
affinity = ax::mojom::TextAffinity::kDownstream;
} else if (affinity_str == base::UTF8ToUTF16("up")) {
affinity = ax::mojom::TextAffinity::kUpstream;
} else {
TEXTMARKER_FAIL(propnode, "3rd argument: wrong affinity")
}
return content::AXTextMarkerFrom(anchor_cocoa, *offset, affinity);
} }
base::Value AccessibilityTreeFormatterMac::PopulateSize( base::Value AccessibilityTreeFormatterMac::PopulateSize(
...@@ -575,6 +625,18 @@ std::string AccessibilityTreeFormatterMac::NodeToLineIndex( ...@@ -575,6 +625,18 @@ std::string AccessibilityTreeFormatterMac::NodeToLineIndex(
return kConstValuePrefix + line_index; return kConstValuePrefix + line_index;
} }
gfx::NativeViewAccessible AccessibilityTreeFormatterMac::LineIndexToNode(
const base::string16 line_index,
const LineIndexesMap& line_indexes_map) const {
for (std::pair<const gfx::NativeViewAccessible, base::string16> item :
line_indexes_map) {
if (item.second == line_index) {
return item.first;
}
}
return nil;
}
base::string16 AccessibilityTreeFormatterMac::ProcessTreeForOutput( base::string16 AccessibilityTreeFormatterMac::ProcessTreeForOutput(
const base::DictionaryValue& dict, const base::DictionaryValue& dict,
base::DictionaryValue* filtered_dict_result) { base::DictionaryValue* filtered_dict_result) {
......
...@@ -266,4 +266,28 @@ IN_PROC_BROWSER_TEST_F(AccessibilityTreeFormatterMacBrowserTest, ...@@ -266,4 +266,28 @@ IN_PROC_BROWSER_TEST_F(AccessibilityTreeFormatterMacBrowserTest,
)~~"); )~~");
} }
IN_PROC_BROWSER_TEST_F(AccessibilityTreeFormatterMacBrowserTest,
ParameterizedAttributes_TextMarker) {
TestAndCheck(R"~~(data:text/html,
<p>Text</p>)~~",
{":1;AXIndexForTextMarker({:2, 1, down})=*"},
R"~~(AXWebArea AXIndexForTextMarker({:2, 1, down})=1
++AXGroup
++++AXStaticText AXValue='Text'
)~~");
}
IN_PROC_BROWSER_TEST_F(AccessibilityTreeFormatterMacBrowserTest,
ParameterizedAttributes_TextMarker_WrongParameters) {
TestWrongParameters(
R"~~(data:text/html,
<p>Text</p>)~~",
{"1, 2", "2", "{2, 1, down}", "{:2, NaN, down}", "{:2, 1, hoho}"},
":1;AXIndexForTextMarker(Argument)=*",
R"~~(AXWebArea AXIndexForTextMarker(Argument)=ERROR:FAILED_TO_PARSE_ARGS
++AXGroup
++++AXStaticText AXValue='Text'
)~~");
}
} // namespace content } // namespace content
...@@ -40,6 +40,11 @@ BrowserAccessibilityPosition::AXPositionInstance AXTextMarkerToPosition(id); ...@@ -40,6 +40,11 @@ BrowserAccessibilityPosition::AXPositionInstance AXTextMarkerToPosition(id);
// Returns browser accessibility range for the given AXTextMarkerRange. // Returns browser accessibility range for the given AXTextMarkerRange.
BrowserAccessibilityPosition::AXRangeType AXTextMarkerRangeToRange(id); BrowserAccessibilityPosition::AXRangeType AXTextMarkerRangeToRange(id);
// Returns AXTextMarker for the given browser accessibility position.
id AXTextMarkerFrom(const BrowserAccessibilityCocoa* anchor,
int offset,
ax::mojom::TextAffinity affinity);
} // namespace content } // namespace content
// BrowserAccessibilityCocoa is a cocoa wrapper around the BrowserAccessibility // BrowserAccessibilityCocoa is a cocoa wrapper around the BrowserAccessibility
......
...@@ -756,6 +756,15 @@ content::AXTextMarkerRangeToRange(id text_marker_range) { ...@@ -756,6 +756,15 @@ content::AXTextMarkerRangeToRange(id text_marker_range) {
return CreateRangeFromTextMarkerRange(text_marker_range); return CreateRangeFromTextMarkerRange(text_marker_range);
} }
id content::AXTextMarkerFrom(const BrowserAccessibilityCocoa* anchor,
int offset,
ax::mojom::TextAffinity affinity) {
BrowserAccessibility* anchor_node = [anchor owner];
BrowserAccessibilityPositionInstance position =
CreateTextPosition(*anchor_node, offset, affinity);
return CreateTextMarker(std::move(position));
}
@implementation BrowserAccessibilityCocoa @implementation BrowserAccessibilityCocoa
+ (void)initialize { + (void)initialize {
......
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