forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASTextNodeTests.mm
324 lines (265 loc) · 14.6 KB
/
ASTextNodeTests.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//
// ASTextNodeTests.mm
// Texture
//
// Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
// Changes after 4/13/2017 are: Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import <CoreText/CoreText.h>
#import "ASTestCase.h"
#import <OCMock/OCMock.h>
#import <AsyncDisplayKit/ASAvailability.h>
#import <AsyncDisplayKit/ASLayout.h>
#import <AsyncDisplayKit/ASTextNode.h>
#import <AsyncDisplayKit/ASTextNode+Beta.h>
#import <XCTest/XCTest.h>
#import <AsyncDisplayKit/CoreGraphics+ASConvenience.h>
@interface ASTextNodeTestDelegate : NSObject <ASTextNodeDelegate>
@property (nonatomic, copy, readonly) NSString *tappedLinkAttribute;
@property (nonatomic, readonly) id tappedLinkValue;
@end
@interface ASTextNodeSubclass : ASTextNode
@end
@interface ASTextNodeSecondSubclass : ASTextNodeSubclass
@end
@implementation ASTextNodeTestDelegate
- (void)textNode:(ASTextNode *)textNode tappedLinkAttribute:(NSString *)attribute value:(id)value atPoint:(CGPoint)point textRange:(NSRange)textRange
{
_tappedLinkAttribute = attribute;
_tappedLinkValue = value;
}
- (BOOL)textNode:(ASTextNode *)textNode shouldHighlightLinkAttribute:(NSString *)attribute value:(id)value atPoint:(CGPoint)point
{
return YES;
}
@end
@interface ASTextNodeTests : XCTestCase
@property (nonatomic) ASTextNode *textNode;
@property (nonatomic, copy) NSAttributedString *attributedText;
@property (nonatomic) NSMutableArray *textNodeBucket;
@end
@implementation ASTextNodeTests
- (void)setUp
{
[super setUp];
_textNode = [[ASTextNode alloc] init];
_textNodeBucket = [[NSMutableArray alloc] init];
UIFontDescriptor *desc =
[UIFontDescriptor fontDescriptorWithName:@"Didot" size:18];
NSArray *arr =
@[@{UIFontFeatureTypeIdentifierKey:@(kLetterCaseType),
UIFontFeatureSelectorIdentifierKey:@(kSmallCapsSelector)}];
desc =
[desc fontDescriptorByAddingAttributes:
@{UIFontDescriptorFeatureSettingsAttribute:arr}];
UIFont *f = [UIFont fontWithDescriptor:desc size:0];
NSDictionary *d = @{NSFontAttributeName: f};
NSMutableAttributedString *mas =
[[NSMutableAttributedString alloc] initWithString:@"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." attributes:d];
NSMutableParagraphStyle *para = [NSMutableParagraphStyle new];
para.alignment = NSTextAlignmentCenter;
para.lineSpacing = 1.0;
[mas addAttribute:NSParagraphStyleAttributeName value:para
range:NSMakeRange(0, mas.length - 1)];
// Vary the linespacing on the last line
NSMutableParagraphStyle *lastLinePara = [NSMutableParagraphStyle new];
lastLinePara.alignment = para.alignment;
lastLinePara.lineSpacing = 5.0;
[mas addAttribute:NSParagraphStyleAttributeName value:lastLinePara
range:NSMakeRange(mas.length - 1, 1)];
_attributedText = mas;
_textNode.attributedText = _attributedText;
}
#pragma mark - ASTextNode
- (void)testAllocASTextNode
{
ASTextNode *node = [[ASTextNode alloc] init];
XCTAssertTrue([[node class] isSubclassOfClass:[ASTextNode class]], @"ASTextNode alloc should return an instance of ASTextNode, instead returned %@", [node class]);
}
#pragma mark - ASTextNode
- (void)testTruncation
{
XCTAssertTrue([_textNode shouldTruncateForConstrainedSize:ASSizeRangeMake(CGSizeMake(100, 100))], @"");
_textNode.frame = CGRectMake(0, 0, 100, 100);
XCTAssertTrue(_textNode.isTruncated, @"Text Node should be truncated");
}
- (void)testSettingTruncationMessage
{
NSAttributedString *truncation = [[NSAttributedString alloc] initWithString:@"..." attributes:nil];
_textNode.truncationAttributedText = truncation;
XCTAssertTrue([_textNode.truncationAttributedText isEqualToAttributedString:truncation], @"Failed to set truncation message");
}
- (void)testSettingAdditionalTruncationMessage
{
NSAttributedString *additionalTruncationMessage = [[NSAttributedString alloc] initWithString:@"read more" attributes:nil];
_textNode.additionalTruncationMessage = additionalTruncationMessage;
XCTAssertTrue([_textNode.additionalTruncationMessage isEqualToAttributedString:additionalTruncationMessage], @"Failed to set additionalTruncationMessage message");
}
- (void)testCalculatedSizeIsGreaterThanOrEqualToConstrainedSize
{
for (NSInteger i = 10; i < 500; i += 50) {
CGSize constrainedSize = CGSizeMake(i, i);
CGSize calculatedSize = [_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, constrainedSize)].size;
XCTAssertTrue(calculatedSize.width <= constrainedSize.width, @"Calculated width (%f) should be less than or equal to constrained width (%f)", calculatedSize.width, constrainedSize.width);
XCTAssertTrue(calculatedSize.height <= constrainedSize.height, @"Calculated height (%f) should be less than or equal to constrained height (%f)", calculatedSize.height, constrainedSize.height);
}
}
- (void)testRecalculationOfSizeIsSameAsOriginallyCalculatedSize
{
for (NSInteger i = 10; i < 500; i += 50) {
CGSize constrainedSize = CGSizeMake(i, i);
CGSize calculatedSize = [_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, constrainedSize)].size;
CGSize recalculatedSize = [_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, constrainedSize)].size;
XCTAssertTrue(CGSizeEqualToSizeWithIn(calculatedSize, recalculatedSize, 4.0), @"Recalculated size %@ should be same as original size %@", NSStringFromCGSize(recalculatedSize), NSStringFromCGSize(calculatedSize));
}
}
- (void)testRecalculationOfSizeIsSameAsOriginallyCalculatedFloatingPointSize
{
for (CGFloat i = 10; i < 500; i *= 1.3) {
CGSize constrainedSize = CGSizeMake(i, i);
CGSize calculatedSize = [_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, constrainedSize)].size;
CGSize recalculatedSize = [_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, constrainedSize)].size;
XCTAssertTrue(CGSizeEqualToSizeWithIn(calculatedSize, recalculatedSize, 11.0), @"Recalculated size %@ should be same as original size %@", NSStringFromCGSize(recalculatedSize), NSStringFromCGSize(calculatedSize));
}
}
- (void)testMeasureWithZeroSizeAndPlaceholder
{
_textNode.placeholderEnabled = YES;
XCTAssertNoThrow([_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, CGSizeZero)], @"Measure with zero size and placeholder enabled should not throw an exception");
XCTAssertNoThrow([_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, CGSizeMake(0, 100))], @"Measure with zero width and placeholder enabled should not throw an exception");
XCTAssertNoThrow([_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, CGSizeMake(100, 0))], @"Measure with zero height and placeholder enabled should not throw an exception");
}
- (void)testAccessibility
{
_textNode.attributedText = _attributedText;
XCTAssertTrue(_textNode.isAccessibilityElement, @"Should be an accessibility element");
XCTAssertTrue(_textNode.accessibilityTraits == UIAccessibilityTraitStaticText, @"Should have static text accessibility trait, instead has %llu", _textNode.accessibilityTraits);
XCTAssertTrue([_textNode.accessibilityLabel isEqualToString:_attributedText.string], @"Accessibility label is incorrectly set to \n%@\n when it should be \n%@\n", _textNode.accessibilityLabel, _attributedText.string);
}
- (void)testLinkAttribute
{
NSString *linkAttributeName = @"MockLinkAttributeName";
NSString *linkAttributeValue = @"MockLinkAttributeValue";
NSString *linkString = @"Link";
NSRange linkRange = NSMakeRange(0, linkString.length);
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:linkString attributes:@{ linkAttributeName : linkAttributeValue}];
_textNode.attributedText = attributedString;
_textNode.linkAttributeNames = @[linkAttributeName];
ASTextNodeTestDelegate *delegate = [ASTextNodeTestDelegate new];
_textNode.delegate = delegate;
ASLayout *layout = [_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, CGSizeMake(100, 100))];
_textNode.frame = CGRectMake(0, 0, layout.size.width, layout.size.height);
NSRange returnedLinkRange;
NSString *returnedAttributeName;
NSString *returnedLinkAttributeValue = [_textNode linkAttributeValueAtPoint:CGPointMake(3, 3) attributeName:&returnedAttributeName range:&returnedLinkRange];
XCTAssertTrue([linkAttributeName isEqualToString:returnedAttributeName], @"Expecting a link attribute name of %@, returned %@", linkAttributeName, returnedAttributeName);
XCTAssertTrue([linkAttributeValue isEqualToString:returnedLinkAttributeValue], @"Expecting a link attribute value of %@, returned %@", linkAttributeValue, returnedLinkAttributeValue);
XCTAssertTrue(NSEqualRanges(linkRange, returnedLinkRange), @"Expected a range of %@, got a link range of %@", NSStringFromRange(linkRange), NSStringFromRange(returnedLinkRange));
}
- (void)testTapNotOnALinkAttribute
{
NSString *linkAttributeName = @"MockLinkAttributeName";
NSString *linkAttributeValue = @"MockLinkAttributeValue";
NSString *linkString = @"Link notalink";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:linkString];
[attributedString addAttribute:linkAttributeName value:linkAttributeValue range:NSMakeRange(0, 4)];
_textNode.attributedText = attributedString;
_textNode.linkAttributeNames = @[linkAttributeName];
ASTextNodeTestDelegate *delegate = [ASTextNodeTestDelegate new];
_textNode.delegate = delegate;
CGSize calculatedSize = [_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, CGSizeMake(100, 100))].size;
NSRange returnedLinkRange = NSMakeRange(NSNotFound, 0);
NSRange expectedRange = NSMakeRange(NSNotFound, 0);
NSString *returnedAttributeName;
CGPoint pointNearEndOfString = CGPointMake(calculatedSize.width - 3, calculatedSize.height / 2);
NSString *returnedLinkAttributeValue = [_textNode linkAttributeValueAtPoint:pointNearEndOfString attributeName:&returnedAttributeName range:&returnedLinkRange];
XCTAssertFalse(returnedAttributeName, @"Expecting no link attribute name, returned %@", returnedAttributeName);
XCTAssertFalse(returnedLinkAttributeValue, @"Expecting no link attribute value, returned %@", returnedLinkAttributeValue);
XCTAssertTrue(NSEqualRanges(expectedRange, returnedLinkRange), @"Expected a range of %@, got a link range of %@", NSStringFromRange(expectedRange), NSStringFromRange(returnedLinkRange));
XCTAssertFalse(delegate.tappedLinkAttribute, @"Expected the delegate to be told that %@ was tapped, instead it thinks the tapped attribute is %@", linkAttributeName, delegate.tappedLinkAttribute);
XCTAssertFalse(delegate.tappedLinkValue, @"Expected the delegate to be told that the value %@ was tapped, instead it thinks the tapped attribute value is %@", linkAttributeValue, delegate.tappedLinkValue);
}
#pragma mark exclusion Paths
- (void)testSettingExclusionPaths
{
NSArray *exclusionPaths = @[[UIBezierPath bezierPathWithRect:CGRectMake(10, 20, 30, 40)]];
_textNode.exclusionPaths = exclusionPaths;
XCTAssertTrue([_textNode.exclusionPaths isEqualToArray:exclusionPaths], @"Failed to set exclusion paths");
}
- (void)testAddingExclusionPathsShouldInvalidateAndIncreaseTheSize
{
CGSize constrainedSize = CGSizeMake(100, CGFLOAT_MAX);
CGSize sizeWithoutExclusionPaths = [_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, constrainedSize)].size;
_textNode.exclusionPaths = @[[UIBezierPath bezierPathWithRect:CGRectMake(50, 20, 30, 40)]];
CGSize sizeWithExclusionPaths = [_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, constrainedSize)].size;
XCTAssertGreaterThan(sizeWithExclusionPaths.height, sizeWithoutExclusionPaths.height, @"Setting exclusions paths should invalidate the calculated size and return a greater size");
}
#if AS_ENABLE_TEXTNODE
- (void)testThatTheExperimentWorksCorrectly
{
ASConfiguration *config = [ASConfiguration new];
config.experimentalFeatures = ASExperimentalTextNode;
[ASConfigurationManager test_resetWithConfiguration:config];
ASTextNode *plainTextNode = [[ASTextNode alloc] init];
XCTAssertEqualObjects(plainTextNode.class, [ASTextNode2 class]);
ASTextNodeSecondSubclass *sc2 = [[ASTextNodeSecondSubclass alloc] init];
XCTAssertEqualObjects([ASTextNodeSubclass superclass], [ASTextNode2 class]);
XCTAssertEqualObjects(sc2.superclass, [ASTextNodeSubclass class]);
}
- (void)testTextNodeSwitchWorksInMultiThreadEnvironment
{
ASConfiguration *config = [ASConfiguration new];
config.experimentalFeatures = ASExperimentalTextNode;
[ASConfigurationManager test_resetWithConfiguration:config];
XCTestExpectation *exp = [self expectationWithDescription:@"wait for full bucket"];
dispatch_queue_t queue = dispatch_queue_create("com.texture.AsyncDisplayKit.ASTextNodeTestsQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t g = dispatch_group_create();
for (int i = 0; i < 20; i++) {
dispatch_group_async(g, queue, ^{
ASTextNode *textNode = [[ASTextNodeSecondSubclass alloc] init];
XCTAssert([textNode isKindOfClass:[ASTextNode2 class]]);
@synchronized(self.textNodeBucket) {
[self.textNodeBucket addObject:textNode];
if (self.textNodeBucket.count == 20) {
[exp fulfill];
}
}
});
}
[self waitForExpectations:@[exp] timeout:3];
exp = nil;
[self.textNodeBucket removeAllObjects];
}
- (void)testTextNodeSwitchWorksInMultiThreadEnvironment2
{
ASConfiguration *config = [ASConfiguration new];
config.experimentalFeatures = ASExperimentalTextNode;
[ASConfigurationManager test_resetWithConfiguration:config];
XCTestExpectation *exp = [self expectationWithDescription:@"wait for full bucket"];
NSLock *lock = [[NSLock alloc] init];
NSMutableArray *textNodeBucket = [[NSMutableArray alloc] init];
dispatch_queue_t queue = dispatch_queue_create("com.texture.AsyncDisplayKit.ASTextNodeTestsQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t g = dispatch_group_create();
for (int i = 0; i < 20; i++) {
dispatch_group_async(g, queue, ^{
ASTextNode *textNode = [[ASTextNodeSecondSubclass alloc] init];
XCTAssert([textNode isKindOfClass:[ASTextNode2 class]]);
[lock lock];
[textNodeBucket addObject:textNode];
if (textNodeBucket.count == 20) {
[exp fulfill];
}
[lock unlock];
});
}
[self waitForExpectations:@[exp] timeout:3];
exp = nil;
[textNodeBucket removeAllObjects];
}
#endif
@end
@implementation ASTextNodeSubclass
@end
@implementation ASTextNodeSecondSubclass
@end