forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASTextKitTruncationTests.mm
168 lines (151 loc) · 9.52 KB
/
ASTextKitTruncationTests.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//
// ASTextKitTruncationTests.mm
// Texture
//
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the /ASDK-Licenses directory of this source tree. An additional
// grant of patent rights can be found in the PATENTS file in the same directory.
//
// Modifications to this file made after 4/13/2017 are: Copyright (c) 2017-present,
// Pinterest, Inc. Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
#import <AsyncDisplayKit/ASTextKitContext.h>
#import <AsyncDisplayKit/ASTextKitTailTruncater.h>
@interface ASTextKitTruncationTests : XCTestCase
@end
@implementation ASTextKitTruncationTests
- (NSString *)_sentenceString
{
return @"90's cray photo booth tote bag bespoke Carles. Plaid wayfarers Odd Future master cleanse tattooed four dollar toast small batch kale chips leggings meh photo booth occupy irony.";
}
- (NSAttributedString *)_sentenceAttributedString
{
return [[NSAttributedString alloc] initWithString:[self _sentenceString] attributes:@{}];
}
- (NSAttributedString *)_simpleTruncationAttributedString
{
return [[NSAttributedString alloc] initWithString:@"..." attributes:@{}];
}
- (void)testEmptyTruncationStringSameAsStraightTextKitTailTruncation
{
CGSize constrainedSize = CGSizeMake(100, 50);
NSAttributedString *attributedString = [self _sentenceAttributedString];
ASTextKitContext *context = [[ASTextKitContext alloc] initWithAttributedString:attributedString
lineBreakMode:NSLineBreakByWordWrapping
maximumNumberOfLines:0
exclusionPaths:nil
constrainedSize:constrainedSize];
__block NSRange textKitVisibleRange;
[context performBlockWithLockedTextKitComponents:^(NSLayoutManager *layoutManager, NSTextStorage *textStorage, NSTextContainer *textContainer) {
textKitVisibleRange = [layoutManager characterRangeForGlyphRange:[layoutManager glyphRangeForTextContainer:textContainer]
actualGlyphRange:NULL];
}];
ASTextKitTailTruncater *tailTruncater = [[ASTextKitTailTruncater alloc] initWithContext:context
truncationAttributedString:nil
avoidTailTruncationSet:nil];
[tailTruncater truncate];
XCTAssert(NSEqualRanges(textKitVisibleRange, tailTruncater.visibleRanges[0]));
XCTAssert(NSEqualRanges(textKitVisibleRange, tailTruncater.firstVisibleRange));
}
- (void)testSimpleTailTruncation
{
CGSize constrainedSize = CGSizeMake(100, 60);
NSAttributedString *attributedString = [self _sentenceAttributedString];
ASTextKitContext *context = [[ASTextKitContext alloc] initWithAttributedString:attributedString
lineBreakMode:NSLineBreakByWordWrapping
maximumNumberOfLines:0
exclusionPaths:nil
constrainedSize:constrainedSize];
ASTextKitTailTruncater *tailTruncater = [[ASTextKitTailTruncater alloc] initWithContext:context
truncationAttributedString:[self _simpleTruncationAttributedString]
avoidTailTruncationSet:[NSCharacterSet characterSetWithCharactersInString:@""]];
[tailTruncater truncate];
__block NSString *drawnString;
[context performBlockWithLockedTextKitComponents:^(NSLayoutManager *layoutManager, NSTextStorage *textStorage, NSTextContainer *textContainer) {
drawnString = textStorage.string;
}];
NSString *expectedString = @"90's cray photo booth tote bag bespoke Carles. Plaid wayfarers...";
XCTAssertEqualObjects(expectedString, drawnString);
XCTAssert(NSEqualRanges(NSMakeRange(0, 62), tailTruncater.visibleRanges[0]));
XCTAssert(NSEqualRanges(NSMakeRange(0, 62), tailTruncater.firstVisibleRange));
}
- (void)testAvoidedCharTailWordBoundaryTruncation
{
CGSize constrainedSize = CGSizeMake(100, 50);
NSAttributedString *attributedString = [self _sentenceAttributedString];
ASTextKitContext *context = [[ASTextKitContext alloc] initWithAttributedString:attributedString
lineBreakMode:NSLineBreakByWordWrapping
maximumNumberOfLines:0
exclusionPaths:nil
constrainedSize:constrainedSize];
ASTextKitTailTruncater *tailTruncater = [[ASTextKitTailTruncater alloc] initWithContext:context
truncationAttributedString:[self _simpleTruncationAttributedString]
avoidTailTruncationSet:[NSCharacterSet characterSetWithCharactersInString:@"."]];
[tailTruncater truncate];
__block NSString *drawnString;
[context performBlockWithLockedTextKitComponents:^(NSLayoutManager *layoutManager, NSTextStorage *textStorage, NSTextContainer *textContainer) {
drawnString = textStorage.string;
}];
// This should have removed the additional "." in the string right after Carles.
NSString *expectedString = @"90's cray photo booth tote bag bespoke Carles...";
XCTAssertEqualObjects(expectedString, drawnString);
}
- (void)testAvoidedCharTailCharBoundaryTruncation
{
CGSize constrainedSize = CGSizeMake(50, 50);
NSAttributedString *attributedString = [self _sentenceAttributedString];
ASTextKitContext *context = [[ASTextKitContext alloc] initWithAttributedString:attributedString
lineBreakMode:NSLineBreakByCharWrapping
maximumNumberOfLines:0
exclusionPaths:nil
constrainedSize:constrainedSize];
ASTextKitTailTruncater *tailTruncater = [[ASTextKitTailTruncater alloc] initWithContext:context
truncationAttributedString:[self _simpleTruncationAttributedString]
avoidTailTruncationSet:[NSCharacterSet characterSetWithCharactersInString:@"."]];
[tailTruncater truncate];
__block NSString *drawnString;
[context performBlockWithLockedTextKitComponents:^(NSLayoutManager *layoutManager, NSTextStorage *textStorage, NSTextContainer *textContainer) {
drawnString = textStorage.string;
}];
// This should have removed the additional "." in the string right after Carles.
NSString *expectedString = @"90's cray photo booth t...";
XCTAssertEqualObjects(expectedString, drawnString);
}
- (void)testHandleZeroSizeConstrainedSize
{
CGSize constrainedSize = CGSizeZero;
NSAttributedString *attributedString = [self _sentenceAttributedString];
ASTextKitContext *context = [[ASTextKitContext alloc] initWithAttributedString:attributedString
lineBreakMode:NSLineBreakByWordWrapping
maximumNumberOfLines:0
exclusionPaths:nil
constrainedSize:constrainedSize];
ASTextKitTailTruncater *tailTruncater = [[ASTextKitTailTruncater alloc] initWithContext:context
truncationAttributedString:[self _simpleTruncationAttributedString]
avoidTailTruncationSet:nil];
XCTAssertNoThrow([tailTruncater truncate]);
XCTAssert(tailTruncater.visibleRanges.size() == 0);
NSEqualRanges(NSMakeRange(0, 0), tailTruncater.firstVisibleRange);
}
- (void)testHandleZeroHeightConstrainedSize
{
CGSize constrainedSize = CGSizeMake(50, 0);
NSAttributedString *attributedString = [self _sentenceAttributedString];
ASTextKitContext *context = [[ASTextKitContext alloc] initWithAttributedString:attributedString
lineBreakMode:NSLineBreakByCharWrapping
maximumNumberOfLines:0
exclusionPaths:nil
constrainedSize:constrainedSize];
ASTextKitTailTruncater *tailTruncater = [[ASTextKitTailTruncater alloc] initWithContext:context
truncationAttributedString:[self _simpleTruncationAttributedString]
avoidTailTruncationSet:[NSCharacterSet characterSetWithCharactersInString:@"."]];
XCTAssertNoThrow([tailTruncater truncate]);
}
@end