[fuchsia][a11y] Converts AxRole to Fuchsia Semantic Roles.

The Fuchsia Semantics API changed, so this change converts the new added
roles.

Note that this change is not converting the added roles related to
lists, which will be solved in a future change.

(cherry picked from commit 0e8e56f18b82ebae157fc3aac4fd62228a261b66)

Test: AXTreeConverterTest.ConvertRoles
Bug: fuchsia:60180,1136536
Change-Id: I23e8b0dc02e2e9f85cae16be81c400744831905a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2461954
Commit-Queue: Lucas Radaelli <[email protected]>
Reviewed-by: Sharon Yang <[email protected]>
Reviewed-by: Sergey Ulanov <[email protected]>
Cr-Original-Commit-Position: refs/heads/master@{#816302}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2467016
Commit-Queue: Sharon Yang <[email protected]>
Cr-Commit-Position: refs/branch-heads/4280@{#318}
Cr-Branched-From: ea420fb963f9658c9969b6513c56b8f47efa1a2a-refs/heads/master@{#812852}
diff --git a/fuchsia/engine/browser/ax_tree_converter.cc b/fuchsia/engine/browser/ax_tree_converter.cc
index 652c7ae..da2b25e0 100644
--- a/fuchsia/engine/browser/ax_tree_converter.cc
+++ b/fuchsia/engine/browser/ax_tree_converter.cc
@@ -29,21 +29,6 @@
 // so as to not conflict with other values used by Chromium.
 constexpr uint32_t kZeroIdRemappedForFuchsia = 1u + INT32_MAX;
 
-fuchsia::accessibility::semantics::Role ConvertRole(ax::mojom::Role role) {
-  if (role == ax::mojom::Role::kButton)
-    return fuchsia::accessibility::semantics::Role::BUTTON;
-  if (role == ax::mojom::Role::kHeader)
-    return fuchsia::accessibility::semantics::Role::HEADER;
-  if (role == ax::mojom::Role::kImage)
-    return fuchsia::accessibility::semantics::Role::IMAGE;
-  if (role == ax::mojom::Role::kSlider)
-    return fuchsia::accessibility::semantics::Role::SLIDER;
-  if (role == ax::mojom::Role::kTextField)
-    return fuchsia::accessibility::semantics::Role::TEXT_FIELD;
-
-  return fuchsia::accessibility::semantics::Role::UNKNOWN;
-}
-
 fuchsia::accessibility::semantics::Attributes ConvertAttributes(
     const ui::AXNodeData& node) {
   fuchsia::accessibility::semantics::Attributes attributes;
@@ -79,6 +64,35 @@
   return attributes;
 }
 
+// Converts an ax::mojom::Role to a fuchsia::accessibility::semantics::Role.
+fuchsia::accessibility::semantics::Role AxRoleToFuchsiaSemanticRole(
+    ax::mojom::Role role) {
+  switch (role) {
+    case ax::mojom::Role::kButton:
+      return fuchsia::accessibility::semantics::Role::BUTTON;
+    case ax::mojom::Role::kCheckBox:
+      return fuchsia::accessibility::semantics::Role::CHECK_BOX;
+    case ax::mojom::Role::kHeader:
+      return fuchsia::accessibility::semantics::Role::HEADER;
+    case ax::mojom::Role::kImage:
+      return fuchsia::accessibility::semantics::Role::IMAGE;
+    case ax::mojom::Role::kLink:
+      return fuchsia::accessibility::semantics::Role::LINK;
+    case ax::mojom::Role::kRadioButton:
+      return fuchsia::accessibility::semantics::Role::RADIO_BUTTON;
+    case ax::mojom::Role::kSlider:
+      return fuchsia::accessibility::semantics::Role::SLIDER;
+    case ax::mojom::Role::kTextField:
+      return fuchsia::accessibility::semantics::Role::TEXT_FIELD;
+    case ax::mojom::Role::kStaticText:
+      return fuchsia::accessibility::semantics::Role::STATIC_TEXT;
+    default:
+      return fuchsia::accessibility::semantics::Role::UNKNOWN;
+  }
+
+  return fuchsia::accessibility::semantics::Role::UNKNOWN;
+}
+
 // This function handles conversions for all data that is part of a Semantic
 // Node's state. The corresponding data in an AXNodeData is stored in various
 // attributes.
@@ -196,7 +210,7 @@
     const ui::AXNodeData& node) {
   fuchsia::accessibility::semantics::Node fuchsia_node;
   fuchsia_node.set_node_id(base::checked_cast<uint32_t>(node.id));
-  fuchsia_node.set_role(ConvertRole(node.role));
+  fuchsia_node.set_role(AxRoleToFuchsiaSemanticRole(node.role));
   fuchsia_node.set_states(ConvertStates(node));
   fuchsia_node.set_attributes(ConvertAttributes(node));
   fuchsia_node.set_actions(ConvertActions(node));
diff --git a/fuchsia/engine/browser/ax_tree_converter_unittest.cc b/fuchsia/engine/browser/ax_tree_converter_unittest.cc
index a0f3f94..00c9a9ec 100644
--- a/fuchsia/engine/browser/ax_tree_converter_unittest.cc
+++ b/fuchsia/engine/browser/ax_tree_converter_unittest.cc
@@ -250,4 +250,44 @@
   EXPECT_EQ(10u, ConvertToFuchsiaNodeId(10, 0));
 }
 
+TEST_F(AXTreeConverterTest, ConvertRoles) {
+  ui::AXNodeData node;
+  node.id = 0;
+  node.role = ax::mojom::Role::kButton;
+  EXPECT_EQ(fuchsia::accessibility::semantics::Role::BUTTON,
+            AXNodeDataToSemanticNode(node).role());
+
+  node.role = ax::mojom::Role::kCheckBox;
+  EXPECT_EQ(fuchsia::accessibility::semantics::Role::CHECK_BOX,
+            AXNodeDataToSemanticNode(node).role());
+
+  node.role = ax::mojom::Role::kHeader;
+  EXPECT_EQ(fuchsia::accessibility::semantics::Role::HEADER,
+            AXNodeDataToSemanticNode(node).role());
+
+  node.role = ax::mojom::Role::kImage;
+  EXPECT_EQ(fuchsia::accessibility::semantics::Role::IMAGE,
+            AXNodeDataToSemanticNode(node).role());
+
+  node.role = ax::mojom::Role::kLink;
+  EXPECT_EQ(fuchsia::accessibility::semantics::Role::LINK,
+            AXNodeDataToSemanticNode(node).role());
+
+  node.role = ax::mojom::Role::kRadioButton;
+  EXPECT_EQ(fuchsia::accessibility::semantics::Role::RADIO_BUTTON,
+            AXNodeDataToSemanticNode(node).role());
+
+  node.role = ax::mojom::Role::kSlider;
+  EXPECT_EQ(fuchsia::accessibility::semantics::Role::SLIDER,
+            AXNodeDataToSemanticNode(node).role());
+
+  node.role = ax::mojom::Role::kTextField;
+  EXPECT_EQ(fuchsia::accessibility::semantics::Role::TEXT_FIELD,
+            AXNodeDataToSemanticNode(node).role());
+
+  node.role = ax::mojom::Role::kStaticText;
+  EXPECT_EQ(fuchsia::accessibility::semantics::Role::STATIC_TEXT,
+            AXNodeDataToSemanticNode(node).role());
+}
+
 }  // namespace