| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <memory> | 5 #include <memory> |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #import "testing/gtest_mac.h" | 11 #import "testing/gtest_mac.h" |
| 12 #include "ui/accessibility/ax_enums.h" | 12 #include "ui/accessibility/ax_enums.h" |
| 13 #include "ui/accessibility/ax_view_state.h" | 13 #include "ui/accessibility/ax_view_state.h" |
| 14 #import "ui/accessibility/platform/ax_platform_node_mac.h" | 14 #import "ui/accessibility/platform/ax_platform_node_mac.h" |
| 15 #include "ui/base/ime/text_input_type.h" | 15 #include "ui/base/ime/text_input_type.h" |
| 16 #import "ui/gfx/mac/coordinate_conversion.h" | 16 #import "ui/gfx/mac/coordinate_conversion.h" |
| 17 #include "ui/views/controls/textfield/textfield.h" | 17 #include "ui/views/controls/textfield/textfield.h" |
| 18 #include "ui/views/test/widget_test.h" | 18 #include "ui/views/test/widget_test.h" |
| 19 #include "ui/views/widget/widget.h" | 19 #include "ui/views/widget/widget.h" |
| 20 | 20 |
| 21 // Expose some methods from AXPlatformNodeCocoa for testing purposes only. | 21 // Expose some methods from AXPlatformNodeCocoa for testing purposes only. |
| 22 @interface AXPlatformNodeCocoa (Testing) | 22 @interface AXPlatformNodeCocoa (Testing) |
| 23 - (NSString*)AXRole; | 23 - (NSString*)AXRole; |
| 24 @end | 24 @end |
| 25 | 25 |
| 26 namespace views { | 26 namespace views { |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 NSString* const kTestPlaceholderText = @"Test placeholder text"; |
| 30 NSString* const kTestStringValue = @"Test string value"; | 31 NSString* const kTestStringValue = @"Test string value"; |
| 31 NSString* const kTestTitle = @"Test textfield"; | 32 NSString* const kTestTitle = @"Test textfield"; |
| 32 | 33 |
| 33 class FlexibleRoleTestView : public View { | 34 class FlexibleRoleTestView : public View { |
| 34 public: | 35 public: |
| 35 explicit FlexibleRoleTestView(ui::AXRole role) : role_(role) {} | 36 explicit FlexibleRoleTestView(ui::AXRole role) : role_(role) {} |
| 36 void set_role(ui::AXRole role) { role_ = role; } | 37 void set_role(ui::AXRole role) { role_ = role; } |
| 37 | 38 |
| 38 // Add a child view and resize to fit the child. | 39 // Add a child view and resize to fit the child. |
| 39 void FitBoundsToNewChild(View* view) { | 40 void FitBoundsToNewChild(View* view) { |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 widget()->SetBounds(new_bounds); | 164 widget()->SetBounds(new_bounds); |
| 164 widget_origin = [NSValue | 165 widget_origin = [NSValue |
| 165 valueWithPoint:gfx::ScreenPointToNSPoint(new_bounds.bottom_left())]; | 166 valueWithPoint:gfx::ScreenPointToNSPoint(new_bounds.bottom_left())]; |
| 166 EXPECT_NSEQ(widget_origin, | 167 EXPECT_NSEQ(widget_origin, |
| 167 AttributeValueAtMidpoint(NSAccessibilityPositionAttribute)); | 168 AttributeValueAtMidpoint(NSAccessibilityPositionAttribute)); |
| 168 } | 169 } |
| 169 | 170 |
| 170 // Tests for accessibility attributes on a views::Textfield. | 171 // Tests for accessibility attributes on a views::Textfield. |
| 171 // TODO(patricialor): Test against Cocoa-provided attributes as well to ensure | 172 // TODO(patricialor): Test against Cocoa-provided attributes as well to ensure |
| 172 // consistency between Cocoa and toolkit-views. | 173 // consistency between Cocoa and toolkit-views. |
| 173 TEST_F(NativeWidgetMacAccessibilityTest, TextfieldAccessibility) { | 174 TEST_F(NativeWidgetMacAccessibilityTest, TextfieldGenericAttributes) { |
| 174 Textfield* textfield = AddChildTextfield(GetWidgetBounds().size()); | 175 Textfield* textfield = AddChildTextfield(GetWidgetBounds().size()); |
| 175 | 176 |
| 177 // NSAccessibilityEnabledAttribute. |
| 178 textfield->SetEnabled(false); |
| 179 EXPECT_EQ(NO, [AttributeValueAtMidpoint(NSAccessibilityEnabledAttribute) |
| 180 boolValue]); |
| 181 textfield->SetEnabled(true); |
| 182 EXPECT_EQ(YES, [AttributeValueAtMidpoint(NSAccessibilityEnabledAttribute) |
| 183 boolValue]); |
| 184 |
| 185 // NSAccessibilityFocusedAttribute. |
| 186 EXPECT_EQ(NO, [AttributeValueAtMidpoint(NSAccessibilityFocusedAttribute) |
| 187 boolValue]); |
| 188 textfield->RequestFocus(); |
| 189 EXPECT_EQ(YES, [AttributeValueAtMidpoint(NSAccessibilityFocusedAttribute) |
| 190 boolValue]); |
| 191 |
| 176 // NSAccessibilityTitleAttribute. | 192 // NSAccessibilityTitleAttribute. |
| 177 EXPECT_NSEQ(kTestTitle, | 193 EXPECT_NSEQ(kTestTitle, |
| 178 AttributeValueAtMidpoint(NSAccessibilityTitleAttribute)); | 194 AttributeValueAtMidpoint(NSAccessibilityTitleAttribute)); |
| 179 | 195 |
| 180 // NSAccessibilityValueAttribute. | 196 // NSAccessibilityValueAttribute. |
| 181 EXPECT_NSEQ(kTestStringValue, | 197 EXPECT_NSEQ(kTestStringValue, |
| 182 AttributeValueAtMidpoint(NSAccessibilityValueAttribute)); | 198 AttributeValueAtMidpoint(NSAccessibilityValueAttribute)); |
| 183 | 199 |
| 184 // NSAccessibilityRoleAttribute. | 200 // NSAccessibilityRoleAttribute. |
| 185 EXPECT_NSEQ(NSAccessibilityTextFieldRole, | 201 EXPECT_NSEQ(NSAccessibilityTextFieldRole, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 209 EXPECT_EQ(GetWidgetBounds().size(), | 225 EXPECT_EQ(GetWidgetBounds().size(), |
| 210 gfx::Size([AttributeValueAtMidpoint(NSAccessibilitySizeAttribute) | 226 gfx::Size([AttributeValueAtMidpoint(NSAccessibilitySizeAttribute) |
| 211 sizeValue])); | 227 sizeValue])); |
| 212 // Check the attribute is updated when the Widget is resized. | 228 // Check the attribute is updated when the Widget is resized. |
| 213 gfx::Size new_size(200, 40); | 229 gfx::Size new_size(200, 40); |
| 214 widget()->SetSize(new_size); | 230 widget()->SetSize(new_size); |
| 215 EXPECT_EQ(new_size, gfx::Size([AttributeValueAtMidpoint( | 231 EXPECT_EQ(new_size, gfx::Size([AttributeValueAtMidpoint( |
| 216 NSAccessibilitySizeAttribute) sizeValue])); | 232 NSAccessibilitySizeAttribute) sizeValue])); |
| 217 } | 233 } |
| 218 | 234 |
| 235 TEST_F(NativeWidgetMacAccessibilityTest, TextfieldEditableAttributes) { |
| 236 Textfield* textfield = AddChildTextfield(GetWidgetBounds().size()); |
| 237 textfield->set_placeholder_text( |
| 238 base::SysNSStringToUTF16(kTestPlaceholderText)); |
| 239 |
| 240 // NSAccessibilityInsertionPointLineNumberAttribute. |
| 241 EXPECT_EQ(0, [AttributeValueAtMidpoint( |
| 242 NSAccessibilityInsertionPointLineNumberAttribute) intValue]); |
| 243 |
| 244 // NSAccessibilityNumberOfCharactersAttribute. |
| 245 EXPECT_EQ( |
| 246 kTestStringValue.length, |
| 247 [AttributeValueAtMidpoint(NSAccessibilityNumberOfCharactersAttribute) |
| 248 unsignedIntegerValue]); |
| 249 |
| 250 // NSAccessibilityPlaceholderAttribute. |
| 251 EXPECT_NSEQ( |
| 252 kTestPlaceholderText, |
| 253 AttributeValueAtMidpoint(NSAccessibilityPlaceholderValueAttribute)); |
| 254 |
| 255 // NSAccessibilitySelectedTextAttribute and |
| 256 // NSAccessibilitySelectedTextRangeAttribute. |
| 257 EXPECT_NSEQ(@"", |
| 258 AttributeValueAtMidpoint(NSAccessibilitySelectedTextAttribute)); |
| 259 // The cursor will be at the end of the textfield, so the selection range will |
| 260 // span 0 characters and be located at the index after the last character. |
| 261 EXPECT_EQ(gfx::Range(kTestStringValue.length, kTestStringValue.length), |
| 262 gfx::Range([AttributeValueAtMidpoint( |
| 263 NSAccessibilitySelectedTextRangeAttribute) rangeValue])); |
| 264 // Select some text in the middle of the textfield. |
| 265 gfx::Range selection_range(2, 6); |
| 266 textfield->SelectRange(selection_range); |
| 267 EXPECT_NSEQ([kTestStringValue substringWithRange:selection_range.ToNSRange()], |
| 268 AttributeValueAtMidpoint(NSAccessibilitySelectedTextAttribute)); |
| 269 EXPECT_EQ(selection_range, |
| 270 gfx::Range([AttributeValueAtMidpoint( |
| 271 NSAccessibilitySelectedTextRangeAttribute) rangeValue])); |
| 272 |
| 273 // NSAccessibilityVisibleCharacterRangeAttribute. |
| 274 EXPECT_EQ(gfx::Range(0, kTestStringValue.length), |
| 275 gfx::Range([AttributeValueAtMidpoint( |
| 276 NSAccessibilityVisibleCharacterRangeAttribute) rangeValue])); |
| 277 } |
| 278 |
| 219 } // namespace views | 279 } // namespace views |
| OLD | NEW |