Commit 28eac79d authored by Sharon Yang's avatar Sharon Yang Committed by Commit Bot

[Fuchsia]: Convert AXNodeData to Fuchsia Semantic Node

* For all fields currently available for a Fuchsia Semantic Node,
convert from a Chrome AXNodeData.

Testing: Added unit tests, AXTreeConverterTest.

Bug: 973095
Change-Id: I95b899f1bdd9a516067483ed441cfe098fd6e5a6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1832822
Commit-Queue: Kevin Marshall <kmarshall@chromium.org>
Reviewed-by: default avatarDavid Tseng <dtseng@chromium.org>
Reviewed-by: default avatarKevin Marshall <kmarshall@chromium.org>
Reviewed-by: default avatarMichael Wasserman <msw@chromium.org>
Cr-Commit-Position: refs/heads/master@{#706236}
parent 67ace9dd
......@@ -93,6 +93,7 @@ component("web_engine_core") {
"//third_party/fuchsia-sdk/sdk:math",
"//third_party/fuchsia-sdk/sdk:scenic_cpp",
"//third_party/fuchsia-sdk/sdk:sys_cpp",
"//third_party/fuchsia-sdk/sdk:ui_gfx",
"//third_party/fuchsia-sdk/sdk:web",
"//third_party/widevine/cdm:headers",
"//ui/aura",
......@@ -117,6 +118,8 @@ component("web_engine_core") {
sources = [
"browser/accessibility_bridge.cc",
"browser/accessibility_bridge.h",
"browser/ax_tree_converter.cc",
"browser/ax_tree_converter.h",
"browser/content_directory_loader_factory.cc",
"browser/content_directory_loader_factory.h",
"browser/context_impl.cc",
......@@ -264,6 +267,7 @@ test("web_engine_browsertests") {
test("web_engine_unittests") {
sources = [
"browser/accessibility_bridge_unittest.cc",
"browser/ax_tree_converter_unittest.cc",
"browser/cookie_manager_impl_unittest.cc",
"browser/frame_impl_unittest.cc",
"browser/url_request_rewrite_rules_manager_unittest.cc",
......
......@@ -14,10 +14,12 @@ include_rules = [
"+third_party/blink/public/common/messaging",
"+third_party/blink/public/mojom/messaging",
"+third_party/widevine/cdm",
"+ui/accessibility",
"+ui/aura",
"+ui/base/ime",
"+ui/display",
"+ui/events",
"+ui/gfx",
"+ui/ozone/public",
"+ui/platform_window",
"+ui/wm/core",
......
// Copyright 2019 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 "fuchsia/engine/browser/ax_tree_converter.h"
#include <fuchsia/math/cpp/fidl.h>
#include <fuchsia/ui/gfx/cpp/fidl.h>
#include <fuchsia/ui/views/cpp/fidl.h>
#include <lib/ui/scenic/cpp/commands.h>
#include <vector>
#include "base/bit_cast.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_tree_id.h"
#include "ui/gfx/geometry/rect_f.h"
namespace {
fuchsia::accessibility::semantics::Role ConvertRole(ax::mojom::Role role) {
if (role == ax::mojom::Role::kUnknown)
return fuchsia::accessibility::semantics::Role::UNKNOWN;
// TODO(crbug.com/973095): Currently Fuchsia only has one Role option. Add
// more and update tests as they become supported.
return fuchsia::accessibility::semantics::Role::UNKNOWN;
}
fuchsia::accessibility::semantics::Attributes SetAttributes(std::string name) {
fuchsia::accessibility::semantics::Attributes attributes;
attributes.set_label(name);
return attributes;
}
std::vector<fuchsia::accessibility::semantics::Action> ConvertActions(
uint32_t actions) {
std::vector<fuchsia::accessibility::semantics::Action> fuchsia_actions;
ax::mojom::Action action_enum = static_cast<ax::mojom::Action>(actions);
switch (action_enum) {
case ax::mojom::Action::kDoDefault:
fuchsia_actions.push_back(
fuchsia::accessibility::semantics::Action::DEFAULT);
break;
case ax::mojom::Action::kNone:
case ax::mojom::Action::kAnnotatePageImages:
case ax::mojom::Action::kBlur:
case ax::mojom::Action::kClearAccessibilityFocus:
case ax::mojom::Action::kCustomAction:
case ax::mojom::Action::kDecrement:
case ax::mojom::Action::kFocus:
case ax::mojom::Action::kGetImageData:
case ax::mojom::Action::kGetTextLocation:
case ax::mojom::Action::kHideTooltip:
case ax::mojom::Action::kHitTest:
case ax::mojom::Action::kIncrement:
case ax::mojom::Action::kInternalInvalidateTree:
case ax::mojom::Action::kLoadInlineTextBoxes:
case ax::mojom::Action::kReplaceSelectedText:
case ax::mojom::Action::kScrollBackward:
case ax::mojom::Action::kScrollDown:
case ax::mojom::Action::kScrollForward:
case ax::mojom::Action::kScrollLeft:
case ax::mojom::Action::kScrollRight:
case ax::mojom::Action::kScrollUp:
case ax::mojom::Action::kScrollToMakeVisible:
case ax::mojom::Action::kScrollToPoint:
case ax::mojom::Action::kSetAccessibilityFocus:
case ax::mojom::Action::kSetScrollOffset:
case ax::mojom::Action::kSetSelection:
case ax::mojom::Action::kSetSequentialFocusNavigationStartingPoint:
case ax::mojom::Action::kSetValue:
case ax::mojom::Action::kShowContextMenu:
case ax::mojom::Action::kSignalEndOfTest:
case ax::mojom::Action::kShowTooltip:
DVLOG(2) << "Action: " << action_enum;
break;
}
return fuchsia_actions;
}
std::vector<uint32_t> ConvertChildIds(std::vector<int32_t> ids) {
std::vector<uint32_t> child_ids;
child_ids.reserve(ids.size());
for (auto i : ids) {
child_ids.push_back(bit_cast<uint32_t>(i));
}
return child_ids;
}
fuchsia::ui::gfx::BoundingBox ConvertBoundingBox(gfx::RectF bounds) {
fuchsia::ui::gfx::BoundingBox box;
float min[3] = {bounds.bottom_left().x(), bounds.bottom_left().y(), 0.0f};
float max[3] = {bounds.top_right().x(), bounds.top_right().y(), 0.0f};
box.min = scenic::NewVector3(min);
box.max = scenic::NewVector3(max);
return box;
}
// The Semantics Manager applies this matrix to position the node and its
// subtree as an optimization to handle resizing or repositioning. This requires
// only one node to be updated on such an event.
fuchsia::ui::gfx::mat4 ConvertTransform(gfx::Transform* transform) {
float mat[16] = {};
transform->matrix().asColMajorf(mat);
fuchsia::ui::gfx::Matrix4Value fuchsia_transform =
scenic::NewMatrix4Value(mat);
return fuchsia_transform.value;
}
} // namespace
fuchsia::accessibility::semantics::Node AXNodeDataToSemanticNode(
const ui::AXNodeData& node) {
fuchsia::accessibility::semantics::Node fuchsia_node;
fuchsia_node.set_node_id(bit_cast<uint32_t>(node.id));
fuchsia_node.set_role(ConvertRole(node.role));
// TODO(fxb/18796): Handle node state conversions once available.
if (node.HasStringAttribute(ax::mojom::StringAttribute::kName)) {
fuchsia_node.set_attributes(SetAttributes(
node.GetStringAttribute(ax::mojom::StringAttribute::kName)));
}
fuchsia_node.set_actions(ConvertActions(node.actions));
fuchsia_node.set_child_ids(ConvertChildIds(node.child_ids));
fuchsia_node.set_location(ConvertBoundingBox(node.relative_bounds.bounds));
if (node.relative_bounds.transform) {
fuchsia_node.set_transform(
ConvertTransform(node.relative_bounds.transform.get()));
}
return fuchsia_node;
}
// Copyright 2019 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.
#ifndef FUCHSIA_ENGINE_BROWSER_AX_TREE_CONVERTER_H_
#define FUCHSIA_ENGINE_BROWSER_AX_TREE_CONVERTER_H_
#include <fuchsia/accessibility/semantics/cpp/fidl.h>
#include "content/public/browser/ax_event_notification_details.h"
#include "fuchsia/engine/web_engine_export.h"
// Converts an AXNodeData to a Fuchsia Semantic Node.
// Both data types represent a single node, and no additional state is needed.
// AXNodeData is used to convey partial updates, so not all fields may be
// present. Those that are will be converted. The Fuchsia SemanticsManager
// accepts partial updates, so |node| does not require all fields to be set.
WEB_ENGINE_EXPORT fuchsia::accessibility::semantics::Node
AXNodeDataToSemanticNode(const ui::AXNodeData& node);
#endif // FUCHSIA_ENGINE_BROWSER_AX_TREE_CONVERTER_H_
// Copyright 2019 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 "fuchsia/engine/browser/ax_tree_converter.h"
#include <lib/ui/scenic/cpp/commands.h>
#include <vector>
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/transform.h"
using fuchsia::accessibility::semantics::Action;
using fuchsia::accessibility::semantics::Attributes;
using fuchsia::accessibility::semantics::Node;
using fuchsia::accessibility::semantics::Role;
namespace {
const char kLabel1[] = "label nodes, not people";
const char kLabel2[] = "fancy stickers";
const int32_t kChildId1 = 23901;
const int32_t kChildId2 = 484345;
const int32_t kChildId3 = 4156877;
const int32_t kRectX = 1;
const int32_t kRectY = 2;
const int32_t kRectWidth = 7;
const int32_t kRectHeight = 8;
const float k4DIdentityMatrix[16] = {1, 0, 0, 0, 0, 1, 0, 0,
0, 0, 1, 0, 0, 0, 0, 1};
bool SemanticNodesAreEqual(const Node& first, const Node& second) {
if (first.node_id() != second.node_id())
return false;
if (first.has_role() && second.has_role() && first.role() != second.role()) {
return false;
}
if (first.has_attributes() && second.has_attributes() &&
first.attributes().label() != second.attributes().label()) {
return false;
}
if (first.has_actions() && second.has_actions() &&
first.actions() != second.actions()) {
return false;
}
if (first.has_child_ids() && second.has_child_ids() &&
first.child_ids() != second.child_ids()) {
return false;
}
if (first.has_location() && second.has_location()) {
if (first.location().min.x != second.location().min.x)
return false;
if (first.location().min.y != second.location().min.y)
return false;
if (first.location().min.z != second.location().min.z)
return false;
if (first.location().max.x != second.location().max.x)
return false;
if (first.location().max.y != second.location().max.y)
return false;
if (first.location().max.z != second.location().max.z)
return false;
}
if (first.has_transform() && second.has_transform() &&
first.transform().matrix != second.transform().matrix) {
return false;
}
return true;
}
ui::AXNodeData CreateAXNodeData(ax::mojom::Role role,
uint32_t actions,
std::vector<int32_t> child_ids,
ui::AXRelativeBounds relative_bounds,
base::StringPiece name) {
ui::AXNodeData node;
node.role = role;
node.actions = actions;
node.child_ids = child_ids;
node.relative_bounds = relative_bounds;
if (!name.empty())
node.AddStringAttribute(ax::mojom::StringAttribute::kName, name.data());
return node;
}
Node CreateSemanticNode(uint32_t id,
Role role,
Attributes attributes,
std::vector<Action> actions,
std::vector<uint32_t> child_ids,
fuchsia::ui::gfx::BoundingBox location,
fuchsia::ui::gfx::mat4 transform) {
Node node;
node.set_node_id(id);
node.set_role(role);
node.set_attributes(std::move(attributes));
node.set_actions(actions);
node.set_child_ids(child_ids);
node.set_location(location);
node.set_transform(transform);
return node;
}
class AXTreeConverterTest : public testing::Test {
public:
AXTreeConverterTest() = default;
~AXTreeConverterTest() override = default;
DISALLOW_COPY_AND_ASSIGN(AXTreeConverterTest);
};
TEST_F(AXTreeConverterTest, AllFieldsSetAndEqual) {
ui::AXRelativeBounds relative_bounds = ui::AXRelativeBounds();
relative_bounds.bounds = gfx::RectF(kRectX, kRectY, kRectWidth, kRectHeight);
relative_bounds.transform =
std::make_unique<gfx::Transform>(gfx::Transform::kSkipInitialization);
relative_bounds.transform->MakeIdentity();
auto source_node_data =
CreateAXNodeData(ax::mojom::Role::kButton,
static_cast<uint32_t>(ax::mojom::Action::kDoDefault),
std::vector<int32_t>{kChildId1, kChildId2, kChildId3},
relative_bounds, kLabel1);
auto converted_node = AXNodeDataToSemanticNode(source_node_data);
EXPECT_EQ(static_cast<uint32_t>(source_node_data.id),
converted_node.node_id());
Attributes attributes;
attributes.set_label(kLabel1);
fuchsia::ui::gfx::BoundingBox box;
float min[3] = {kRectX, kRectY + kRectHeight, 0.0f};
float max[3] = {kRectHeight, kRectY, 0.0f};
box.min = scenic::NewVector3(min);
box.max = scenic::NewVector3(max);
fuchsia::ui::gfx::Matrix4Value mat =
scenic::NewMatrix4Value(k4DIdentityMatrix);
auto expected_node = CreateSemanticNode(
source_node_data.id, Role::UNKNOWN, std::move(attributes),
std::vector<Action>{Action::DEFAULT},
std::vector<uint32_t>{kChildId1, kChildId2, kChildId3}, box, mat.value);
EXPECT_TRUE(SemanticNodesAreEqual(std::move(converted_node),
std::move(expected_node)));
}
TEST_F(AXTreeConverterTest, SomeFieldsSetAndEqual) {
ui::AXNodeData source_node_data;
source_node_data.actions =
static_cast<uint32_t>(ax::mojom::Action::kDoDefault);
source_node_data.child_ids = std::vector<int32_t>{kChildId1};
auto converted_node = AXNodeDataToSemanticNode(source_node_data);
EXPECT_EQ(static_cast<uint32_t>(source_node_data.id),
converted_node.node_id());
Node expected_node;
expected_node.set_node_id(source_node_data.id);
expected_node.set_actions(std::vector<Action>{Action::DEFAULT});
expected_node.set_child_ids(std::vector<uint32_t>{kChildId1});
EXPECT_TRUE(SemanticNodesAreEqual(std::move(converted_node),
std::move(expected_node)));
}
TEST_F(AXTreeConverterTest, FieldMismatch) {
ui::AXRelativeBounds relative_bounds = ui::AXRelativeBounds();
relative_bounds.bounds = gfx::RectF(kRectX, kRectY, kRectWidth, kRectHeight);
relative_bounds.transform =
std::make_unique<gfx::Transform>(gfx::Transform::kSkipInitialization);
relative_bounds.transform->MakeIdentity();
auto source_node_data =
CreateAXNodeData(ax::mojom::Role::kButton,
static_cast<uint32_t>(ax::mojom::Action::kDoDefault),
std::vector<int32_t>{kChildId1, kChildId2, kChildId3},
relative_bounds, kLabel1);
auto converted_node = AXNodeDataToSemanticNode(source_node_data);
EXPECT_EQ(static_cast<uint32_t>(source_node_data.id),
converted_node.node_id());
Attributes attributes;
attributes.set_label(kLabel1);
fuchsia::ui::gfx::BoundingBox box;
float min[3] = {kRectX, kRectY + kRectHeight, 0.0f};
float max[3] = {kRectHeight, kRectY, 0.0f};
box.min = scenic::NewVector3(min);
box.max = scenic::NewVector3(max);
fuchsia::ui::gfx::Matrix4Value mat =
scenic::NewMatrix4Value(k4DIdentityMatrix);
auto expected_node = CreateSemanticNode(
source_node_data.id, Role::UNKNOWN, std::move(attributes),
std::vector<Action>{Action::DEFAULT},
std::vector<uint32_t>{kChildId1, kChildId2, kChildId3}, box, mat.value);
// Start with nodes being equal.
EXPECT_TRUE(SemanticNodesAreEqual(converted_node, expected_node));
// Make a copy of |source_node_data| and change the name attribute. Check that
// the resulting |converted_node| is different from |expected_node|.
auto modified_node_data = source_node_data;
modified_node_data.AddStringAttribute(ax::mojom::StringAttribute::kName,
kLabel2);
converted_node = AXNodeDataToSemanticNode(modified_node_data);
EXPECT_FALSE(SemanticNodesAreEqual(converted_node, expected_node));
// The same as above, this time changing |child_ids|.
modified_node_data = source_node_data;
modified_node_data.child_ids = std::vector<int32_t>{};
converted_node = AXNodeDataToSemanticNode(modified_node_data);
EXPECT_FALSE(SemanticNodesAreEqual(converted_node, expected_node));
}
} // namespace
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