Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes accessibilityLabel bug on ASButtonNode that has no title #1573

Merged
merged 2 commits into from
Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Source/ASButtonNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,10 @@ - (void)updateTitle
newTitle = _normalAttributedTitle;
}

// Calling self.titleNode is essential here because _titleNode is lazily created by the getter.
if ((_titleNode != nil || newTitle.length > 0) && [self.titleNode.attributedText isEqualToAttributedString:newTitle] == NO) {
_titleNode.attributedText = newTitle;
NSAttributedString *attributedString = _titleNode.attributedText;
if ((attributedString.length > 0 || newTitle.length > 0) && [attributedString isEqualToAttributedString:newTitle] == NO) {
// Calling self.titleNode is essential here because _titleNode is lazily created by the getter.
self.titleNode.attributedText = newTitle;
[self unlock];

self.accessibilityLabel = self.defaultAccessibilityLabel;
Expand Down
24 changes: 24 additions & 0 deletions Tests/ASButtonNodeTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#import <AsyncDisplayKit/ASButtonNode.h>
#import <AsyncDisplayKit/ASDisplayNode+Beta.h>
#import <AsyncDisplayKit/ASTextNode.h>

@interface ASButtonNodeTests : XCTestCase
@end
Expand Down Expand Up @@ -51,4 +52,27 @@ - (void)testAccessibility
buttonNode.defaultAccessibilityTraits);
}

/// Test the accessbility label consistency for buttons that do not have a title
/// In this test case, the button is empty but its titleNode is not nil.
/// If we give this button an accessibility label and then change its state,
/// we still want the accessbility unchanged, instead of going back to the default accessibility label.
- (void)testAccessibilityWithoutATitle
{
ASButtonNode *buttonNode = [[ASButtonNode alloc] init];
buttonNode.accessibilityLabel = @"My Test";
// Make sure the title node is not nil.
buttonNode.titleNode.placeholderColor = [UIColor whiteColor];
buttonNode.selected = YES;
XCTAssertTrue([buttonNode.accessibilityLabel isEqualToString:@"My Test"]);
}

- (void)testUpdateTitle
{
NSAttributedString *title = [[NSAttributedString alloc] initWithString:@"MyTitle"];
ASButtonNode *buttonNode = [[ASButtonNode alloc] init];
[buttonNode setAttributedTitle:title forState:UIControlStateNormal];
XCTAssertTrue([[buttonNode attributedTitleForState:UIControlStateNormal] isEqualToAttributedString:title]);
XCTAssert([buttonNode.titleNode.attributedText isEqualToAttributedString:title]);
}

@end