Skip to content

Commit

Permalink
Fix test util to measure text properly based on flex direction
Browse files Browse the repository at this point in the history
Summary: Depending on the flex direction text will either be capped to the measured size or to the longest word, so I added that functionality

Differential Revision: D67106199
  • Loading branch information
joevilches authored and facebook-github-bot committed Dec 11, 2024
1 parent 13f4adb commit 7ef49a9
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 37 deletions.
25 changes: 25 additions & 0 deletions gentest/fixtures/YGIntrinsicSizeTest.html
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,31 @@
</div>
</div>

<div id="contains_inner_text_max_width_max_height_column" style="width:2000px; align-items: flex-start;">
<div style="max-width: 50px;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eleifasd et tortor ac auctor. Integer at
volutpat
libero, sed elementum dui interdum id. Aliquam consectetur massa vel neque aliquet, quis consequat risus
fringilla. Fusce rhoncus ipsum tempor eros aliquam, vel tempus metus ullamcorper. Nam at nulla sed tellus
vestibulum fringilla vel sit amet ligula. Proin velit lectus, euismod sit amet quam vel ultricies dolor,
vitae
finibus lorem ipsum. Pellentesque molestie at mi sit amet dictum. Donec vehicula lacinia felis sit amet
consectetur. Praesent sodales enim sapien, sed varius ipsum pellentesque vel. Aenean eu mi eu justo
tincidunt
finibus vel sit amet ipsum. Sed bibasdum purus vel ipsum sagittis, quis fermentum dolor lobortis. Etiam
vulputate eleifasd lectus vel varius.
Phasellus imperdiet lectus sit amet ipsum egestas, ut bibasdum ipsum malesuada. Vestibulum ante ipsum primis
in
faucibus orci luctus et ultrices posuere cubilia Curae; Sed mollis eros sit amet elit porttitor, vel
venenatis
turpis venenatis. Nulla tempus tortor at eros efficitur, sit amet dapibus ipsum malesuada. Ut at mauris sed
nunc
malesuada convallis. Duis id sem vel magna varius eleifasd vel at est. Donec eget orci a ipsum tempor
lobortis.
Sed at consectetur ipsum.
</div>
</div>


