Janic Duplessis 1d37dd063c Native Animated - Add tests on iOS
Summary:
Adds unit tests to the Native Animated implementation on iOS. This pretty much mirrors the tests we currently have on Android.

It also fixes 2 bugs I've found when adding the tests and pass the current time in `stepAnimation` instead of using `CACurrentMediaTime` to make testing easier.

- `stopListeningToAnimatedNodeValue` did not actually work at all, it should set the listener to nil.
- The finished value in the animation end callback was always true, this simplifies the `RCTAnimationDriver` interface to get rid of `removeAnimation` and fixes the end callback value.

**Test plan**
- Run the tests
- Make sure the UIExplorer example still works
Closes https://github.com/facebook/react-native/pull/13068

Differential Revision: D4786701

Pulled By: javache

fbshipit-source-id: a4f07e6eec1f363ca47b6f27984041793c915bfc
2017-03-28 09:16:58 -07:00

123 lines
4.4 KiB
Objective-C

/**
* The examples provided by Facebook are for non-commercial testing and
* evaluation purposes only.
*
* Facebook reserves all rights not expressly granted.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#import <XCTest/XCTest.h>
#import <RCTAnimation/RCTAnimationUtils.h>
@interface RCTAnimationUtilsTests : XCTestCase
@end
static CGFloat RCTSimpleInterpolation(CGFloat value, NSArray<NSNumber *> *inputRange, NSArray<NSNumber *> *outputRange) {
return RCTInterpolateValueInRange(value,
inputRange,
outputRange,
EXTRAPOLATE_TYPE_EXTEND,
EXTRAPOLATE_TYPE_EXTEND);
}
@implementation RCTAnimationUtilsTests
// RCTInterpolateValueInRange
- (void)testSimpleOneToOneMapping
{
NSArray<NSNumber *> *input = @[@0, @1];
NSArray<NSNumber *> *output = @[@0, @1];
XCTAssertEqual(RCTSimpleInterpolation(0, input, output), 0);
XCTAssertEqual(RCTSimpleInterpolation(0.5, input, output), 0.5);
XCTAssertEqual(RCTSimpleInterpolation(0.8, input, output), 0.8);
XCTAssertEqual(RCTSimpleInterpolation(1, input, output), 1);
}
- (void)testWiderOutputRange
{
NSArray<NSNumber *> *input = @[@0, @1];
NSArray<NSNumber *> *output = @[@100, @200];
XCTAssertEqual(RCTSimpleInterpolation(0, input, output), 100);
XCTAssertEqual(RCTSimpleInterpolation(0.5, input, output), 150);
XCTAssertEqual(RCTSimpleInterpolation(0.8, input, output), 180);
XCTAssertEqual(RCTSimpleInterpolation(1, input, output), 200);
}
- (void)testWiderInputRange
{
NSArray<NSNumber *> *input = @[@2000, @3000];
NSArray<NSNumber *> *output = @[@1, @2];
XCTAssertEqual(RCTSimpleInterpolation(2000, input, output), 1);
XCTAssertEqual(RCTSimpleInterpolation(2250, input, output), 1.25);
XCTAssertEqual(RCTSimpleInterpolation(2800, input, output), 1.8);
XCTAssertEqual(RCTSimpleInterpolation(3000, input, output), 2);
}
- (void)testManySegments
{
NSArray<NSNumber *> *input = @[@-1, @1, @5];
NSArray<NSNumber *> *output = @[@0, @10, @20];
XCTAssertEqual(RCTSimpleInterpolation(-1, input, output), 0);
XCTAssertEqual(RCTSimpleInterpolation(0, input, output), 5);
XCTAssertEqual(RCTSimpleInterpolation(1, input, output), 10);
XCTAssertEqual(RCTSimpleInterpolation(2, input, output), 12.5);
XCTAssertEqual(RCTSimpleInterpolation(5, input, output), 20);
}
- (void)testExtendExtrapolate
{
NSArray<NSNumber *> *input = @[@10, @20];
NSArray<NSNumber *> *output = @[@0, @1];
XCTAssertEqual(RCTSimpleInterpolation(30, input, output), 2);
XCTAssertEqual(RCTSimpleInterpolation(5, input, output), -0.5);
}
- (void)testClampExtrapolate
{
NSArray<NSNumber *> *input = @[@10, @20];
NSArray<NSNumber *> *output = @[@0, @1];
CGFloat value;
value = RCTInterpolateValueInRange(30,
input,
output,
EXTRAPOLATE_TYPE_CLAMP,
EXTRAPOLATE_TYPE_CLAMP);
XCTAssertEqual(value, 1);
value = RCTInterpolateValueInRange(5,
input,
output,
EXTRAPOLATE_TYPE_CLAMP,
EXTRAPOLATE_TYPE_CLAMP);
XCTAssertEqual(value, 0);
}
- (void)testIdentityExtrapolate
{
NSArray<NSNumber *> *input = @[@10, @20];
NSArray<NSNumber *> *output = @[@0, @1];
CGFloat value;
value = RCTInterpolateValueInRange(30,
input,
output,
EXTRAPOLATE_TYPE_IDENTITY,
EXTRAPOLATE_TYPE_IDENTITY);
XCTAssertEqual(value, 30);
value = RCTInterpolateValueInRange(5,
input,
output,
EXTRAPOLATE_TYPE_IDENTITY,
EXTRAPOLATE_TYPE_IDENTITY);
XCTAssertEqual(value, 5);
}
@end