Skip to content
Merged
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
119 changes: 119 additions & 0 deletions packages/react-native/React/Tests/Text/RCTTextMeasureCacheTests.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* 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 <XCTest/XCTest.h>

#import <react/renderer/textlayoutmanager/TextLayoutManager.h>

#include <react/renderer/attributedstring/AttributedString.h>
#include <react/renderer/attributedstring/AttributedStringBox.h>
#include <react/renderer/attributedstring/ParagraphAttributes.h>
#include <react/renderer/attributedstring/TextAttributes.h>
#include <react/renderer/core/LayoutConstraints.h>
#include <react/renderer/telemetry/TransactionTelemetry.h>
#include <react/utils/ContextContainer.h>

#include <memory>

using namespace facebook::react;

@interface RCTTextMeasureCacheTests : XCTestCase

@end

@implementation RCTTextMeasureCacheTests {
std::unique_ptr<TextLayoutManager> _textLayoutManager;
std::unique_ptr<TransactionTelemetry> _telemetry;
}

- (void)setUp
{
[super setUp];
_textLayoutManager = std::make_unique<TextLayoutManager>(std::make_shared<const ContextContainer>());
_telemetry = std::make_unique<TransactionTelemetry>();
_telemetry->setAsThreadLocal();
}

- (void)tearDown
{
_telemetry->unsetAsThreadLocal();
_telemetry.reset();
_textLayoutManager.reset();
[super tearDown];
}

static AttributedStringBox AttributedStringBoxWithText(std::string text)
{
auto attributedString = AttributedString{};
attributedString.appendFragment(
{.string = std::move(text), .textAttributes = TextAttributes::defaultTextAttributes()});
return AttributedStringBox{attributedString};
}

static LayoutConstraints ConstraintsWithMaximumWidth(Float maximumWidth)
{
auto constraints = LayoutConstraints{};
constraints.maximumSize.width = maximumWidth;
return constraints;
}

- (TextMeasurement)measure:(const AttributedStringBox &)attributedStringBox
constraints:(const LayoutConstraints &)constraints
{
return _textLayoutManager->measure(attributedStringBox, ParagraphAttributes{}, TextLayoutContext{}, constraints);
}

- (void)testConstraintsThatDifferOnlyInMinimumSizeShareAMeasurement
{
auto text = AttributedStringBoxWithText("Hello, world");

auto constraints = ConstraintsWithMaximumWidth(200);
auto measurement = [self measure:text constraints:constraints];
XCTAssertEqual(_telemetry->getNumberOfTextMeasurements(), 1);

auto withMinimumSize = constraints;
withMinimumSize.minimumSize = facebook::react::Size{.width = 150, .height = 60};
auto measurementWithMinimumSize = [self measure:text constraints:withMinimumSize];

XCTAssertEqual(_telemetry->getNumberOfTextMeasurements(), 1);
// The shared measurement is still clamped to the caller's constraints.
XCTAssertLessThan(measurement.size.width, 150);
XCTAssertEqual(measurementWithMinimumSize.size.width, 150);
XCTAssertEqual(measurementWithMinimumSize.size.height, 60);
}

- (void)testConstraintsThatDifferOnlyInMaximumHeightShareAMeasurement
{
auto text = AttributedStringBoxWithText("Hello, world");

auto constraints = ConstraintsWithMaximumWidth(200);
auto measurement = [self measure:text constraints:constraints];
XCTAssertEqual(_telemetry->getNumberOfTextMeasurements(), 1);
XCTAssertGreaterThan(measurement.size.height, 5);

auto withMaximumHeight = constraints;
withMaximumHeight.maximumSize.height = 5;
auto measurementWithMaximumHeight = [self measure:text constraints:withMaximumHeight];

XCTAssertEqual(_telemetry->getNumberOfTextMeasurements(), 1);
// The shared measurement is still clamped to the caller's constraints.
XCTAssertEqual(measurementWithMaximumHeight.size.height, 5);
XCTAssertEqual(measurementWithMaximumHeight.size.width, measurement.size.width);
}

- (void)testConstraintsWithADifferentMaximumWidthMeasureAgain
{
auto text = AttributedStringBoxWithText("Hello, world");

[self measure:text constraints:ConstraintsWithMaximumWidth(200)];
XCTAssertEqual(_telemetry->getNumberOfTextMeasurements(), 1);

[self measure:text constraints:ConstraintsWithMaximumWidth(30)];
XCTAssertEqual(_telemetry->getNumberOfTextMeasurements(), 2);
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ - (TextMeasurement)measureNSAttributedString:(NSAttributedString *)attributedStr
return {};
}

// Only the maximum width is read here. TextLayoutManager::measure relies on that: its cache key leaves out the
// minimum size and the maximum height.
CGSize maximumSize = CGSize{layoutConstraints.maximumSize.width, CGFLOAT_MAX};
NSTextStorage *textStorage = [self _textStorageAndLayoutManagerWithAttributesString:attributedString
paragraphAttributes:paragraphAttributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#import "TextLayoutManager.h"
#import "RCTTextLayoutManager.h"

#import <limits>
#import <optional>

#import <react/renderer/attributedstring/PlaceholderAttributedString.h>
Expand Down Expand Up @@ -53,10 +54,17 @@
const auto &attributedString =
placeholderAttributedString ? *placeholderAttributedString : originalAttributedString;

// The native measurement depends only on the maximum width, and the result is clamped to the
// full constraints below, outside the cache. So the minimum size and the maximum height are left
// out of the key, and the measurement gets the key's constraints, so that every caller sharing
// an entry gets the same result.
LayoutConstraints keyConstraints = layoutConstraints;
keyConstraints.minimumSize = Size{};
keyConstraints.maximumSize.height = std::numeric_limits<Float>::infinity();
measurement = textMeasureCache_.get(
{.attributedString = attributedString,
.paragraphAttributes = paragraphAttributes,
.layoutConstraints = layoutConstraints,
.layoutConstraints = keyConstraints,
.pointScaleFactor = layoutContext.pointScaleFactor},
[&]() {
auto telemetry = TransactionTelemetry::threadLocalTelemetry();
Expand All @@ -67,7 +75,7 @@
auto measurement = [textLayoutManager measureAttributedString:attributedString
paragraphAttributes:paragraphAttributes
layoutContext:layoutContext
layoutConstraints:layoutConstraints];
layoutConstraints:keyConstraints];

if (telemetry) {
telemetry->didMeasureText();
Expand Down
Loading