Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Side by Side Diff: ui/views/widget/native_widget_mac_accessibility_unittest.mm

Issue 2016243002: Mac a11y: Add RoleDescription and Value attributes to accessibility information. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 <memory>
6
7 #import <Cocoa/Cocoa.h>
8
9 #include "base/strings/sys_string_conversions.h"
10 #include "base/strings/utf_string_conversions.h"
11 #import "testing/gtest_mac.h"
12 #include "ui/accessibility/ax_enums.h"
13 #include "ui/accessibility/ax_view_state.h"
14 #import "ui/accessibility/platform/ax_platform_node_mac.h"
15 #include "ui/base/ime/text_input_type.h"
16 #import "ui/gfx/mac/coordinate_conversion.h"
17 #include "ui/views/controls/textfield/textfield.h"
18 #include "ui/views/test/widget_test.h"
19 #include "ui/views/widget/widget.h"
20
21 // TODO(patricialor): Test against Cocoa attributes as well to ensure
22 // consistency between Cocoa and toolkit-views.
23
24 // Expose some methods from AXPlatformNodeCocoa for testing purposes only.
25 @interface AXPlatformNodeCocoa (Testing)
26 - (NSString*)AXRole;
27 @end
28
29 namespace views {
30
31 namespace {
32
33 class FlexibleRoleTestView : public View {
34 public:
35 FlexibleRoleTestView(ui::AXRole role) : role_(role) {}
36 void SetRole(ui::AXRole role) { role_ = role; }
37
38 // View:
39 void GetAccessibleState(ui::AXViewState* state) override {
40 View::GetAccessibleState(state);
41 state->role = role_;
42 }
43
44 void AddChildView(View* view) {
45 View::AddChildView(view);
46 // Allow the child to take its full size for accurate hit tests.
47 SetBoundsRect(view->bounds());
48 }
49
50 private:
51 ui::AXRole role_;
52 DISALLOW_COPY_AND_ASSIGN(FlexibleRoleTestView);
53 };
54
55 class NativeWidgetMacAccessibilityTest : public test::WidgetTest {
56 public:
57 void SetUp() override {
58 test::WidgetTest::SetUp();
59 widget_ = CreateTopLevelPlatformWidget();
60 widget_->SetBounds(gfx::Rect(50, 50, 100, 100));
61 midpoint_in_screen_ = gfx::ScreenPointToNSPoint(
62 widget_->GetWindowBoundsInScreen().CenterPoint());
63 widget()->Show();
64 }
65
66 void TearDown() override {
67 widget_->CloseNow();
68 test::WidgetTest::TearDown();
69 }
70
71 id AttributeValueAtMidpoint(NSString* attribute) {
72 // Accessibility hit tests come in Cocoa screen coordinates.
73 id hit =
74 [widget_->GetNativeWindow() accessibilityHitTest:midpoint_in_screen_];
75 id value = [hit accessibilityAttributeValue:attribute];
76 return value;
77 }
78
79 Textfield* AddChildTextfield(const gfx::Size& size) {
80 Textfield* textfield = new Textfield;
81 textfield->SetText(kTestString);
82 textfield->SetAccessibleName(kTestTextfieldTitle);
83 textfield->SetSize(size);
84 widget()->GetContentsView()->AddChildView(textfield);
85 return textfield;
86 }
87
88 Widget* widget() { return widget_; }
89 gfx::Rect widget_bounds() { return widget_->GetClientAreaBoundsInScreen(); }
tapted 2016/06/10 03:17:18 nit: GetWidgetBounds
Patti Lor 2016/06/16 07:05:22 Done.
90
91 const base::string16 kTestString = base::ASCIIToUTF16("Green");
92 const base::string16 kTestTextfieldTitle =
93 base::ASCIIToUTF16("Test textfield");
94
95 private:
96 Widget* widget_;
97
98 NSPoint midpoint_in_screen_;
99 };
100
101 } // namespace
102
103 // Test for NSAccessibilityChildrenAttribute, including with ignored children in
tapted 2016/06/10 03:17:18 including with -> to ensure it excludes?
Patti Lor 2016/06/16 07:05:22 Done.
104 // the accessibility tree.
105 TEST_F(NativeWidgetMacAccessibilityTest, ChildrenAttribute) {
106 // Check childless views don't have accessibility children.
107 EXPECT_EQ(0u,
108 [AttributeValueAtMidpoint(NSAccessibilityChildrenAttribute) count]);
109
110 const size_t kNumChildren = 3;
111 for (size_t i = 0; i < kNumChildren; ++i) {
112 // Make sure the labels won't interfere with the hit test.
113 AddChildTextfield(gfx::Size());
114 }
115
116 EXPECT_EQ(kNumChildren,
117 [AttributeValueAtMidpoint(NSAccessibilityChildrenAttribute) count]);
118
119 // Check ignored children don't show up in the accessibility tree.
120 widget()->GetContentsView()->AddChildView(
121 new FlexibleRoleTestView(ui::AX_ROLE_IGNORED));
122 EXPECT_EQ(kNumChildren,
123 [AttributeValueAtMidpoint(NSAccessibilityChildrenAttribute) count]);
124 }
125
126 // Test for NSAccessibilityParentAttribute, including for a widget with no
127 // parent.
128 TEST_F(NativeWidgetMacAccessibilityTest, ParentAttribute) {
129 Textfield* child = AddChildTextfield(widget()->GetContentsView()->size());
130
131 // Views with widget parents will have a NSWindow parent.
tapted 2016/06/10 03:17:18 nit widget -> Widget. more below
Patti Lor 2016/06/16 07:05:22 Done.
132 EXPECT_NSEQ(
133 NSAccessibilityWindowRole,
134 [AttributeValueAtMidpoint(NSAccessibilityParentAttribute) AXRole]);
135
136 // Views with non-widget parents will have the role of the parent view.
137 widget()->GetContentsView()->RemoveChildView(child);
138 FlexibleRoleTestView* parent = new FlexibleRoleTestView(ui::AX_ROLE_GROUP);
139 parent->AddChildView(child);
140 widget()->GetContentsView()->AddChildView(parent);
141 EXPECT_NSEQ(
142 NSAccessibilityGroupRole,
143 [AttributeValueAtMidpoint(NSAccessibilityParentAttribute) AXRole]);
144
145 // Test an ignored role parent is skipped in favor of the grandparent.
146 parent->SetRole(ui::AX_ROLE_IGNORED);
147 EXPECT_NSEQ(
148 NSAccessibilityWindowRole,
149 [AttributeValueAtMidpoint(NSAccessibilityParentAttribute) AXRole]);
150 }
151
152 // Test for NSAccessibilityPositionAttribute, including on widget movement
153 // updates.
154 TEST_F(NativeWidgetMacAccessibilityTest, PositionAttribute) {
155 NSValue* widget_origin = [NSValue
156 valueWithPoint:gfx::ScreenPointToNSPoint(widget_bounds().bottom_left())];
157 EXPECT_NSEQ(widget_origin,
158 AttributeValueAtMidpoint(NSAccessibilityPositionAttribute));
159
160 // Check the attribute is updated when the widget is moved.
161 gfx::Rect new_bounds(25, 30, 60, 30);
162 widget()->SetBounds(new_bounds);
163 widget_origin = [NSValue
164 valueWithPoint:gfx::ScreenPointToNSPoint(new_bounds.bottom_left())];
165 EXPECT_NSEQ(widget_origin,
166 AttributeValueAtMidpoint(NSAccessibilityPositionAttribute));
167 }
168
169 // Test for NSAccessibilityRoleAttribute.
170 TEST_F(NativeWidgetMacAccessibilityTest, RoleAttribute) {
tapted 2016/06/10 03:17:18 I think we can merge the rest of the tests into on
Patti Lor 2016/06/16 07:05:22 Done.
171 AddChildTextfield(widget()->GetContentsView()->size());
172
173 EXPECT_NSEQ(NSAccessibilityTextFieldRole,
174 AttributeValueAtMidpoint(NSAccessibilityRoleAttribute));
175 }
176
177 // Test for NSAccessibilitySizeAttribute, including on size updates.
178 TEST_F(NativeWidgetMacAccessibilityTest, SizeAttribute) {
179 AddChildTextfield(widget()->GetContentsView()->size());
180
181 EXPECT_EQ(widget_bounds().size(),
182 gfx::Size([AttributeValueAtMidpoint(NSAccessibilitySizeAttribute)
183 sizeValue]));
184
185 // Check the attribute is updated when the widget is resized.
186 gfx::Size new_size(40, 30);
187 widget()->SetSize(new_size);
188 EXPECT_EQ(new_size, gfx::Size([AttributeValueAtMidpoint(
189 NSAccessibilitySizeAttribute) sizeValue]));
190 }
191
192 // Test for NSAccessibilitySubroleAttribute.
193 TEST_F(NativeWidgetMacAccessibilityTest, SubroleAttribute) {
194 Textfield* textfield = AddChildTextfield(widget()->GetContentsView()->size());
195 EXPECT_NSEQ(nil, AttributeValueAtMidpoint(NSAccessibilitySubroleAttribute));
196
197 textfield->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
198 EXPECT_NSEQ(NSAccessibilitySecureTextFieldSubrole,
199 AttributeValueAtMidpoint(NSAccessibilitySubroleAttribute));
200 }
201
202 // Test for NSAccessibilityTitleAttribute.
203 TEST_F(NativeWidgetMacAccessibilityTest, TitleAttribute) {
204 AddChildTextfield(widget()->GetContentsView()->size());
205
206 EXPECT_NSEQ(base::SysUTF16ToNSString(kTestTextfieldTitle),
207 AttributeValueAtMidpoint(NSAccessibilityTitleAttribute));
208 }
209
210 // Test for NSAccessibilityRoleDescriptionAttribute, including for subroles as
211 // well.
212 TEST_F(NativeWidgetMacAccessibilityTest, RoleDescriptionAttribute) {
213 Textfield* textfield = AddChildTextfield(widget()->GetContentsView()->size());
214
215 NSString* role_description =
216 NSAccessibilityRoleDescription(NSAccessibilityTextFieldRole, nil);
217 EXPECT_NSEQ(role_description, AttributeValueAtMidpoint(
218 NSAccessibilityRoleDescriptionAttribute));
219
220 // Test subrole role descriptions take precedence over the main role.
221 textfield->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
222 role_description = NSAccessibilityRoleDescription(
223 NSAccessibilityTextFieldRole, NSAccessibilitySecureTextFieldSubrole);
224 EXPECT_NSEQ(role_description, AttributeValueAtMidpoint(
225 NSAccessibilityRoleDescriptionAttribute));
226 }
227
228 // Test for NSAccessibilityValueAttribute.
229 TEST_F(NativeWidgetMacAccessibilityTest, ValueAttribute) {
230 AddChildTextfield(widget()->GetContentsView()->size());
231
232 EXPECT_NSEQ(base::SysUTF16ToNSString(kTestString),
233 AttributeValueAtMidpoint(NSAccessibilityValueAttribute));
234 }
235
236 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698