Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import <React/RCTTextInputComponentView.h>
#import <XCTest/XCTest.h>
#import <react/renderer/components/iostextinput/TextInputProps.h>
#import <react/renderer/components/iostextinput/TextInputShadowNode.h>

using namespace facebook::react;

// Props with the text attributes `<TextInput>` always sends and the given line break mode.
static std::shared_ptr<const TextInputProps> propsWithLineBreakMode(std::optional<LineBreakMode> lineBreakMode)
{
auto props = std::make_shared<TextInputProps>();
props->textAttributes.allowFontScaling = true;
props->textAttributes.lineBreakMode = lineBreakMode;
return props;
}

static UITextField *backedTextField(RCTTextInputComponentView *view)
{
return [view valueForKey:@"_backedTextInputView"];
}

static NSParagraphStyle *defaultParagraphStyle(RCTTextInputComponentView *view)
{
return backedTextField(view).defaultTextAttributes[NSParagraphStyleAttributeName];
}

@interface RCTTextInputComponentViewTests : XCTestCase
@end

@implementation RCTTextInputComponentViewTests

- (void)testChangeOfLineBreakModeAloneSetsDefaultTextAttributes
{
RCTTextInputComponentView *view = [RCTTextInputComponentView new];
auto headProps = propsWithLineBreakMode(LineBreakMode::Head);
[view updateProps:headProps oldProps:nullptr];
XCTAssertEqual(defaultParagraphStyle(view).lineBreakMode, NSLineBreakByTruncatingHead);

auto middleProps = propsWithLineBreakMode(LineBreakMode::Middle);
[view updateProps:middleProps oldProps:headProps];
XCTAssertEqual(defaultParagraphStyle(view).lineBreakMode, NSLineBreakByTruncatingMiddle);

// A recycled view keeps the props of the input it showed before and compares the next input's with them
[view prepareForRecycle];
[view updateProps:propsWithLineBreakMode(std::nullopt) oldProps:nullptr];
XCTAssertNil(defaultParagraphStyle(view));
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ bool TextAttributes::operator==(const TextAttributes& rhs) const {
alignment,
baseWritingDirection,
lineBreakStrategy,
lineBreakMode,
textDecorationColor,
textDecorationLineType,
textDecorationStyle,
Expand All @@ -171,6 +172,7 @@ bool TextAttributes::operator==(const TextAttributes& rhs) const {
rhs.alignment,
rhs.baseWritingDirection,
rhs.lineBreakStrategy,
rhs.lineBreakMode,
rhs.textDecorationColor,
rhs.textDecorationLineType,
rhs.textDecorationStyle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <gtest/gtest.h>
#include <memory>
#include <utility>
#include <vector>

#include <react/featureflags/ReactNativeFeatureFlags.h>
#include <react/featureflags/ReactNativeFeatureFlagsDefaults.h>
Expand Down Expand Up @@ -100,4 +102,80 @@ INSTANTIATE_TEST_SUITE_P(
TextAttributesTest,
::testing::Bool());

namespace {

template <typename T, typename V>
void expectFieldAffectsEquality(
const char* fieldName,
T TextAttributes::* field,
V&& value) {
SCOPED_TRACE(fieldName);
TextAttributes baseline;
TextAttributes changed;
changed.*field = std::forward<V>(value);

EXPECT_FALSE(baseline == changed);
}

} // namespace

// A field added to TextAttributes belongs in operator== and in this list.
TEST(TextAttributesEqualityTest, everyFieldAffectsEquality) {
#define EXPECT_FIELD_AFFECTS_EQUALITY(field, value) \
expectFieldAffectsEquality(#field, &TextAttributes::field, value)

// Color
EXPECT_FIELD_AFFECTS_EQUALITY(foregroundColor, blackColor());
EXPECT_FIELD_AFFECTS_EQUALITY(backgroundColor, blackColor());
EXPECT_FIELD_AFFECTS_EQUALITY(opacity, 0.5);

// Font
EXPECT_FIELD_AFFECTS_EQUALITY(fontFamily, "Menlo");
EXPECT_FIELD_AFFECTS_EQUALITY(fontSize, 14);
EXPECT_FIELD_AFFECTS_EQUALITY(fontSizeMultiplier, 2);
EXPECT_FIELD_AFFECTS_EQUALITY(maxFontSizeMultiplier, 2);
EXPECT_FIELD_AFFECTS_EQUALITY(letterSpacing, 1);
EXPECT_FIELD_AFFECTS_EQUALITY(fontVariant, FontVariant::SmallCaps);
EXPECT_FIELD_AFFECTS_EQUALITY(fontVariationSettings, "'wght' 400");
EXPECT_FIELD_AFFECTS_EQUALITY(fontWeight, FontWeight::Bold);
EXPECT_FIELD_AFFECTS_EQUALITY(fontStyle, FontStyle::Italic);
EXPECT_FIELD_AFFECTS_EQUALITY(allowFontScaling, false);
EXPECT_FIELD_AFFECTS_EQUALITY(dynamicTypeRamp, DynamicTypeRamp::Body);
EXPECT_FIELD_AFFECTS_EQUALITY(textTransform, TextTransform::Uppercase);

// Paragraph Styles
EXPECT_FIELD_AFFECTS_EQUALITY(lineHeight, 20);
EXPECT_FIELD_AFFECTS_EQUALITY(alignment, TextAlignment::Center);
EXPECT_FIELD_AFFECTS_EQUALITY(
baseWritingDirection, WritingDirection::RightToLeft);
EXPECT_FIELD_AFFECTS_EQUALITY(lineBreakStrategy, LineBreakStrategy::Standard);
EXPECT_FIELD_AFFECTS_EQUALITY(lineBreakMode, LineBreakMode::Tail);

// Decoration
EXPECT_FIELD_AFFECTS_EQUALITY(textDecorationColor, blackColor());
EXPECT_FIELD_AFFECTS_EQUALITY(
textDecorationLineType, TextDecorationLineType::Underline);
EXPECT_FIELD_AFFECTS_EQUALITY(
textDecorationStyle, TextDecorationStyle::Dashed);

// Shadow
EXPECT_FIELD_AFFECTS_EQUALITY(
textShadowOffset, (Size{.width = 1, .height = 2}));
EXPECT_FIELD_AFFECTS_EQUALITY(textShadowRadius, 1);
EXPECT_FIELD_AFFECTS_EQUALITY(textShadowColor, blackColor());

// Special
EXPECT_FIELD_AFFECTS_EQUALITY(isHighlighted, false);
EXPECT_FIELD_AFFECTS_EQUALITY(isPressable, false);
EXPECT_FIELD_AFFECTS_EQUALITY(layoutDirection, LayoutDirection::RightToLeft);
EXPECT_FIELD_AFFECTS_EQUALITY(accessibilityRole, AccessibilityRole::Button);
EXPECT_FIELD_AFFECTS_EQUALITY(role, Role::Button);

// Text Effects
EXPECT_FIELD_AFFECTS_EQUALITY(
textEffects, (std::vector<TextEffectInfo>{{.name = "effect"}}));

#undef EXPECT_FIELD_AFFECTS_EQUALITY
}

} // namespace facebook::react
Loading