Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "fuchsia/engine/browser/ax_tree_converter.h" |
| 6 | |
| 7 | #include <lib/ui/scenic/cpp/commands.h> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | #include "ui/gfx/transform.h" |
| 12 | |
| 13 | using fuchsia::accessibility::semantics::Action; |
| 14 | using fuchsia::accessibility::semantics::Attributes; |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 15 | using fuchsia::accessibility::semantics::CheckedState; |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 16 | using fuchsia::accessibility::semantics::Node; |
| 17 | using fuchsia::accessibility::semantics::Role; |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 18 | using fuchsia::accessibility::semantics::States; |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 19 | |
| 20 | namespace { |
| 21 | |
| 22 | const char kLabel1[] = "label nodes, not people"; |
| 23 | const char kLabel2[] = "fancy stickers"; |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 24 | const char kDescription1[] = "this node does some stuff"; |
| 25 | const char kValue1[] = "user entered value"; |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 26 | const int32_t kChildId1 = 23901; |
| 27 | const int32_t kChildId2 = 484345; |
| 28 | const int32_t kChildId3 = 4156877; |
Tess Eisenberger | 77314d5 | 2020-10-12 23:30:21 | [diff] [blame] | 29 | const int32_t kRootId = 5; |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 30 | const int32_t kRectX = 1; |
| 31 | const int32_t kRectY = 2; |
| 32 | const int32_t kRectWidth = 7; |
| 33 | const int32_t kRectHeight = 8; |
Yilong Li | 4ada619 | 2020-01-06 21:01:19 | [diff] [blame] | 34 | const std::array<float, 16> k4DIdentityMatrix = {1, 0, 0, 0, 0, 1, 0, 0, |
| 35 | 0, 0, 1, 0, 0, 0, 0, 1}; |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 36 | |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 37 | ui::AXNodeData CreateAXNodeData(ax::mojom::Role role, |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 38 | ax::mojom::Action action, |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 39 | std::vector<int32_t> child_ids, |
| 40 | ui::AXRelativeBounds relative_bounds, |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 41 | base::StringPiece name, |
| 42 | base::StringPiece description, |
| 43 | ax::mojom::CheckedState checked_state) { |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 44 | ui::AXNodeData node; |
Lucas Radaelli | 9019dc2 | 2020-10-12 18:51:45 | [diff] [blame] | 45 | // Important! ID must be set to zero here because its default value (-1), will |
| 46 | // fail when getting converted to an unsigned int (Fuchsia's ID format). |
| 47 | node.id = 0; |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 48 | node.role = role; |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 49 | node.AddAction(action); |
| 50 | node.AddIntAttribute(ax::mojom::IntAttribute::kCheckedState, |
| 51 | static_cast<int32_t>(checked_state)); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 52 | node.child_ids = child_ids; |
| 53 | node.relative_bounds = relative_bounds; |
| 54 | if (!name.empty()) |
| 55 | node.AddStringAttribute(ax::mojom::StringAttribute::kName, name.data()); |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 56 | if (!description.empty()) { |
| 57 | node.AddStringAttribute(ax::mojom::StringAttribute::kDescription, |
| 58 | description.data()); |
| 59 | } |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 60 | return node; |
| 61 | } |
| 62 | |
| 63 | Node CreateSemanticNode(uint32_t id, |
| 64 | Role role, |
| 65 | Attributes attributes, |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 66 | States states, |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 67 | std::vector<Action> actions, |
| 68 | std::vector<uint32_t> child_ids, |
| 69 | fuchsia::ui::gfx::BoundingBox location, |
| 70 | fuchsia::ui::gfx::mat4 transform) { |
| 71 | Node node; |
| 72 | node.set_node_id(id); |
| 73 | node.set_role(role); |
| 74 | node.set_attributes(std::move(attributes)); |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 75 | node.set_states(std::move(states)); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 76 | node.set_actions(actions); |
| 77 | node.set_child_ids(child_ids); |
| 78 | node.set_location(location); |
| 79 | node.set_transform(transform); |
| 80 | return node; |
| 81 | } |
| 82 | |
Tess Eisenberger | 77314d5 | 2020-10-12 23:30:21 | [diff] [blame] | 83 | // Create an AXNodeData and a Fuchsia node that represent the same information. |
| 84 | std::pair<ui::AXNodeData, Node> CreateSemanticNodeAllFieldsSet() { |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 85 | ui::AXRelativeBounds relative_bounds = ui::AXRelativeBounds(); |
| 86 | relative_bounds.bounds = gfx::RectF(kRectX, kRectY, kRectWidth, kRectHeight); |
| 87 | relative_bounds.transform = |
| 88 | std::make_unique<gfx::Transform>(gfx::Transform::kSkipInitialization); |
| 89 | relative_bounds.transform->MakeIdentity(); |
Tess Eisenberger | 77314d5 | 2020-10-12 23:30:21 | [diff] [blame] | 90 | auto ax_node_data = CreateAXNodeData( |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 91 | ax::mojom::Role::kButton, ax::mojom::Action::kFocus, |
| 92 | std::vector<int32_t>{kChildId1, kChildId2, kChildId3}, relative_bounds, |
| 93 | kLabel1, kDescription1, ax::mojom::CheckedState::kMixed); |
Tess Eisenberger | 77314d5 | 2020-10-12 23:30:21 | [diff] [blame] | 94 | ax_node_data.AddBoolAttribute(ax::mojom::BoolAttribute::kSelected, false); |
| 95 | ax_node_data.RemoveState(ax::mojom::State::kIgnored); |
| 96 | ax_node_data.id = kChildId1; |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 97 | |
| 98 | Attributes attributes; |
| 99 | attributes.set_label(kLabel1); |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 100 | attributes.set_secondary_label(kDescription1); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 101 | fuchsia::ui::gfx::BoundingBox box; |
Yilong Li | 4ada619 | 2020-01-06 21:01:19 | [diff] [blame] | 102 | box.min = scenic::NewVector3({kRectX, kRectY + kRectHeight, 0.0f}); |
| 103 | box.max = scenic::NewVector3({kRectHeight, kRectY, 0.0f}); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 104 | fuchsia::ui::gfx::Matrix4Value mat = |
| 105 | scenic::NewMatrix4Value(k4DIdentityMatrix); |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 106 | States states; |
| 107 | states.set_checked_state(CheckedState::MIXED); |
| 108 | states.set_hidden(false); |
| 109 | states.set_selected(false); |
Tess Eisenberger | 77314d5 | 2020-10-12 23:30:21 | [diff] [blame] | 110 | auto fuchsia_node = CreateSemanticNode( |
| 111 | ConvertToFuchsiaNodeId(ax_node_data.id, kRootId), Role::BUTTON, |
Lucas Radaelli | 9019dc2 | 2020-10-12 18:51:45 | [diff] [blame] | 112 | std::move(attributes), std::move(states), |
| 113 | std::vector<Action>{Action::SET_FOCUS}, |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 114 | std::vector<uint32_t>{kChildId1, kChildId2, kChildId3}, box, mat.value); |
| 115 | |
Tess Eisenberger | 77314d5 | 2020-10-12 23:30:21 | [diff] [blame] | 116 | return std::make_pair(std::move(ax_node_data), std::move(fuchsia_node)); |
| 117 | } |
| 118 | |
| 119 | class AXTreeConverterTest : public testing::Test { |
| 120 | public: |
| 121 | AXTreeConverterTest() = default; |
| 122 | ~AXTreeConverterTest() override = default; |
| 123 | |
| 124 | DISALLOW_COPY_AND_ASSIGN(AXTreeConverterTest); |
| 125 | }; |
| 126 | |
| 127 | TEST_F(AXTreeConverterTest, AllFieldsSetAndEqual) { |
| 128 | auto nodes = CreateSemanticNodeAllFieldsSet(); |
| 129 | auto& source_node_data = nodes.first; |
| 130 | auto& expected_node = nodes.second; |
| 131 | |
| 132 | auto converted_node = AXNodeDataToSemanticNode(source_node_data); |
| 133 | EXPECT_EQ(ConvertToFuchsiaNodeId(source_node_data.id, kRootId), |
| 134 | converted_node.node_id()); |
| 135 | |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 136 | EXPECT_TRUE(fidl::Equals(converted_node, expected_node)); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | TEST_F(AXTreeConverterTest, SomeFieldsSetAndEqual) { |
| 140 | ui::AXNodeData source_node_data; |
Lucas Radaelli | 9019dc2 | 2020-10-12 18:51:45 | [diff] [blame] | 141 | source_node_data.id = 0; |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 142 | source_node_data.AddAction(ax::mojom::Action::kFocus); |
| 143 | source_node_data.AddAction(ax::mojom::Action::kSetValue); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 144 | source_node_data.child_ids = std::vector<int32_t>{kChildId1}; |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 145 | source_node_data.role = ax::mojom::Role::kImage; |
| 146 | source_node_data.AddStringAttribute(ax::mojom::StringAttribute::kValue, |
| 147 | kValue1); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 148 | auto converted_node = AXNodeDataToSemanticNode(source_node_data); |
| 149 | EXPECT_EQ(static_cast<uint32_t>(source_node_data.id), |
| 150 | converted_node.node_id()); |
| 151 | |
| 152 | Node expected_node; |
Lucas Radaelli | 9019dc2 | 2020-10-12 18:51:45 | [diff] [blame] | 153 | expected_node.set_node_id(static_cast<uint32_t>(source_node_data.id)); |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 154 | expected_node.set_actions( |
| 155 | std::vector<Action>{Action::SET_FOCUS, Action::SET_VALUE}); |
Lucas Radaelli | 9019dc2 | 2020-10-12 18:51:45 | [diff] [blame] | 156 | expected_node.set_child_ids( |
| 157 | std::vector<uint32_t>{static_cast<uint32_t>(kChildId1)}); |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 158 | expected_node.set_role(Role::IMAGE); |
| 159 | States states; |
| 160 | states.set_hidden(false); |
| 161 | states.set_value(kValue1); |
| 162 | expected_node.set_states(std::move(states)); |
| 163 | Attributes attributes; |
| 164 | expected_node.set_attributes(std::move(attributes)); |
| 165 | fuchsia::ui::gfx::BoundingBox box; |
| 166 | expected_node.set_location(std::move(box)); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 167 | |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 168 | EXPECT_TRUE(fidl::Equals(converted_node, expected_node)); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | TEST_F(AXTreeConverterTest, FieldMismatch) { |
| 172 | ui::AXRelativeBounds relative_bounds = ui::AXRelativeBounds(); |
| 173 | relative_bounds.bounds = gfx::RectF(kRectX, kRectY, kRectWidth, kRectHeight); |
| 174 | relative_bounds.transform = |
| 175 | std::make_unique<gfx::Transform>(gfx::Transform::kSkipInitialization); |
| 176 | relative_bounds.transform->MakeIdentity(); |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 177 | auto source_node_data = CreateAXNodeData( |
| 178 | ax::mojom::Role::kHeader, ax::mojom::Action::kSetValue, |
| 179 | std::vector<int32_t>{kChildId1, kChildId2, kChildId3}, relative_bounds, |
| 180 | kLabel1, kDescription1, ax::mojom::CheckedState::kFalse); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 181 | auto converted_node = AXNodeDataToSemanticNode(source_node_data); |
| 182 | EXPECT_EQ(static_cast<uint32_t>(source_node_data.id), |
| 183 | converted_node.node_id()); |
| 184 | |
| 185 | Attributes attributes; |
| 186 | attributes.set_label(kLabel1); |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 187 | attributes.set_secondary_label(kDescription1); |
| 188 | States states; |
| 189 | states.set_hidden(false); |
| 190 | states.set_checked_state(CheckedState::UNCHECKED); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 191 | fuchsia::ui::gfx::BoundingBox box; |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 192 | box.min = scenic::NewVector3({kRectX, kRectY + kRectHeight, 0.0f}); |
| 193 | box.max = scenic::NewVector3({kRectHeight, kRectY, 0.0f}); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 194 | fuchsia::ui::gfx::Matrix4Value mat = |
| 195 | scenic::NewMatrix4Value(k4DIdentityMatrix); |
| 196 | auto expected_node = CreateSemanticNode( |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 197 | source_node_data.id, Role::HEADER, std::move(attributes), |
| 198 | std::move(states), std::vector<Action>{Action::SET_VALUE}, |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 199 | std::vector<uint32_t>{kChildId1, kChildId2, kChildId3}, box, mat.value); |
| 200 | |
| 201 | // Start with nodes being equal. |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 202 | EXPECT_TRUE(fidl::Equals(converted_node, expected_node)); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 203 | |
| 204 | // Make a copy of |source_node_data| and change the name attribute. Check that |
| 205 | // the resulting |converted_node| is different from |expected_node|. |
| 206 | auto modified_node_data = source_node_data; |
| 207 | modified_node_data.AddStringAttribute(ax::mojom::StringAttribute::kName, |
| 208 | kLabel2); |
| 209 | converted_node = AXNodeDataToSemanticNode(modified_node_data); |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 210 | EXPECT_FALSE(fidl::Equals(converted_node, expected_node)); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 211 | |
| 212 | // The same as above, this time changing |child_ids|. |
| 213 | modified_node_data = source_node_data; |
| 214 | modified_node_data.child_ids = std::vector<int32_t>{}; |
| 215 | converted_node = AXNodeDataToSemanticNode(modified_node_data); |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 216 | EXPECT_FALSE(fidl::Equals(converted_node, expected_node)); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 217 | } |
| 218 | |
Tess Eisenberger | 77314d5 | 2020-10-12 23:30:21 | [diff] [blame] | 219 | TEST_F(AXTreeConverterTest, DefaultAction) { |
| 220 | auto nodes = CreateSemanticNodeAllFieldsSet(); |
| 221 | auto& source_node_data = nodes.first; |
| 222 | auto& expected_node = nodes.second; |
| 223 | |
| 224 | // Default action verb on an AXNodeData is equivalent to Action::DEFAULT on a |
| 225 | // Fuchsia semantic node. |
| 226 | source_node_data.SetDefaultActionVerb(ax::mojom::DefaultActionVerb::kClick); |
| 227 | expected_node.mutable_actions()->insert( |
| 228 | expected_node.mutable_actions()->begin(), |
| 229 | fuchsia::accessibility::semantics::Action::DEFAULT); |
| 230 | |
| 231 | auto converted_node = AXNodeDataToSemanticNode(source_node_data); |
| 232 | EXPECT_EQ(ConvertToFuchsiaNodeId(source_node_data.id, kRootId), |
| 233 | converted_node.node_id()); |
| 234 | |
| 235 | EXPECT_TRUE(fidl::Equals(converted_node, expected_node)); |
| 236 | } |
| 237 | |
Lucas Radaelli | 9019dc2 | 2020-10-12 18:51:45 | [diff] [blame] | 238 | TEST_F(AXTreeConverterTest, ConvertToFuchsiaNodeId) { |
| 239 | // Root AxNode is 0, Fuchsia is also 0. |
| 240 | EXPECT_EQ(0u, ConvertToFuchsiaNodeId(0, 0)); |
| 241 | |
| 242 | // Root AxNode is not 0, Fuchsia is still 0. |
| 243 | EXPECT_EQ(0u, ConvertToFuchsiaNodeId(2, 2)); |
| 244 | |
| 245 | // Regular AxNode is 0, Fuchsia can't be 0. |
| 246 | EXPECT_EQ(static_cast<uint32_t>(std::numeric_limits<int32_t>::max()) + 1, |
| 247 | ConvertToFuchsiaNodeId(0, 2)); |
| 248 | |
| 249 | // Regular AxNode is not 0, Fuchsia is same value. |
| 250 | EXPECT_EQ(10u, ConvertToFuchsiaNodeId(10, 0)); |
| 251 | } |
| 252 | |
Lucas Radaelli | 3ea1ece2 | 2020-10-13 17:10:06 | [diff] [blame^] | 253 | TEST_F(AXTreeConverterTest, ConvertRoles) { |
| 254 | ui::AXNodeData node; |
| 255 | node.id = 0; |
| 256 | node.role = ax::mojom::Role::kButton; |
| 257 | EXPECT_EQ(fuchsia::accessibility::semantics::Role::BUTTON, |
| 258 | AXNodeDataToSemanticNode(node).role()); |
| 259 | |
| 260 | node.role = ax::mojom::Role::kCheckBox; |
| 261 | EXPECT_EQ(fuchsia::accessibility::semantics::Role::CHECK_BOX, |
| 262 | AXNodeDataToSemanticNode(node).role()); |
| 263 | |
| 264 | node.role = ax::mojom::Role::kHeader; |
| 265 | EXPECT_EQ(fuchsia::accessibility::semantics::Role::HEADER, |
| 266 | AXNodeDataToSemanticNode(node).role()); |
| 267 | |
| 268 | node.role = ax::mojom::Role::kImage; |
| 269 | EXPECT_EQ(fuchsia::accessibility::semantics::Role::IMAGE, |
| 270 | AXNodeDataToSemanticNode(node).role()); |
| 271 | |
| 272 | node.role = ax::mojom::Role::kLink; |
| 273 | EXPECT_EQ(fuchsia::accessibility::semantics::Role::LINK, |
| 274 | AXNodeDataToSemanticNode(node).role()); |
| 275 | |
| 276 | node.role = ax::mojom::Role::kRadioButton; |
| 277 | EXPECT_EQ(fuchsia::accessibility::semantics::Role::RADIO_BUTTON, |
| 278 | AXNodeDataToSemanticNode(node).role()); |
| 279 | |
| 280 | node.role = ax::mojom::Role::kSlider; |
| 281 | EXPECT_EQ(fuchsia::accessibility::semantics::Role::SLIDER, |
| 282 | AXNodeDataToSemanticNode(node).role()); |
| 283 | |
| 284 | node.role = ax::mojom::Role::kTextField; |
| 285 | EXPECT_EQ(fuchsia::accessibility::semantics::Role::TEXT_FIELD, |
| 286 | AXNodeDataToSemanticNode(node).role()); |
| 287 | |
| 288 | node.role = ax::mojom::Role::kStaticText; |
| 289 | EXPECT_EQ(fuchsia::accessibility::semantics::Role::STATIC_TEXT, |
| 290 | AXNodeDataToSemanticNode(node).role()); |
| 291 | } |
| 292 | |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 293 | } // namespace |