2015-05-22 14:17:08 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2015-05-22 14:17:08 +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-05-22 14:17:08 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <CoreGraphics/CoreGraphics.h>
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
#import <UIKit/UIView.h>
|
|
|
|
#import <XCTest/XCTest.h>
|
2016-11-24 17:44:51 +00:00
|
|
|
|
|
|
|
#import <RCTImage/RCTImageUtils.h>
|
2015-05-22 14:17:08 +00:00
|
|
|
|
|
|
|
#define RCTAssertEqualPoints(a, b) { \
|
|
|
|
XCTAssertEqual(a.x, b.x); \
|
|
|
|
XCTAssertEqual(a.y, b.y); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define RCTAssertEqualSizes(a, b) { \
|
|
|
|
XCTAssertEqual(a.width, b.width); \
|
|
|
|
XCTAssertEqual(a.height, b.height); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define RCTAssertEqualRects(a, b) { \
|
|
|
|
RCTAssertEqualPoints(a.origin, b.origin); \
|
|
|
|
RCTAssertEqualSizes(a.size, b.size); \
|
|
|
|
}
|
|
|
|
|
2015-08-12 14:07:24 +00:00
|
|
|
@interface RCTImageUtilTests : XCTestCase
|
2015-05-22 14:17:08 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-08-12 14:07:24 +00:00
|
|
|
@implementation RCTImageUtilTests
|
2015-05-22 14:17:08 +00:00
|
|
|
|
|
|
|
- (void)testLandscapeSourceLandscapeTarget
|
|
|
|
{
|
|
|
|
CGSize content = {1000, 100};
|
|
|
|
CGSize target = {100, 20};
|
|
|
|
|
|
|
|
{
|
|
|
|
CGRect expected = {CGPointZero, {100, 20}};
|
2016-01-20 19:03:22 +00:00
|
|
|
CGRect result = RCTTargetRect(content, target, 1, RCTResizeModeStretch);
|
2015-05-22 14:17:08 +00:00
|
|
|
RCTAssertEqualRects(expected, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-01-20 19:03:22 +00:00
|
|
|
CGRect expected = {{0, 5}, {100, 10}};
|
|
|
|
CGRect result = RCTTargetRect(content, target, 1, RCTResizeModeContain);
|
2015-05-22 14:17:08 +00:00
|
|
|
RCTAssertEqualRects(expected, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
CGRect expected = {{-50, 0}, {200, 20}};
|
2016-01-20 19:03:22 +00:00
|
|
|
CGRect result = RCTTargetRect(content, target, 1, RCTResizeModeCover);
|
2015-05-22 14:17:08 +00:00
|
|
|
RCTAssertEqualRects(expected, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testPortraitSourceLandscapeTarget
|
|
|
|
{
|
|
|
|
CGSize content = {10, 100};
|
|
|
|
CGSize target = {100, 20};
|
|
|
|
|
|
|
|
{
|
2015-08-12 14:07:24 +00:00
|
|
|
CGRect expected = {CGPointZero, {100, 20}};
|
2016-01-20 19:03:22 +00:00
|
|
|
CGRect result = RCTTargetRect(content, target, 1, RCTResizeModeStretch);
|
2015-05-22 14:17:08 +00:00
|
|
|
RCTAssertEqualRects(expected, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-01-20 19:03:22 +00:00
|
|
|
CGRect expected = {{49, 0}, {2, 20}};
|
|
|
|
CGRect result = RCTTargetRect(content, target, 1, RCTResizeModeContain);
|
2015-05-22 14:17:08 +00:00
|
|
|
RCTAssertEqualRects(expected, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2015-08-12 14:07:24 +00:00
|
|
|
CGRect expected = {{0, -490}, {100, 1000}};
|
2016-01-20 19:03:22 +00:00
|
|
|
CGRect result = RCTTargetRect(content, target, 1, RCTResizeModeCover);
|
2015-05-22 14:17:08 +00:00
|
|
|
RCTAssertEqualRects(expected, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testPortraitSourcePortraitTarget
|
|
|
|
{
|
|
|
|
CGSize content = {10, 100};
|
|
|
|
CGSize target = {20, 50};
|
|
|
|
|
|
|
|
{
|
2015-08-12 14:07:24 +00:00
|
|
|
CGRect expected = {CGPointZero, {20, 50}};
|
2016-01-20 19:03:22 +00:00
|
|
|
CGRect result = RCTTargetRect(content, target, 1, RCTResizeModeStretch);
|
2015-05-22 14:17:08 +00:00
|
|
|
RCTAssertEqualRects(expected, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-01-20 19:03:22 +00:00
|
|
|
CGRect expected = {{7,0}, {5, 50}};
|
|
|
|
CGRect result = RCTTargetRect(content, target, 1, RCTResizeModeContain);
|
2015-05-22 14:17:08 +00:00
|
|
|
RCTAssertEqualRects(expected, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2015-08-12 14:07:24 +00:00
|
|
|
CGRect expected = {{0, -75}, {20, 200}};
|
2016-01-20 19:03:22 +00:00
|
|
|
CGRect result = RCTTargetRect(content, target, 2, RCTResizeModeCover);
|
2015-07-21 12:40:06 +00:00
|
|
|
RCTAssertEqualRects(expected, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testRounding
|
|
|
|
{
|
|
|
|
CGSize content = {10, 100};
|
|
|
|
CGSize target = {20, 50};
|
|
|
|
|
|
|
|
{
|
2015-08-12 14:07:24 +00:00
|
|
|
CGRect expected = {{0, -75}, {20, 200}};
|
2016-01-20 19:03:22 +00:00
|
|
|
CGRect result = RCTTargetRect(content, target, 1, RCTResizeModeCover);
|
2015-05-22 14:17:08 +00:00
|
|
|
RCTAssertEqualRects(expected, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testScaling
|
|
|
|
{
|
|
|
|
CGSize content = {2, 2};
|
|
|
|
CGSize target = {3, 3};
|
|
|
|
|
|
|
|
CGRect expected = {CGPointZero, {3, 3}};
|
2016-01-20 19:03:22 +00:00
|
|
|
CGRect result = RCTTargetRect(content, target, 1, RCTResizeModeStretch);
|
2015-05-22 14:17:08 +00:00
|
|
|
RCTAssertEqualRects(expected, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|