File: tabbed_pane_accessibility_mac_unittest.mm

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (180 lines) | stat: -rw-r--r-- 6,797 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <Cocoa/Cocoa.h>

#import "base/apple/foundation_util.h"
#import "base/mac/mac_util.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#import "testing/gtest_mac.h"
#include "ui/gfx/geometry/point.h"
#import "ui/gfx/mac/coordinate_conversion.h"
#include "ui/views/controls/tabbed_pane/tabbed_pane.h"
#include "ui/views/test/widget_test.h"
#include "ui/views/widget/widget.h"

namespace views::test {

namespace {

id<NSAccessibility> ToNSAccessibility(id obj) {
  return [obj conformsToProtocol:@protocol(NSAccessibility)] ? obj : nil;
}

// Unboxes an accessibilityValue into an int via NSNumber.
int IdToInt(id value) {
  return base::apple::ObjCCastStrict<NSNumber>(value).intValue;
}

// TODO(crbug.com/41444230): NSTabItemView is not an NSView (despite the
// name) and doesn't conform to NSAccessibility, so we have to fall back to the
// legacy NSObject accessibility API to get its accessibility properties.
id GetLegacyA11yAttributeValue(id obj, NSString* attribute) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
  return [obj accessibilityAttributeValue:attribute];
#pragma clang diagnostic pop
}

}  // namespace

class TabbedPaneAccessibilityMacTest : public WidgetTest {
 public:
  static constexpr int kTabbedPaneID = 123;

  // WidgetTest:
  void SetUp() override {
    WidgetTest::SetUp();
    widget_ = CreateTopLevelPlatformWidget();
    widget_->SetBounds(gfx::Rect(50, 50, 100, 100));
    auto tabbed_pane = std::make_unique<TabbedPane>();
    tabbed_pane->SetSize(gfx::Size(100, 100));

    // Create two tabs and position/size them.
    tabbed_pane->AddTab(u"Tab 1", std::make_unique<View>());
    tabbed_pane->AddTab(u"Tab 2", std::make_unique<View>());
    tabbed_pane->DeprecatedLayoutImmediately();
    tabbed_pane->SetID(kTabbedPaneID);

    widget_->GetContentsView()->AddChildView(std::move(tabbed_pane));
    widget_->Show();
  }

  void TearDown() override {
    widget_.ExtractAsDangling()->CloseNow();
    WidgetTest::TearDown();
  }

  TabbedPane* tabbed_pane() {
    return static_cast<TabbedPane*>(
        widget_->GetContentsView()->GetViewByID(kTabbedPaneID));
  }

  TabbedPaneTab* GetTabAt(size_t index) {
    return static_cast<TabbedPaneTab*>(
        tabbed_pane()->tab_strip_->children()[index]);
  }

  id<NSAccessibility> A11yElementAtPoint(const gfx::Point& point) {
    // Accessibility hit tests come in Cocoa screen coordinates.
    NSPoint ns_point = gfx::ScreenPointToNSPoint(point);
    return ToNSAccessibility([widget_->GetNativeWindow().GetNativeNSWindow()
        accessibilityHitTest:ns_point]);
  }

  gfx::Point TabCenterPoint(size_t index) {
    return GetTabAt(index)->GetBoundsInScreen().CenterPoint();
  }

 protected:
  raw_ptr<Widget> widget_ = nullptr;
};

