forked from fachrifaul/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASImageNodeSnapshotTests.mm
283 lines (241 loc) · 9.9 KB
/
ASImageNodeSnapshotTests.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
//
// ASImageNodeSnapshotTests.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 "ASSnapshotTestCase.h"
#import <AsyncDisplayKit/AsyncDisplayKit.h>
#if AS_AT_LEAST_IOS13
static UIImage* makeImageWithColor(UIColor *color, CGSize size) {
CGRect rect = CGRect{.origin = CGPointZero, .size = size};
UIGraphicsBeginImageContextWithOptions(size, false, 0);
[color setFill];
UIRectFill(rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
#endif
@interface ASImageNodeSnapshotTests : ASSnapshotTestCase
@end
@implementation ASImageNodeSnapshotTests
- (void)setUp
{
[super setUp];
self.recordMode = NO;
}
- (UIImage *)testImage
{
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"logo-square"
ofType:@"png"
inDirectory:@"TestResources"];
return [UIImage imageWithContentsOfFile:path];
}
- (UIImage *)testGrayscaleImage
{
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"logo-square-black"
ofType:@"png"
inDirectory:@"TestResources"];
return [UIImage imageWithContentsOfFile:path];
}
- (void)testRenderLogoSquare
{
// trivial test case to ensure ASSnapshotTestCase works
ASImageNode *imageNode = [[ASImageNode alloc] init];
imageNode.image = [self testImage];
ASDisplayNodeSizeToFitSize(imageNode, CGSizeMake(100, 100));
ASSnapshotVerifyNode(imageNode, nil);
}
- (void)testForcedScaling
{
CGSize forcedImageSize = CGSizeMake(100, 100);
ASImageNode *imageNode = [[ASImageNode alloc] init];
imageNode.forcedSize = forcedImageSize;
imageNode.image = [self testImage];
// Snapshot testing requires that node is formally laid out.
imageNode.style.width = ASDimensionMake(forcedImageSize.width);
imageNode.style.height = ASDimensionMake(forcedImageSize.height);
ASDisplayNodeSizeToFitSize(imageNode, forcedImageSize);
ASSnapshotVerifyNode(imageNode, @"first");
imageNode.style.width = ASDimensionMake(200);
imageNode.style.height = ASDimensionMake(200);
ASDisplayNodeSizeToFitSize(imageNode, CGSizeMake(200, 200));
ASSnapshotVerifyNode(imageNode, @"second");
XCTAssert(CGImageGetWidth((CGImageRef)imageNode.contents) == forcedImageSize.width * imageNode.contentsScale &&
CGImageGetHeight((CGImageRef)imageNode.contents) == forcedImageSize.height * imageNode.contentsScale,
@"Contents should be 100 x 100 by contents scale.");
}
- (void)testTintColorOnNodePropertyAlwaysTemplate
{
UIImage *test = [self testImage];
ASImageNode *node = [[ASImageNode alloc] init];
node.image = [test imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
node.tintColor = UIColor.redColor;
ASDisplayNodeSizeToFitSize(node, test.size);
// Tint color should change view
ASSnapshotVerifyNode(node, @"red_tint");
node.tintColor = UIColor.blueColor;
// Tint color should change view
ASSnapshotVerifyNode(node, @"blue_tint");
}
- (void)testTintColorOnGrayscaleNodePropertyAlwaysTemplate
{
ASConfiguration *config = [ASConfiguration new];
config.experimentalFeatures = ASExperimentalDrawingGlobal;
[ASConfigurationManager test_resetWithConfiguration:config];
UIImage *test = [self testGrayscaleImage];
ASImageNode *node = [[ASImageNode alloc] init];
node.image = [test imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
node.tintColor = UIColor.redColor;
ASDisplayNodeSizeToFitSize(node, test.size);
// Tint color should change view
ASSnapshotVerifyNode(node, @"red_tint");
node.tintColor = UIColor.blueColor;
// Tint color should change view
ASSnapshotVerifyNode(node, @"blue_tint");
}
- (void)testTintColorOnNodePropertyAutomatic
{
UIImage *test = [self testImage];
ASImageNode *node = [[ASImageNode alloc] init];
node.image = test;
// Tint color should not change view since it depends on being contained within certain views
// for automatic rendering to utilize tint color.
node.tintColor = UIColor.redColor;
ASDisplayNodeSizeToFitSize(node, test.size);
ASSnapshotVerifyNode(node, nil);
}
- (void)testTintColorOnNodePropertyAlwaysOriginal
{
UIImage *test = [self testImage];
ASImageNode *node = [[ASImageNode alloc] init];
node.image = [test imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// Tint color should not have changed since the image render mode is original
node.tintColor = UIColor.redColor;
ASDisplayNodeSizeToFitSize(node, test.size);
ASSnapshotVerifyNode(node, nil);
}
- (void)testTintColorOnNodePropertyAlwaysTemplateLayerBackedNode
{
// Test support for layerBacked image node tinting
UIImage *test = [self testImage];
ASImageNode *node = [[ASImageNode alloc] init];
[node setLayerBacked:YES];
node.tintColor = UIColor.redColor;
node.image = [test imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
ASDisplayNodeSizeToFitSize(node, test.size);
ASSnapshotVerifyNode(node, nil);
}
- (void)testTintColorInheritsFromSupernodeLayerBacked
{
// Test support for layerBacked image node tinting
ASDisplayNode *container = [[ASDisplayNode alloc] init];
[container setLayerBacked:YES];
container.tintColor = UIColor.redColor;
UIImage *test = [self testImage];
ASImageNode *node = [[ASImageNode alloc] init];
[node setLayerBacked:YES];
node.tintColor = UIColor.redColor;
node.image = [test imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[container addSubnode:node];
container.style.preferredSize = test.size;
ASDisplayNodeSizeToFitSize(node, test.size);
ASSnapshotVerifyNode(node, nil);
}
- (void)testTintColorInheritsFromSupernodeViewBacked
{
// Test support for layerBacked image node tinting
ASDisplayNode *container = [[ASDisplayNode alloc] init];
[container setLayerBacked:NO];
container.tintColor = UIColor.redColor;
UIImage *test = [self testImage];
ASImageNode *node = [[ASImageNode alloc] init];
[node setLayerBacked:YES];
node.image = [test imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[container addSubnode:node];
container.style.preferredSize = test.size;
ASDisplayNodeSizeToFitSize(node, test.size);
ASSnapshotVerifyNode(node, nil);
}
- (void)testRoundedCornerBlock
{
UIGraphicsBeginImageContext(CGSizeMake(100, 100));
[[UIColor blueColor] setFill];
UIRectFill(CGRectMake(0, 0, 100, 100));
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
ASPrimitiveTraitCollection traitCollection;
if (@available(iOS 13.0, *)) {
traitCollection = ASPrimitiveTraitCollectionFromUITraitCollection(UITraitCollection.currentTraitCollection);
} else {
traitCollection = ASPrimitiveTraitCollectionMakeDefault();
}
UIImage *rounded = ASImageNodeRoundBorderModificationBlock(2, [UIColor redColor])(result, traitCollection);
ASImageNode *node = [[ASImageNode alloc] init];
node.image = rounded;
ASDisplayNodeSizeToFitSize(node, rounded.size);
ASSnapshotVerifyNode(node, nil);
}
- (void)testTintColorBlock
{
UIGraphicsBeginImageContext(CGSizeMake(100, 100));
[[UIColor blueColor] setFill];
UIRectFill(CGRectMake(0, 0, 100, 100));
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImage *rounded = ASImageNodeTintColorModificationBlock([UIColor redColor])(result, ASPrimitiveTraitCollectionMakeDefault());
ASImageNode *node = [[ASImageNode alloc] init];
node.image = rounded;
ASDisplayNodeSizeToFitSize(node, rounded.size);
ASSnapshotVerifyNode(node, nil);
}
- (void)testUIGraphicsRendererDrawingExperiment
{
// Test to ensure that rendering with UIGraphicsRenderer don't regress
ASConfiguration *config = [ASConfiguration new];
config.experimentalFeatures = ASExperimentalDrawingGlobal;
[ASConfigurationManager test_resetWithConfiguration:config];
ASImageNode *imageNode = [[ASImageNode alloc] init];
imageNode.image = [self testImage];
ASDisplayNodeSizeToFitSize(imageNode, CGSizeMake(100, 100));
ASSnapshotVerifyNode(imageNode, nil);
}
#if AS_AT_LEAST_IOS13
- (void)testDynamicAssetImage
{
if (@available(iOS 13.0, *)) {
UIImage *image = [UIImage imageNamed:@"light-dark" inBundle:[NSBundle bundleForClass:[self class]] compatibleWithTraitCollection:nil];
ASImageNode *node = [[ASImageNode alloc] init];
node.image = image;
ASDisplayNodeSizeToFitSize(node, image.size);
[[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleLight] performAsCurrentTraitCollection:^{
ASSnapshotVerifyNode(node, @"user_interface_style_light");
}];
[[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark] performAsCurrentTraitCollection:^{
ASSnapshotVerifyNode(node, @"user_interface_style_dark");
}];
}
}
- (void)testDynamicTintColor
{
if (@available(iOS 13.0, *)) {
UIImage *image = makeImageWithColor(UIColor.redColor, CGSize{.width = 100, .height = 100});
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIColor* tintColor = UIColor.systemBackgroundColor;
ASImageNode *node = [[ASImageNode alloc] init];
node.image = image;
node.tintColor = tintColor;
ASDisplayNodeSizeToFitSize(node, image.size);
[[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleLight] performAsCurrentTraitCollection:^{
ASSnapshotVerifyNode(node, @"user_interface_style_light");
}];
[[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark] performAsCurrentTraitCollection:^{
ASSnapshotVerifyNode(node, @"user_interface_style_dark");
}];
}
}
#endif // #if AS_AT_LEAST_IOS13
@end