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

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: Add TODO for "feed" ARIA role. 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
« no previous file with comments | « ui/accessibility/platform/ax_platform_node_mac.mm ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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() const { return mouse_was_pressed_; }
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 }
82 return value; 90
91 id AttributeValueAtMidpoint(NSString* attribute) {
92 return [A11yElementAtMidpoint() accessibilityAttributeValue:attribute];
83 } 93 }
84 94
85 Textfield* AddChildTextfield(const gfx::Size& size) { 95 Textfield* AddChildTextfield(const gfx::Size& size) {
86 Textfield* textfield = new Textfield; 96 Textfield* textfield = new Textfield;
87 textfield->SetText(base::SysNSStringToUTF16(kTestStringValue)); 97 textfield->SetText(base::SysNSStringToUTF16(kTestStringValue));
88 textfield->SetAccessibleName(base::SysNSStringToUTF16(kTestTitle)); 98 textfield->SetAccessibleName(base::SysNSStringToUTF16(kTestTitle));
89 textfield->SetSize(size); 99 textfield->SetSize(size);
90 widget()->GetContentsView()->AddChildView(textfield); 100 widget()->GetContentsView()->AddChildView(textfield);
91 return textfield; 101 return textfield;
92 } 102 }
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 NSAccessibilityVisibleCharacterRangeAttribute) rangeValue])); 314 NSAccessibilityVisibleCharacterRangeAttribute) rangeValue]));
305 } 315 }
306 316
307 // Test writing accessibility attributes via an accessibility client for normal 317 // Test writing accessibility attributes via an accessibility client for normal
308 // Views. 318 // Views.
309 TEST_F(NativeWidgetMacAccessibilityTest, ViewWritableAttributes) { 319 TEST_F(NativeWidgetMacAccessibilityTest, ViewWritableAttributes) {
310 FlexibleRoleTestView* view = new FlexibleRoleTestView(ui::AX_ROLE_GROUP); 320 FlexibleRoleTestView* view = new FlexibleRoleTestView(ui::AX_ROLE_GROUP);
311 view->SetSize(GetWidgetBounds().size()); 321 view->SetSize(GetWidgetBounds().size());
312 widget()->GetContentsView()->AddChildView(view); 322 widget()->GetContentsView()->AddChildView(view);
313 323
314 // Get the FlexibleRoleTestView accessibility object. 324 // Make sure the accessibility object tested is the correct one.
315 NSPoint midpoint = gfx::ScreenPointToNSPoint(GetWidgetBounds().CenterPoint()); 325 id ax_node = A11yElementAtMidpoint();
316 id ax_node = [widget()->GetNativeWindow() accessibilityHitTest:midpoint];
317 EXPECT_TRUE(ax_node); 326 EXPECT_TRUE(ax_node);
318 327 EXPECT_NSEQ(NSAccessibilityGroupRole,
319 // Make sure it's the correct accessibility object. 328 AttributeValueAtMidpoint(NSAccessibilityRoleAttribute));
320 id value = [ax_node accessibilityAttributeValue:NSAccessibilityRoleAttribute];
321 EXPECT_NSEQ(NSAccessibilityGroupRole, value);
322 329
323 // Make sure |view| is focusable, then focus/unfocus it. 330 // Make sure |view| is focusable, then focus/unfocus it.
324 view->SetFocusBehavior(View::FocusBehavior::ALWAYS); 331 view->SetFocusBehavior(View::FocusBehavior::ALWAYS);
325 EXPECT_FALSE(view->HasFocus()); 332 EXPECT_FALSE(view->HasFocus());
326 EXPECT_FALSE( 333 EXPECT_FALSE(
327 [AttributeValueAtMidpoint(NSAccessibilityFocusedAttribute) boolValue]); 334 [AttributeValueAtMidpoint(NSAccessibilityFocusedAttribute) boolValue]);
328 EXPECT_TRUE([ax_node 335 EXPECT_TRUE([ax_node
329 accessibilityIsAttributeSettable:NSAccessibilityFocusedAttribute]); 336 accessibilityIsAttributeSettable:NSAccessibilityFocusedAttribute]);
330 [ax_node accessibilitySetValue:[NSNumber numberWithBool:YES] 337 [ax_node accessibilitySetValue:[NSNumber numberWithBool:YES]
331 forAttribute:NSAccessibilityFocusedAttribute]; 338 forAttribute:NSAccessibilityFocusedAttribute];
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 [ax_node accessibilitySetValue:base::SysUTF16ToNSString(replacement) 413 [ax_node accessibilitySetValue:base::SysUTF16ToNSString(replacement)
407 forAttribute:NSAccessibilitySelectedTextAttribute]; 414 forAttribute:NSAccessibilitySelectedTextAttribute];
408 EXPECT_NSEQ(new_string, 415 EXPECT_NSEQ(new_string,
409 AttributeValueAtMidpoint(NSAccessibilityValueAttribute)); 416 AttributeValueAtMidpoint(NSAccessibilityValueAttribute));
410 EXPECT_EQ(base::SysNSStringToUTF16(new_string), textfield->text()); 417 EXPECT_EQ(base::SysNSStringToUTF16(new_string), textfield->text());
411 // Make sure the cursor is at the end of the replacement. 418 // Make sure the cursor is at the end of the replacement.
412 EXPECT_EQ(gfx::Range(front.length() + replacement.length()), 419 EXPECT_EQ(gfx::Range(front.length() + replacement.length()),
413 textfield->GetSelectedRange()); 420 textfield->GetSelectedRange());
414 } 421 }
415 422
423 // Test performing a 'click' on Views with clickable roles work.
424 TEST_F(NativeWidgetMacAccessibilityTest, PressAction) {
425 FlexibleRoleTestView* view = new FlexibleRoleTestView(ui::AX_ROLE_BUTTON);
426 widget()->GetContentsView()->AddChildView(view);
427 view->SetSize(GetWidgetBounds().size());
428
429 id ax_node = A11yElementAtMidpoint();
430 EXPECT_NSEQ(NSAccessibilityButtonRole,
431 AttributeValueAtMidpoint(NSAccessibilityRoleAttribute));
432
433 EXPECT_TRUE([[ax_node accessibilityActionNames]
434 containsObject:NSAccessibilityPressAction]);
435 [ax_node accessibilityPerformAction:NSAccessibilityPressAction];
436 EXPECT_TRUE(view->mouse_was_pressed());
437 }
438
416 } // namespace views 439 } // namespace views
OLDNEW
« no previous file with comments | « ui/accessibility/platform/ax_platform_node_mac.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698