<div id="contains_inner_text_max_width" style="width:2000px;height:2000px;align-items: flex-start;">
<div style="flex-direction:row;max-width:100px">
Expand Down
2 changes: 1 addition & 1 deletion gentest/gentest-cpp.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ CPPEmitter.prototype = Object.create(Emitter.prototype, {
},

YGNodeSetMeasureFunc: {
value: function (nodeName, innerText) {
value: function (nodeName, innerText, _) {
this.push(`YGNodeSetContext(${nodeName}, (void*)"${innerText}");`);
this.push(
`YGNodeSetMeasureFunc(${nodeName}, &facebook::yoga::test::IntrinsicSizeMeasure);`,
Expand Down
2 changes: 1 addition & 1 deletion gentest/gentest-java.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ JavaEmitter.prototype = Object.create(Emitter.prototype, {
},

YGNodeSetMeasureFunc: {
value: function (nodeName, innerText) {
value: function (nodeName, innerText, _) {
this.push(`${nodeName}.setData("${innerText}");`);
this.push(
`${nodeName}.setMeasureFunction(new TestUtils.intrinsicMeasureFunction());`,
Expand Down
6 changes: 4 additions & 2 deletions gentest/gentest-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,11 @@ JavascriptEmitter.prototype = Object.create(Emitter.prototype, {
},

YGNodeSetMeasureFunc: {
value: function (nodeName, innerText) {
value: function (nodeName, innerText, flexDirection) {
this.push(
`${nodeName}.setMeasureFunc(instrinsicSizeMeasureFunc.bind("${innerText}"));`,
`${nodeName}.setMeasureFunc(instrinsicSizeMeasureFunc.bind({text: "${innerText}", flexDirection: ${toValueJavascript(
flexDirection,
)}}));`,
);
},
},
Expand Down
6 changes: 5 additions & 1 deletion gentest/gentest.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,11 @@ function setupTestTree(
}

if (node.innerText && node.children.length === 0) {
e.YGNodeSetMeasureFunc(nodeName, node.innerText);
e.YGNodeSetMeasureFunc(
nodeName,
node.innerText,
flexDirectionValue(e, node.style['flex-direction']),
);
}

for (let i = 0; i < node.children.length; i++) {
Expand Down
9 changes: 7 additions & 2 deletions java/tests/com/facebook/yoga/utils/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.facebook.yoga.YogaMeasureOutput;
import com.facebook.yoga.YogaMeasureFunction;
import com.facebook.yoga.YogaNode;
import com.facebook.yoga.YogaFlexDirection;

public class TestUtils {

Expand Down Expand Up @@ -43,9 +44,13 @@ public long measure(
} else if (heightMode == YogaMeasureMode.AT_MOST) {
measuredHeight =
Math.min(
caclulateHeight(text, Math.max(measuredWidth, getWidestWordWidth(text))), height);
caclulateHeight(text, node.getFlexDirection() == YogaFlexDirection.COLUMN
? measuredWidth
: Math.max(measuredWidth, getWidestWordWidth(text))), height);
} else {
measuredHeight = caclulateHeight(text, Math.max(measuredWidth, getWidestWordWidth(text)));
measuredHeight = caclulateHeight(text, node.getFlexDirection() == YogaFlexDirection.COLUMN
? measuredWidth
: Math.max(measuredWidth, getWidestWordWidth(text)));
}

return YogaMeasureOutput.make(measuredWidth, measuredHeight);
Expand Down
43 changes: 42 additions & 1 deletion java/tests/generated/com/facebook/yoga/YGIntrinsicSizeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<687a3aca94b61dfa74ab408189b3fc48>>
* @generated SignedSource<<96b528db5cda18b4993df8a0a5ce62a1>>
* generated by gentest/gentest-driver.ts from gentest/fixtures/YGIntrinsicSizeTest.html
*/

Expand Down Expand Up @@ -327,6 +327,47 @@ public void test_contains_inner_text_max_width_max_height() {
assertEquals(20f, root_child0.getLayoutHeight(), 0.0f);
}

@Test
public void test_contains_inner_text_max_width_max_height_column() {
YogaConfig config = YogaConfigFactory.create();

final YogaNode root = createNode(config);
root.setAlignItems(YogaAlign.FLEX_START);
root.setPositionType(YogaPositionType.ABSOLUTE);
root.setWidth(2000f);

final YogaNode root_child0 = createNode(config);
root_child0.setMaxWidth(50f);
root.addChildAt(root_child0, 0);
root_child0.setData("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eleifasd et tortor ac auctor. Integer at volutpat libero, sed elementum dui interdum id. Aliquam consectetur massa vel neque aliquet, quis consequat risus fringilla. Fusce rhoncus ipsum tempor eros aliquam, vel tempus metus ullamcorper. Nam at nulla sed tellus vestibulum fringilla vel sit amet ligula. Proin velit lectus, euismod sit amet quam vel ultricies dolor, vitae finibus lorem ipsum. Pellentesque molestie at mi sit amet dictum. Donec vehicula lacinia felis sit amet consectetur. Praesent sodales enim sapien, sed varius ipsum pellentesque vel. Aenean eu mi eu justo tincidunt finibus vel sit amet ipsum. Sed bibasdum purus vel ipsum sagittis, quis fermentum dolor lobortis. Etiam vulputate eleifasd lectus vel varius. Phasellus imperdiet lectus sit amet ipsum egestas, ut bibasdum ipsum malesuada. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed mollis eros sit amet elit porttitor, vel venenatis turpis venenatis. Nulla tempus tortor at eros efficitur, sit amet dapibus ipsum malesuada. Ut at mauris sed nunc malesuada convallis. Duis id sem vel magna varius eleifasd vel at est. Donec eget orci a ipsum tempor lobortis. Sed at consectetur ipsum.");
root_child0.setMeasureFunction(new TestUtils.intrinsicMeasureFunction());
root.setDirection(YogaDirection.LTR);
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);

assertEquals(0f, root.getLayoutX(), 0.0f);
assertEquals(0f, root.getLayoutY(), 0.0f);
assertEquals(2000f, root.getLayoutWidth(), 0.0f);
assertEquals(1890f, root.getLayoutHeight(), 0.0f);

assertEquals(0f, root_child0.getLayoutX(), 0.0f);
assertEquals(0f, root_child0.getLayoutY(), 0.0f);
assertEquals(50f, root_child0.getLayoutWidth(), 0.0f);
assertEquals(1890f, root_child0.getLayoutHeight(), 0.0f);

root.setDirection(YogaDirection.RTL);
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);

assertEquals(0f, root.getLayoutX(), 0.0f);
assertEquals(0f, root.getLayoutY(), 0.0f);
assertEquals(2000f, root.getLayoutWidth(), 0.0f);
assertEquals(1890f, root.getLayoutHeight(), 0.0f);

assertEquals(1950f, root_child0.getLayoutX(), 0.0f);
assertEquals(0f, root_child0.getLayoutY(), 0.0f);
assertEquals(50f, root_child0.getLayoutWidth(), 0.0f);
assertEquals(1890f, root_child0.getLayoutHeight(), 0.0f);
}

@Test
public void test_contains_inner_text_max_width() {
YogaConfig config = YogaConfigFactory.create();
Expand Down
Loading

0 comments on commit 7ef49a9

Please sign in to comment.