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

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

Issue 2671563002: MacViews: Implement NSAccessibilityPressAction for Views with clickable roles. (Closed)
Patch Set: Use method instead of private member. Created 3 years, 10 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
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"
(...skipping 21 matching lines...) Expand all
32 NSString* const kTestStringValue = @"Test string value"; 32 NSString* const kTestStringValue = @"Test string value";
33 NSString* const kTestTitle = @"Test textfield"; 33 NSString* const kTestTitle = @"Test textfield";
34 34
35 class FlexibleRoleTestView : public View { 35 class FlexibleRoleTestView : public View {
36 public: 36 public:
37 explicit FlexibleRoleTestView(ui::AXRole role) : role_(role) {} 37 explicit FlexibleRoleTestView(ui::AXRole role) : role_(role) {}
38 void set_role(ui::AXRole role) { role_ = role; } 38 void set_role(ui::AXRole role) { role_ = role; }
39 39
40 // Add a child view and resize to fit the child. 40 // Add a child view and resize to fit the child.
41 void FitBoundsToNewChild(View* view) { 41 void FitBoundsToNewChild(View* view) {
42 View::AddChildView(view); 42 AddChildView(view);
43 // Fit the parent widget to the size of the child for accurate hit tests. 43 // Fit the parent widget to the size of the child for accurate hit tests.
44 SetBoundsRect(view->bounds()); 44 SetBoundsRect(view->bounds());
45 } 45 }
46 46
47 bool mouse_was_pressed() { return mouse_was_pressed_; }
tapted 2017/02/09 02:45:18 nit: const method
Patti Lor 2017/02/09 23:11:23 Done.
48
47 // View: 49 // View:
48 void GetAccessibleNodeData(ui::AXNodeData* node_data) override { 50 void GetAccessibleNodeData(ui::AXNodeData* node_data) override {
49 View::GetAccessibleNodeData(node_data); 51 View::GetAccessibleNodeData(node_data);
50 node_data->role = role_; 52 node_data->role = role_;
51 } 53 }
52 54
55 bool OnMousePressed(const ui::MouseEvent& event) override {
56 mouse_was_pressed_ = true;
57 return false;
58 }
59
53 private: 60 private:
54 ui::AXRole role_; 61 ui::AXRole role_;
62 bool mouse_was_pressed_ = false;
55 63
56 DISALLOW_COPY_AND_ASSIGN(FlexibleRoleTestView); 64 DISALLOW_COPY_AND_ASSIGN(FlexibleRoleTestView);
57 }; 65 };
58 66
59 class NativeWidgetMacAccessibilityTest : public test::WidgetTest { 67 class NativeWidgetMacAccessibilityTest : public test::WidgetTest {
60 public: 68 public:
61 NativeWidgetMacAccessibilityTest() {} 69 NativeWidgetMacAccessibilityTest() {}
62 70
63 void SetUp() override { 71 void SetUp() override {
64 test::WidgetTest::SetUp(); 72 test::WidgetTest::SetUp();
65 widget_ = CreateTopLevelPlatformWidget(); 73 widget_ = CreateTopLevelPlatformWidget();
66 widget_->SetBounds(gfx::Rect(50, 50, 100, 100)); 74 widget_->SetBounds(gfx::Rect(50, 50, 100, 100));
67 widget()->Show(); 75 widget()->Show();
68 } 76 }
69 77
70 void TearDown() override { 78 void TearDown() override {
71 widget_->CloseNow(); 79 widget_->CloseNow();
72 test::WidgetTest::TearDown(); 80 test::WidgetTest::TearDown();
73 } 81 }
74 82
75 id AttributeValueAtMidpoint(NSString* attribute) { 83 id A11yElementAtMidpoint() {
76 // Accessibility hit tests come in Cocoa screen coordinates. 84 // Accessibility hit tests come in Cocoa screen coordinates.
77 NSPoint midpoint_in_screen_ = gfx::ScreenPointToNSPoint( 85 NSPoint midpoint_in_screen_ = gfx::ScreenPointToNSPoint(
78 widget_->GetWindowBoundsInScreen().CenterPoint()); 86 widget_->GetWindowBoundsInScreen().CenterPoint());
79 id hit = 87 return
80 [widget_->GetNativeWindow() accessibilityHitTest:midpoint_in_screen_]; 88 [widget_->GetNativeWindow() accessibilityHitTest:midpoint_in_screen_];
81 id value = [hit accessibilityAttributeValue:attribute]; 89 }
90
91 id AttributeValueAtMidpoint(NSString* attribute) {
92 id value = [A11yElementAtMidpoint() accessibilityAttributeValue:attribute];
tapted 2017/02/09 02:45:18 nit: just return? (i.e. |value| not required)
Patti Lor 2017/02/09 23:11:23 Done, thanks!
82 return value; 93 return value;
83 } 94 }
84 95
85 Textfield* AddChildTextfield(const gfx::Size& size) { 96 Textfield* AddChildTextfield(const gfx::Size& size) {
86 Textfield* textfield = new Textfield; 97 Textfield* textfield = new Textfield;
87 textfield->SetText(base::SysNSStringToUTF16(kTestStringValue)); 98 textfield->SetText(base::SysNSStringToUTF16(kTestStringValue));
88 textfield->SetAccessibleName(base::SysNSStringToUTF16(kTestTitle)); 99 textfield->SetAccessibleName(base::SysNSStringToUTF16(kTestTitle));
89 textfield->SetSize(size); 100 textfield->SetSize(size);
90 widget()->GetContentsView()->AddChildView(textfield); 101 widget()->GetContentsView()->AddChildView(textfield);
91 return textfield; 102 return textfield;
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 NSAccessibilityVisibleCharacterRangeAttribute) rangeValue])); 315 NSAccessibilityVisibleCharacterRangeAttribute) rangeValue]));
305 } 316 }
306 317
307 // Test writing accessibility attributes via an accessibility client for normal 318 // Test writing accessibility attributes via an accessibility client for normal
308 // Views. 319 // Views.
309 TEST_F(NativeWidgetMacAccessibilityTest, ViewWritableAttributes) { 320 TEST_F(NativeWidgetMacAccessibilityTest, ViewWritableAttributes) {
310 FlexibleRoleTestView* view = new FlexibleRoleTestView(ui::AX_ROLE_GROUP); 321 FlexibleRoleTestView* view = new FlexibleRoleTestView(ui::AX_ROLE_GROUP);
311 view->SetSize(GetWidgetBounds().size()); 322 view->SetSize(GetWidgetBounds().size());
312 widget()->GetContentsView()->AddChildView(view); 323 widget()->GetContentsView()->AddChildView(view);
313 324
314 // Get the FlexibleRoleTestView accessibility object. 325 // Make sure the accessibility object tested is the correct one.
315 NSPoint midpoint = gfx::ScreenPointToNSPoint(GetWidgetBounds().CenterPoint()); 326 id ax_node = A11yElementAtMidpoint();
316 id ax_node = [widget()->GetNativeWindow() accessibilityHitTest:midpoint];
317 EXPECT_TRUE(ax_node); 327 EXPECT_TRUE(ax_node);
318 328 EXPECT_NSEQ(NSAccessibilityGroupRole,
319 // Make sure it's the correct accessibility object. 329 AttributeValueAtMidpoint(NSAccessibilityRoleAttribute));
320 id value = [ax_node accessibilityAttributeValue:NSAccessibilityRoleAttribute];
321 EXPECT_NSEQ(NSAccessibilityGroupRole, value);
322 330
323 // Make sure |view| is focusable, then focus/unfocus it. 331 // Make sure |view| is focusable, then focus/unfocus it.
324 view->SetFocusBehavior(View::FocusBehavior::ALWAYS); 332 view->SetFocusBehavior(View::FocusBehavior::ALWAYS);
325 EXPECT_FALSE(view->HasFocus()); 333 EXPECT_FALSE(view->HasFocus());
326 EXPECT_FALSE( 334 EXPECT_FALSE(
327 [AttributeValueAtMidpoint(NSAccessibilityFocusedAttribute) boolValue]); 335 [AttributeValueAtMidpoint(NSAccessibilityFocusedAttribute) boolValue]);
328 EXPECT_TRUE([ax_node 336 EXPECT_TRUE([ax_node
329 accessibilityIsAttributeSettable:NSAccessibilityFocusedAttribute]); 337 accessibilityIsAttributeSettable:NSAccessibilityFocusedAttribute]);
330 [ax_node accessibilitySetValue:[NSNumber numberWithBool:YES] 338 [ax_node accessibilitySetValue:[NSNumber numberWithBool:YES]
331 forAttribute:NSAccessibilityFocusedAttribute]; 339 forAttribute:NSAccessibilityFocusedAttribute];
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 [ax_node accessibilitySetValue:base::SysUTF16ToNSString(replacement) 414 [ax_node accessibilitySetValue:base::SysUTF16ToNSString(replacement)
407 forAttribute:NSAccessibilitySelectedTextAttribute]; 415 forAttribute:NSAccessibilitySelectedTextAttribute];
408 EXPECT_NSEQ(new_string, 416 EXPECT_NSEQ(new_string,
409 AttributeValueAtMidpoint(NSAccessibilityValueAttribute)); 417 AttributeValueAtMidpoint(NSAccessibilityValueAttribute));
410 EXPECT_EQ(base::SysNSStringToUTF16(new_string), textfield->text()); 418 EXPECT_EQ(base::SysNSStringToUTF16(new_string), textfield->text());
411 // Make sure the cursor is at the end of the replacement. 419 // Make sure the cursor is at the end of the replacement.
412 EXPECT_EQ(gfx::Range(front.length() + replacement.length()), 420 EXPECT_EQ(gfx::Range(front.length() + replacement.length()),
413 textfield->GetSelectedRange()); 421 textfield->GetSelectedRange());
414 } 422 }
415 423
424 // Test performing a 'click' on Views with clickable roles work.
425 TEST_F(NativeWidgetMacAccessibilityTest, PressAction) {
426 FlexibleRoleTestView* view = new FlexibleRoleTestView(ui::AX_ROLE_BUTTON);
427 widget()->GetContentsView()->AddChildView(view);
428 view->SetSize(GetWidgetBounds().size());
429
430 id ax_node = A11yElementAtMidpoint();
431 EXPECT_NSEQ(NSAccessibilityButtonRole,
432 AttributeValueAtMidpoint(NSAccessibilityRoleAttribute));
433
434 EXPECT_TRUE([[ax_node accessibilityActionNames]
435 containsObject:NSAccessibilityPressAction]);
436 [ax_node accessibilityPerformAction:NSAccessibilityPressAction];
437 EXPECT_TRUE(view->mouse_was_pressed());
438 }
439
416 } // namespace views 440 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698