Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some snapshot tests for ASTextNode2 truncation modes. #1296

Merged
merged 6 commits into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 152 additions & 1 deletion Tests/ASTextNode2SnapshotTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,76 @@ @interface ASTextNode2SnapshotTests : ASSnapshotTestCase

@end

@interface LineBreakConfig : NSObject
@property (nonatomic, assign) NSUInteger numberOfLines;
@property (nonatomic, assign) NSLineBreakMode lineBreakMode;

- (instancetype)initWithNumberOfLines:(NSUInteger)numberOfLines lineBreakMode:(NSLineBreakMode)lineBreakMode;

+ (NSArray<LineBreakConfig *> *)configs;
wiseoldduck marked this conversation as resolved.
Show resolved Hide resolved
@end

@implementation LineBreakConfig
static dispatch_once_t init_predicate;
wiseoldduck marked this conversation as resolved.
Show resolved Hide resolved
static NSArray<LineBreakConfig *> *allConfigs = nil;

- (instancetype)initWithNumberOfLines:(NSUInteger)numberOfLines lineBreakMode:(NSLineBreakMode)lineBreakMode
{
if ((self = [super init]) != nil) {
wiseoldduck marked this conversation as resolved.
Show resolved Hide resolved
_numberOfLines = numberOfLines;
_lineBreakMode = lineBreakMode;

return self;
}
return nil;
}

- (NSString *)description
{
NSString *lineBreak = nil;
switch (self.lineBreakMode) {
case NSLineBreakByTruncatingHead:
lineBreak = @"NSLineBreakByTruncatingHead";
break;
case NSLineBreakByCharWrapping:
lineBreak = @"NSLineBreakByCharWrapping";
break;
case NSLineBreakByClipping:
lineBreak = @"NSLineBreakByClipping";
break;
case NSLineBreakByWordWrapping:
lineBreak = @"NSLineBreakByWordWrapping";
break;
case NSLineBreakByTruncatingTail:
lineBreak = @"NSLineBreakByTruncatingTail";
break;
case NSLineBreakByTruncatingMiddle:
lineBreak = @"NSLineBreakByTruncatingMiddle";
break;
default:
lineBreak = @"Unknown?";
break;
}
return [NSString stringWithFormat:@"numberOfLines: %lu\nlineBreakMode: %@", (unsigned long) self.numberOfLines, lineBreak];
}

+ (NSArray<LineBreakConfig *> *)configs
wiseoldduck marked this conversation as resolved.
Show resolved Hide resolved
{
dispatch_once(&init_predicate, ^{
NSMutableArray *setup = [NSMutableArray new];
for (int i = 0; i <= 3; i++) {
for (int j = NSLineBreakByWordWrapping; j <= NSLineBreakByTruncatingMiddle; j++) {
if (j == NSLineBreakByClipping) continue;
[setup addObject:[[LineBreakConfig alloc] initWithNumberOfLines:i lineBreakMode:(NSLineBreakMode) j]];
}

allConfigs = [NSArray arrayWithArray:setup];
}
});
return allConfigs;
}
@end

@implementation ASTextNode2SnapshotTests

- (void)setUp
Expand Down Expand Up @@ -45,13 +115,94 @@ - (void)testTextContainerInset_ASTextNode2
// trivial test case to ensure ASSnapshotTestCase works
ASTextNode *textNode = [[ASTextNode alloc] init];
textNode.attributedText = [[NSAttributedString alloc] initWithString:@"judar"
attributes:@{NSFontAttributeName : [UIFont italicSystemFontOfSize:24]}];
attributes:@{NSFontAttributeName: [UIFont italicSystemFontOfSize:24]}];
textNode.textContainerInset = UIEdgeInsetsMake(0, 2, 0, 2);
ASDisplayNodeSizeToFitSizeRange(textNode, ASSizeRangeMake(CGSizeZero, CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)));

ASSnapshotVerifyNode(textNode, nil);
}

