Skip to content

Commit

Permalink
Fixes accessibilityLabel bug on ASButtonNode that has no title (Textu…
Browse files Browse the repository at this point in the history
  • Loading branch information
SterlingWaves authored and nguyenhuy committed Jul 11, 2019
1 parent 00d76ce commit 901f4dd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
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

0 comments on commit 901f4dd

Please sign in to comment.