Skip to content

Commit

Permalink
[TextFields] Invalidate caret timers in snapshot tests. (#6182)
Browse files Browse the repository at this point in the history
Text fields snapshot tests were taking longer and longer to execute. The root
cause was "caret blink" timers added to the main run loop for every text field
that was set to `isEditing = YES`. It is sufficient to remove all subviews of
the text field from their superviews to invalidate the timer. The actual
culprit appears to be a private subview of class `UITextSelectionView`, but
since accessing it would be more fragile than this solution, it's best to just
destroy the view hierarchy.

Closes #6181
  • Loading branch information
Robert Moore committed Jan 4, 2019
1 parent 5f525e3 commit 3650061
Showing 1 changed file with 12 additions and 0 deletions.
Expand Up @@ -25,7 +25,19 @@ - (void)setUp {
self.textField = [[SnapshotFakeMDCTextField alloc] init];
}

- (void)removeAllSubviewsFromSuperviews:(UIView *)view {
NSArray *subviews = [view.subviews copy];
for (UIView *subview in subviews) {
[self removeAllSubviewsFromSuperviews:subview];
}
[view removeFromSuperview];
}

- (void)tearDown {
// This is required to invalidate any pending UITextField caret blink timers.
// Calling `removeFromSuperview` on `self.textField` is insufficient.
// See https://github.com/material-components/material-components-ios/issues/6181
[self removeAllSubviewsFromSuperviews:self.textField];
self.textField = nil;

[super tearDown];
Expand Down

0 comments on commit 3650061

Please sign in to comment.