1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/accessibility/test_ax_tree_update_json_reader.h"
#include <algorithm>
#include "base/containers/contains.h"
#include "base/containers/flat_set.h"
#include "base/numerics/clamped_math.h"
#include "base/strings/string_split.h"
#include "ui/accessibility/ax_enum_util.h"
namespace {
using RoleConversions = const std::map<std::string, ax::mojom::Role>;
// The 3 lists below include all terms that are not parsed now if they are in a
// JSON file. Since this class is only used for testing, we will only encounter
// errors regarding that in the following two cases:
// - We add a new JSON file (or modify one) for testing that includes different
// unsupported.
// - There is a new property added to AXNode that is not covered in the existing
// JSON parsor and a test relies on it.
// In both above cases, existing tests will catch the issue and warn about the
// missing/changed property.
const base::flat_set<std::string> kUnusedAxNodeProperties = {
"controls", "describedby", "details", "disabled", "editable",
"focused", "hidden", "hiddenRoot", "live", "multiline",
"readonly", "relevant", "required", "settable"};
const base::flat_set<std::string> kUnusedAxNodeItems = {
"frameId", "ignoredReasons", "parentId"};
const base::flat_set<std::string> kUnusedStyles = {
"background-image", "background-size", "clip", "font-style",
"margin-bottom", "margin-left", "margin-right", "margin-top",
"opacity", "padding-bottom", "padding-left", "padding-right",
"padding-top", "position", "text-align", "text-decoration",
"z-index"};
int GetAsInt(const base::Value& value) {
if (value.is_int())
return value.GetInt();
if (value.is_string())
return atoi(value.GetString().c_str());
NOTREACHED() << "Unexpected: " << value;
return 0;
}
double GetAsDouble(const base::Value& value) {
if (value.is_double())
return value.GetDouble();
if (value.is_int())
return value.GetInt();
if (value.is_string())
return atof(value.GetString().c_str());
NOTREACHED() << "Unexpected: " << value;
return 0;
}
bool GetAsBoolean(const base::Value& value) {
if (value.is_bool())
return value.GetBool();
if (value.is_string()) {
if (value.GetString() == "false")
return false;
if (value.GetString() == "true")
return true;
}
NOTREACHED() << "Unexpected: " << value;
return false;
}
void GetTypeAndValue(const base::Value& node,
std::string& type,
std::string& value) {
type = node.GetDict().Find("type")->GetString();
value = node.GetDict().Find("value")->GetString();
}
ui::AXNodeID AddNode(ui::AXTreeUpdate& tree_update,
const base::Value& node,
RoleConversions* role_conversions);
void ParseAxNodeChildIds(ui::AXNodeData& node_data,
const base::Value& child_ids) {
for (const auto& item : child_ids.GetList())
node_data.child_ids.push_back(GetAsInt(item));
}
void ParseAxNodeDescription(ui::AXNodeData& node_data,
const base::Value& description) {
std::string type, value;
GetTypeAndValue(description, type, value);
DCHECK_EQ(type, "computedString");
node_data.SetDescription(value);
}
void ParseAxNodeName(ui::AXNodeData& node_data, const base::Value& name) {
std::string type, value;
GetTypeAndValue(name, type, value);
DCHECK_EQ(type, "computedString");
node_data.SetName(value);
}
void ParseAxNodeProperties(ui::AXNodeData& node_data,
const base::Value& properties) {
if (properties.is_list()) {
for (const auto& item : properties.GetList())
ParseAxNodeProperties(node_data, item);
return;
}
const std::string prop_type = properties.GetDict().Find("name")->GetString();
const base::Value* prop_value =
properties.GetDict().Find("value")->GetDict().Find("value");
if (prop_type == "atomic") {
node_data.AddBoolAttribute(
ax::mojom::BoolAttribute::kNonAtomicTextFieldRoot,
!GetAsBoolean(*prop_value));
} else if (prop_type == "focusable") {
if (GetAsBoolean(*prop_value))
node_data.AddState(ax::mojom::State::kFocusable);
} else if (prop_type == "expanded") {
if (GetAsBoolean(*prop_value))
node_data.AddState(ax::mojom::State::kExpanded);
} else if (prop_type == "hasPopup") {
node_data.SetHasPopup(
ui::ParseAXEnum<ax::mojom::HasPopup>(prop_value->GetString().c_str()));
} else if (prop_type == "invalid") {
node_data.SetInvalidState(GetAsBoolean(*prop_value)
? ax::mojom::InvalidState::kTrue
: ax::mojom::InvalidState::kFalse);
} else if (prop_type == "level") {
node_data.AddIntAttribute(ax::mojom::IntAttribute::kHierarchicalLevel,
GetAsInt(*prop_value));
} else {
DCHECK(base::Contains(kUnusedAxNodeProperties, prop_type)) << prop_type;
}
}
ax::mojom::Role RoleFromString(std::string role,
RoleConversions* role_conversions) {
const auto& item = role_conversions->find(role);
DCHECK(item != role_conversions->end()) << role;
return item->second;
}
void ParseAxNodeRole(ui::AXNodeData& node_data,
const base::Value& role,
RoleConversions* role_conversions) {
const std::string role_type = role.GetDict().Find("type")->GetString();
std::string role_value = role.GetDict().Find("value")->GetString();
DCHECK(role_type == "role" || role_type == "internalRole");
node_data.role = RoleFromString(role_value, role_conversions);
}
void ParseAxNode(ui::AXNodeData& node_data,
const base::Value& ax_node,
RoleConversions* role_conversions) {
// Store the name and set it at the end because |AXNodeData::SetName|
// expects a valid role to have already been set prior to calling it.
base::Value name_value;
for (const auto item : ax_node.GetDict()) {
if (item.first == "backendDOMNodeId") {
node_data.AddIntAttribute(ax::mojom::IntAttribute::kDOMNodeId,
GetAsInt(item.second));
} else if (item.first == "childIds") {
ParseAxNodeChildIds(node_data, item.second);
} else if (item.first == "description") {
ParseAxNodeDescription(node_data, item.second);
} else if (item.first == "ignored") {
DCHECK(item.second.is_bool());
if (item.second.GetBool())
node_data.AddState(ax::mojom::State::kIgnored);
} else if (item.first == "name") {
name_value = item.second.Clone();
} else if (item.first == "nodeId") {
node_data.id = GetAsInt(item.second);
} else if (item.first == "properties") {
ParseAxNodeProperties(node_data, item.second);
} else if (item.first == "role") {
ParseAxNodeRole(node_data, item.second, role_conversions);
} else {
DCHECK(base::Contains(kUnusedAxNodeItems, item.first)) << item.first;
}
}
if (!name_value.is_none())
ParseAxNodeName(node_data, name_value);
}
void ParseChildren(ui::AXTreeUpdate& tree_update,
const base::Value& children,
RoleConversions* role_conversions) {
for (const auto& child : children.GetList())
AddNode(tree_update, child, role_conversions);
}
// Converts "rgb(R,G,B)" or "rgba(R,G,B,A)" to one ARGB integer where R,G, and B
// are integers and A is float < 1.
uint32_t ConvertRgbaStringToArgbInt(const std::string& argb_string) {
std::vector<std::string> values = base::SplitString(
argb_string, ",()", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
uint32_t a, r, g, b;
if (values.size() == 4 && values[0] == "rgb") {
a = 0;
} else if (values.size() == 5 && values[0] == "rgba") {
a = base::ClampRound(atof(values[4].c_str()) * 255);
} else {
NOTREACHED() << "Unexpected color value: " << argb_string;
return -1;
}
r = atoi(values[1].c_str());
g = atoi(values[2].c_str());
b = atoi(values[3].c_str());
return (a << 24) + (r << 16) + (g << 8) + b;
}
void ParseStyle(ui::AXNodeData& node_data, const base::Value& style) {
const std::string& name = style.GetDict().Find("name")->GetString();
const std::string& value = style.GetDict().Find("value")->GetString();
if (name == "color") {
node_data.AddIntAttribute(ax::mojom::IntAttribute::kColor,
ConvertRgbaStringToArgbInt(value));
} else if (name == "direction") {
node_data.AddIntAttribute(
ax::mojom::IntAttribute::kTextDirection,
static_cast<int>(
ui::ParseAXEnum<ax::mojom::WritingDirection>(value.c_str())));
} else if (name == "display") {
node_data.AddStringAttribute(ax::mojom::StringAttribute::kDisplay, value);
} else if (name == "font-size") {
// Drop the 'px' at the end of font size.
DCHECK(style.GetDict().Find("value")->is_string());
node_data.AddFloatAttribute(
ax::mojom::FloatAttribute::kFontSize,
atof(value.substr(0, value.length() - 2).c_str()));
} else if (name == "font-weight") {
DCHECK(style.GetDict().Find("value")->is_string());
node_data.AddFloatAttribute(ax::mojom::FloatAttribute::kFontSize,
atof(value.c_str()));
} else if (name == "list-style-type") {
node_data.AddIntAttribute(
ax::mojom::IntAttribute::kListStyle,
static_cast<int>(ui::ParseAXEnum<ax::mojom::ListStyle>(value.c_str())));
} else if (name == "visibility") {
if (value == "hidden")
node_data.AddState(ax::mojom::State::kInvisible);
else
DCHECK_EQ(value, "visible");
} else {
DCHECK(base::Contains(kUnusedStyles, name)) << name;
}
}
void ParseExtras(ui::AXNodeData& node_data, const base::Value& extras) {
for (const auto extra : extras.GetDict()) {
const base::Value::List& items = extra.second.GetList();
if (extra.first == "bounds") {
node_data.relative_bounds.bounds.set_x(GetAsDouble(items[0]));
node_data.relative_bounds.bounds.set_y(GetAsDouble(items[1]));
node_data.relative_bounds.bounds.set_width(GetAsDouble(items[2]));
node_data.relative_bounds.bounds.set_height(GetAsDouble(items[3]));
} else if (extra.first == "styles") {
for (const auto& style : items)
ParseStyle(node_data, style);
} else {
NOTREACHED() << "Unexpected: " << extra.first;
}
}
}
// Adds a node and returns its id.
ui::AXNodeID AddNode(ui::AXTreeUpdate& tree_update,
const base::Value& node,
RoleConversions* role_conversions) {
ui::AXNodeData node_data;
// Store the string and set it at the end because |AXNodeData::SetName|
// expects a valid role to have already been set prior to calling it.
std::string name_string;
for (const auto item : node.GetDict()) {
if (item.first == "axNode") {
ParseAxNode(node_data, item.second, role_conversions);
} else if (item.first == "backendDomId") {
node_data.AddIntAttribute(ax::mojom::IntAttribute::kDOMNodeId,
GetAsInt(item.second));
} else if (item.first == "children") {
ParseChildren(tree_update, item.second, role_conversions);
} else if (item.first == "description") {
node_data.SetDescription(item.second.GetString());
} else if (item.first == "extras") {
ParseExtras(node_data, item.second);
} else if (item.first == "interesting") {
// Not used yet, boolean.
} else if (item.first == "name") {
name_string = item.second.GetString();
} else if (item.first == "role") {
node_data.role =
RoleFromString(item.second.GetString(), role_conversions);
} else {
NOTREACHED() << "Unexpected: " << item.first;
}
}
node_data.SetName(name_string);
tree_update.nodes.push_back(node_data);
return node_data.id;
}
} // namespace
namespace ui {
AXTreeUpdate AXTreeUpdateFromJSON(const base::Value& json,
RoleConversions* role_conversions) {
AXTreeUpdate tree_update;
// Input should be a list with one item, which is the root node.
DCHECK(json.is_list() && json.GetList().size() == 1);
tree_update.root_id =
AddNode(tree_update, json.GetList().front(), role_conversions);
// |AddNode| adds child nodes before parent nodes, while AXTree deserializer
// expects parents first.
std::reverse(tree_update.nodes.begin(), tree_update.nodes.end());
return tree_update;
}
} // namespace ui
|