Introducing RCTBlockingImageManager, an image manager for tests in Fabric

Summary:
Changelog: [Internal]

This allows to block queue until image is fetched.

In paper, the photo of dog will not appear unless the network request is "fast enough".

{F228670428}

Reviewed By: shergin

Differential Revision: D19907906

fbshipit-source-id: 98d68abf1255388f9b97fb9da367d8f583a4220d
This commit is contained in:
Samuel Susla 2020-02-18 16:48:49 -08:00 коммит произвёл Facebook Github Bot
Родитель b53aeab10c
Коммит aa0a50c518
5 изменённых файлов: 150 добавлений и 5 удалений

Просмотреть файл

@ -8,9 +8,11 @@
#include "ImageManager.h"
#import <React/RCTImageLoaderWithAttributionProtocol.h>
#import <React/RCTUtils.h>
#import <react/utils/ManagedObjectWrapper.h>
#import "RCTImageManager.h"
#import "RCTSyncImageManager.h"
namespace facebook {
namespace react {
@ -20,7 +22,11 @@ ImageManager::ImageManager(ContextContainer::Shared const &contextContainer)
id<RCTImageLoaderWithAttributionProtocol> imageLoader =
(id<RCTImageLoaderWithAttributionProtocol>)unwrapManagedObject(
contextContainer->at<std::shared_ptr<void>>("RCTImageLoader"));
self_ = (__bridge_retained void *)[[RCTImageManager alloc] initWithImageLoader:imageLoader];
if (RCTRunningInTestEnvironment()) {
self_ = (__bridge_retained void *)[[RCTSyncImageManager alloc] initWithImageLoader:imageLoader];
} else {
self_ = (__bridge_retained void *)[[RCTImageManager alloc] initWithImageLoader:imageLoader];
}
}
ImageManager::~ImageManager()

Просмотреть файл

@ -7,9 +7,7 @@
#import <UIKit/UIKit.h>
#import <react/core/ReactPrimitives.h>
#import <react/imagemanager/ImageRequest.h>
#import <react/imagemanager/primitives.h>
#import <RCTImageManagerProtocol.h>
NS_ASSUME_NONNULL_BEGIN
@ -18,7 +16,7 @@ NS_ASSUME_NONNULL_BEGIN
/**
* iOS-specific ImageManager.
*/
@interface RCTImageManager : NSObject
@interface RCTImageManager : NSObject <RCTImageManagerProtocol>
- (instancetype)initWithImageLoader:(id<RCTImageLoaderWithAttributionProtocol>)imageLoader;

Просмотреть файл

@ -0,0 +1,16 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <Foundation/Foundation.h>
#import <react/core/ReactPrimitives.h>
#import <react/imagemanager/ImageRequest.h>
@protocol RCTImageManagerProtocol <NSObject>
- (facebook::react::ImageRequest)requestImage:(facebook::react::ImageSource)imageSource
surfaceId:(facebook::react::SurfaceId)surfaceId;
@end

Просмотреть файл

@ -0,0 +1,28 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <UIKit/UIKit.h>
#import <RCTImageManagerProtocol.h>
NS_ASSUME_NONNULL_BEGIN
@protocol RCTImageLoaderWithAttributionProtocol;
/**
* iOS-specific ImageManager.
*/
@interface RCTSyncImageManager : NSObject <RCTImageManagerProtocol>
- (instancetype)initWithImageLoader:(id<RCTImageLoaderWithAttributionProtocol>)imageLoader;
- (facebook::react::ImageRequest)requestImage:(facebook::react::ImageSource)imageSource
surfaceId:(facebook::react::SurfaceId)surfaceId;
@end
NS_ASSUME_NONNULL_END

Просмотреть файл

@ -0,0 +1,97 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTSyncImageManager.h"
#import <react/utils/ManagedObjectWrapper.h>
#import <react/utils/SharedFunction.h>
#import <React/RCTAssert.h>
#import <React/RCTImageLoaderWithAttributionProtocol.h>
#import <React/RCTLog.h>
#import <react/imagemanager/ImageResponse.h>
#import <react/imagemanager/ImageResponseObserver.h>
#import "RCTImagePrimitivesConversions.h"
using namespace facebook::react;
@implementation RCTSyncImageManager {
id<RCTImageLoaderWithAttributionProtocol> _imageLoader;
}
- (instancetype)initWithImageLoader:(id<RCTImageLoaderWithAttributionProtocol>)imageLoader
{
if (self = [super init]) {
RCTAssert(RCTRunningInTestEnvironment(), @"This class is only meant to be used in test environment");
_imageLoader = imageLoader;
}
return self;
}
- (ImageRequest)requestImage:(ImageSource)imageSource surfaceId:(SurfaceId)surfaceId
{
auto imageRequest = ImageRequest(imageSource, nullptr);
auto weakObserverCoordinator =
(std::weak_ptr<const ImageResponseObserverCoordinator>)imageRequest.getSharedObserverCoordinator();
auto sharedCancelationFunction = SharedFunction<>();
imageRequest.setCancelationFunction(sharedCancelationFunction);
dispatch_group_t imageWaitGroup = dispatch_group_create();
dispatch_group_enter(imageWaitGroup);
NSURLRequest *request = NSURLRequestFromImageSource(imageSource);
auto completionBlock = ^(NSError *error, UIImage *image) {
auto observerCoordinator = weakObserverCoordinator.lock();
if (!observerCoordinator) {
return;
}
if (image && !error) {
observerCoordinator->nativeImageResponseComplete(ImageResponse(wrapManagedObject(image)));
} else {
observerCoordinator->nativeImageResponseFailed();
}
dispatch_group_leave(imageWaitGroup);
};
auto progressBlock = ^(int64_t progress, int64_t total) {
auto observerCoordinator = weakObserverCoordinator.lock();
if (!observerCoordinator) {
return;
}
observerCoordinator->nativeImageResponseProgress(progress / (float)total);
};
RCTImageURLLoaderRequest *loaderRequest =
[self->_imageLoader loadImageWithURLRequest:request
size:CGSizeMake(imageSource.size.width, imageSource.size.height)
scale:imageSource.scale
clipped:YES
resizeMode:RCTResizeModeStretch
attribution:{
.surfaceId = surfaceId,
}
progressBlock:progressBlock
partialLoadBlock:nil
completionBlock:completionBlock];
RCTImageLoaderCancellationBlock cancelationBlock = loaderRequest.cancellationBlock;
sharedCancelationFunction.assign([cancelationBlock]() { cancelationBlock(); });
auto result = dispatch_group_wait(imageWaitGroup, dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC));
if (result != 0) {
RCTLogError(@"Getting an image timed out");
}
return imageRequest;
}
@end