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

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: TODO for comparing against Cocoa. 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 #include "testing/gtest_mac.h"
tapted 2016/06/06 04:09:52 nit: import
Patti Lor 2016/06/10 01:36:10 Done.
12 #include "ui/accessibility/ax_view_state.h"
13 #import "ui/gfx/mac/coordinate_conversion.h"
14 #include "ui/views/controls/label.h"
15 #include "ui/views/test/widget_test.h"
16 #include "ui/views/widget/widget.h"
17
18 // TODO(patricialor): Test against Cocoa attributes as well to ensure
19 // consistency between Cocoa and toolkit-views.
20
21 namespace views {
22
23 namespace test {
tapted 2016/06/06 04:09:52 remove `test` -- we can have an anonymous namespac
Patti Lor 2016/06/10 01:36:10 Done.
24
25 class AXIgnoredTestView : public View {
tapted 2016/06/06 04:09:52 nit: remove the `AX` prefix - IgnoredTestView is g
Patti Lor 2016/06/10 01:36:10 Done.
26 void GetAccessibleState(ui::AXViewState* state) override {
27 state->role = ui::AX_ROLE_IGNORED;
tapted 2016/06/06 04:09:52 #include "ui/accessibility/ax_enums.h" for IWYU
Patti Lor 2016/06/10 01:36:10 Done.
28 }
29 };
tapted 2016/06/06 04:09:52 nit: private:\n DISALLOW_COPY_AND_ASSIGN - you'll
Patti Lor 2016/06/10 01:36:10 Done.
30
31 class AXTestView : public Label {
tapted 2016/06/06 04:09:52 TestLabel? But also it's weird for Labels to have
Patti Lor 2016/06/10 01:36:10 Have removed this test class entirely, using textf
32 public:
33 explicit AXTestView(base::string16 text) : Label(text) {}
34
35 // Label:
36 void GetAccessibleState(ui::AXViewState* state) override {
37 Label::GetAccessibleState(state);
38 state->value = state->name;
39 }
40 };
41
42 class NativeWidgetMacAccessibilityTest : public WidgetTest {
43 public:
44 void SetUp() override {
45 WidgetTest::SetUp();
46 widget_ = CreateTopLevelPlatformWidget();
47 widget_bounds_ = gfx::Rect(50, 50, 100, 100);
48 widget_->SetBounds(widget_bounds_);
49 NSRect ns_widget_bounds = gfx::ScreenRectToNSRect(widget_bounds_);
tapted 2016/06/06 04:09:52 nit: perhaps just midpoint_in_screen_ = gfx::Scre
Patti Lor 2016/06/10 01:36:10 Done.
50 widget_midpoint_ =
51 NSMakePoint(NSMidX(ns_widget_bounds), NSMidY(ns_widget_bounds));
52 }
53
54 void TearDown() override {
55 widget_->CloseNow();
56 WidgetTest::TearDown();
57 }
58
59 id AttributeValueAtMidpoint(NSString* attribute) {
60 // Accessibility hit tests come in Cocoa screen coordinates.
61 id hit = [widget_->GetNativeWindow() accessibilityHitTest:widget_midpoint_];
62 id value = [hit accessibilityAttributeValue:attribute];
63 return value;
64 }
65
66 Label* AddChildLabel(gfx::Size size) {
tapted 2016/06/06 04:09:52 const gfx::Size& size
Patti Lor 2016/06/10 01:36:10 Done.
67 Label* label = new Label(kTestString);
68 label->SetSize(size);
69 widget()->GetContentsView()->AddChildView(label);
70 return label;
71 }
72
73 Widget* widget() { return widget_; }
74 gfx::Rect widget_bounds() { return widget_bounds_; }
tapted 2016/06/06 04:09:52 we should remove |widget_bounds_| -- we can call a
Patti Lor 2016/06/10 01:36:10 Done.
75
76 const base::string16 kTestString = base::ASCIIToUTF16("Green");
77
78 private:
79 gfx::Rect widget_bounds_;
80 Widget* widget_;
81
82 NSPoint widget_midpoint_;
83 };
84
tapted 2016/06/06 04:09:52 anonymous namespace should be closed here
Patti Lor 2016/06/10 01:36:10 Done.
85 TEST_F(NativeWidgetMacAccessibilityTest, ChildrenAttribute) {
tapted 2016/06/06 04:09:52 All tests should have a short description describi
Patti Lor 2016/06/10 01:36:10 Done.
86 // Check childless views don't have accessibility children.
87 EXPECT_EQ(0u,
88 [AttributeValueAtMidpoint(NSAccessibilityChildrenAttribute) count]);
89
90 const unsigned int num_children = 3;
tapted 2016/06/06 04:09:51 we don't use `unsigned int` except in rare cases -
Patti Lor 2016/06/10 01:36:10 Ooh, thanks for the links - thought the k thing wa
91 for (unsigned int i = 0; i < num_children; ++i) {
92 // Make sure the labels won't interfere with the hit test.
93 AddChildLabel(gfx::Size(1, 1));
tapted 2016/06/06 04:09:52 does gfx::Size() work? (i.e. empty?). Otherwise we
Patti Lor 2016/06/10 01:36:11 Yep, it works, have changed to empty sizes.
94 }
95 widget()->Show();
tapted 2016/06/06 04:09:52 can this be done in SetUp()?
Patti Lor 2016/06/10 01:36:11 Done.
96
97 EXPECT_EQ(num_children,
98 [AttributeValueAtMidpoint(NSAccessibilityChildrenAttribute) count]);
99
100 // Check ignored children don't show up in the accessibility tree.
101 widget()->GetContentsView()->AddChildView(new AXIgnoredTestView);
102 EXPECT_EQ(num_children,
103 [AttributeValueAtMidpoint(NSAccessibilityChildrenAttribute) count]);
104 }
105
106 TEST_F(NativeWidgetMacAccessibilityTest, ParentAttribute) {
107 Label* label = AddChildLabel(widget_bounds().size());
tapted 2016/06/06 04:09:51 the widget bounds should include the titlebar - pe
Patti Lor 2016/06/10 01:36:10 Done.
108 widget()->Show();
109
110 // TODO(patricialor): Figure out why NativeViewAccessibility::GetParent
111 // sometimes returns a gfx::NativeView and other times a AXPlatformNodeCocoa.
tapted 2016/06/06 04:09:52 sometimes? (is it random?) Perhaps we need to wait
Patti Lor 2016/06/10 01:36:11 No, not random - it returns a gfx::NativeView / NS
tapted 2016/06/10 03:17:17 Acknowledged.
112 EXPECT_NSEQ(widget()->GetNativeWindow(),
tapted 2016/06/06 04:09:52 comparing types directly (particularly an NSView)
Patti Lor 2016/06/10 01:36:11 Changed to compare the expected role of the parent
113 AttributeValueAtMidpoint(NSAccessibilityParentAttribute));
114
115 // Test an ignored role parent is skipped in favor of the grandparent widget.
tapted 2016/06/06 04:09:52 remove " widget". (i.e .grandparent doesn't have t
Patti Lor 2016/06/10 01:36:11 Done.
116 widget()->GetContentsView()->RemoveChildView(label);
117 View* ignored_view = new AXIgnoredTestView;
118 ignored_view->SetBoundsRect(widget_bounds());
119 ignored_view->AddChildView(label);
120 widget()->GetContentsView()->AddChildView(ignored_view);
121 EXPECT_NSEQ(widget()->GetNativeWindow(),
122 AttributeValueAtMidpoint(NSAccessibilityParentAttribute));
123 }
124
125 TEST_F(NativeWidgetMacAccessibilityTest, PositionAttribute) {
126 widget()->Show();
127
128 NSValue* widget_origin = [NSValue
129 valueWithPoint:gfx::ScreenPointToNSPoint(widget_bounds().bottom_left())];
130 EXPECT_NSEQ(widget_origin,
131 AttributeValueAtMidpoint(NSAccessibilityPositionAttribute));
132
133 // Check the attribute is updated when the widget is moved.
134 gfx::Rect new_bounds(25, 30, 60, 30);
135 widget()->SetBounds(new_bounds);
136 widget_origin = [NSValue
137 valueWithPoint:gfx::ScreenPointToNSPoint(new_bounds.bottom_left())];
138 EXPECT_NSEQ(widget_origin,
139 AttributeValueAtMidpoint(NSAccessibilityPositionAttribute));
140 }
141
142 TEST_F(NativeWidgetMacAccessibilityTest, RoleAttribute) {
143 AddChildLabel(widget_bounds().size());
144 widget()->Show();
145
146 EXPECT_NSEQ(NSAccessibilityStaticTextRole,
147 AttributeValueAtMidpoint(NSAccessibilityRoleAttribute));
148 }
149
150 TEST_F(NativeWidgetMacAccessibilityTest, SizeAttribute) {
151 AddChildLabel(widget_bounds().size());
152 widget()->Show();
153
154 EXPECT_EQ(widget_bounds().size(),
155 gfx::ScreenSizeFromNSSize([AttributeValueAtMidpoint(
156 NSAccessibilitySizeAttribute) sizeValue]));
157
158 // Check the attribute is updated when the widget is resized.
159 gfx::Size new_size(40, 30);
160 widget()->SetSize(new_size);
161 EXPECT_EQ(new_size, gfx::ScreenSizeFromNSSize([AttributeValueAtMidpoint(
162 NSAccessibilitySizeAttribute) sizeValue]));
163 }
164
165 TEST_F(NativeWidgetMacAccessibilityTest, TitleAttribute) {
166 AddChildLabel(widget_bounds().size());
167 widget()->Show();
168
169 EXPECT_NSEQ(base::SysUTF16ToNSString(kTestString),
170 AttributeValueAtMidpoint(NSAccessibilityTitleAttribute));
171 }
172
173 TEST_F(NativeWidgetMacAccessibilityTest, RoleDescriptionAttribute) {
174 AddChildLabel(widget_bounds().size());
175 widget()->Show();
176
177 // TODO(patricialor): Fix - AX_ATTR_DESCRIPTION doesn't seem to be set
tapted 2016/06/06 04:09:52 So this is bad, we should provide AX_ATTR_DESCRIPT
Patti Lor 2016/06/10 01:36:11 Done, thanks for that!
178 // anywhere (at least outside blink)?
179 EXPECT_NSEQ(
180 base::SysUTF16ToNSString(base::ASCIIToUTF16("label")),
181 AttributeValueAtMidpoint(NSAccessibilityRoleDescriptionAttribute));
182 }
183
184 TEST_F(NativeWidgetMacAccessibilityTest, ValueAttribute) {
185 View* view = new AXTestView(kTestString);
186 view->SetSize(widget_bounds().size());
187 widget()->GetContentsView()->AddChildView(view);
188 widget()->Show();
189
190 EXPECT_NSEQ(base::SysUTF16ToNSString(kTestString),
191 AttributeValueAtMidpoint(NSAccessibilityValueAttribute));
192 }
193
194 } // namespace test
195
196 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698