Skip to content

Commit

Permalink
调整标签格式
Browse files Browse the repository at this point in the history
  • Loading branch information
yulingtianxia committed Dec 1, 2018
1 parent 7d81781 commit 3e5c6f8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 56 deletions.
2 changes: 1 addition & 1 deletion TBUIAutoTest.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "TBUIAutoTest"
s.version = "1.1.8"
s.version = "1.2.0"
s.summary = "Generating accessibilityIdentifier for UIAutomation."
s.description = <<-DESC
TBUIAutoTest generates UIAutomation `accessibilityIdentifier` for you.
Expand Down
96 changes: 41 additions & 55 deletions TBUIAutoTest/UIView+TBUIAutoTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ + (void)load
dispatch_once(&onceToken, ^{
Class viewClass = NSClassFromString(@"UIView");
[viewClass swizzleSelector:@selector(accessibilityIdentifier) withAnotherSelector:@selector(tb_accessibilityIdentifier)];
[viewClass swizzleSelector:@selector(accessibilityLabel) withAnotherSelector:@selector(tb_accessibilityLabel)];
if ([NSUserDefaults.standardUserDefaults boolForKey:kAutoTestUILongPressKey]) {
[viewClass swizzleSelector:@selector(addSubview:) withAnotherSelector:@selector(tb_addSubview:)];
}
Expand All @@ -53,75 +52,52 @@ - (void)longPress:(UILongPressGestureRecognizer *)recognizer
}
}

#pragma mark - Method Swizzling
#pragma mark - Generate Label

