Skip to content

Commit

Permalink
Add some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maicki committed May 30, 2019
1 parent 907b19e commit c96944a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Source/Details/_ASDisplayViewAccessiblity.mm
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ static void AggregateSublabelsOrCustomActionsForContainerNode(ASDisplayNode *con

NSMutableArray<ASAccessibilityElement *> *labeledNodes = [[NSMutableArray alloc] init];
NSMutableArray<ASAccessibilityCustomAction *> *actions = [[NSMutableArray alloc] init];
std::queue<ASDisplayNode *> queue;
queue.push(container);

// If the container does not have an accessibility label set, or if the label is meant for custom
// actions only, then aggregate its subnodes' labels. Otherwise, treat the label as an overriden
Expand All @@ -157,6 +155,8 @@ static void AggregateSublabelsOrCustomActionsForContainerNode(ASDisplayNode *con
(container.accessibilityLabel.length == 0) ||
(container.accessibilityTraits & InteractiveAccessibilityTraitsMask());

std::queue<ASDisplayNode *> queue;
queue.push(container);
ASDisplayNode *node = nil;
while (!queue.empty()) {
node = queue.front();
Expand Down
38 changes: 35 additions & 3 deletions Tests/ASTextNode2Tests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,19 @@ - (void)testAccessibilityLayerBackedTextNode2
[container addSubnode:text];

// Trigger calculation of layouts on both nodes manually otherwise the internal
// text container will not have any size
// text container will not have any size and the accessibility elements are not layed out
// properly
(void)[text layoutThatFits:ASSizeRangeMake(CGSizeZero, container.frame.size)];
(void)[container layoutThatFits:ASSizeRangeMake(CGSizeZero, container.frame.size)];
[container layoutIfNeeded];
[container.layer displayIfNeeded];

NSArray<UIAccessibilityElement *> *elements = container.view.accessibilityElements;
XCTAssertTrue(elements.count == 1);
XCTAssertTrue([[elements.firstObject accessibilityLabel] isEqualToString:@"hello"]);
// TODO(maicki): Also check for accessibilityFrame

UIAccessibilityElement *firstElement = elements.firstObject;
XCTAssertTrue([firstElement.accessibilityLabel isEqualToString:@"hello"]);
XCTAssertTrue(CGRectEqualToRect(CGRectMake(50, 102, 26, 13), CGRectIntegral(firstElement.accessibilityFrame)));
}

- (void)testThatASTextNode2SubnodeAccessibilityLabelAggregationWorks
Expand Down Expand Up @@ -204,6 +207,34 @@ - (void)testThatLayeredBackedASTextNode2SubnodeAccessibilityLabelAggregationWork

}

- (void)testThatASTextNode2SubnodeCustomActionsAreWorking
{
ASDisplayNode *node = [[ASDisplayNode alloc] init];
ASTextNode2 *innerNode1 = [[ASTextNode2 alloc] init];
innerNode1.accessibilityTraits = UIAccessibilityTraitButton;
ASTextNode2 *innerNode2 = [[ASTextNode2 alloc] init];
innerNode2.accessibilityTraits = UIAccessibilityTraitButton;

// Initialize nodes with relevant accessibility data
node.isAccessibilityContainer = YES;
innerNode1.attributedText = [[NSAttributedString alloc] initWithString:@"hello"];
innerNode2.attributedText = [[NSAttributedString alloc] initWithString:@"world"];

// Attach the subnodes to the parent node, then ensure their accessibility labels have been'
// aggregated to the parent's accessibility label
[node addSubnode:innerNode1];
[node addSubnode:innerNode2];

__unused NSArray<UIAccessibilityElement *> *accessibilityElements = node.view.accessibilityElements;
XCTAssertTrue(accessibilityElements.count == 1, @"Container node should have one accessibility element for custom actions");

NSArray<UIAccessibilityCustomAction *> *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
{
NSString *link = @"https://texturegroup.com";
Expand All @@ -215,6 +246,7 @@ - (void)testAccessibilityExposeA11YLinks

NSArray<UIAccessibilityElement *> *accessibilityElements = _textNode.accessibilityElements;
XCTAssertTrue(accessibilityElements.count == 2, @"Link should be exposed as accessibility element");

XCTAssertTrue([[accessibilityElements[0] accessibilityLabel] isEqualToString:attributedText.string], @"First accessibility element should be the full text");
XCTAssertTrue([[accessibilityElements[1] accessibilityLabel] isEqualToString:link], @"Second accessibility element should be the link");
}
Expand Down

0 comments on commit c96944a

Please sign in to comment.