From 6b338e7fecc6e37fdcc5ea00b0503c6d316112f2 Mon Sep 17 00:00:00 2001 From: Michael Schneider Date: Thu, 30 May 2019 13:25:48 -0700 Subject: [PATCH] Adding more testing and add default handling for isAccessibilityElements back for non experiment case --- Source/ASTextNode2.mm | 11 ++++++- Tests/ASTextNode2Tests.mm | 67 +++++++++++++++++++++++++++++++++++---- 2 files changed, 70 insertions(+), 8 deletions(-) diff --git a/Source/ASTextNode2.mm b/Source/ASTextNode2.mm index a31f8299b..56fccd5a2 100644 --- a/Source/ASTextNode2.mm +++ b/Source/ASTextNode2.mm @@ -230,6 +230,7 @@ - (instancetype)init self.linkAttributeNames = DefaultLinkAttributeNames(); // Accessibility + self.isAccessibilityElement = YES; self.accessibilityTraits = self.defaultAccessibilityTraits; // Placeholders @@ -422,7 +423,8 @@ - (BOOL)isAccessibilityElement { return NO; } - return YES; + // Use whatever the default is + return [super isAccessibilityElement]; } #pragma mark - Layout and Sizing @@ -510,6 +512,7 @@ - (void)setAttributedText:(NSAttributedString *)attributedText // Holding it for the duration of the method is more efficient in this case. ASLockScopeSelf(); + NSAttributedString *oldAttributedText = _attributedText; if (!ASCompareAssignCopy(_attributedText, attributedText)) { return; } @@ -526,6 +529,12 @@ - (void)setAttributedText:(NSAttributedString *)attributedText // Accessiblity self.accessibilityLabel = self.defaultAccessibilityLabel; + + // We update the isAccessibilityElement setting if this node is not switching between strings. + if (oldAttributedText.length == 0 || length == 0) { + // We're an accessibility element by default if there is a string. + self.isAccessibilityElement = (length != 0); + } // Tell the display node superclasses that the cached layout is incorrect now [self setNeedsLayout]; diff --git a/Tests/ASTextNode2Tests.mm b/Tests/ASTextNode2Tests.mm index fa91e09a9..bd00f3891 100644 --- a/Tests/ASTextNode2Tests.mm +++ b/Tests/ASTextNode2Tests.mm @@ -29,8 +29,8 @@ - (void)setUp { [super setUp]; + // Reset configuration on every setup ASConfiguration *config = [[ASConfiguration alloc] initWithDictionary:nil]; - config.experimentalFeatures = ASExperimentalExposeTextLinksForA11Y | ASExperimentalTextNode2A11YContainer; [ASConfigurationManager test_resetWithConfiguration:config]; _textNode = [[ASTextNode2 alloc] init]; @@ -69,6 +69,13 @@ - (void)setUp _textNode.attributedText = _attributedText; } +- (void)setUpEnablingExperiments +{ + ASConfiguration *config = [[ASConfiguration alloc] initWithDictionary:nil]; + config.experimentalFeatures = ASExperimentalTextNode2A11YContainer | ASExperimentalExposeTextLinksForA11Y; + [ASConfigurationManager test_resetWithConfiguration:config]; +} + - (void)testTruncation { XCTAssertTrue([(ASTextNode *)_textNode shouldTruncateForConstrainedSize:ASSizeRangeMake(CGSizeMake(100, 100))], @"Text Node should truncate"); @@ -79,6 +86,27 @@ - (void)testTruncation - (void)testBasicAccessibility { + XCTAssertTrue(_textNode.isAccessibilityElement, @"Should be an accessibility element"); + XCTAssertTrue(_textNode.accessibilityTraits == UIAccessibilityTraitStaticText, + @"Should have static text accessibility trait, instead has %llu", + _textNode.accessibilityTraits); + XCTAssertTrue(_textNode.defaultAccessibilityTraits == UIAccessibilityTraitStaticText, + @"Default accessibility traits should return static text accessibility trait, " + @"instead returns %llu", + _textNode.defaultAccessibilityTraits); + + XCTAssertTrue([_textNode.accessibilityLabel isEqualToString:_attributedText.string], + @"Accessibility label is incorrectly set to \n%@\n when it should be \n%@\n", + _textNode.accessibilityLabel, _attributedText.string); + XCTAssertTrue([_textNode.defaultAccessibilityLabel isEqualToString:_attributedText.string], + @"Default accessibility label incorrectly returns \n%@\n when it should be \n%@\n", + _textNode.defaultAccessibilityLabel, _attributedText.string); +} + +- (void)testBasicAccessibilityWithExperiments +{ + [self setUpEnablingExperiments]; + XCTAssertFalse(_textNode.isAccessibilityElement, @"Is not an accessiblity element as it's a UIAccessibilityContainer"); XCTAssertTrue(_textNode.accessibilityTraits == UIAccessibilityTraitStaticText, @"Should have static text accessibility trait, instead has %llu", @@ -134,8 +162,16 @@ - (void)testAccessibilityLayerBackedContainerAndTextNode2 XCTAssertTrue([[elements[1] accessibilityLabel] isEqualToString:@"world"]); } -- (void)testAccessibilityLayerBackedTextNode2 +- (void)testBasicAccessibilityWithExperimentsWithExperiments { + [self setUpEnablingExperiments]; + [self testAccessibilityLayerBackedContainerAndTextNode2]; +} + +- (void)testAccessibilityLayerBackedTextNode2WithExperiments +{ + [self setUpEnablingExperiments]; + ASDisplayNode *container = [[ASDisplayNode alloc] init]; container.frame = CGRectMake(50, 50, 200, 600); container.backgroundColor = [UIColor grayColor]; @@ -183,6 +219,12 @@ - (void)testThatASTextNode2SubnodeAccessibilityLabelAggregationWorks [node.view.accessibilityElements.firstObject accessibilityLabel]); } +- (void)testThatASTextNode2SubnodeAccessibilityLabelAggregationWorksWithExperiments +{ + [self setUpEnablingExperiments]; + [self testThatASTextNode2SubnodeAccessibilityLabelAggregationWorks]; +} + - (void)testThatLayeredBackedASTextNode2SubnodeAccessibilityLabelAggregationWorks { // Setup nodes @@ -207,6 +249,12 @@ - (void)testThatLayeredBackedASTextNode2SubnodeAccessibilityLabelAggregationWork } +- (void)testThatLayeredBackedASTextNode2SubnodeAccessibilityLabelAggregationWorksWithExperiments +{ + [self setUpEnablingExperiments]; + [self testThatLayeredBackedASTextNode2SubnodeAccessibilityLabelAggregationWorks]; +} + - (void)testThatASTextNode2SubnodeCustomActionsAreWorking { ASDisplayNode *node = [[ASDisplayNode alloc] init]; @@ -225,18 +273,23 @@ - (void)testThatASTextNode2SubnodeCustomActionsAreWorking [node addSubnode:innerNode1]; [node addSubnode:innerNode2]; - __unused NSArray *accessibilityElements = node.view.accessibilityElements; + NSArray *accessibilityElements = node.view.accessibilityElements; XCTAssertTrue(accessibilityElements.count == 1, @"Container node should have one accessibility element for custom actions"); NSArray *accessibilityCustomActions = accessibilityElements.firstObject.accessibilityCustomActions; XCTAssertTrue(accessibilityCustomActions.count == 2, @"Text nodes should be exposed as a11y custom actions."); -// XCTAssertEqualObjects([node.view.accessibilityElements.firstObject accessibilityLabel], -// @"hello, world", @"Subnode accessibility label aggregation broken %@", -// [node.view.accessibilityElements.firstObject accessibilityLabel]); } -- (void)testAccessibilityExposeA11YLinks +- (void)testThatASTextNode2SubnodeCustomActionsAreWorkingWithExperiments { + [self setUpEnablingExperiments]; + [self testThatASTextNode2SubnodeCustomActionsAreWorking]; +} + +- (void)testAccessibilityExposeA11YLinksWithExperiments +{ + [self setUpEnablingExperiments]; + NSString *link = @"https://texturegroup.com"; NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Texture Website: %@", link]]; NSRange linkRange = [attributedText.string rangeOfString:link];