Add the test case for SDFileAttributeHelper, fix the issue that associated object is lost

This commit is contained in:
DreamPiggy 2019-11-28 19:49:51 +08:00
parent 9aa4ac1ca7
commit 5f2a9695d8
4 changed files with 33 additions and 1 deletions

View File

@ -9,6 +9,7 @@
#import "SDWebImageDefine.h"
#import "UIImage+Metadata.h"
#import "NSImage+Compatibility.h"
#import "UIImage+ExtendedCacheData.h"
#pragma mark - Image scale
@ -112,6 +113,7 @@ inline UIImage * _Nullable SDScaledImageForScaleFactor(CGFloat scale, UIImage *
}
scaledImage.sd_isIncremental = image.sd_isIncremental;
scaledImage.sd_imageFormat = image.sd_imageFormat;
scaledImage.sd_extendedObject = image.sd_extendedObject;
return scaledImage;
}

View File

@ -343,6 +343,7 @@ static id<SDImageLoader> _defaultImageLoader;
cacheData = (imageWasTransformed ? nil : downloadedData);
}
// keep the original image format and extended data
transformedImage.sd_isIncremental = downloadedImage.sd_isIncremental;
transformedImage.sd_imageFormat = downloadedImage.sd_imageFormat;
transformedImage.sd_extendedObject = downloadedImage.sd_extendedObject;
[self.imageCache storeImage:transformedImage imageData:cacheData forKey:cacheKey cacheType:storeCacheType completion:nil];

View File

@ -16,7 +16,7 @@
Read and Write the extended object and bind it to the image. Which can hold some extra metadata like Image's scale factor, URL rich link, date, etc.
The extended object should conforms to NSCoding, which we use `NSKeyedArchiver` and `NSKeyedUnarchiver` to archive it to data, and write to disk cache.
@note The disk cache preserve both of the data and extended data with the same cache key. For manual query, use the `SDDiskCache` protocol method `extendedDataForKey:` instead.
@note You can specify arbitrary object conforms to NSCoding (NSObject protocol here is used to support object like `dispatch_data_t`, which is not NSObject subclass). If you load image from disk cache, you should check the extended object class to avoid corrupted data.
@note You can specify arbitrary object conforms to NSCoding (NSObject protocol here is used to support object using `NS_ROOT_CLASS`, which is not NSObject subclass). If you load image from disk cache, you should check the extended object class to avoid corrupted data.
@warning This object don't need to implements NSSecureCoding (but it's recommended), because we allows arbitrary class.
*/
@property (nonatomic, strong, nullable) id<NSObject, NSCoding> sd_extendedObject;

View File

@ -11,6 +11,7 @@
#import "SDWeakProxy.h"
#import "SDDisplayLink.h"
#import "SDInternalMacros.h"
#import "SDFileAttributeHelper.h"
@interface SDUtilsTests : SDTestCase
@ -74,6 +75,34 @@
expect(duration).beLessThan(0.02);
}
- (void)testSDFileAttributeHelper {
NSData *fileData = [@"File Data" dataUsingEncoding:NSUTF8StringEncoding];
NSData *extendedData = [@"Extended Data" dataUsingEncoding:NSUTF8StringEncoding];
NSString *filePath = @"/tmp/file.dat";
[NSFileManager.defaultManager removeItemAtPath:filePath error:nil];
[fileData writeToFile:filePath atomically:YES];
BOOL exist = [NSFileManager.defaultManager fileExistsAtPath:filePath];
expect(exist).beTruthy();
NSArray *names = [SDFileAttributeHelper extendedAttributeNamesAtPath:filePath traverseLink:NO error:nil];
expect(names.count).equal(0);
NSString *attr = @"com.com.hackemist.test";
[SDFileAttributeHelper setExtendedAttribute:@"com.com.hackemist.test" value:extendedData atPath:filePath traverseLink:NO overwrite:YES error:nil];
BOOL hasAttr =[SDFileAttributeHelper hasExtendedAttribute:attr atPath:filePath traverseLink:NO error:nil];
expect(hasAttr).beTruthy();
NSData *queriedData = [SDFileAttributeHelper extendedAttribute:attr atPath:filePath traverseLink:NO error:nil];
expect(extendedData).equal(queriedData);
BOOL removed = [SDFileAttributeHelper removeExtendedAttribute:attr atPath:filePath traverseLink:NO error:nil];
expect(removed).beTruthy();
hasAttr = [SDFileAttributeHelper hasExtendedAttribute:attr atPath:filePath traverseLink:NO error:nil];
expect(hasAttr).beFalsy();
}
- (void)testSDScaledImageForKey {
// Test nil
expect(SDScaledImageForKey(nil, nil)).beNil();