Skip to content

Commit

Permalink
[TIMOB-26973] iOS: Add accessibilityIdentifier (#10864)
Browse files Browse the repository at this point in the history
* feat(ios): compose accessibility identifier

For UI testing purposes compose "accessibilityIdentifier" property for views (same way it's done
with "contentDescription" on Android).

fix TIMOB-26973

* fix(ios): accessibility properties for navButtons

refs TIMOB-26973

* fix(ios): warnings on navButtons

refs TIMOB-26973

* fix(iOS): Updated accessibilityIdentifier logic
  • Loading branch information
drauggres authored and keerthi1032 committed May 16, 2019
1 parent 1b8e0a2 commit 2a16df7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions iphone/Classes/TiUINavBarButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ - (id)initWithProxy:(TiUIButtonProxy *)proxy_
proxy = proxy_; // Don't retain

self.accessibilityLabel = [proxy_ valueForUndefinedKey:@"accessibilityLabel"];
self.accessibilityValue = [proxy_ valueForUndefinedKey:@"accessibilityValue"];
self.accessibilityHint = [proxy_ valueForUndefinedKey:@"accessibilityHint"];
self.accessibilityIdentifier = [TiUtils composeAccessibilityIdentifier:self];

self.width = [TiUtils floatValue:[proxy_ valueForKey:@"width"] def:0.0];
//A width of 0 is treated as Auto by the iPhone OS, so this is safe.
Expand Down
3 changes: 3 additions & 0 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/TiUIView.m
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ - (void)setAccessibilityLabel_:(id)accessibilityLabel
if (accessibilityElement != nil) {
[accessibilityElement setIsAccessibilityElement:YES];
[accessibilityElement setAccessibilityLabel:[TiUtils stringValue:accessibilityLabel]];
[accessibilityElement setAccessibilityIdentifier:[TiUtils composeAccessibilityIdentifier:accessibilityElement]];
}
}

Expand All @@ -346,6 +347,7 @@ - (void)setAccessibilityValue_:(id)accessibilityValue
if (accessibilityElement != nil) {
[accessibilityElement setIsAccessibilityElement:YES];
[accessibilityElement setAccessibilityValue:[TiUtils stringValue:accessibilityValue]];
[accessibilityElement setAccessibilityIdentifier:[TiUtils composeAccessibilityIdentifier:accessibilityElement]];
}
}

Expand All @@ -355,6 +357,7 @@ - (void)setAccessibilityHint_:(id)accessibilityHint
if (accessibilityElement != nil) {
[accessibilityElement setIsAccessibilityElement:YES];
[accessibilityElement setAccessibilityHint:[TiUtils stringValue:accessibilityHint]];
[accessibilityElement setAccessibilityIdentifier:[TiUtils composeAccessibilityIdentifier:accessibilityElement]];
}
}

Expand Down
2 changes: 2 additions & 0 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/TiUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,8 @@ typedef enum {

+ (void)applyConstraintToView:(TiUIView *)view forProxy:(TiViewProxy *)proxy withBounds:(CGRect)bounds;

+ (NSString *)composeAccessibilityIdentifier:(id)object;

+ (CGRect)viewPositionRect:(UIView *)view;

+ (BOOL)barTranslucencyForColor:(TiColor *)color;
Expand Down
46 changes: 46 additions & 0 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/TiUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,52 @@ + (void)applyConstraintToView:(TiUIView *)view forProxy:(TiViewProxy *)proxy wit
ApplyConstraintToViewWithBounds([proxy layoutProperties], view, bounds);
}

+ (NSString *)composeAccessibilityIdentifier:(id)object
{
NSString *accessibilityLabel = [object accessibilityLabel];
NSString *accessibilityValue = [object accessibilityValue];
NSString *accessibilityHint = [object accessibilityHint];

NSString *pattern = @"^.*[!\"#$%&'()*+,\\-./:;<=>?@\\[\\]^_`{|}~]\\s*$";
NSString *dot = @".";
NSString *space = @" ";
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
NSMutableArray *array = [NSMutableArray array];
NSUInteger numberOfMatches;

if (accessibilityLabel != nil && accessibilityLabel.length) {
[array addObject:accessibilityLabel];
numberOfMatches = [regex numberOfMatchesInString:accessibilityLabel options:0 range:NSMakeRange(0, [accessibilityLabel length])];
if (numberOfMatches == 0) {
[array addObject:dot];
}
}

if (accessibilityValue != nil && accessibilityValue.length) {
if ([array count] > 0) {
[array addObject:space];
}
[array addObject:accessibilityValue];
numberOfMatches = [regex numberOfMatchesInString:accessibilityValue options:0 range:NSMakeRange(0, [accessibilityValue length])];
if (numberOfMatches == 0) {
[array addObject:dot];
}
}

if (accessibilityHint != nil && accessibilityHint.length) {
if ([array count] > 0) {
[array addObject:space];
}
[array addObject:accessibilityHint];
numberOfMatches = [regex numberOfMatchesInString:accessibilityHint options:0 range:NSMakeRange(0, [accessibilityHint length])];
if (numberOfMatches == 0) {
[array addObject:dot];
}
}
return [array componentsJoinedByString:@""];
}

+ (CGRect)viewPositionRect:(UIView *)view
{
#if USEFRAME
Expand Down

0 comments on commit 2a16df7

Please sign in to comment.