diff --git a/Tests/ASSnapshotTestCase.h b/Tests/ASSnapshotTestCase.h index f2b3d99e0..a59079f1d 100644 --- a/Tests/ASSnapshotTestCase.h +++ b/Tests/ASSnapshotTestCase.h @@ -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 /** diff --git a/Tests/ASTextNode2SnapshotTests.mm b/Tests/ASTextNode2SnapshotTests.mm index 27073f09b..9b48a82e4 100644 --- a/Tests/ASTextNode2SnapshotTests.mm +++ b/Tests/ASTextNode2SnapshotTests.mm @@ -15,6 +15,86 @@ @interface ASTextNode2SnapshotTests : ASSnapshotTestCase @end +@interface LineBreakConfig : NSObject + +@property (nonatomic, assign) NSUInteger numberOfLines; +@property (nonatomic, assign) NSLineBreakMode lineBreakMode; + ++ (NSArray *)configs; + +- (instancetype)initWithNumberOfLines:(NSUInteger)numberOfLines lineBreakMode:(NSLineBreakMode)lineBreakMode; +- (NSString *)breakModeDescription; + +@end + +@implementation LineBreakConfig + ++ (NSArray *)configs +{ + static dispatch_once_t init_predicate; + static NSArray *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 @@ -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.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 *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]; diff --git a/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByCharWrapping_0Lines@2x.png b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByCharWrapping_0Lines@2x.png new file mode 100644 index 000000000..e080782fa Binary files /dev/null and b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByCharWrapping_0Lines@2x.png differ diff --git a/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByCharWrapping_1Lines@2x.png b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByCharWrapping_1Lines@2x.png new file mode 100644 index 000000000..7139ae654 Binary files /dev/null and b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByCharWrapping_1Lines@2x.png differ diff --git a/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByCharWrapping_2Lines@2x.png b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByCharWrapping_2Lines@2x.png new file mode 100644 index 000000000..7cab68aa7 Binary files /dev/null and b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByCharWrapping_2Lines@2x.png differ diff --git a/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByCharWrapping_3Lines@2x.png b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByCharWrapping_3Lines@2x.png new file mode 100644 index 000000000..79c7db1ee Binary files /dev/null and b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByCharWrapping_3Lines@2x.png differ diff --git a/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingHead_0Lines@2x.png b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingHead_0Lines@2x.png new file mode 100644 index 000000000..4293eb9df Binary files /dev/null and b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingHead_0Lines@2x.png differ diff --git a/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingHead_1Lines@2x.png b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingHead_1Lines@2x.png new file mode 100644 index 000000000..1422cb5b1 Binary files /dev/null and b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingHead_1Lines@2x.png differ diff --git a/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingHead_2Lines@2x.png b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingHead_2Lines@2x.png new file mode 100644 index 000000000..e9472fc15 Binary files /dev/null and b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingHead_2Lines@2x.png differ diff --git a/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingHead_3Lines@2x.png b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingHead_3Lines@2x.png new file mode 100644 index 000000000..85f8b9688 Binary files /dev/null and b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingHead_3Lines@2x.png differ diff --git a/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingMiddle_0Lines@2x.png b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingMiddle_0Lines@2x.png new file mode 100644 index 000000000..5aa97aa29 Binary files /dev/null and b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingMiddle_0Lines@2x.png differ diff --git a/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingMiddle_1Lines@2x.png b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingMiddle_1Lines@2x.png new file mode 100644 index 000000000..ad11cb5ba Binary files /dev/null and b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingMiddle_1Lines@2x.png differ diff --git a/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingMiddle_2Lines@2x.png b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingMiddle_2Lines@2x.png new file mode 100644 index 000000000..a4a47c6a5 Binary files /dev/null and b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingMiddle_2Lines@2x.png differ diff --git a/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingMiddle_3Lines@2x.png b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingMiddle_3Lines@2x.png new file mode 100644 index 000000000..56162901d Binary files /dev/null and b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingMiddle_3Lines@2x.png differ diff --git a/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingTail_0Lines@2x.png b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingTail_0Lines@2x.png new file mode 100644 index 000000000..387d0b8d4 Binary files /dev/null and b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingTail_0Lines@2x.png differ diff --git a/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingTail_1Lines@2x.png b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingTail_1Lines@2x.png new file mode 100644 index 000000000..fd36b638c Binary files /dev/null and b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingTail_1Lines@2x.png differ diff --git a/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingTail_2Lines@2x.png b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingTail_2Lines@2x.png new file mode 100644 index 000000000..ebfd10910 Binary files /dev/null and b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingTail_2Lines@2x.png differ diff --git a/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingTail_3Lines@2x.png b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingTail_3Lines@2x.png new file mode 100644 index 000000000..29e13b8d0 Binary files /dev/null and b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByTruncatingTail_3Lines@2x.png differ diff --git a/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByWordWrapping_0Lines@2x.png b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByWordWrapping_0Lines@2x.png new file mode 100644 index 000000000..6202ed987 Binary files /dev/null and b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByWordWrapping_0Lines@2x.png differ diff --git a/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByWordWrapping_1Lines@2x.png b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByWordWrapping_1Lines@2x.png new file mode 100644 index 000000000..6316fb81f Binary files /dev/null and b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByWordWrapping_1Lines@2x.png differ diff --git a/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByWordWrapping_2Lines@2x.png b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByWordWrapping_2Lines@2x.png new file mode 100644 index 000000000..ffe6f9c3a Binary files /dev/null and b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByWordWrapping_2Lines@2x.png differ diff --git a/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByWordWrapping_3Lines@2x.png b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByWordWrapping_3Lines@2x.png new file mode 100644 index 000000000..f9248c2ec Binary files /dev/null and b/Tests/ReferenceImages_iOS_10/ASTextNode2SnapshotTests/testTextTruncationModes_ASTextNode2_NSLineBreakByWordWrapping_3Lines@2x.png differ