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; |
| 29 | const int32_t kRectX = 1; |
| 30 | const int32_t kRectY = 2; |
| 31 | const int32_t kRectWidth = 7; |
| 32 | const int32_t kRectHeight = 8; |
Yilong Li | 4ada619 | 2020-01-06 21:01:19 | [diff] [blame] | 33 | const std::array<float, 16> k4DIdentityMatrix = {1, 0, 0, 0, 0, 1, 0, 0, |
| 34 | 0, 0, 1, 0, 0, 0, 0, 1}; |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 35 | |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 36 | ui::AXNodeData CreateAXNodeData(ax::mojom::Role role, |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 37 | ax::mojom::Action action, |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 38 | std::vector<int32_t> child_ids, |
| 39 | ui::AXRelativeBounds relative_bounds, |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 40 | base::StringPiece name, |
| 41 | base::StringPiece description, |
| 42 | ax::mojom::CheckedState checked_state) { |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 43 | ui::AXNodeData node; |
| 44 | node.role = role; |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 45 | node.AddAction(action); |
| 46 | node.AddIntAttribute(ax::mojom::IntAttribute::kCheckedState, |
| 47 | static_cast<int32_t>(checked_state)); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 48 | node.child_ids = child_ids; |
| 49 | node.relative_bounds = relative_bounds; |
| 50 | if (!name.empty()) |
| 51 | node.AddStringAttribute(ax::mojom::StringAttribute::kName, name.data()); |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 52 | if (!description.empty()) { |
| 53 | node.AddStringAttribute(ax::mojom::StringAttribute::kDescription, |
| 54 | description.data()); |
| 55 | } |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 56 | return node; |
| 57 | } |
| 58 | |
| 59 | Node CreateSemanticNode(uint32_t id, |
| 60 | Role role, |
| 61 | Attributes attributes, |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 62 | States states, |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 63 | std::vector<Action> actions, |
| 64 | std::vector<uint32_t> child_ids, |
| 65 | fuchsia::ui::gfx::BoundingBox location, |
| 66 | fuchsia::ui::gfx::mat4 transform) { |
| 67 | Node node; |
| 68 | node.set_node_id(id); |
| 69 | node.set_role(role); |
| 70 | node.set_attributes(std::move(attributes)); |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 71 | node.set_states(std::move(states)); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 72 | node.set_actions(actions); |
| 73 | node.set_child_ids(child_ids); |
| 74 | node.set_location(location); |
| 75 | node.set_transform(transform); |
| 76 | return node; |
| 77 | } |
| 78 | |
| 79 | class AXTreeConverterTest : public testing::Test { |
| 80 | public: |
| 81 | AXTreeConverterTest() = default; |
| 82 | ~AXTreeConverterTest() override = default; |
| 83 | |
| 84 | DISALLOW_COPY_AND_ASSIGN(AXTreeConverterTest); |
| 85 | }; |
| 86 | |
| 87 | TEST_F(AXTreeConverterTest, AllFieldsSetAndEqual) { |
| 88 | ui::AXRelativeBounds relative_bounds = ui::AXRelativeBounds(); |
| 89 | relative_bounds.bounds = gfx::RectF(kRectX, kRectY, kRectWidth, kRectHeight); |
| 90 | relative_bounds.transform = |
| 91 | std::make_unique<gfx::Transform>(gfx::Transform::kSkipInitialization); |
| 92 | relative_bounds.transform->MakeIdentity(); |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 93 | auto source_node_data = CreateAXNodeData( |
| 94 | ax::mojom::Role::kButton, ax::mojom::Action::kFocus, |
| 95 | std::vector<int32_t>{kChildId1, kChildId2, kChildId3}, relative_bounds, |
| 96 | kLabel1, kDescription1, ax::mojom::CheckedState::kMixed); |
| 97 | source_node_data.AddBoolAttribute(ax::mojom::BoolAttribute::kSelected, false); |
Lucas Radaelli | 6afb902 | 2020-10-09 19:32:00 | [diff] [blame^] | 98 | source_node_data.RemoveState(ax::mojom::State::kIgnored); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 99 | auto converted_node = AXNodeDataToSemanticNode(source_node_data); |
| 100 | EXPECT_EQ(static_cast<uint32_t>(source_node_data.id), |
| 101 | converted_node.node_id()); |
| 102 | |
| 103 | Attributes attributes; |
| 104 | attributes.set_label(kLabel1); |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 105 | attributes.set_secondary_label(kDescription1); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 106 | fuchsia::ui::gfx::BoundingBox box; |
Yilong Li | 4ada619 | 2020-01-06 21:01:19 | [diff] [blame] | 107 | box.min = scenic::NewVector3({kRectX, kRectY + kRectHeight, 0.0f}); |
| 108 | box.max = scenic::NewVector3({kRectHeight, kRectY, 0.0f}); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 109 | fuchsia::ui::gfx::Matrix4Value mat = |
| 110 | scenic::NewMatrix4Value(k4DIdentityMatrix); |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 111 | States states; |
| 112 | states.set_checked_state(CheckedState::MIXED); |
| 113 | states.set_hidden(false); |
| 114 | states.set_selected(false); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 115 | auto expected_node = CreateSemanticNode( |
Findit | 066982f1 | 2020-09-30 05:18:34 | [diff] [blame] | 116 | source_node_data.id, Role::BUTTON, std::move(attributes), |
| 117 | std::move(states), std::vector<Action>{Action::SET_FOCUS}, |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 118 | std::vector<uint32_t>{kChildId1, kChildId2, kChildId3}, box, mat.value); |
| 119 | |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 120 | EXPECT_TRUE(fidl::Equals(converted_node, expected_node)); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | TEST_F(AXTreeConverterTest, SomeFieldsSetAndEqual) { |
| 124 | ui::AXNodeData source_node_data; |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 125 | source_node_data.AddAction(ax::mojom::Action::kFocus); |
| 126 | source_node_data.AddAction(ax::mojom::Action::kSetValue); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 127 | source_node_data.child_ids = std::vector<int32_t>{kChildId1}; |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 128 | source_node_data.role = ax::mojom::Role::kImage; |
| 129 | source_node_data.AddStringAttribute(ax::mojom::StringAttribute::kValue, |
| 130 | kValue1); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 131 | auto converted_node = AXNodeDataToSemanticNode(source_node_data); |
| 132 | EXPECT_EQ(static_cast<uint32_t>(source_node_data.id), |
| 133 | converted_node.node_id()); |
| 134 | |
| 135 | Node expected_node; |
Findit | 066982f1 | 2020-09-30 05:18:34 | [diff] [blame] | 136 | expected_node.set_node_id(source_node_data.id); |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 137 | expected_node.set_actions( |
| 138 | std::vector<Action>{Action::SET_FOCUS, Action::SET_VALUE}); |
Findit | 066982f1 | 2020-09-30 05:18:34 | [diff] [blame] | 139 | expected_node.set_child_ids(std::vector<uint32_t>{kChildId1}); |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 140 | expected_node.set_role(Role::IMAGE); |
| 141 | States states; |
| 142 | states.set_hidden(false); |
| 143 | states.set_value(kValue1); |
| 144 | expected_node.set_states(std::move(states)); |
| 145 | Attributes attributes; |
| 146 | expected_node.set_attributes(std::move(attributes)); |
| 147 | fuchsia::ui::gfx::BoundingBox box; |
| 148 | expected_node.set_location(std::move(box)); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 149 | |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 150 | EXPECT_TRUE(fidl::Equals(converted_node, expected_node)); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | TEST_F(AXTreeConverterTest, FieldMismatch) { |
| 154 | ui::AXRelativeBounds relative_bounds = ui::AXRelativeBounds(); |
| 155 | relative_bounds.bounds = gfx::RectF(kRectX, kRectY, kRectWidth, kRectHeight); |
| 156 | relative_bounds.transform = |
| 157 | std::make_unique<gfx::Transform>(gfx::Transform::kSkipInitialization); |
| 158 | relative_bounds.transform->MakeIdentity(); |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 159 | auto source_node_data = CreateAXNodeData( |
| 160 | ax::mojom::Role::kHeader, ax::mojom::Action::kSetValue, |
| 161 | std::vector<int32_t>{kChildId1, kChildId2, kChildId3}, relative_bounds, |
| 162 | kLabel1, kDescription1, ax::mojom::CheckedState::kFalse); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 163 | auto converted_node = AXNodeDataToSemanticNode(source_node_data); |
| 164 | EXPECT_EQ(static_cast<uint32_t>(source_node_data.id), |
| 165 | converted_node.node_id()); |
| 166 | |
| 167 | Attributes attributes; |
| 168 | attributes.set_label(kLabel1); |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 169 | attributes.set_secondary_label(kDescription1); |
| 170 | States states; |
| 171 | states.set_hidden(false); |
| 172 | states.set_checked_state(CheckedState::UNCHECKED); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 173 | fuchsia::ui::gfx::BoundingBox box; |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 174 | box.min = scenic::NewVector3({kRectX, kRectY + kRectHeight, 0.0f}); |
| 175 | box.max = scenic::NewVector3({kRectHeight, kRectY, 0.0f}); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 176 | fuchsia::ui::gfx::Matrix4Value mat = |
| 177 | scenic::NewMatrix4Value(k4DIdentityMatrix); |
| 178 | auto expected_node = CreateSemanticNode( |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 179 | source_node_data.id, Role::HEADER, std::move(attributes), |
| 180 | std::move(states), std::vector<Action>{Action::SET_VALUE}, |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 181 | std::vector<uint32_t>{kChildId1, kChildId2, kChildId3}, box, mat.value); |
| 182 | |
| 183 | // Start with nodes being equal. |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 184 | EXPECT_TRUE(fidl::Equals(converted_node, expected_node)); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 185 | |
| 186 | // Make a copy of |source_node_data| and change the name attribute. Check that |
| 187 | // the resulting |converted_node| is different from |expected_node|. |
| 188 | auto modified_node_data = source_node_data; |
| 189 | modified_node_data.AddStringAttribute(ax::mojom::StringAttribute::kName, |
| 190 | kLabel2); |
| 191 | converted_node = AXNodeDataToSemanticNode(modified_node_data); |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 192 | EXPECT_FALSE(fidl::Equals(converted_node, expected_node)); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 193 | |
| 194 | // The same as above, this time changing |child_ids|. |
| 195 | modified_node_data = source_node_data; |
| 196 | modified_node_data.child_ids = std::vector<int32_t>{}; |
| 197 | converted_node = AXNodeDataToSemanticNode(modified_node_data); |
Sharon Yang | 6f3a47b | 2020-01-08 21:32:29 | [diff] [blame] | 198 | EXPECT_FALSE(fidl::Equals(converted_node, expected_node)); |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 199 | } |
| 200 | |
Sharon Yang | 28eac79 | 2019-10-16 00:28:23 | [diff] [blame] | 201 | } // namespace |