Skip to content

Commit 4b785fa

Browse files
committed
Add test cases for YYCache plugin, update codecov report
1 parent 708ce17 commit 4b785fa

File tree

8 files changed

+174
-18
lines changed

8 files changed

+174
-18
lines changed

Example/SDWebImageYYPlugin.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
325E282C213D01AD00044370 /* TestImageAnimated.apng in Resources */ = {isa = PBXBuildFile; fileRef = 325E2829213D01AD00044370 /* TestImageAnimated.apng */; };
1515
325E282D213D01AD00044370 /* TestImageStatic.webp in Resources */ = {isa = PBXBuildFile; fileRef = 325E282A213D01AD00044370 /* TestImageStatic.webp */; };
1616
325E282E213D01AD00044370 /* TestImageAnimated.webp in Resources */ = {isa = PBXBuildFile; fileRef = 325E282B213D01AD00044370 /* TestImageAnimated.webp */; };
17+
325E2832213D09D800044370 /* TestImage.png in Resources */ = {isa = PBXBuildFile; fileRef = 325E2830213D09D800044370 /* TestImage.png */; };
18+
325E2833213D09D800044370 /* TestImage.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 325E2831213D09D800044370 /* TestImage.jpg */; };
1719
6003F58E195388D20070C39A /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6003F58D195388D20070C39A /* Foundation.framework */; };
1820
6003F590195388D20070C39A /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6003F58F195388D20070C39A /* CoreGraphics.framework */; };
1921
6003F592195388D20070C39A /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6003F591195388D20070C39A /* UIKit.framework */; };
@@ -54,6 +56,8 @@
5456
325E2829213D01AD00044370 /* TestImageAnimated.apng */ = {isa = PBXFileReference; lastKnownFileType = file; path = TestImageAnimated.apng; sourceTree = "<group>"; };
5557
325E282A213D01AD00044370 /* TestImageStatic.webp */ = {isa = PBXFileReference; lastKnownFileType = file; path = TestImageStatic.webp; sourceTree = "<group>"; };
5658
325E282B213D01AD00044370 /* TestImageAnimated.webp */ = {isa = PBXFileReference; lastKnownFileType = file; path = TestImageAnimated.webp; sourceTree = "<group>"; };
59+
325E2830213D09D800044370 /* TestImage.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = TestImage.png; sourceTree = "<group>"; };
60+
325E2831213D09D800044370 /* TestImage.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = TestImage.jpg; sourceTree = "<group>"; };
5761
5C9C832718252AAAF79D98FE /* Pods-SDWebImageYYPlugin_Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SDWebImageYYPlugin_Tests.release.xcconfig"; path = "Pods/Target Support Files/Pods-SDWebImageYYPlugin_Tests/Pods-SDWebImageYYPlugin_Tests.release.xcconfig"; sourceTree = "<group>"; };
5862
6003F58A195388D20070C39A /* SDWebImageYYPlugin_Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SDWebImageYYPlugin_Example.app; sourceTree = BUILT_PRODUCTS_DIR; };
5963
6003F58D195388D20070C39A /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
@@ -111,6 +115,8 @@
111115
325E2824213CFB2300044370 /* Images */ = {
112116
isa = PBXGroup;
113117
children = (
118+
325E2831213D09D800044370 /* TestImage.jpg */,
119+
325E2830213D09D800044370 /* TestImage.png */,
114120
325E2825213CFB2300044370 /* TestImage.gif */,
115121
325E2829213D01AD00044370 /* TestImageAnimated.apng */,
116122
325E282B213D01AD00044370 /* TestImageAnimated.webp */,
@@ -319,6 +325,8 @@
319325
buildActionMask = 2147483647;
320326
files = (
321327
325E282E213D01AD00044370 /* TestImageAnimated.webp in Resources */,
328+
325E2832213D09D800044370 /* TestImage.png in Resources */,
329+
325E2833213D09D800044370 /* TestImage.jpg in Resources */,
322330
325E2826213CFB2300044370 /* TestImage.gif in Resources */,
323331
325E282C213D01AD00044370 /* TestImageAnimated.apng in Resources */,
324332
6003F5BA195388D20070C39A /* InfoPlist.strings in Resources */,

Example/SDWebImageYYPlugin.xcodeproj/xcshareddata/xcschemes/SDWebImageYYPlugin_Tests.xcscheme

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
buildConfiguration = "Debug"
1111
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
1212
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
13+
codeCoverageEnabled = "YES"
1314
shouldUseLaunchSchemeArgsEnv = "YES">
1415
<Testables>
1516
<TestableReference

Example/Tests/Images/TestImage.jpg

3.13 KB
Loading

Example/Tests/Images/TestImage.png

6.66 KB
Loading

Example/Tests/SDTestCase.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/*
2-
* This file is part of the SDWebImage package.
3-
* (c) Olivier Poitrey <[email protected]>
4-
* (c) Matt Galloway
2+
* This file is part of the SDWebImageYYPlugin package.
53
*
64
* For the full copyright and license information, please view the LICENSE
75
* file that was distributed with this source code.
@@ -17,7 +15,7 @@
1715

1816
FOUNDATION_EXPORT const int64_t kAsyncTestTimeout;
1917
FOUNDATION_EXPORT const int64_t kMinDelayNanosecond;
20-
FOUNDATION_EXPORT NSString * _Nonnull const kTestJpegURL;
18+
FOUNDATION_EXPORT NSString * _Nonnull const kTestJPEGURL;
2119
FOUNDATION_EXPORT NSString * _Nonnull const kTestPNGURL;
2220
FOUNDATION_EXPORT NSString * _Nonnull const kTestGIFURL;
2321
FOUNDATION_EXPORT NSString * _Nonnull const kTestWebPURL;

Example/Tests/SDTestCase.m

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/*
2-
* This file is part of the SDWebImage package.
3-
* (c) Olivier Poitrey <[email protected]>
4-
* (c) Matt Galloway
2+
* This file is part of the SDWebImageYYPlugin package.
53
*
64
* For the full copyright and license information, please view the LICENSE
75
* file that was distributed with this source code.
@@ -11,7 +9,7 @@
119

1210
const int64_t kAsyncTestTimeout = 5;
1311
const int64_t kMinDelayNanosecond = NSEC_PER_MSEC * 100; // 0.1s
14-
NSString *const kTestJpegURL = @"http://via.placeholder.com/50x50.jpg";
12+
NSString *const kTestJPEGURL = @"http://via.placeholder.com/50x50.jpg";
1513
NSString *const kTestPNGURL = @"http://via.placeholder.com/50x50.png";
1614
NSString *const kTestGIFURL = @"https://media.giphy.com/media/UEsrLdv7ugRTq/giphy.gif";
1715
NSString *const kTestWebPURL = @"http://littlesvr.ca/apng/images/SteamEngine.webp";

Example/Tests/SDYYCacheTests.m

Lines changed: 144 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,154 @@
1-
//
2-
// SDYYCacheTests.m
3-
// SDWebImageYYPlugin_Tests
4-
//
5-
// Created by lizhuoli on 2018/9/3.
6-
// Copyright © 2018年 DreamPiggy. All rights reserved.
7-
//
1+
/*
2+
* This file is part of the SDWebImageYYPlugin package.
3+
*
4+
* For the full copyright and license information, please view the LICENSE
5+
* file that was distributed with this source code.
6+
*/
87

98
#import "SDTestCase.h"
109

10+
static NSString *kTestImageKeyJPEG = @"TestImageKey.jpg";
11+
static NSString *kTestImageKeyPNG = @"TestImageKey.png";
12+
13+
@interface SDImageCache ()
14+
15+
@property (nonatomic, strong, nonnull) id<SDMemoryCache> memCache;
16+
@property (nonatomic, strong, nonnull) id<SDDiskCache> diskCache;
17+
18+
@end
19+
1120
@interface SDYYCacheTests : SDTestCase
1221

22+
@property (nonatomic, class, readonly) YYCache *sharedCache;
23+
1324
@end
1425

1526
@implementation SDYYCacheTests
1627

28+
+ (YYCache *)sharedCache {
29+
static dispatch_once_t onceToken;
30+
static YYCache *cache;
31+
dispatch_once(&onceToken, ^{
32+
cache = [YYCache cacheWithName:@"default"];
33+
});
34+
return cache;
35+
}
36+
37+
#pragma mark - YYMemoryCache & YYDiskCache
38+
- (void)testCustomMemoryCache {
39+
SDImageCacheConfig *config = [[SDImageCacheConfig alloc] init];
40+
config.memoryCacheClass = [YYMemoryCache class];
41+
NSString *nameSpace = @"YYMemoryCache";
42+
NSString *cacheDictionary = [self makeDiskCachePath:nameSpace];
43+
SDImageCache *cache = [[SDImageCache alloc] initWithNamespace:nameSpace diskCacheDirectory:cacheDictionary config:config];
44+
YYMemoryCache *memCache = cache.memCache;
45+
expect([memCache isKindOfClass:[YYMemoryCache class]]).to.beTruthy();
46+
}
47+
48+
- (void)testCustomDiskCache {
49+
SDImageCacheConfig *config = [[SDImageCacheConfig alloc] init];
50+
config.diskCacheClass = [YYDiskCache class];
51+
NSString *nameSpace = @"YYDiskCache";
52+
NSString *cacheDictionary = [self makeDiskCachePath:nameSpace];
53+
SDImageCache *cache = [[SDImageCache alloc] initWithNamespace:nameSpace diskCacheDirectory:cacheDictionary config:config];
54+
YYDiskCache *diskCache = cache.diskCache;
55+
expect([diskCache isKindOfClass:[YYDiskCache class]]).to.beTruthy();
56+
}
57+
58+
#pragma mark - YYCache
59+
60+
- (void)testCustomImageCache {
61+
SDWebImageManager *manager = [[SDWebImageManager alloc] initWithCache:SDYYCacheTests.sharedCache loader:SDWebImageDownloader.sharedDownloader];
62+
expect(manager.imageCache).to.equal(SDYYCacheTests.sharedCache);
63+
}
64+
65+
- (void)testYYCacheQueryOp {
66+
XCTestExpectation *expectation = [self expectationWithDescription:@"SDImageCache query op works"];
67+
[SDYYCacheTests.sharedCache setObject:[self testJPEGImage] forKey:kTestImageKeyJPEG];
68+
[SDYYCacheTests.sharedCache queryImageForKey:kTestImageKeyJPEG options:0 context:nil completion:^(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType) {
69+
expect(image).notTo.beNil();
70+
[expectation fulfill];
71+
}];
72+
[self waitForExpectationsWithCommonTimeout];
73+
}
74+
75+
- (void)testYYCacheStoreOp {
76+
XCTestExpectation *expectation = [self expectationWithDescription:@"SDImageCache store op works"];
77+
[SDYYCacheTests.sharedCache storeImage:[self testJPEGImage] imageData:nil forKey:kTestImageKeyJPEG cacheType:SDImageCacheTypeAll completion:^{
78+
UIImage *memoryImage = [SDYYCacheTests.sharedCache.memoryCache objectForKey:kTestImageKeyJPEG];
79+
expect(memoryImage).notTo.beNil();
80+
NSData *diskData = (NSData *)[SDYYCacheTests.sharedCache.diskCache objectForKey:kTestImageKeyJPEG];
81+
expect(diskData).notTo.beNil();
82+
[expectation fulfill];
83+
}];
84+
[self waitForExpectationsWithCommonTimeout];
85+
}
86+
87+
- (void)testYYCacheRemoveOp {
88+
XCTestExpectation *expectation = [self expectationWithDescription:@"SDImageCache remove op works"];
89+
[SDYYCacheTests.sharedCache removeImageForKey:kTestImageKeyJPEG cacheType:SDImageCacheTypeDisk completion:^{
90+
UIImage *memoryImage = [SDYYCacheTests.sharedCache.memoryCache objectForKey:kTestImageKeyJPEG];
91+
expect(memoryImage).notTo.beNil();
92+
NSData *diskData = (NSData *)[SDYYCacheTests.sharedCache.diskCache objectForKey:kTestImageKeyJPEG];
93+
expect(diskData).to.beNil();
94+
[expectation fulfill];
95+
}];
96+
[self waitForExpectationsWithCommonTimeout];
97+
}
98+
99+
- (void)testYYCacheContainsOp {
100+
XCTestExpectation *expectation = [self expectationWithDescription:@"SDImageCache contains op works"];
101+
[SDYYCacheTests.sharedCache setObject:[self testPNGImage] forKey:kTestImageKeyPNG];
102+
[SDYYCacheTests.sharedCache containsImageForKey:kTestImageKeyPNG cacheType:SDImageCacheTypeAll completion:^(SDImageCacheType containsCacheType) {
103+
expect(containsCacheType).equal(SDImageCacheTypeMemory);
104+
[expectation fulfill];
105+
}];
106+
[self waitForExpectationsWithCommonTimeout];
107+
}
108+
109+
- (void)testYYCacheClearOp {
110+
XCTestExpectation *expectation = [self expectationWithDescription:@"SDImageCache clear op works"];
111+
[SDYYCacheTests.sharedCache clearWithCacheType:SDImageCacheTypeAll completion:^{
112+
UIImage *memoryImage = [SDYYCacheTests.sharedCache.memoryCache objectForKey:kTestImageKeyJPEG];
113+
expect(memoryImage).to.beNil();
114+
NSData *diskData = (NSData *)[SDYYCacheTests.sharedCache.diskCache objectForKey:kTestImageKeyJPEG];
115+
expect(diskData).to.beNil();
116+
[expectation fulfill];
117+
}];
118+
[self waitForExpectationsWithCommonTimeout];
119+
}
120+
121+
#pragma mark Helper methods
122+
123+
- (UIImage *)testJPEGImage {
124+
static UIImage *reusableImage = nil;
125+
if (!reusableImage) {
126+
reusableImage = [[UIImage alloc] initWithContentsOfFile:[self testJPEGPath]];
127+
}
128+
return reusableImage;
129+
}
130+
131+
- (UIImage *)testPNGImage {
132+
static UIImage *reusableImage = nil;
133+
if (!reusableImage) {
134+
reusableImage = [[UIImage alloc] initWithContentsOfFile:[self testPNGPath]];
135+
}
136+
return reusableImage;
137+
}
138+
139+
- (NSString *)testJPEGPath {
140+
NSBundle *testBundle = [NSBundle bundleForClass:[self class]];
141+
return [testBundle pathForResource:@"TestImage" ofType:@"jpg"];
142+
}
143+
144+
- (NSString *)testPNGPath {
145+
NSBundle *testBundle = [NSBundle bundleForClass:[self class]];
146+
return [testBundle pathForResource:@"TestImage" ofType:@"png"];
147+
}
148+
149+
- (nullable NSString *)makeDiskCachePath:(nonnull NSString*)fullNamespace {
150+
NSArray<NSString *> *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
151+
return [paths[0] stringByAppendingPathComponent:fullNamespace];
152+
}
153+
17154
@end

Example/Tests/SDYYImageTests.m

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/*
2-
* This file is part of the SDWebImage package.
3-
* (c) Olivier Poitrey <[email protected]>
4-
* (c) Matt Galloway
2+
* This file is part of the SDWebImageYYPlugin package.
53
*
64
* For the full copyright and license information, please view the LICENSE
75
* file that was distributed with this source code.
@@ -61,6 +59,22 @@ - (void)testYYImageWorksForSDAnimatedImageView {
6159
expect(imageView.isAnimating).to.beTruthy(); // animating
6260
}
6361

62+
- (void)testSDImageYYCoderPNGWorks {
63+
NSURL *staticWebPURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImage" withExtension:@"png"];
64+
[self verifyCoder:[SDImageYYCoder sharedCoder]
65+
withLocalImageURL:staticWebPURL
66+
supportsEncoding:YES
67+
isAnimatedImage:NO];
68+
}
69+
70+
- (void)testSDImageYYCoderJPEGWorks {
71+
NSURL *staticWebPURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImage" withExtension:@"jpg"];
72+
[self verifyCoder:[SDImageYYCoder sharedCoder]
73+
withLocalImageURL:staticWebPURL
74+
supportsEncoding:YES
75+
isAnimatedImage:NO];
76+
}
77+
6478
- (void)testSDImageYYCoderStaticWebPCoderWorks {
6579
NSURL *staticWebPURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImageStatic" withExtension:@"webp"];
6680
[self verifyCoder:[SDImageYYCoder sharedCoder]

0 commit comments

Comments
 (0)