- (void)testTextTruncationModes_ASTextNode2
{
ASTextNode *textNode;
wiseoldduck marked this conversation as resolved.
Show resolved Hide resolved
UILabel *reference;

UILabel *textNodeLabel;
UILabel *uiLabelLabel;
UILabel *description;
UIView *container;

container = [[UIView alloc] initWithFrame:(CGRect) {CGPointZero, (CGSize) {375.0f, 667.0f}}];

textNodeLabel = [[UILabel alloc] init];
uiLabelLabel = [[UILabel alloc] init];
description = [[UILabel alloc] init];
textNodeLabel.text = @"ASTextNode2:";
textNodeLabel.font = [UIFont boldSystemFontOfSize:16.0];
textNodeLabel.textColor = [UIColor colorWithRed:0.0 green:0.7 blue:0.0 alpha:1.0];
uiLabelLabel.text = @"UILabel:";
uiLabelLabel.font = [UIFont boldSystemFontOfSize:16.0];
uiLabelLabel.textColor = [UIColor colorWithRed:0.0 green:0.7 blue:0.0 alpha:1.0];

description.text = @"<Description>";
description.font = [UIFont italicSystemFontOfSize:16.0];
description.numberOfLines = 0;

uiLabelLabel.textColor = [UIColor colorWithRed:0.0 green:0.7 blue:0.0 alpha:1.0];

reference = [[UILabel alloc] init];
textNode = [[ASTextNode alloc] init]; // ASTextNode2

NSMutableAttributedString *refString = [[NSMutableAttributedString alloc] initWithString:@"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:18.0f] }];
NSMutableAttributedString *asString = [refString mutableCopy];

reference.attributedText = refString;
textNode.attributedText = asString;

CGSize size = (CGSize) {container.bounds.size.width, 120.0};
CGPoint origin = (CGPoint) {CGRectGetWidth(container.bounds) / 2 - size.width / 2, CGRectGetHeight(container.bounds) / 2 - size.height / 2}; // center

textNode.frame = (CGRect) {origin, size};
reference.frame = CGRectOffset(textNode.frame, 0, -160.0f);

textNodeLabel.bounds = (CGRect) {CGPointZero, (CGSize) {container.bounds.size.width, textNodeLabel.font.lineHeight}};
origin = (CGPoint) {textNode.frame.origin.x, textNode.frame.origin.y - textNodeLabel.bounds.size.height};
textNodeLabel.frame = (CGRect) {origin, textNodeLabel.bounds.size};

uiLabelLabel.bounds = (CGRect) {CGPointZero, (CGSize) {container.bounds.size.width, uiLabelLabel.font.lineHeight}};
origin = (CGPoint) {reference.frame.origin.x, reference.frame.origin.y - uiLabelLabel.bounds.size.height};
uiLabelLabel.frame = (CGRect) {origin, uiLabelLabel.bounds.size};

uiLabelLabel.bounds = (CGRect) {CGPointZero, (CGSize) {container.bounds.size.width, uiLabelLabel.font.lineHeight}};
origin = (CGPoint) {textNode.frame.origin.x, textNode.frame.origin.y - uiLabelLabel.bounds.size.height};
uiLabelLabel.frame = (CGRect) {origin, uiLabelLabel.bounds.size};

uiLabelLabel.bounds = (CGRect) {CGPointZero, (CGSize) {container.bounds.size.width, uiLabelLabel.font.lineHeight}};
origin = (CGPoint) {reference.frame.origin.x, reference.frame.origin.y - uiLabelLabel.bounds.size.height};
uiLabelLabel.frame = (CGRect) {origin, uiLabelLabel.bounds.size};

description.bounds = textNode.bounds;
description.frame = (CGRect) {(CGPoint) {0, container.bounds.size.height * 0.8}, description.bounds.size};

[container addSubview:reference];
[container addSubview:textNode.view];
[container addSubview:textNodeLabel];
[container addSubview:uiLabelLabel];
[container addSubview:description];

NSArray<LineBreakConfig *> *c = [LineBreakConfig configs];
for (LineBreakConfig *config in c) {
reference.lineBreakMode = textNode.truncationMode = config.lineBreakMode;
reference.numberOfLines = textNode.maximumNumberOfLines = config.numberOfLines;
description.text = config.description;
[container setNeedsLayout];
NSString *identifier = [NSString stringWithFormat:@"%lu_%lu", (unsigned long)config.lineBreakMode, (unsigned long)config.numberOfLines];
wiseoldduck marked this conversation as resolved.
Show resolved Hide resolved
[ASSnapshotTestCase hackilySynchronouslyRecursivelyRenderNode:textNode];
ASSnapshotVerifyView(container, identifier);
}
}

- (void)testTextContainerInsetIsIncludedWithSmallerConstrainedSize_ASTextNode2
{
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.