Skip to content

Commit

Permalink
Add some snapshot tests for ASTextNode2 truncation modes. (#1296)
Browse files Browse the repository at this point in the history
* Add some snapshot tests for ASTextNode2 truncation modes

* Allow some tolerance in snapshots

* Express tolerance percentage correctly

* Code review comments: Move class methods up, combine declaration&initializaiton lines

* Give png files clearer names

* Oops
  • Loading branch information
wiseoldduck authored and nguyenhuy committed Jan 17, 2019
1 parent d8cc3c9 commit 9428fd4
Show file tree
Hide file tree
Showing 22 changed files with 157 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Tests/ASSnapshotTestCase.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ NSOrderedSet *ASSnapshotTestCaseDefaultSuffixes(void);
#define ASSnapshotVerifyView(view__, identifier__) \
FBSnapshotVerifyViewWithOptions(view__, identifier__, ASSnapshotTestCaseDefaultSuffixes(), 0);

#define ASSnapshotVerifyViewWithTolerance(view__, identifier__, tolerance__) \
FBSnapshotVerifyLayerWithOptions(view__, identifier__, ASSnapshotTestCaseDefaultSuffixes(), tolerance__);

@interface ASSnapshotTestCase : FBSnapshotTestCase

/**
Expand Down
155 changes: 154 additions & 1 deletion Tests/ASTextNode2SnapshotTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,86 @@ @interface ASTextNode2SnapshotTests : ASSnapshotTestCase

@end

@interface LineBreakConfig : NSObject

@property (nonatomic, assign) NSUInteger numberOfLines;
@property (nonatomic, assign) NSLineBreakMode lineBreakMode;

+ (NSArray<LineBreakConfig *> *)configs;

- (instancetype)initWithNumberOfLines:(NSUInteger)numberOfLines lineBreakMode:(NSLineBreakMode)lineBreakMode;
- (NSString *)breakModeDescription;

@end

@implementation LineBreakConfig

+ (NSArray<LineBreakConfig *> *)configs
{
static dispatch_once_t init_predicate;
static NSArray<LineBreakConfig *> *allConfigs = nil;

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;
}

- (instancetype)initWithNumberOfLines:(NSUInteger)numberOfLines lineBreakMode:(NSLineBreakMode)lineBreakMode
{
self = [super init];
if (self != nil) {
_numberOfLines = numberOfLines;
_lineBreakMode = lineBreakMode;

return self;
}
return nil;
}

- (NSString *)breakModeDescription {
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 lineBreak;
}

- (NSString *)description
{
return [NSString stringWithFormat:@"numberOfLines: %lu\nlineBreakMode: %@", (unsigned long) self.numberOfLines, [self breakModeDescription]];
}

@end

@implementation ASTextNode2SnapshotTests

- (void)setUp
Expand Down Expand Up @@ -45,13 +125,86 @@ - (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
{
UIView *container = [[UIView alloc] initWithFrame:(CGRect) {CGPointZero, (CGSize) {375.0f, 667.0f}}];

UILabel *textNodeLabel = [[UILabel alloc] init];
UILabel *uiLabelLabel = [[UILabel alloc] init];
UILabel *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];

UILabel *reference = [[UILabel alloc] init];
ASTextNode *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:@"%@_%luLines", [config breakModeDescription], (unsigned long)config.numberOfLines];
[ASSnapshotTestCase hackilySynchronouslyRecursivelyRenderNode:textNode];
ASSnapshotVerifyViewWithTolerance(container, identifier, 0.01);
}
}

- (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.

0 comments on commit 9428fd4

Please sign in to comment.