- (NSString *)tb_accessibilityIdentifier
- (NSString *)labelForOriginalLabel:(NSString *)originalLabel
{
NSString *accessibilityIdentifier = [self tb_accessibilityIdentifier];
if (accessibilityIdentifier.length > 0) {
if ([accessibilityIdentifier hasPrefix:@"("]) {
if (originalLabel.length > 0) {
if ([originalLabel hasPrefix:@"TBUIAutoTest_"]) {
NSString *reuseLabel = [self labelForReuseView];
if (reuseLabel.length > 0) {
return reuseLabel;
}
}
return accessibilityIdentifier;
return originalLabel;
}
else if ([accessibilityIdentifier isEqualToString:@"null"]) {
accessibilityIdentifier = @"";
else if ([originalLabel isEqualToString:@"null"]) {
originalLabel = @"";
}

NSString *labelStr = [self.superview findNameWithInstance:self];

if (labelStr && ![labelStr isEqualToString:@""]) {
labelStr = [NSString stringWithFormat:@"(%@)",labelStr];
labelStr = [NSString stringWithFormat:@"TBUIAutoTest_Property_%@", labelStr];
}
else {
if ([self isKindOfClass:[UILabel class]]) {//UILabel 使用 text
labelStr = [NSString stringWithFormat:@"(%@)",((UILabel *)self).text?:@""];
labelStr = [NSString stringWithFormat:@"TBUIAutoTest_Label_%@", ((UILabel *)self).text ?: @""];
}
else if ([self isKindOfClass:[UIImageView class]]) {//UIImageView 使用 image 的 imageName
labelStr = [NSString stringWithFormat:@"(%@)",((UIImageView *)self).image.accessibilityIdentifier?:[NSString stringWithFormat:@"image%ld",(long)((UIImageView *)self).tag]];
labelStr = [NSString stringWithFormat:@"TBUIAutoTest_ImageView_%@", ((UIImageView *)self).image.accessibilityIdentifier ?: [NSString stringWithFormat:@"image%ld", (long)((UIImageView *)self).tag]];
}
else if ([self isKindOfClass:[UIButton class]]) {//UIButton 使用 button 的 text 和 image
labelStr = [NSString stringWithFormat:@"(%@%@)",((UIButton *)self).titleLabel.text?:@"",((UIButton *)self).imageView.image.accessibilityIdentifier?:@""];
labelStr = [NSString stringWithFormat:@"TBUIAutoTest_Button_%@_%@",((UIButton *)self).titleLabel.text ?: @"", ((UIButton *)self).imageView.image.accessibilityIdentifier ?: @""];
}
else if (accessibilityIdentifier) {// 已有 label,则在此基础上再次添加更多信息
labelStr = [NSString stringWithFormat:@"(%@)",accessibilityIdentifier];
NSString *label = [self labelForReuseView];
if (label.length > 0) {
labelStr = label;
}
if ([self isKindOfClass:[UIButton class]]) {
self.accessibilityValue = [NSString stringWithFormat:@"(%@)",((UIButton *)self).currentBackgroundImage.accessibilityIdentifier?:@""];
self.accessibilityValue = [NSString stringWithFormat:@"TBUIAutoTest_Button_%@", ((UIButton *)self).currentBackgroundImage.accessibilityIdentifier ?: @""];
}
}
if ([labelStr isEqualToString:@"()"] || [labelStr isEqualToString:@"(null)"] || [labelStr isEqualToString:@"null"]) {
if ([labelStr isEqualToString:@"TBUIAutoTest_"] || [labelStr isEqualToString:@"TBUIAutoTest_null"] || [labelStr isEqualToString:@"null"]) {
labelStr = @"";
}
[self setAccessibilityIdentifier:labelStr];
return labelStr;
}

- (NSString *)tb_accessibilityLabel
{
NSString *accessibilityIdentifier = [self tb_accessibilityIdentifier];
if (accessibilityIdentifier.length > 0 && ![accessibilityIdentifier hasPrefix:@"("]) {
return [self tb_accessibilityLabel];
}
if ([self isKindOfClass:[UIImageView class]]) {//UIImageView 特殊处理
NSString *name = [self.superview findNameWithInstance:self];
if (name) {
self.accessibilityIdentifier = [NSString stringWithFormat:@"(%@)",name];
}
else {
self.accessibilityIdentifier = [NSString stringWithFormat:@"(%@)",((UIImageView *)self).image.accessibilityIdentifier?:[NSString stringWithFormat:@"image%ld",(long)((UIImageView *)self).tag]];
}
}
NSString *label = [self labelForReuseView];
if (label.length > 0) {
self.accessibilityIdentifier = label;
}
return [self tb_accessibilityLabel];
}

- (NSString *)labelForReuseView
{
if ([self isKindOfClass:[UITableViewCell class]]) {//UITableViewCell 特殊处理
Expand All @@ -131,7 +107,7 @@ - (NSString *)labelForReuseView
}
UITableView *tableView = (UITableView *)view;
NSIndexPath *indexPath = [tableView indexPathForCell:(UITableViewCell *)self];
return [NSString stringWithFormat:@"(%@-%ld.%ld)", ((UITableViewCell *)self).reuseIdentifier, (long)indexPath.section, (long)indexPath.row];
return [NSString stringWithFormat:@"TBUIAutoTest_TableCell_%@_%ld_%ld", ((UITableViewCell *)self).reuseIdentifier, (long)indexPath.section, (long)indexPath.row];
}
if ([self isKindOfClass:[UICollectionViewCell class]]) {//UICollectionViewCell 特殊处理
UIView *view = [self superview];
Expand All @@ -140,17 +116,38 @@ - (NSString *)labelForReuseView
}
UICollectionView *collectionView = (UICollectionView *)view;
NSIndexPath *indexPath = [collectionView indexPathForCell:(UICollectionViewCell *)self];
return [NSString stringWithFormat:@"(%@-%ld.%ld)", ((UICollectionViewCell *)self).reuseIdentifier, (long)indexPath.section, (long)indexPath.row];
return [NSString stringWithFormat:@"TBUIAutoTest_CollectionCell_%@_%ld_%ld", ((UICollectionViewCell *)self).reuseIdentifier, (long)indexPath.section, (long)indexPath.row];
}
return @"";
}

- (UIViewController*)viewController
{
for (UIView* next = self; next; next = next.superview) {
UIResponder *nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
return (UIViewController*)nextResponder;
}
}
return nil;
}

#pragma mark - Method Swizzling

- (NSString *)tb_accessibilityIdentifier
{
NSString *accessibilityIdentifier = [self tb_accessibilityIdentifier];
NSString *labelStr = [self labelForOriginalLabel:accessibilityIdentifier];
[self setAccessibilityIdentifier:labelStr];
return labelStr;
}

- (void)tb_addSubview:(UIView *)view
{
if (!view) {
[self tb_addSubview:view];
if (!view.isAccessibilityElement) {
return;
}
[self tb_addSubview:view];
UILongPressGestureRecognizer *longPress = objc_getAssociatedObject(view, _cmd);
if (!longPress) {
longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:view action:@selector(longPress:)];
Expand All @@ -160,15 +157,4 @@ - (void)tb_addSubview:(UIView *)view
}
}

- (UIViewController*)viewController
{
for (UIView* next = self; next; next = next.superview) {
UIResponder *nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
return (UIViewController*)nextResponder;
}
}
return nil;
}

@end

0 comments on commit 3e5c6f8

Please sign in to comment.