forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASImageNodeBackingSizeTests.mm
80 lines (64 loc) · 3.12 KB
/
ASImageNodeBackingSizeTests.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
//
// ASImageNodeBackingSizeTests.mm
// Texture
//
// Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
#import <XCTest/XCTest.h>
#import <AsyncDisplayKit/ASImageNode+CGExtras.h>
static CGSize _FitSizeWithAspectRatio(CGFloat imageRatio, CGSize backingSize);
static CGSize _FillSizeWithAspectRatio(CGFloat imageRatio, CGSize backingSize);
static CGSize _FitSizeWithAspectRatio(CGFloat imageRatio, CGSize backingSize)
{
CGFloat backingRatio = backingSize.width / backingSize.height;
// fit size should be constrained in backing size
if (imageRatio > backingRatio) {
return CGSizeMake(backingSize.width, backingSize.width / imageRatio);
} else {
return CGSizeMake(backingSize.height * imageRatio, backingSize.height);
}
}
static CGSize _FillSizeWithAspectRatio(CGFloat imageRatio, CGSize backingSize)
{
CGFloat backingRatio = backingSize.width / backingSize.height;
// backing size should be constrained in fill size
if (imageRatio > backingRatio) {
return CGSizeMake(backingSize.height * imageRatio, backingSize.height);
} else {
return CGSizeMake(backingSize.width, backingSize.width / imageRatio);
}
}
@interface ASImageNodeBackingSizeTests : XCTestCase
@end
@implementation ASImageNodeBackingSizeTests
- (void)setUp {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
// ScaleAspectFit mode calculation.
- (void)testScaleAspectFitModeBackingSizeCalculation {
CGSize imageSize = CGSizeMake(100, 100);
CGSize boundsSize = CGSizeMake(200, 400);
CGSize backingSize = CGSizeZero;
CGRect imageDrawRect = CGRectZero;
ASCroppedImageBackingSizeAndDrawRectInBounds(imageSize, boundsSize, UIViewContentModeScaleAspectFit, CGRectZero, false, CGSizeZero, &backingSize, &imageDrawRect);
CGSize backingSizeShouldBe = _FillSizeWithAspectRatio(boundsSize.width / boundsSize.height, imageSize);
CGSize drawRectSizeShouldBe = _FitSizeWithAspectRatio(imageSize.width / imageSize.height, backingSizeShouldBe);
XCTAssertTrue(CGSizeEqualToSize(backingSizeShouldBe, backingSize));
XCTAssertTrue(CGSizeEqualToSize(drawRectSizeShouldBe, imageDrawRect.size));
}
// ScaleAspectFill mode calculation.
- (void)testScaleAspectFillModeBackingSizeCalculation {
CGSize imageSize = CGSizeMake(100, 100);
CGSize boundsSize = CGSizeMake(200, 400);
CGSize backingSize = CGSizeZero;
CGRect imageDrawRect = CGRectZero;
ASCroppedImageBackingSizeAndDrawRectInBounds(imageSize, boundsSize, UIViewContentModeScaleAspectFill, CGRectZero, false, CGSizeZero, &backingSize, &imageDrawRect);
CGSize backingSizeShouldBe = _FitSizeWithAspectRatio(boundsSize.width / boundsSize.height, imageSize);
CGSize drawRectSizeShouldBe = _FillSizeWithAspectRatio(imageSize.width / imageSize.height, backingSizeShouldBe);
XCTAssertTrue(CGSizeEqualToSize(backingSizeShouldBe, backingSize));
XCTAssertTrue(CGSizeEqualToSize(drawRectSizeShouldBe, imageDrawRect.size));
}
@end