From 3650061f6596b161ea9cf0d28d588fc373fcc4e3 Mon Sep 17 00:00:00 2001 From: Robert Moore Date: Fri, 4 Jan 2019 14:35:24 -0500 Subject: [PATCH] [TextFields] Invalidate caret timers in snapshot tests. (#6182) 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 --- .../supplemental/MDCTextFieldSnapshotTestCase.m | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/components/TextFields/tests/snapshot/supplemental/MDCTextFieldSnapshotTestCase.m b/components/TextFields/tests/snapshot/supplemental/MDCTextFieldSnapshotTestCase.m index 7107c75801b..607bf89013b 100644 --- a/components/TextFields/tests/snapshot/supplemental/MDCTextFieldSnapshotTestCase.m +++ b/components/TextFields/tests/snapshot/supplemental/MDCTextFieldSnapshotTestCase.m @@ -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];