|
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 | + */ |
8 | 7 |
|
9 | 8 | #import "SDTestCase.h"
|
10 | 9 |
|
| 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 | + |
11 | 20 | @interface SDYYCacheTests : SDTestCase
|
12 | 21 |
|
| 22 | +@property (nonatomic, class, readonly) YYCache *sharedCache; |
| 23 | + |
13 | 24 | @end
|
14 | 25 |
|
15 | 26 | @implementation SDYYCacheTests
|
16 | 27 |
|
| 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 | + |
17 | 154 | @end
|
0 commit comments