Skip to content

Commit

Permalink
Reevaluate RTL logic because of internal snapshot testing infrastructure
Browse files Browse the repository at this point in the history
Some internal snapshot tests force RTL by setting the semantic content attribute. The "is RTL" logic in MDCBaseTextFields up to this point only looked at effective layout direction. This change makes it so that it checks if either RTL or LTR semantic content attributes are being forced before consulting effective layout direction.

PiperOrigin-RevId: 293201200
  • Loading branch information
andrewoverton authored and material-automation committed Feb 4, 2020
1 parent 5b17884 commit 1d7746f
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 95 deletions.
75 changes: 33 additions & 42 deletions components/TextControls/src/BaseTextFields/MDCBaseTextField.m
Expand Up @@ -28,7 +28,6 @@ @interface MDCBaseTextField () <MDCTextControl>
@property(strong, nonatomic) UILabel *label;
@property(nonatomic, strong) MDCTextControlAssistiveLabelView *assistiveLabelView;
@property(strong, nonatomic) MDCBaseTextFieldLayout *layout;
@property(nonatomic, assign) UIUserInterfaceLayoutDirection layoutDirection;
@property(nonatomic, assign) MDCTextControlState textControlState;
@property(nonatomic, assign) MDCTextControlLabelState labelState;
@property(nonatomic, assign) CGRect labelFrame;
Expand Down Expand Up @@ -85,7 +84,6 @@ - (void)dealloc {
- (void)initializeProperties {
self.animationDuration = kMDCTextControlDefaultAnimationDuration;
self.labelBehavior = MDCTextControlLabelBehaviorFloats;
self.layoutDirection = self.mdf_effectiveUserInterfaceLayoutDirection;
self.labelState = [self determineCurrentLabelState];
self.textControlState = [self determineCurrentTextControlState];
self.containerStyle = [[MDCTextControlStyleBase alloc] init];
Expand All @@ -106,8 +104,8 @@ - (void)setUpAssistiveLabels {
self.assistiveLabelView = [[MDCTextControlAssistiveLabelView alloc] init];
CGFloat assistiveFontSize = MDCRound([UIFont systemFontSize] * (CGFloat)0.75);
UIFont *assistiveFont = [UIFont systemFontOfSize:assistiveFontSize];
self.assistiveLabelView.leftAssistiveLabel.font = assistiveFont;
self.assistiveLabelView.rightAssistiveLabel.font = assistiveFont;
self.assistiveLabelView.leadingAssistiveLabel.font = assistiveFont;
self.assistiveLabelView.trailingAssistiveLabel.font = assistiveFont;
[self addSubview:self.assistiveLabelView];
}

Expand Down Expand Up @@ -138,8 +136,12 @@ - (CGSize)intrinsicContentSize {

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
[super traitCollectionDidChange:previousTraitCollection];
[self setNeedsLayout];
}

self.layoutDirection = self.mdf_effectiveUserInterfaceLayoutDirection;
- (void)setSemanticContentAttribute:(UISemanticContentAttribute)semanticContentAttribute {
[super setSemanticContentAttribute:semanticContentAttribute];
[self setNeedsLayout];
}

#pragma mark Layout
Expand Down Expand Up @@ -226,11 +228,11 @@ - (MDCBaseTextFieldLayout *)calculateLayoutWithTextFieldSize:(CGSize)textFieldSi
rightViewMode:self.rightViewMode
clearButtonSideLength:clearButtonSideLength
clearButtonMode:self.clearButtonMode
leftAssistiveLabel:self.assistiveLabelView.leftAssistiveLabel
rightAssistiveLabel:self.assistiveLabelView.rightAssistiveLabel
leadingAssistiveLabel:self.assistiveLabelView.leadingAssistiveLabel
trailingAssistiveLabel:self.assistiveLabelView.trailingAssistiveLabel
assistiveLabelDrawPriority:self.assistiveLabelDrawPriority
customAssistiveLabelDrawPriority:clampedCustomAssistiveLabelDrawPriority
isRTL:self.isRTL
isRTL:self.shouldLayoutForRTL
isEditing:self.isEditing];
}

