|
5 | 5 | * LICENSE file in the root directory of this source tree. |
6 | 6 | */ |
7 | 7 |
|
| 8 | +#import <React/RCTConversions.h> |
| 9 | +#import <React/RCTTextInputComponentView.h> |
8 | 10 | #import <React/RCTUITextField.h> |
| 11 | +#import <React/RCTUITextView.h> |
9 | 12 | #import <XCTest/XCTest.h> |
| 13 | +#import <react/renderer/components/iostextinput/TextInputProps.h> |
| 14 | +#import <react/renderer/core/RawPropsParser.h> |
| 15 | +#import <react/renderer/graphics/Color.h> |
10 | 16 |
|
11 | 17 | static NSParagraphStyle *RCTPlaceholderParagraphStyle(RCTUITextField *textField) |
12 | 18 | { |
@@ -54,4 +60,138 @@ - (void)testPlaceholderKeepsParagraphStyleFromTextAttributes |
54 | 60 | XCTAssertEqual(RCTPlaceholderParagraphStyle(textField).alignment, NSTextAlignmentCenter); |
55 | 61 | } |
56 | 62 |
|
| 63 | +- (void)testCaretHiddenMakesTheCaretTransparent |
| 64 | +{ |
| 65 | + RCTUITextField *textField = [RCTUITextField new]; |
| 66 | + textField.tintColor = UIColor.redColor; |
| 67 | + |
| 68 | + textField.caretHidden = YES; |
| 69 | + |
| 70 | + XCTAssertEqualObjects(textField.tintColor, UIColor.clearColor); |
| 71 | +} |
| 72 | + |
| 73 | +- (void)testClearingCaretHiddenRestoresTheSelectionColor |
| 74 | +{ |
| 75 | + RCTUITextField *textField = [RCTUITextField new]; |
| 76 | + textField.tintColor = UIColor.redColor; |
| 77 | + textField.caretHidden = YES; |
| 78 | + |
| 79 | + textField.caretHidden = NO; |
| 80 | + |
| 81 | + XCTAssertEqualObjects(textField.tintColor, UIColor.redColor); |
| 82 | +} |
| 83 | + |
| 84 | +- (void)testSelectionColorSetWhileCaretHiddenIsAppliedOnceTheCaretIsShown |
| 85 | +{ |
| 86 | + RCTUITextField *textField = [RCTUITextField new]; |
| 87 | + textField.caretHidden = YES; |
| 88 | + |
| 89 | + textField.tintColor = UIColor.redColor; |
| 90 | + |
| 91 | + XCTAssertEqualObjects(textField.tintColor, UIColor.clearColor); |
| 92 | + |
| 93 | + textField.caretHidden = NO; |
| 94 | + |
| 95 | + XCTAssertEqualObjects(textField.tintColor, UIColor.redColor); |
| 96 | +} |
| 97 | + |
| 98 | +- (void)testSelectionColorIsRestoredForSelectionsAndHiddenAgainWhenCollapsed |
| 99 | +{ |
| 100 | + RCTUITextField *textField = [RCTUITextField new]; |
| 101 | + textField.attributedText = [[NSAttributedString alloc] initWithString:@"Hello"]; |
| 102 | + textField.tintColor = UIColor.redColor; |
| 103 | + textField.caretHidden = YES; |
| 104 | + |
| 105 | + UITextPosition *start = textField.beginningOfDocument; |
| 106 | + UITextPosition *end = [textField positionFromPosition:start offset:textField.attributedText.length]; |
| 107 | + for (NSNumber *notifyDelegate in @[ @YES, @NO ]) { |
| 108 | + [textField setSelectedTextRange:[textField textRangeFromPosition:start toPosition:end] |
| 109 | + notifyDelegate:notifyDelegate.boolValue]; |
| 110 | + XCTAssertEqualObjects(textField.tintColor, UIColor.redColor); |
| 111 | + |
| 112 | + [textField setSelectedTextRange:[textField textRangeFromPosition:end toPosition:end] |
| 113 | + notifyDelegate:notifyDelegate.boolValue]; |
| 114 | + XCTAssertEqualObjects(textField.tintColor, UIColor.clearColor); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +- (void)testReplacingASelectionHidesTheCaretAgain |
| 119 | +{ |
| 120 | + RCTUITextField *textField = [RCTUITextField new]; |
| 121 | + textField.delegate = nil; |
| 122 | + textField.attributedText = [[NSAttributedString alloc] initWithString:@"Hello"]; |
| 123 | + textField.tintColor = UIColor.redColor; |
| 124 | + textField.caretHidden = YES; |
| 125 | + [textField setSelectedTextRange:[textField textRangeFromPosition:textField.beginningOfDocument |
| 126 | + toPosition:textField.endOfDocument] |
| 127 | + notifyDelegate:NO]; |
| 128 | + XCTAssertEqualObjects(textField.tintColor, UIColor.redColor); |
| 129 | + |
| 130 | + [textField insertText:@"X"]; |
| 131 | + |
| 132 | + XCTAssertEqualObjects(textField.attributedText.string, @"X"); |
| 133 | + XCTAssertTrue(textField.selectedTextRange.isEmpty); |
| 134 | + XCTAssertEqualObjects(textField.tintColor, UIColor.clearColor); |
| 135 | +} |
| 136 | + |
| 137 | +- (void)testHidingCaretPreservesItsGeometry |
| 138 | +{ |
| 139 | + RCTUITextField *textField = [[RCTUITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 40)]; |
| 140 | + textField.attributedText = [[NSAttributedString alloc] initWithString:@"Hello"]; |
| 141 | + [textField layoutIfNeeded]; |
| 142 | + CGRect visibleCaret = [textField caretRectForPosition:textField.endOfDocument]; |
| 143 | + XCTAssertGreaterThan(visibleCaret.size.width, 0); |
| 144 | + XCTAssertGreaterThan(visibleCaret.size.height, 0); |
| 145 | + |
| 146 | + textField.caretHidden = YES; |
| 147 | + |
| 148 | + XCTAssertTrue(CGRectEqualToRect([textField caretRectForPosition:textField.endOfDocument], visibleCaret)); |
| 149 | +} |
| 150 | + |
| 151 | +- (void)testClearingSelectionColorRestoresInheritedTint |
| 152 | +{ |
| 153 | + UIView *parent = [UIView new]; |
| 154 | + parent.tintColor = UIColor.blueColor; |
| 155 | + RCTUITextField *textField = [RCTUITextField new]; |
| 156 | + [parent addSubview:textField]; |
| 157 | + textField.tintColor = UIColor.redColor; |
| 158 | + textField.caretHidden = YES; |
| 159 | + |
| 160 | + textField.tintColor = nil; |
| 161 | + XCTAssertEqualObjects(textField.tintColor, UIColor.clearColor); |
| 162 | + textField.caretHidden = NO; |
| 163 | + XCTAssertEqualObjects(textField.tintColor, UIColor.blueColor); |
| 164 | +} |
| 165 | + |
| 166 | +- (void)testSwitchingBackingViewsPreservesSelectionColor |
| 167 | +{ |
| 168 | + using namespace facebook::react; |
| 169 | + |
| 170 | + RCTTextInputComponentView *componentView = [RCTTextInputComponentView new]; |
| 171 | + ContextContainer contextContainer{}; |
| 172 | + PropsParserContext context{-1, contextContainer}; |
| 173 | + RawProps rawProps(folly::dynamic::object("caretHidden", true)); |
| 174 | + RawPropsParser parser; |
| 175 | + parser.prepare<TextInputProps>(); |
| 176 | + rawProps.parse(parser); |
| 177 | + auto singleline = std::make_shared<TextInputProps>(context, TextInputProps{}, rawProps); |
| 178 | + singleline->selectionColor = colorFromRGBA(255, 0, 0, 255); |
| 179 | + UIColor *selectionColor = RCTUIColorFromSharedColor(singleline->selectionColor); |
| 180 | + [componentView updateProps:singleline oldProps:nullptr]; |
| 181 | + XCTAssertEqualObjects(componentView.subviews.firstObject.tintColor, UIColor.clearColor); |
| 182 | + |
| 183 | + auto multiline = std::make_shared<TextInputProps>(*singleline); |
| 184 | + multiline->multiline = true; |
| 185 | + [componentView updateProps:multiline oldProps:singleline]; |
| 186 | + XCTAssertTrue([componentView.subviews.firstObject isKindOfClass:RCTUITextView.class]); |
| 187 | + XCTAssertEqualObjects(componentView.subviews.firstObject.tintColor, selectionColor); |
| 188 | + |
| 189 | + [componentView updateProps:singleline oldProps:multiline]; |
| 190 | + RCTUITextField *textField = (RCTUITextField *)componentView.subviews.firstObject; |
| 191 | + XCTAssertTrue([textField isKindOfClass:RCTUITextField.class]); |
| 192 | + XCTAssertEqualObjects(textField.tintColor, UIColor.clearColor); |
| 193 | + textField.caretHidden = NO; |
| 194 | + XCTAssertEqualObjects(textField.tintColor, selectionColor); |
| 195 | +} |
| 196 | + |
57 | 197 | @end |
0 commit comments