-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathASDisplayNodeSnapshotTests.mm
140 lines (115 loc) · 4.94 KB
/
ASDisplayNodeSnapshotTests.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
//
// ASDisplayNodeSnapshotTests.mm
// Texture
//
// Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import "ASSnapshotTestCase.h"
#import <AsyncDisplayKit/AsyncDisplayKit.h>
#import <AsyncDisplayKit/ASDisplayNodeInternal.h>
@interface ASDisplayNodeSnapshotTests : ASSnapshotTestCase
@end
@implementation ASDisplayNodeSnapshotTests
- (void)testBasicHierarchySnapshotTesting
{
ASDisplayNode *node = [[ASDisplayNode alloc] init];
node.backgroundColor = [UIColor blueColor];
ASTextNode *subnode = [[ASTextNode alloc] init];
subnode.backgroundColor = [UIColor whiteColor];
subnode.attributedText = [[NSAttributedString alloc] initWithString:@"Hello"];
node.automaticallyManagesSubnodes = YES;
node.layoutSpecBlock = ^(ASDisplayNode * _Nonnull node, ASSizeRange constrainedSize) {
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(5, 5, 5, 5) child:subnode];
};
ASDisplayNodeSizeToFitSizeRange(node, ASSizeRangeMake(CGSizeZero, CGSizeMake(INFINITY, INFINITY)));
ASSnapshotVerifyNode(node, nil);
}
NS_INLINE UIImage *BlueImageMake(CGRect bounds)
{
UIGraphicsBeginImageContextWithOptions(bounds.size, YES, 0);
[[UIColor blueColor] setFill];
UIRectFill(bounds);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (void)testPrecompositedCornerRounding
{
for (CACornerMask c = 1; c <= kASCACornerAllCorners; c |= (c << 1)) {
auto node = [[ASImageNode alloc] init];
auto bounds = CGRectMake(0, 0, 100, 100);
node.image = BlueImageMake(bounds);
node.frame = bounds;
node.cornerRoundingType = ASCornerRoundingTypePrecomposited;
node.backgroundColor = UIColor.greenColor;
node.maskedCorners = c;
node.cornerRadius = 15;
ASSnapshotVerifyNode(node, ([NSString stringWithFormat:@"%d", (int)c]));
}
}
- (void)testClippingCornerRounding
{
for (CACornerMask c = 1; c <= kASCACornerAllCorners; c |= (c << 1)) {
auto node = [[ASImageNode alloc] init];
auto bounds = CGRectMake(0, 0, 100, 100);
node.image = BlueImageMake(bounds);
node.frame = bounds;
node.cornerRoundingType = ASCornerRoundingTypeClipping;
node.backgroundColor = UIColor.systemBackgroundColor;
node.maskedCorners = c;
node.cornerRadius = 15;
// A layout pass is required, because that's where we lay out the clip layers.
[node.layer layoutIfNeeded];
[[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleLight] performAsCurrentTraitCollection:^{
ASSnapshotVerifyNode(node, ([NSString stringWithFormat:@"%d_light", (int)c]));
}];
[[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark] performAsCurrentTraitCollection:^{
ASSnapshotVerifyNode(node, ([NSString stringWithFormat:@"%d_dark", (int)c]));
}];
}
}
- (void)testUserInterfaceStyleSnapshotTesting
{
ASDisplayNode *node = [[ASDisplayNode alloc] init];
[node setLayerBacked:YES];
node.backgroundColor = [UIColor systemBackgroundColor];
node.style.preferredSize = CGSizeMake(100, 100);
ASDisplayNodeSizeToFitSizeRange(node, ASSizeRangeMake(CGSizeZero, CGSizeMake(INFINITY, INFINITY)));
[[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleLight] performAsCurrentTraitCollection:^{
ASSnapshotVerifyNode(node, @"user_interface_style_light");
}];
[[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark] performAsCurrentTraitCollection:^{
ASSnapshotVerifyNode(node, @"user_interface_style_dark");
}];
}
- (void)testBackgroundDynamicColor {
ASDisplayNode *node = [[ASImageNode alloc] init];
node.backgroundColor = [UIColor systemGray6Color];
auto bounds = CGRectMake(0, 0, 100, 100);
node.frame = bounds;
UITraitCollection *tcLight = [UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleLight];
[tcLight performAsCurrentTraitCollection: ^{
ASSnapshotVerifyNode(node, @"light");
}];
UITraitCollection *tcDark = [UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark];
[tcDark performAsCurrentTraitCollection: ^{
ASSnapshotVerifyNode(node, @"dark");
}];
}
- (void)testBackgroundDynamicColorLayerBacked {
ASDisplayNode *node = [[ASImageNode alloc] init];
node.backgroundColor = [UIColor systemGray6Color];
node.layerBacked = YES;
auto bounds = CGRectMake(0, 0, 100, 100);
node.frame = bounds;
UITraitCollection *tcLight = [UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleLight];
[tcLight performAsCurrentTraitCollection: ^{
ASSnapshotVerifyNode(node, @"light");
}];
UITraitCollection *tcDark = [UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark];
[tcDark performAsCurrentTraitCollection: ^{
ASSnapshotVerifyNode(node, @"dark");
}];
}
@end