// Test the Tab's a11y information compared to a Cocoa NSTabViewItem.
TEST_F(TabbedPaneAccessibilityMacTest, AttributesMatchAppKit) {
  // Create a Cocoa NSTabView to test against and select the first tab.
  NSTabView* cocoa_tab_group =
      [[NSTabView alloc] initWithFrame:NSMakeRect(50, 50, 100, 100)];
  NSArray* cocoa_tabs = @[
    [[NSTabViewItem alloc] init],
    [[NSTabViewItem alloc] init],
  ];
  for (size_t i = 0; i < [cocoa_tabs count]; ++i) {
    [cocoa_tabs[i] setLabel:[NSString stringWithFormat:@"Tab %zu", i + 1]];
    [cocoa_tab_group addTabViewItem:cocoa_tabs[i]];
  }

  // General a11y information.
  EXPECT_NSEQ(
      GetLegacyA11yAttributeValue(cocoa_tabs[0], NSAccessibilityRoleAttribute),
      A11yElementAtPoint(TabCenterPoint(0)).accessibilityRole);

  // Older versions of Cocoa expose browser tabs with the accessible role
  // description of "radio button." We match the user experience of more recent
  // versions of Cocoa by exposing the role description of "tab" even in older
  // versions of macOS. Doing so causes a mismatch between native Cocoa and our
  // tabs.
  if (base::mac::MacOSMajorVersion() >= 12) {
    EXPECT_NSEQ(
        GetLegacyA11yAttributeValue(cocoa_tabs[0],
                                    NSAccessibilityRoleDescriptionAttribute),
        A11yElementAtPoint(TabCenterPoint(0)).accessibilityRoleDescription);
  }
  EXPECT_NSEQ(
      GetLegacyA11yAttributeValue(cocoa_tabs[0], NSAccessibilityTitleAttribute),
      A11yElementAtPoint(TabCenterPoint(0)).accessibilityTitle);

  // Compare the value attribute against native Cocoa and check it matches up
  // with whether tabs are actually selected.
  for (size_t i : {0, 1}) {
    NSNumber* cocoa_value = GetLegacyA11yAttributeValue(
        cocoa_tabs[i], NSAccessibilityValueAttribute);
    // Verify that only the second tab is selected.
    EXPECT_EQ(i ? 0 : 1, [cocoa_value intValue]);
    EXPECT_NSEQ(cocoa_value,
                A11yElementAtPoint(TabCenterPoint(i)).accessibilityValue);
  }

  // NSTabViewItem doesn't support NSAccessibilitySelectedAttribute, so don't
  // compare against Cocoa here.
  EXPECT_TRUE(A11yElementAtPoint(TabCenterPoint(0)).accessibilitySelected);
  EXPECT_FALSE(A11yElementAtPoint(TabCenterPoint(1)).accessibilitySelected);
}

// Make sure tabs can be selected by writing the value attribute.
TEST_F(TabbedPaneAccessibilityMacTest, WritableValue) {
  id<NSAccessibility> tab1_a11y = A11yElementAtPoint(TabCenterPoint(0));
  id<NSAccessibility> tab2_a11y = A11yElementAtPoint(TabCenterPoint(1));

  // Only unselected tabs should be writable.
  EXPECT_FALSE([tab1_a11y
      isAccessibilitySelectorAllowed:@selector(setAccessibilityValue:)]);
  EXPECT_TRUE([tab2_a11y
      isAccessibilitySelectorAllowed:@selector(setAccessibilityValue:)]);

  // Select the second tab. AXValue actually accepts any type, but for tabs,
  // Cocoa uses an integer. Despite this, the Accessibility Inspector provides a
  // textfield to set the value for a control, so test this with an NSString.
  tab2_a11y.accessibilityValue = @"string";

  EXPECT_EQ(0, IdToInt(tab1_a11y.accessibilityValue));
  EXPECT_EQ(1, IdToInt(tab2_a11y.accessibilityValue));
  EXPECT_FALSE(tab1_a11y.accessibilitySelected);
  EXPECT_TRUE(tab2_a11y.accessibilitySelected);

  EXPECT_TRUE(GetTabAt(1)->selected());

  // It doesn't make sense to 'deselect' a tab (i.e., without specifying another
  // to select). So any value passed to -accessibilitySetValue: should select
  // that tab. Try an empty string.
  tab1_a11y.accessibilityValue = @"";
  EXPECT_EQ(1, IdToInt(tab1_a11y.accessibilityValue));
  EXPECT_EQ(0, IdToInt(tab2_a11y.accessibilityValue));
  EXPECT_TRUE(tab1_a11y.accessibilitySelected);
  EXPECT_FALSE(tab2_a11y.accessibilitySelected);
  EXPECT_TRUE(GetTabAt(0)->selected());
}

}  // namespace views::test