2015-09-02 15:25:10 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2015-09-02 15:25:10 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-09-02 15:25:10 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <XCTest/XCTest.h>
|
|
|
|
|
2016-11-24 17:44:51 +00:00
|
|
|
#import <React/RCTBridge.h>
|
|
|
|
#import <React/RCTImageLoader.h>
|
|
|
|
|
2015-09-02 15:25:10 +00:00
|
|
|
#import "RCTImageLoaderHelpers.h"
|
|
|
|
|
|
|
|
unsigned char blackGIF[] = {
|
|
|
|
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00, 0x80, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x44, 0x01, 0x00, 0x3b
|
|
|
|
};
|
|
|
|
|
|
|
|
RCTDefineImageURLLoader(RCTImageLoaderTestsURLLoader1)
|
|
|
|
RCTDefineImageURLLoader(RCTImageLoaderTestsURLLoader2)
|
|
|
|
RCTDefineImageDecoder(RCTImageLoaderTestsDecoder1)
|
|
|
|
RCTDefineImageDecoder(RCTImageLoaderTestsDecoder2)
|
|
|
|
|
|
|
|
@interface RCTImageLoaderTests : XCTestCase
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2016-07-11 20:16:09 +00:00
|
|
|
@implementation RCTImageLoaderTests {
|
|
|
|
NSURL *_bundleURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setUp
|
|
|
|
{
|
|
|
|
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
|
2017-05-06 03:50:47 +00:00
|
|
|
_bundleURL = [bundle URLForResource:@"RNTesterUnitTestsBundle" withExtension:@"js"];
|
2016-07-11 20:16:09 +00:00
|
|
|
}
|
2015-09-02 15:25:10 +00:00
|
|
|
|
|
|
|
- (void)testImageLoading
|
|
|
|
{
|
|
|
|
UIImage *image = [UIImage new];
|
|
|
|
|
|
|
|
id<RCTImageURLLoader> loader = [[RCTImageLoaderTestsURLLoader1 alloc] initWithPriority:1.0 canLoadImageURLHandler:^BOOL(__unused NSURL *requestURL) {
|
|
|
|
return YES;
|
2016-01-20 19:03:22 +00:00
|
|
|
} loadImageURLHandler:^RCTImageLoaderCancellationBlock(__unused NSURL *imageURL, __unused CGSize size, __unused CGFloat scale, __unused RCTResizeMode resizeMode, RCTImageLoaderProgressBlock progressHandler, RCTImageLoaderCompletionBlock completionHandler) {
|
2015-09-02 15:25:10 +00:00
|
|
|
progressHandler(1, 1);
|
|
|
|
completionHandler(nil, image);
|
|
|
|
return nil;
|
|
|
|
}];
|
|
|
|
|
2016-07-11 20:16:09 +00:00
|
|
|
NS_VALID_UNTIL_END_OF_SCOPE RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:_bundleURL moduleProvider:^{ return @[loader]; } launchOptions:nil];
|
2015-09-02 15:25:10 +00:00
|
|
|
|
2017-12-07 16:28:35 +00:00
|
|
|
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://facebook.github.io/react-native/img/opengraph.png"]];
|
2016-06-01 17:32:20 +00:00
|
|
|
[bridge.imageLoader loadImageWithURLRequest:urlRequest size:CGSizeMake(100, 100) scale:1.0 clipped:YES resizeMode:RCTResizeModeContain progressBlock:^(int64_t progress, int64_t total) {
|
2015-09-02 15:25:10 +00:00
|
|
|
XCTAssertEqual(progress, 1);
|
|
|
|
XCTAssertEqual(total, 1);
|
2016-09-21 19:11:19 +00:00
|
|
|
} partialLoadBlock:nil completionBlock:^(NSError *loadError, id loadedImage) {
|
2015-09-02 15:25:10 +00:00
|
|
|
XCTAssertEqualObjects(loadedImage, image);
|
|
|
|
XCTAssertNil(loadError);
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testImageLoaderUsesImageURLLoaderWithHighestPriority
|
|
|
|
{
|
|
|
|
UIImage *image = [UIImage new];
|
|
|
|
|
|
|
|
id<RCTImageURLLoader> loader1 = [[RCTImageLoaderTestsURLLoader1 alloc] initWithPriority:1.0 canLoadImageURLHandler:^BOOL(__unused NSURL *requestURL) {
|
|
|
|
return YES;
|
2016-01-20 19:03:22 +00:00
|
|
|
} loadImageURLHandler:^RCTImageLoaderCancellationBlock(__unused NSURL *imageURL, __unused CGSize size, __unused CGFloat scale, __unused RCTResizeMode resizeMode, RCTImageLoaderProgressBlock progressHandler, RCTImageLoaderCompletionBlock completionHandler) {
|
2015-09-02 15:25:10 +00:00
|
|
|
progressHandler(1, 1);
|
|
|
|
completionHandler(nil, image);
|
|
|
|
return nil;
|
|
|
|
}];
|
|
|
|
|
|
|
|
id<RCTImageURLLoader> loader2 = [[RCTImageLoaderTestsURLLoader2 alloc] initWithPriority:0.5 canLoadImageURLHandler:^BOOL(__unused NSURL *requestURL) {
|
|
|
|
return YES;
|
2016-01-20 19:03:22 +00:00
|
|
|
} loadImageURLHandler:^RCTImageLoaderCancellationBlock(__unused NSURL *imageURL, __unused CGSize size, __unused CGFloat scale, __unused RCTResizeMode resizeMode, __unused RCTImageLoaderProgressBlock progressHandler, __unused RCTImageLoaderCompletionBlock completionHandler) {
|
2015-09-02 15:25:10 +00:00
|
|
|
XCTFail(@"Should not have used loader2");
|
|
|
|
return nil;
|
|
|
|
}];
|
|
|
|
|
2016-07-11 20:16:09 +00:00
|
|
|
NS_VALID_UNTIL_END_OF_SCOPE RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:_bundleURL moduleProvider:^{ return @[loader1, loader2]; } launchOptions:nil];
|
2015-09-02 15:25:10 +00:00
|
|
|
|
2017-12-07 16:28:35 +00:00
|
|
|
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://facebook.github.io/react-native/img/opengraph.png"]];
|
2016-06-01 17:32:20 +00:00
|
|
|
[bridge.imageLoader loadImageWithURLRequest:urlRequest size:CGSizeMake(100, 100) scale:1.0 clipped:YES resizeMode:RCTResizeModeContain progressBlock:^(int64_t progress, int64_t total) {
|
2015-09-02 15:25:10 +00:00
|
|
|
XCTAssertEqual(progress, 1);
|
|
|
|
XCTAssertEqual(total, 1);
|
2016-09-21 19:11:19 +00:00
|
|
|
} partialLoadBlock:nil completionBlock:^(NSError *loadError, id loadedImage) {
|
2015-09-02 15:25:10 +00:00
|
|
|
XCTAssertEqualObjects(loadedImage, image);
|
|
|
|
XCTAssertNil(loadError);
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testImageDecoding
|
|
|
|
{
|
|
|
|
NSData *data = [NSData dataWithBytesNoCopy:blackGIF length:sizeof(blackGIF) freeWhenDone:NO];
|
|
|
|
UIImage *image = [[UIImage alloc] initWithData:data];
|
|
|
|
|
2015-10-19 16:04:54 +00:00
|
|
|
id<RCTImageDataDecoder> decoder = [[RCTImageLoaderTestsDecoder1 alloc] initWithPriority:1.0 canDecodeImageDataHandler:^BOOL(__unused NSData *imageData) {
|
2015-09-02 15:25:10 +00:00
|
|
|
return YES;
|
2016-01-20 19:03:22 +00:00
|
|
|
} decodeImageDataHandler:^RCTImageLoaderCancellationBlock(NSData *imageData, __unused CGSize size, __unused CGFloat scale, __unused RCTResizeMode resizeMode, RCTImageLoaderCompletionBlock completionHandler) {
|
2015-09-02 15:25:10 +00:00
|
|
|
XCTAssertEqualObjects(imageData, data);
|
|
|
|
completionHandler(nil, image);
|
|
|
|
return nil;
|
|
|
|
}];
|
|
|
|
|
2016-07-11 20:16:09 +00:00
|
|
|
NS_VALID_UNTIL_END_OF_SCOPE RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:_bundleURL moduleProvider:^{ return @[decoder]; } launchOptions:nil];
|
2015-09-02 15:25:10 +00:00
|
|
|
|
2016-06-01 17:32:20 +00:00
|
|
|
RCTImageLoaderCancellationBlock cancelBlock = [bridge.imageLoader decodeImageData:data size:CGSizeMake(1, 1) scale:1.0 clipped:NO resizeMode:RCTResizeModeStretch completionBlock:^(NSError *decodeError, id decodedImage) {
|
2015-09-02 15:25:10 +00:00
|
|
|
XCTAssertEqualObjects(decodedImage, image);
|
|
|
|
XCTAssertNil(decodeError);
|
|
|
|
}];
|
2016-05-23 15:27:14 +00:00
|
|
|
XCTAssertNotNil(cancelBlock);
|
2015-09-02 15:25:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testImageLoaderUsesImageDecoderWithHighestPriority
|
|
|
|
{
|
|
|
|
NSData *data = [NSData dataWithBytesNoCopy:blackGIF length:sizeof(blackGIF) freeWhenDone:NO];
|
|
|
|
UIImage *image = [[UIImage alloc] initWithData:data];
|
|
|
|
|
2015-10-19 16:04:54 +00:00
|
|
|
id<RCTImageDataDecoder> decoder1 = [[RCTImageLoaderTestsDecoder1 alloc] initWithPriority:1.0 canDecodeImageDataHandler:^BOOL(__unused NSData *imageData) {
|
2015-09-02 15:25:10 +00:00
|
|
|
return YES;
|
2016-01-20 19:03:22 +00:00
|
|
|
} decodeImageDataHandler:^RCTImageLoaderCancellationBlock(NSData *imageData, __unused CGSize size, __unused CGFloat scale, __unused RCTResizeMode resizeMode, RCTImageLoaderCompletionBlock completionHandler) {
|
2015-09-02 15:25:10 +00:00
|
|
|
XCTAssertEqualObjects(imageData, data);
|
|
|
|
completionHandler(nil, image);
|
|
|
|
return nil;
|
|
|
|
}];
|
|
|
|
|
2015-10-19 16:04:54 +00:00
|
|
|
id<RCTImageDataDecoder> decoder2 = [[RCTImageLoaderTestsDecoder2 alloc] initWithPriority:0.5 canDecodeImageDataHandler:^BOOL(__unused NSData *imageData) {
|
2015-09-02 15:25:10 +00:00
|
|
|
return YES;
|
2016-01-20 19:03:22 +00:00
|
|
|
} decodeImageDataHandler:^RCTImageLoaderCancellationBlock(__unused NSData *imageData, __unused CGSize size, __unused CGFloat scale, __unused RCTResizeMode resizeMode, __unused RCTImageLoaderCompletionBlock completionHandler) {
|
2015-09-02 15:25:10 +00:00
|
|
|
XCTFail(@"Should not have used decoder2");
|
|
|
|
return nil;
|
|
|
|
}];
|
|
|
|
|
2016-07-11 20:16:09 +00:00
|
|
|
NS_VALID_UNTIL_END_OF_SCOPE RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:_bundleURL moduleProvider:^{ return @[decoder1, decoder2]; } launchOptions:nil];
|
2015-09-02 15:25:10 +00:00
|
|
|
|
2016-06-01 17:32:20 +00:00
|
|
|
RCTImageLoaderCancellationBlock cancelBlock = [bridge.imageLoader decodeImageData:data size:CGSizeMake(1, 1) scale:1.0 clipped:NO resizeMode:RCTResizeModeStretch completionBlock:^(NSError *decodeError, id decodedImage) {
|
2015-09-02 15:25:10 +00:00
|
|
|
XCTAssertEqualObjects(decodedImage, image);
|
|
|
|
XCTAssertNil(decodeError);
|
|
|
|
}];
|
2016-05-23 15:27:14 +00:00
|
|
|
XCTAssertNotNil(cancelBlock);
|
2015-09-02 15:25:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|