[views-ax] Migrating Role to view constructor in ui/views/window
directory

This CL migrates setting of the accessible role from
View::GetAccessibleNodeData to the view constructor. This ensures that
the role is updated in the accessibility cache as soon as the view is
constructed. The migration is applied to following files:

 1. ui/views/window/client_view.cc
 2. ui/views/window/non_client_view.cc

The remaining instances will be migrated in follow-up CLs.

This CL is part of the ViewsAX project:
https://docs.google.com/document/d/1Ku7HOyDsiZem1yaV6ccZ-tz3lO2XR2NEcm8HjR6d-VY/edit#heading=h.ke1u3utej413

Bug: 325137417
Change-Id: I7e75db3080e967239318b195b63ac7676962bee9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5771948
Reviewed-by: Scott Violet <sky@chromium.org>
Reviewed-by: Javier Contreras <javiercon@microsoft.com>
Commit-Queue: Sejal Anand <sejalanand@microsoft.com>
Reviewed-by: Thomas Lukaszewicz <tluk@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1345207}
diff --git a/ui/views/widget/widget_unittest.cc b/ui/views/widget/widget_unittest.cc
index 5565211..e7fe956c 100644
--- a/ui/views/widget/widget_unittest.cc
+++ b/ui/views/widget/widget_unittest.cc
@@ -5666,6 +5666,37 @@
   EXPECT_TRUE(widget->GetRootView()->GetViewAccessibility().is_initialized());
 }
 
+TEST_F(WidgetTest, ClientViewAccessibilityProperties) {
+  std::unique_ptr<Widget> widget =
+      CreateTestWidget(Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET,
+                       Widget::InitParams::TYPE_WINDOW);
+  widget->Show();
+
+  ui::AXNodeData node_data;
+  widget->client_view()->GetViewAccessibility().GetAccessibleNodeData(
+      &node_data);
+  EXPECT_EQ(node_data.role, ax::mojom::Role::kClient);
+}
+
+TEST_F(WidgetTest, NonClientViewAccessibilityProperties) {
+  std::unique_ptr<Widget> widget =
+      CreateTestWidget(Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET,
+                       Widget::InitParams::TYPE_WINDOW);
+  NonClientView* non_client_view = widget->non_client_view();
+  non_client_view->SetFrameView(
+      std::make_unique<MinimumSizeFrameView>(widget.get()));
+  widget->Show();
+
+  ui::AXNodeData node_data;
+  non_client_view->GetViewAccessibility().GetAccessibleNodeData(&node_data);
+  EXPECT_EQ(node_data.role, ax::mojom::Role::kClient);
+
+  node_data = ui::AXNodeData();
+  non_client_view->frame_view()->GetViewAccessibility().GetAccessibleNodeData(
+      &node_data);
+  EXPECT_EQ(node_data.role, ax::mojom::Role::kClient);
+}
+
 // Parameterized test that verifies the behavior of SetAspectRatio with respect
 // to the excluded margin.
 class WidgetSetAspectRatioTest
diff --git a/ui/views/window/client_view.cc b/ui/views/window/client_view.cc
index 9759413..ff9aaed 100644
--- a/ui/views/window/client_view.cc
+++ b/ui/views/window/client_view.cc
@@ -11,6 +11,7 @@
 #include "ui/accessibility/ax_node_data.h"
 #include "ui/base/hit_test.h"
 #include "ui/base/metadata/metadata_impl_macros.h"
+#include "ui/views/accessibility/view_accessibility.h"
 #include "ui/views/layout/fill_layout.h"
 #include "ui/views/widget/widget.h"
 #include "ui/views/widget/widget_delegate.h"
@@ -23,6 +24,7 @@
 ClientView::ClientView(Widget* widget, View* contents_view)
     : contents_view_(contents_view) {
   SetLayoutManager(std::make_unique<views::FillLayout>());
+  GetViewAccessibility().SetRole(ax::mojom::Role::kClient);
 }
 
 CloseRequestResult ClientView::OnWindowCloseRequested() {
@@ -63,10 +65,6 @@
   return contents_view_ ? contents_view_->GetMinimumSize() : gfx::Size();
 }
 
-void ClientView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
-  node_data->role = ax::mojom::Role::kClient;
-}
-
 void ClientView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
   // Overridden to do nothing. The NonClientView manually calls Layout on the
   // ClientView when it is itself laid out, see comment in
diff --git a/ui/views/window/client_view.h b/ui/views/window/client_view.h
index b3c101f..298bda3 100644
--- a/ui/views/window/client_view.h
+++ b/ui/views/window/client_view.h
@@ -68,7 +68,6 @@
 
  protected:
   // Overridden from View:
-  void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
   void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
   void ViewHierarchyChanged(
       const ViewHierarchyChangedDetails& details) override;
diff --git a/ui/views/window/non_client_view.cc b/ui/views/window/non_client_view.cc
index 9c87d97..ad45b95a 100644
--- a/ui/views/window/non_client_view.cc
+++ b/ui/views/window/non_client_view.cc
@@ -116,10 +116,6 @@
   return HTNOWHERE;
 }
 
-void NonClientFrameView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
-  node_data->role = ax::mojom::Role::kClient;
-}
-
 void NonClientFrameView::OnThemeChanged() {
   View::OnThemeChanged();
   SchedulePaint();
@@ -155,6 +151,7 @@
 }
 
 NonClientFrameView::NonClientFrameView() {
+  GetViewAccessibility().SetRole(ax::mojom::Role::kClient);
   SetEventTargeter(std::make_unique<views::ViewTargeter>(this));
 }
 
@@ -170,6 +167,10 @@
 NonClientView::NonClientView(views::ClientView* client_view)
     : client_view_(client_view) {
   SetEventTargeter(std::make_unique<views::ViewTargeter>(this));
+
+  // TODO(crbug.com/40866857): Should this be pruned from the accessibility
+  // tree?
+  GetViewAccessibility().SetRole(ax::mojom::Role::kClient);
 }
 
 NonClientView::~NonClientView() {
@@ -293,12 +294,6 @@
     overlay_view_->SetBoundsRect(GetLocalBounds());
 }
 
-void NonClientView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
-  // TODO(crbug.com/40866857): Should this be pruned from the accessibility
-  // tree?
-  node_data->role = ax::mojom::Role::kClient;
-}
-
 View* NonClientView::GetTooltipHandlerForPoint(const gfx::Point& point) {
   // The same logic as for TargetForRect() applies here.
   if (frame_view_->parent() == this) {
diff --git a/ui/views/window/non_client_view.h b/ui/views/window/non_client_view.h
index c36f05d..4d754ae 100644
--- a/ui/views/window/non_client_view.h
+++ b/ui/views/window/non_client_view.h
@@ -113,7 +113,6 @@
   virtual void SizeConstraintsChanged() {}
 
   // View:
-  void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
   void OnThemeChanged() override;
   void Layout(PassKey) override;
   Views GetChildrenInZOrder() override;
@@ -236,7 +235,6 @@
   gfx::Size GetMinimumSize() const override;
   gfx::Size GetMaximumSize() const override;
   void Layout(PassKey) override;
-  void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
   views::View* GetTooltipHandlerForPoint(const gfx::Point& point) override;
 
  protected: