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

Commit fd9710d

Browse files
Robert Mooreianegordon
Robert Moore
authored and
ianegordon
committed
Fix crash in Legacy API for nil completion blocks (#110)
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.
1 parent 05ad80b commit fd9710d

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/MDMMotionAnimator.m

+7-3
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,9 @@ - (void)animateWithTiming:(MDMMotionTiming)timing
261261
completion:(void (^)(void))completion {
262262
MDMAnimationTraits *traits = [[MDMAnimationTraits alloc] initWithMotionTiming:timing];
263263
[self animateWithTraits:traits animations:animations completion:^(BOOL didComplete) {
264-
completion();
264+
if (completion) {
265+
completion();
266+
}
265267
}];
266268
}
267269

@@ -284,8 +286,10 @@ - (void)animateWithTiming:(MDMMotionTiming)timing
284286
layer:layer
285287
keyPath:keyPath
286288
completion:^(BOOL didComplete) {
287-
completion();
288-
}];
289+
if (completion) {
290+
completion();
291+
}
292+
}];
289293
}
290294

291295
#pragma mark - Private

tests/unit/MotionAnimatorTests.m

+43
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,47 @@ - (void)testSpringAnimationFloatValue {
166166
XCTAssertTrue(didAddAnimation);
167167
}
168168

169+
#pragma mark - Legacy API
170+
171+
- (void)testAnimationWithTimingNilCompletion {
172+
// Given
173+
MDMMotionAnimator *animator = [[MDMMotionAnimator alloc] init];
174+
175+
CALayer *layer = [[CALayer alloc] init];
176+
MDMMotionTiming timing = (MDMMotionTiming) {
177+
.duration = 0.250, .curve = MDMMotionCurveMakeBezier(0.42f, 0.00f, 0.58f, 1.00f)
178+
};
179+
180+
// When
181+
[animator animateWithTiming:timing
182+
animations:^{
183+
layer.opacity = 0.7;
184+
}
185+
completion:nil];
186+
187+
// Then
188+
XCTAssertEqualWithAccuracy(layer.opacity, 0.7, 0.0001);
189+
}
190+
191+
- (void)testAnimationWithTimingToLayerWithValuesKeyPathNilCompletion {
192+
// Given
193+
MDMMotionAnimator *animator = [[MDMMotionAnimator alloc] init];
194+
195+
CALayer *layer = [[CALayer alloc] init];
196+
CALayer *anotherLayer = [[CALayer alloc] init];
197+
MDMMotionTiming timing = (MDMMotionTiming) {
198+
.duration = 0.250, .curve = MDMMotionCurveMakeBezier(0.42f, 0.00f, 0.58f, 1.00f)
199+
};
200+
201+
// When
202+
[animator animateWithTiming:timing
203+
toLayer:anotherLayer
204+
withValues:@[@(0), @(1)]
205+
keyPath:@"opacity"
206+
completion:nil];
207+
208+
// Then
209+
XCTAssertEqualWithAccuracy(layer.opacity, 1, 0.0001);
210+
}
211+
169212
@end

0 commit comments

Comments
 (0)