[a11y] AxComputedNodeData can compute missing name for AXInlineTextBox

This patch turns on pruning of duplicate text from inline text boxes,
with corrections to make the optimization a no-op for a11y dump tree
tests.

Last time the HasOrCompute... piece was missing so the name could not
be filled back in again based on the computed value.

Bug: 336294737

Change-Id: I49596fa0b22982f674f71570b5b2ff84d2785984
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5506764
Reviewed-by: Aaron Leventhal <aleventhal@chromium.org>
Commit-Queue: Kevin Ellis <kevers@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1297002}
diff --git a/content/browser/accessibility/dump_accessibility_browsertest_base.cc b/content/browser/accessibility/dump_accessibility_browsertest_base.cc
index d81c8b6..71097c1 100644
--- a/content/browser/accessibility/dump_accessibility_browsertest_base.cc
+++ b/content/browser/accessibility/dump_accessibility_browsertest_base.cc
@@ -231,6 +231,10 @@
   // corresponding code in AXPosition on the browser that collects those
   // markers.
   enabled_features->emplace_back(features::kUseAXPositionForDocumentMarkers);
+  // For improved test coverage ahead of a finch trial, enable the feature that
+  // prunes redundant text for inline text boxes.
+  enabled_features->emplace_back(
+      features::kAccessibilityPruneRedundantInlineText);
 }
 
 std::string DumpAccessibilityTestBase::DumpTreeAsString() const {
diff --git a/third_party/blink/renderer/modules/accessibility/ax_object.cc b/third_party/blink/renderer/modules/accessibility/ax_object.cc
index 26282e49..1db6579 100644
--- a/third_party/blink/renderer/modules/accessibility/ax_object.cc
+++ b/third_party/blink/renderer/modules/accessibility/ax_object.cc
@@ -1764,6 +1764,7 @@
         IsOnlyChild()) {
       // The text of an only-child inline text box can be inferred directly
       // from the parent. No need to serialize redundant data.
+      node_data->SetNameFrom(ax::mojom::blink::NameFrom::kContents);
       return;
     }
   }
diff --git a/ui/accessibility/ax_computed_node_data.cc b/ui/accessibility/ax_computed_node_data.cc
index e0a6ab4..c6419fa 100644
--- a/ui/accessibility/ax_computed_node_data.cc
+++ b/ui/accessibility/ax_computed_node_data.cc
@@ -110,6 +110,13 @@
       // The value attribute could be computed on the browser for content
       // editables and ARIA text/search boxes.
       return owner_->data().IsNonAtomicTextField();
+
+    case ax::mojom::StringAttribute::kName:
+      return ::features::IsAccessibilityPruneRedundantInlineTextEnabled() &&
+             owner_->data().role == ax::mojom::Role::kInlineTextBox &&
+             owner_->GetParent()->data().HasStringAttribute(
+                 ax::mojom::StringAttribute::kName);
+
     default:
       return false;
   }
@@ -151,6 +158,16 @@
       // such controls on the browser. The same for all other controls, other
       // than non-atomic text fields.
       return base::EmptyString();
+
+    case ax::mojom::StringAttribute::kName:
+      // The name may be suppressed when serializing an AXInlineTextBox if it
+      // can be inferred from the parent.
+      if (::features::IsAccessibilityPruneRedundantInlineTextEnabled() &&
+          owner_->data().role == ax::mojom::Role::kInlineTextBox) {
+        return GetOrComputeTextContentUTF8();
+      }
+      return base::EmptyString();
+
     default:
       return base::EmptyString();
   }
@@ -463,7 +480,6 @@
   // set and kNone if the text content is to be inferred from the parent.
   if (::features::IsAccessibilityPruneRedundantInlineTextEnabled()) {
     if (owner_->data().role == ax::mojom::Role::kInlineTextBox &&
-        owner_->data().GetNameFrom() == ax::mojom::NameFrom::kNone &&
         !owner_->data().HasStringAttribute(ax::mojom::StringAttribute::kName)) {
       return owner_->GetParent()->data().GetStringAttribute(
           ax::mojom::StringAttribute::kName);