Expand Down Expand Up @@ -274,6 +276,17 @@ - (BOOL)calculatedHeightHasChangedSinceIntrinsicContentSizeWasLastComputed {
return self.layout.calculatedHeight != self.mostRecentlyComputedIntrinsicContentSize.height;
}

- (BOOL)shouldLayoutForRTL {
if (self.semanticContentAttribute == UISemanticContentAttributeForceRightToLeft) {
return YES;
} else if (self.semanticContentAttribute == UISemanticContentAttributeForceLeftToRight) {
return NO;
} else {
return self.mdf_effectiveUserInterfaceLayoutDirection ==
UIUserInterfaceLayoutDirectionRightToLeft;
}
}

#pragma mark UITextField Accessor Overrides

- (void)setEnabled:(BOOL)enabled {
Expand Down Expand Up @@ -301,47 +314,39 @@ - (void)setRightView:(UIView *)rightView {
#pragma mark Custom Accessors

- (UILabel *)leadingAssistiveLabel {
if ([self isRTL]) {
return self.assistiveLabelView.rightAssistiveLabel;
} else {
return self.assistiveLabelView.leftAssistiveLabel;
}
return self.assistiveLabelView.leadingAssistiveLabel;
}

- (UILabel *)trailingAssistiveLabel {
if ([self isRTL]) {
return self.assistiveLabelView.leftAssistiveLabel;
} else {
return self.assistiveLabelView.rightAssistiveLabel;
}
return self.assistiveLabelView.trailingAssistiveLabel;
}

- (void)setTrailingView:(UIView *)trailingView {
if ([self isRTL]) {
if ([self shouldLayoutForRTL]) {
[self mdc_setLeftView:trailingView];
} else {
[self mdc_setRightView:trailingView];
}
}

- (UIView *)trailingView {
if ([self isRTL]) {
if ([self shouldLayoutForRTL]) {
return self.leftView;
} else {
return self.rightView;
}
}

- (void)setLeadingView:(UIView *)leadingView {
if ([self isRTL]) {
if ([self shouldLayoutForRTL]) {
[self mdc_setRightView:leadingView];
} else {
[self mdc_setLeftView:leadingView];
}
}

- (UIView *)leadingView {
if ([self isRTL]) {
if ([self shouldLayoutForRTL]) {
return self.rightView;
} else {
return self.leftView;
Expand All @@ -357,31 +362,31 @@ - (void)mdc_setRightView:(UIView *)rightView {
}

- (void)setTrailingViewMode:(UITextFieldViewMode)trailingViewMode {
if ([self isRTL]) {
if ([self shouldLayoutForRTL]) {
[self mdc_setLeftViewMode:trailingViewMode];
} else {
[self mdc_setRightViewMode:trailingViewMode];
}
}

- (UITextFieldViewMode)trailingViewMode {
if ([self isRTL]) {
if ([self shouldLayoutForRTL]) {
return self.leftViewMode;
} else {
return self.rightViewMode;
}
}

- (void)setLeadingViewMode:(UITextFieldViewMode)leadingViewMode {
if ([self isRTL]) {
if ([self shouldLayoutForRTL]) {
[self mdc_setRightViewMode:leadingViewMode];
} else {
[self mdc_setLeftViewMode:leadingViewMode];
}
}

- (UITextFieldViewMode)leadingViewMode {
if ([self isRTL]) {
if ([self shouldLayoutForRTL]) {
return self.rightViewMode;
} else {
return self.leftViewMode;
Expand All @@ -396,14 +401,6 @@ - (void)mdc_setRightViewMode:(UITextFieldViewMode)rightViewMode {
[super setRightViewMode:rightViewMode];
}

- (void)setLayoutDirection:(UIUserInterfaceLayoutDirection)layoutDirection {
if (_layoutDirection == layoutDirection) {
return;
}
_layoutDirection = layoutDirection;
[self setNeedsLayout];
}

#pragma mark MDCTextControl accessors

- (void)setLabelBehavior:(MDCTextControlLabelBehavior)labelBehavior {
Expand Down Expand Up @@ -452,15 +449,15 @@ - (CGRect)editingRectForBounds:(CGRect)bounds {
// @c rightView, even though @c rightView is nil. The RTL-aware wrappers around these APIs that
// MDCBaseTextField introduce handle this situation more accurately.
- (CGRect)leftViewRectForBounds:(CGRect)bounds {
if ([self isRTL]) {
if ([self shouldLayoutForRTL]) {
return self.layout.rightViewFrame;
} else {
return self.layout.leftViewFrame;
}
}

- (CGRect)rightViewRectForBounds:(CGRect)bounds {
if ([self isRTL]) {
if ([self shouldLayoutForRTL]) {
return self.layout.leftViewFrame;
} else {
return self.layout.rightViewFrame;
Expand Down Expand Up @@ -641,12 +638,6 @@ - (MDCTextControlLabelState)labelStateWithLabel:(UILabel *)label
}
}

#pragma mark Internationalization

- (BOOL)isRTL {
return self.layoutDirection == UIUserInterfaceLayoutDirectionRightToLeft;
}

#pragma mark Coloring

- (void)applyColorViewModel:(MDCTextControlColorViewModel *)colorViewModel
Expand Down
Expand Up @@ -59,8 +59,8 @@
rightViewMode:(UITextFieldViewMode)rightViewMode
clearButtonSideLength:(CGFloat)clearButtonSideLength
clearButtonMode:(UITextFieldViewMode)clearButtonMode
leftAssistiveLabel:(nonnull UILabel *)leftAssistiveLabel
rightAssistiveLabel:(nonnull UILabel *)rightAssistiveLabel
leadingAssistiveLabel:(nonnull UILabel *)leftAssistiveLabel
trailingAssistiveLabel:(nonnull UILabel *)rightAssistiveLabel
assistiveLabelDrawPriority:
(MDCTextControlAssistiveLabelDrawPriority)assistiveLabelDrawPriority
customAssistiveLabelDrawPriority:(CGFloat)customAssistiveLabelDrawPriority
Expand Down
Expand Up @@ -38,8 +38,8 @@ - (instancetype)initWithTextFieldSize:(CGSize)textFieldSize
rightViewMode:(UITextFieldViewMode)rightViewMode
clearButtonSideLength:(CGFloat)clearButtonSideLength
clearButtonMode:(UITextFieldViewMode)clearButtonMode
leftAssistiveLabel:(nonnull UILabel *)leftAssistiveLabel
rightAssistiveLabel:(nonnull UILabel *)rightAssistiveLabel
leadingAssistiveLabel:(nonnull UILabel *)leadingAssistiveLabel
trailingAssistiveLabel:(nonnull UILabel *)trailingAssistiveLabel
assistiveLabelDrawPriority:
(MDCTextControlAssistiveLabelDrawPriority)assistiveLabelDrawPriority
customAssistiveLabelDrawPriority:(CGFloat)customAssistiveLabelDrawPriority
Expand All @@ -59,8 +59,8 @@ - (instancetype)initWithTextFieldSize:(CGSize)textFieldSize
rightViewMode:rightViewMode
clearButtonSideLength:clearButtonSideLength
clearButtonMode:clearButtonMode
leftAssistiveLabel:leftAssistiveLabel
rightAssistiveLabel:rightAssistiveLabel
leadingAssistiveLabel:leadingAssistiveLabel
trailingAssistiveLabel:trailingAssistiveLabel
assistiveLabelDrawPriority:assistiveLabelDrawPriority
customAssistiveLabelDrawPriority:customAssistiveLabelDrawPriority
isRTL:isRTL
Expand All @@ -85,8 +85,8 @@ - (void)calculateLayoutWithTextFieldSize:(CGSize)textFieldSize
rightViewMode:(UITextFieldViewMode)rightViewMode
clearButtonSideLength:(CGFloat)clearButtonSideLength
clearButtonMode:(UITextFieldViewMode)clearButtonMode
leftAssistiveLabel:(nonnull UILabel *)leftAssistiveLabel
rightAssistiveLabel:(nonnull UILabel *)rightAssistiveLabel
leadingAssistiveLabel:(nonnull UILabel *)leadingAssistiveLabel
trailingAssistiveLabel:(nonnull UILabel *)trailingAssistiveLabel
assistiveLabelDrawPriority:
(MDCTextControlAssistiveLabelDrawPriority)assistiveLabelDrawPriority
customAssistiveLabelDrawPriority:(CGFloat)customAssistiveLabelDrawPriority
Expand Down Expand Up @@ -236,8 +236,8 @@ - (void)calculateLayoutWithTextFieldSize:(CGSize)textFieldSize

self.assistiveLabelViewLayout = [[MDCTextControlAssistiveLabelViewLayout alloc]
initWithWidth:textFieldWidth
leftAssistiveLabel:leftAssistiveLabel
rightAssistiveLabel:rightAssistiveLabel
leadingAssistiveLabel:leadingAssistiveLabel
trailingAssistiveLabel:trailingAssistiveLabel
assistiveLabelDrawPriority:assistiveLabelDrawPriority
customAssistiveLabelDrawPriority:customAssistiveLabelDrawPriority
horizontalPadding:kHorizontalPadding
Expand Down
Expand Up @@ -57,8 +57,8 @@ - (MDCBaseTextFieldLayout *)createLayoutWithSideViewsAndViewMode:(UITextFieldVie
rightViewMode:viewMode
clearButtonSideLength:19
clearButtonMode:viewMode
leftAssistiveLabel:assistiveLabelView.leftAssistiveLabel
rightAssistiveLabel:assistiveLabelView.rightAssistiveLabel
leadingAssistiveLabel:assistiveLabelView.leadingAssistiveLabel
trailingAssistiveLabel:assistiveLabelView.trailingAssistiveLabel
assistiveLabelDrawPriority:MDCTextControlAssistiveLabelDrawPriorityTrailing
customAssistiveLabelDrawPriority:0
isRTL:NO
Expand Down
Expand Up @@ -18,7 +18,7 @@
#import "MaterialTextControlsPrivate+BaseStyle.h"

@interface MDCBaseTextField (Private)
@property(nonatomic, assign) UIUserInterfaceLayoutDirection layoutDirection;
- (BOOL)shouldLayoutForRTL;
- (CGRect)adjustTextAreaFrame:(CGRect)textRect
withParentClassTextAreaFrame:(CGRect)parentClassTextAreaFrame;
- (BOOL)shouldPlaceholderBeVisibleWithPlaceholder:(NSString *)placeholder
Expand All @@ -44,7 +44,7 @@ - (UIView *)createSideView {
- (void)testLeadingViewEqualsLeftViewInLTR {
// Given
MDCBaseTextField *textField = [[MDCBaseTextField alloc] initWithFrame:CGRectMake(0, 0, 100, 60)];
textField.layoutDirection = UIUserInterfaceLayoutDirectionLeftToRight;
textField.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;

// When
UIView *sideView = [self createSideView];
Expand All @@ -58,7 +58,7 @@ - (void)testLeadingViewEqualsLeftViewInLTR {
- (void)testLeadingViewEqualsRightViewInRTL {
// Given
MDCBaseTextField *textField = [[MDCBaseTextField alloc] initWithFrame:CGRectMake(0, 0, 100, 60)];
textField.layoutDirection = UIUserInterfaceLayoutDirectionRightToLeft;
textField.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;

// When
UIView *sideView = [self createSideView];
Expand All @@ -72,7 +72,7 @@ - (void)testLeadingViewEqualsRightViewInRTL {
- (void)testTrailingViewEqualsRightViewInLTR {
// Given
MDCBaseTextField *textField = [[MDCBaseTextField alloc] initWithFrame:CGRectMake(0, 0, 100, 60)];
textField.layoutDirection = UIUserInterfaceLayoutDirectionLeftToRight;
textField.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;

// When
UIView *sideView = [self createSideView];
Expand All @@ -86,7 +86,7 @@ - (void)testTrailingViewEqualsRightViewInLTR {
- (void)testTrailingViewEqualsLeftViewInRTL {
// Given
MDCBaseTextField *textField = [[MDCBaseTextField alloc] initWithFrame:CGRectMake(0, 0, 100, 60)];
textField.layoutDirection = UIUserInterfaceLayoutDirectionRightToLeft;
textField.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;

// When
UIView *sideView = [self createSideView];
Expand All @@ -102,7 +102,7 @@ - (void)testLeadingViewModeEqualsLeftViewModeInLTR {
MDCBaseTextField *textField = [[MDCBaseTextField alloc] initWithFrame:CGRectMake(0, 0, 100, 60)];

// When
textField.layoutDirection = UIUserInterfaceLayoutDirectionLeftToRight;
textField.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
textField.leadingViewMode = UITextFieldViewModeAlways;

// Then
Expand All @@ -115,7 +115,7 @@ - (void)testLeadingViewModeEqualsRightViewModeInRTL {
MDCBaseTextField *textField = [[MDCBaseTextField alloc] initWithFrame:CGRectMake(0, 0, 100, 60)];

// When
textField.layoutDirection = UIUserInterfaceLayoutDirectionRightToLeft;
textField.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
textField.leadingViewMode = UITextFieldViewModeAlways;

// Then
Expand All @@ -128,7 +128,7 @@ - (void)testTrailingViewModeEqualsRightViewModeInLTR {
MDCBaseTextField *textField = [[MDCBaseTextField alloc] initWithFrame:CGRectMake(0, 0, 100, 60)];

// When
textField.layoutDirection = UIUserInterfaceLayoutDirectionLeftToRight;
textField.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
textField.trailingViewMode = UITextFieldViewModeAlways;

// Then
Expand All @@ -141,7 +141,7 @@ - (void)testTrailingViewModeEqualsLeftViewModeInRTL {
MDCBaseTextField *textField = [[MDCBaseTextField alloc] initWithFrame:CGRectMake(0, 0, 100, 60)];

// When
textField.layoutDirection = UIUserInterfaceLayoutDirectionRightToLeft;
textField.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
textField.trailingViewMode = UITextFieldViewModeAlways;

// Then
Expand Down Expand Up @@ -489,4 +489,28 @@ - (void)testIntrinsicContentInvalidationWhenCalculatedHeightChanges {
intrinsicContentSizeBeforeCalculatedHeightChange.height);
}

- (void)testShouldLayoutForRTLWhenForcingRTL {
// Given
CGRect textFieldFrame = CGRectMake(0, 0, 100, 100);
MDCBaseTextField *textField = [[MDCBaseTextField alloc] initWithFrame:textFieldFrame];

// When
textField.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;

// Then
XCTAssertTrue(textField.shouldLayoutForRTL);
}

- (void)testShouldLayoutForRTLWhenForcingLTR {
// Given
CGRect textFieldFrame = CGRectMake(0, 0, 100, 100);
MDCBaseTextField *textField = [[MDCBaseTextField alloc] initWithFrame:textFieldFrame];

// When
textField.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;

// Then
XCTAssertFalse(textField.shouldLayoutForRTL);
}

@end
Expand Up @@ -24,8 +24,8 @@
*/
@interface MDCTextControlAssistiveLabelView : UIView

@property(nonatomic, strong, readonly, nonnull) UILabel *leftAssistiveLabel;
@property(nonatomic, strong, readonly, nonnull) UILabel *rightAssistiveLabel;
@property(nonatomic, strong, readonly, nonnull) UILabel *leadingAssistiveLabel;
@property(nonatomic, strong, readonly, nonnull) UILabel *trailingAssistiveLabel;
@property(nonatomic, strong, nonnull) MDCTextControlAssistiveLabelViewLayout *layout;

@end

0 comments on commit 1d7746f

Please sign in to comment.