Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
Fix crash in Legacy API for nil completion blocks (#110)
Browse files Browse the repository at this point in the history
When passed a `nil` completion block value, the "legacy" API would crash
by calling the block without performing a nil check.  Adding 2 tests to
verify that the animation works as expected with a `nil` completion
block.
  • Loading branch information
Robert Moore authored and ianegordon committed Jan 25, 2018
1 parent 05ad80b commit fd9710d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/MDMMotionAnimator.m
Expand Up @@ -261,7 +261,9 @@ - (void)animateWithTiming:(MDMMotionTiming)timing
completion:(void (^)(void))completion {
MDMAnimationTraits *traits = [[MDMAnimationTraits alloc] initWithMotionTiming:timing];
[self animateWithTraits:traits animations:animations completion:^(BOOL didComplete) {
completion();
if (completion) {
completion();
}
}];
}

Expand All @@ -284,8 +286,10 @@ - (void)animateWithTiming:(MDMMotionTiming)timing
layer:layer
keyPath:keyPath
completion:^(BOOL didComplete) {
completion();
}];
if (completion) {
completion();
}
}];
}

#pragma mark - Private
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/MotionAnimatorTests.m
Expand Up @@ -166,4 +166,47 @@ - (void)testSpringAnimationFloatValue {
XCTAssertTrue(didAddAnimation);
}

#pragma mark - Legacy API

- (void)testAnimationWithTimingNilCompletion {
// Given
MDMMotionAnimator *animator = [[MDMMotionAnimator alloc] init];

CALayer *layer = [[CALayer alloc] init];
MDMMotionTiming timing = (MDMMotionTiming) {
.duration = 0.250, .curve = MDMMotionCurveMakeBezier(0.42f, 0.00f, 0.58f, 1.00f)
};

// When
[animator animateWithTiming:timing
animations:^{
layer.opacity = 0.7;
}
completion:nil];

// Then
XCTAssertEqualWithAccuracy(layer.opacity, 0.7, 0.0001);
}

- (void)testAnimationWithTimingToLayerWithValuesKeyPathNilCompletion {
// Given
MDMMotionAnimator *animator = [[MDMMotionAnimator alloc] init];

CALayer *layer = [[CALayer alloc] init];
CALayer *anotherLayer = [[CALayer alloc] init];
MDMMotionTiming timing = (MDMMotionTiming) {
.duration = 0.250, .curve = MDMMotionCurveMakeBezier(0.42f, 0.00f, 0.58f, 1.00f)
};

// When
[animator animateWithTiming:timing
toLayer:anotherLayer
withValues:@[@(0), @(1)]
keyPath:@"opacity"
completion:nil];

// Then
XCTAssertEqualWithAccuracy(layer.opacity, 1, 0.0001);
}

@end

0 comments on commit fd9710d

Please sign in to comment.