|
12 | 12 |
|
13 | 13 | #include <react/renderer/attributedstring/conversions.h> |
14 | 14 | #include <react/renderer/core/RawValue.h> |
| 15 | +#include <react/renderer/graphics/Color.h> |
15 | 16 |
|
16 | 17 | using namespace facebook::react; |
17 | 18 |
|
@@ -45,6 +46,70 @@ - (void)testTextAlignmentStartAndEndResolveWithLayoutDirection |
45 | 46 | XCTAssertEqual(NSTextAlignmentFromTextAlign(@"end", LayoutDirection::RightToLeft), NSTextAlignmentLeft); |
46 | 47 | } |
47 | 48 |
|
| 49 | +- (void)testDefaultClearBackgroundIsOmitted |
| 50 | +{ |
| 51 | + TextAttributes textAttributes = TextAttributes::defaultTextAttributes(); |
| 52 | + |
| 53 | + NSDictionary<NSAttributedStringKey, id> *attributes = RCTNSTextAttributesFromTextAttributes(textAttributes); |
| 54 | + |
| 55 | + XCTAssertNil(attributes[NSBackgroundColorAttributeName]); |
| 56 | + XCTAssertEqualObjects(attributes[NSForegroundColorAttributeName], [UIColor colorWithRed:0 green:0 blue:0 alpha:1]); |
| 57 | +} |
| 58 | + |
| 59 | +- (void)testOpacityDoesNotAddBackgroundColor |
| 60 | +{ |
| 61 | + TextAttributes textAttributes; |
| 62 | + textAttributes.opacity = 0.5; |
| 63 | + |
| 64 | + NSDictionary<NSAttributedStringKey, id> *attributes = RCTNSTextAttributesFromTextAttributes(textAttributes); |
| 65 | + |
| 66 | + XCTAssertNil(attributes[NSBackgroundColorAttributeName]); |
| 67 | + XCTAssertEqualObjects(attributes[NSForegroundColorAttributeName], [UIColor colorWithWhite:0 alpha:0.5]); |
| 68 | +} |
| 69 | + |
| 70 | +- (void)testDefaultClearBackgroundWithOpacityIsOmitted |
| 71 | +{ |
| 72 | + TextAttributes textAttributes = TextAttributes::defaultTextAttributes(); |
| 73 | + textAttributes.opacity = 0.5; |
| 74 | + |
| 75 | + NSDictionary<NSAttributedStringKey, id> *attributes = RCTNSTextAttributesFromTextAttributes(textAttributes); |
| 76 | + |
| 77 | + XCTAssertNil(attributes[NSBackgroundColorAttributeName]); |
| 78 | + XCTAssertEqualObjects(attributes[NSForegroundColorAttributeName], [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]); |
| 79 | +} |
| 80 | + |
| 81 | +- (void)testVisibleBackgroundColorPreservesAlphaWithOpacity |
| 82 | +{ |
| 83 | + TextAttributes textAttributes; |
| 84 | + textAttributes.backgroundColor = colorFromComponents({.red = 1, .alpha = 0.5, .colorSpace = ColorSpace::sRGB}); |
| 85 | + |
| 86 | + NSDictionary<NSAttributedStringKey, id> *attributes = RCTNSTextAttributesFromTextAttributes(textAttributes); |
| 87 | + |
| 88 | + XCTAssertEqualObjects(attributes[NSBackgroundColorAttributeName], [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5]); |
| 89 | + |
| 90 | + textAttributes.opacity = 0.5; |
| 91 | + attributes = RCTNSTextAttributesFromTextAttributes(textAttributes); |
| 92 | + |
| 93 | + XCTAssertEqualObjects(attributes[NSBackgroundColorAttributeName], [UIColor colorWithRed:1 green:0 blue:0 alpha:0.25]); |
| 94 | +} |
| 95 | + |
| 96 | +- (void)testForegroundColorPreservesAlphaWithOpacity |
| 97 | +{ |
| 98 | + TextAttributes textAttributes = TextAttributes::defaultTextAttributes(); |
| 99 | + textAttributes.foregroundColor = colorFromComponents({.blue = 1, .alpha = 0.5, .colorSpace = ColorSpace::sRGB}); |
| 100 | + |
| 101 | + NSDictionary<NSAttributedStringKey, id> *attributes = RCTNSTextAttributesFromTextAttributes(textAttributes); |
| 102 | + |
| 103 | + XCTAssertNil(attributes[NSBackgroundColorAttributeName]); |
| 104 | + XCTAssertEqualObjects(attributes[NSForegroundColorAttributeName], [UIColor colorWithRed:0 green:0 blue:1 alpha:0.5]); |
| 105 | + |
| 106 | + textAttributes.opacity = 0.5; |
| 107 | + attributes = RCTNSTextAttributesFromTextAttributes(textAttributes); |
| 108 | + |
| 109 | + XCTAssertNil(attributes[NSBackgroundColorAttributeName]); |
| 110 | + XCTAssertEqualObjects(attributes[NSForegroundColorAttributeName], [UIColor colorWithRed:0 green:0 blue:1 alpha:0.25]); |
| 111 | +} |
| 112 | + |
48 | 113 | - (void)testSamenessOfEmptyAttributedStrings |
49 | 114 | { |
50 | 115 | NSAttributedString *attributedString1 = [[NSAttributedString alloc] initWithString:@""]; |
|
0 commit comments