diff --git a/enums.py b/enums.py index ceef4deed3..2ae874364d 100755 --- a/enums.py +++ b/enums.py @@ -95,7 +95,22 @@ } # Temporary filter enums to not upgrade all enums at once -KOTLIN_ENUM_NAMES = {"Direction"} +KOTLIN_ENUM_NAMES = { + "Direction", + "Align", + "BoxSizing", + "Dimension", + "Display", + "Edge", + "Errata", + "ExperimentalFeature", + "FlexDirection", + "GridTrackType", + "Gutter", + "Justify", + "LogLevel", + "MeasureMode", +} ENUMS_KOTLIN = {name: ENUMS[name] for name in KOTLIN_ENUM_NAMES} ENUMS_JAVA = { @@ -294,6 +309,9 @@ def to_hyphenated_lower(symbol): with open(root + "/java/com/facebook/yoga/Yoga%s.kt" % name, "w") as f: f.write(get_license("kotlin")) f.write("package com.facebook.yoga\n\n") + if name in DO_NOT_STRIP: + f.write("import com.facebook.yoga.annotations.DoNotStrip\n\n") + f.write("@DoNotStrip\n") f.write("public enum class Yoga%s(public val intValue: Int) {\n" % name) if len(values) > 0: for value in values: @@ -311,6 +329,8 @@ def to_hyphenated_lower(symbol): f.write(" public fun intValue(): Int = intValue\n") f.write("\n") f.write(" public companion object {\n") + if name in DO_NOT_STRIP: + f.write(" @DoNotStrip\n") f.write(" @JvmStatic\n") f.write(" public fun fromInt(value: Int): Yoga%s =\n" % name) f.write(" when (value) {\n") diff --git a/java/com/facebook/yoga/YogaAlign.java b/java/com/facebook/yoga/YogaAlign.java deleted file mode 100644 index 6f1c456581..0000000000 --- a/java/com/facebook/yoga/YogaAlign.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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. - */ - -// @generated by enums.py - -package com.facebook.yoga; - -public enum YogaAlign { - AUTO(0), - FLEX_START(1), - CENTER(2), - FLEX_END(3), - STRETCH(4), - BASELINE(5), - SPACE_BETWEEN(6), - SPACE_AROUND(7), - SPACE_EVENLY(8), - START(9), - END(10); - - private final int mIntValue; - - YogaAlign(int intValue) { - mIntValue = intValue; - } - - public int intValue() { - return mIntValue; - } - - public static YogaAlign fromInt(int value) { - switch (value) { - case 0: return AUTO; - case 1: return FLEX_START; - case 2: return CENTER; - case 3: return FLEX_END; - case 4: return STRETCH; - case 5: return BASELINE; - case 6: return SPACE_BETWEEN; - case 7: return SPACE_AROUND; - case 8: return SPACE_EVENLY; - case 9: return START; - case 10: return END; - default: throw new IllegalArgumentException("Unknown enum value: " + value); - } - } -} diff --git a/java/com/facebook/yoga/YogaAlign.kt b/java/com/facebook/yoga/YogaAlign.kt new file mode 100644 index 0000000000..e8fa92d454 --- /dev/null +++ b/java/com/facebook/yoga/YogaAlign.kt @@ -0,0 +1,45 @@ +/* + * 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. + */ + +// @generated by enums.py + +package com.facebook.yoga + +public enum class YogaAlign(public val intValue: Int) { + AUTO(0), + FLEX_START(1), + CENTER(2), + FLEX_END(3), + STRETCH(4), + BASELINE(5), + SPACE_BETWEEN(6), + SPACE_AROUND(7), + SPACE_EVENLY(8), + START(9), + END(10); + + public fun intValue(): Int = intValue + + public companion object { + @JvmStatic + public fun fromInt(value: Int): YogaAlign = + when (value) { + 0 -> AUTO + 1 -> FLEX_START + 2 -> CENTER + 3 -> FLEX_END + 4 -> STRETCH + 5 -> BASELINE + 6 -> SPACE_BETWEEN + 7 -> SPACE_AROUND + 8 -> SPACE_EVENLY + 9 -> START + 10 -> END + else -> throw IllegalArgumentException("Unknown enum value: $value") + } + } +} diff --git a/java/com/facebook/yoga/YogaBoxSizing.java b/java/com/facebook/yoga/YogaBoxSizing.java deleted file mode 100644 index fcd25f55dd..0000000000 --- a/java/com/facebook/yoga/YogaBoxSizing.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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. - */ - -// @generated by enums.py - -package com.facebook.yoga; - -public enum YogaBoxSizing { - BORDER_BOX(0), - CONTENT_BOX(1); - - private final int mIntValue; - - YogaBoxSizing(int intValue) { - mIntValue = intValue; - } - - public int intValue() { - return mIntValue; - } - - public static YogaBoxSizing fromInt(int value) { - switch (value) { - case 0: return BORDER_BOX; - case 1: return CONTENT_BOX; - default: throw new IllegalArgumentException("Unknown enum value: " + value); - } - } -} diff --git a/java/com/facebook/yoga/YogaBoxSizing.kt b/java/com/facebook/yoga/YogaBoxSizing.kt new file mode 100644 index 0000000000..c7d055fc7f --- /dev/null +++ b/java/com/facebook/yoga/YogaBoxSizing.kt @@ -0,0 +1,27 @@ +/* + * 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. + */ + +// @generated by enums.py + +package com.facebook.yoga + +public enum class YogaBoxSizing(public val intValue: Int) { + BORDER_BOX(0), + CONTENT_BOX(1); + + public fun intValue(): Int = intValue + + public companion object { + @JvmStatic + public fun fromInt(value: Int): YogaBoxSizing = + when (value) { + 0 -> BORDER_BOX + 1 -> CONTENT_BOX + else -> throw IllegalArgumentException("Unknown enum value: $value") + } + } +} diff --git a/java/com/facebook/yoga/YogaDimension.java b/java/com/facebook/yoga/YogaDimension.java deleted file mode 100644 index a949ddc3ce..0000000000 --- a/java/com/facebook/yoga/YogaDimension.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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. - */ - -// @generated by enums.py - -package com.facebook.yoga; - -public enum YogaDimension { - WIDTH(0), - HEIGHT(1); - - private final int mIntValue; - - YogaDimension(int intValue) { - mIntValue = intValue; - } - - public int intValue() { - return mIntValue; - } - - public static YogaDimension fromInt(int value) { - switch (value) { - case 0: return WIDTH; - case 1: return HEIGHT; - default: throw new IllegalArgumentException("Unknown enum value: " + value); - } - } -} diff --git a/java/com/facebook/yoga/YogaDimension.kt b/java/com/facebook/yoga/YogaDimension.kt new file mode 100644 index 0000000000..132cc29293 --- /dev/null +++ b/java/com/facebook/yoga/YogaDimension.kt @@ -0,0 +1,27 @@ +/* + * 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. + */ + +// @generated by enums.py + +package com.facebook.yoga + +public enum class YogaDimension(public val intValue: Int) { + WIDTH(0), + HEIGHT(1); + + public fun intValue(): Int = intValue + + public companion object { + @JvmStatic + public fun fromInt(value: Int): YogaDimension = + when (value) { + 0 -> WIDTH + 1 -> HEIGHT + else -> throw IllegalArgumentException("Unknown enum value: $value") + } + } +} diff --git a/java/com/facebook/yoga/YogaDisplay.java b/java/com/facebook/yoga/YogaDisplay.java deleted file mode 100644 index 8e7e0f83cd..0000000000 --- a/java/com/facebook/yoga/YogaDisplay.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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. - */ - -// @generated by enums.py - -package com.facebook.yoga; - -public enum YogaDisplay { - FLEX(0), - NONE(1), - CONTENTS(2), - GRID(3); - - private final int mIntValue; - - YogaDisplay(int intValue) { - mIntValue = intValue; - } - - public int intValue() { - return mIntValue; - } - - public static YogaDisplay fromInt(int value) { - switch (value) { - case 0: return FLEX; - case 1: return NONE; - case 2: return CONTENTS; - case 3: return GRID; - default: throw new IllegalArgumentException("Unknown enum value: " + value); - } - } -} diff --git a/java/com/facebook/yoga/YogaDisplay.kt b/java/com/facebook/yoga/YogaDisplay.kt new file mode 100644 index 0000000000..6a56a048bb --- /dev/null +++ b/java/com/facebook/yoga/YogaDisplay.kt @@ -0,0 +1,31 @@ +/* + * 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. + */ + +// @generated by enums.py + +package com.facebook.yoga + +public enum class YogaDisplay(public val intValue: Int) { + FLEX(0), + NONE(1), + CONTENTS(2), + GRID(3); + + public fun intValue(): Int = intValue + + public companion object { + @JvmStatic + public fun fromInt(value: Int): YogaDisplay = + when (value) { + 0 -> FLEX + 1 -> NONE + 2 -> CONTENTS + 3 -> GRID + else -> throw IllegalArgumentException("Unknown enum value: $value") + } + } +} diff --git a/java/com/facebook/yoga/YogaEdge.java b/java/com/facebook/yoga/YogaEdge.java deleted file mode 100644 index 6b91534823..0000000000 --- a/java/com/facebook/yoga/YogaEdge.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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. - */ - -// @generated by enums.py - -package com.facebook.yoga; - -public enum YogaEdge { - LEFT(0), - TOP(1), - RIGHT(2), - BOTTOM(3), - START(4), - END(5), - HORIZONTAL(6), - VERTICAL(7), - ALL(8); - - private final int mIntValue; - - YogaEdge(int intValue) { - mIntValue = intValue; - } - - public int intValue() { - return mIntValue; - } - - public static YogaEdge fromInt(int value) { - switch (value) { - case 0: return LEFT; - case 1: return TOP; - case 2: return RIGHT; - case 3: return BOTTOM; - case 4: return START; - case 5: return END; - case 6: return HORIZONTAL; - case 7: return VERTICAL; - case 8: return ALL; - default: throw new IllegalArgumentException("Unknown enum value: " + value); - } - } -} diff --git a/java/com/facebook/yoga/YogaEdge.kt b/java/com/facebook/yoga/YogaEdge.kt new file mode 100644 index 0000000000..0ab62ebbf3 --- /dev/null +++ b/java/com/facebook/yoga/YogaEdge.kt @@ -0,0 +1,41 @@ +/* + * 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. + */ + +// @generated by enums.py + +package com.facebook.yoga + +public enum class YogaEdge(public val intValue: Int) { + LEFT(0), + TOP(1), + RIGHT(2), + BOTTOM(3), + START(4), + END(5), + HORIZONTAL(6), + VERTICAL(7), + ALL(8); + + public fun intValue(): Int = intValue + + public companion object { + @JvmStatic + public fun fromInt(value: Int): YogaEdge = + when (value) { + 0 -> LEFT + 1 -> TOP + 2 -> RIGHT + 3 -> BOTTOM + 4 -> START + 5 -> END + 6 -> HORIZONTAL + 7 -> VERTICAL + 8 -> ALL + else -> throw IllegalArgumentException("Unknown enum value: $value") + } + } +} diff --git a/java/com/facebook/yoga/YogaErrata.java b/java/com/facebook/yoga/YogaErrata.java deleted file mode 100644 index e0521b3fbb..0000000000 --- a/java/com/facebook/yoga/YogaErrata.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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. - */ - -// @generated by enums.py - -package com.facebook.yoga; - -public enum YogaErrata { - NONE(0), - STRETCH_FLEX_BASIS(1), - ABSOLUTE_POSITION_WITHOUT_INSETS_EXCLUDES_PADDING(2), - ABSOLUTE_PERCENT_AGAINST_INNER_SIZE(4), - ALL(2147483647), - CLASSIC(2147483646); - - private final int mIntValue; - - YogaErrata(int intValue) { - mIntValue = intValue; - } - - public int intValue() { - return mIntValue; - } - - public static YogaErrata fromInt(int value) { - switch (value) { - case 0: return NONE; - case 1: return STRETCH_FLEX_BASIS; - case 2: return ABSOLUTE_POSITION_WITHOUT_INSETS_EXCLUDES_PADDING; - case 4: return ABSOLUTE_PERCENT_AGAINST_INNER_SIZE; - case 2147483647: return ALL; - case 2147483646: return CLASSIC; - default: throw new IllegalArgumentException("Unknown enum value: " + value); - } - } -} diff --git a/java/com/facebook/yoga/YogaErrata.kt b/java/com/facebook/yoga/YogaErrata.kt new file mode 100644 index 0000000000..940f2861e1 --- /dev/null +++ b/java/com/facebook/yoga/YogaErrata.kt @@ -0,0 +1,35 @@ +/* + * 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. + */ + +// @generated by enums.py + +package com.facebook.yoga + +public enum class YogaErrata(public val intValue: Int) { + NONE(0), + STRETCH_FLEX_BASIS(1), + ABSOLUTE_POSITION_WITHOUT_INSETS_EXCLUDES_PADDING(2), + ABSOLUTE_PERCENT_AGAINST_INNER_SIZE(4), + ALL(2147483647), + CLASSIC(2147483646); + + public fun intValue(): Int = intValue + + public companion object { + @JvmStatic + public fun fromInt(value: Int): YogaErrata = + when (value) { + 0 -> NONE + 1 -> STRETCH_FLEX_BASIS + 2 -> ABSOLUTE_POSITION_WITHOUT_INSETS_EXCLUDES_PADDING + 4 -> ABSOLUTE_PERCENT_AGAINST_INNER_SIZE + 2147483647 -> ALL + 2147483646 -> CLASSIC + else -> throw IllegalArgumentException("Unknown enum value: $value") + } + } +} diff --git a/java/com/facebook/yoga/YogaExperimentalFeature.java b/java/com/facebook/yoga/YogaExperimentalFeature.java deleted file mode 100644 index 32e643439e..0000000000 --- a/java/com/facebook/yoga/YogaExperimentalFeature.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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. - */ - -// @generated by enums.py - -package com.facebook.yoga; - -public enum YogaExperimentalFeature { - WEB_FLEX_BASIS(0), - FIX_FLEX_BASIS_FIT_CONTENT(1); - - private final int mIntValue; - - YogaExperimentalFeature(int intValue) { - mIntValue = intValue; - } - - public int intValue() { - return mIntValue; - } - - public static YogaExperimentalFeature fromInt(int value) { - switch (value) { - case 0: return WEB_FLEX_BASIS; - case 1: return FIX_FLEX_BASIS_FIT_CONTENT; - default: throw new IllegalArgumentException("Unknown enum value: " + value); - } - } -} diff --git a/java/com/facebook/yoga/YogaExperimentalFeature.kt b/java/com/facebook/yoga/YogaExperimentalFeature.kt new file mode 100644 index 0000000000..aec70ac246 --- /dev/null +++ b/java/com/facebook/yoga/YogaExperimentalFeature.kt @@ -0,0 +1,27 @@ +/* + * 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. + */ + +// @generated by enums.py + +package com.facebook.yoga + +public enum class YogaExperimentalFeature(public val intValue: Int) { + WEB_FLEX_BASIS(0), + FIX_FLEX_BASIS_FIT_CONTENT(1); + + public fun intValue(): Int = intValue + + public companion object { + @JvmStatic + public fun fromInt(value: Int): YogaExperimentalFeature = + when (value) { + 0 -> WEB_FLEX_BASIS + 1 -> FIX_FLEX_BASIS_FIT_CONTENT + else -> throw IllegalArgumentException("Unknown enum value: $value") + } + } +} diff --git a/java/com/facebook/yoga/YogaFlexDirection.java b/java/com/facebook/yoga/YogaFlexDirection.java deleted file mode 100644 index 719888a186..0000000000 --- a/java/com/facebook/yoga/YogaFlexDirection.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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. - */ - -// @generated by enums.py - -package com.facebook.yoga; - -public enum YogaFlexDirection { - COLUMN(0), - COLUMN_REVERSE(1), - ROW(2), - ROW_REVERSE(3); - - private final int mIntValue; - - YogaFlexDirection(int intValue) { - mIntValue = intValue; - } - - public int intValue() { - return mIntValue; - } - - public static YogaFlexDirection fromInt(int value) { - switch (value) { - case 0: return COLUMN; - case 1: return COLUMN_REVERSE; - case 2: return ROW; - case 3: return ROW_REVERSE; - default: throw new IllegalArgumentException("Unknown enum value: " + value); - } - } -} diff --git a/java/com/facebook/yoga/YogaFlexDirection.kt b/java/com/facebook/yoga/YogaFlexDirection.kt new file mode 100644 index 0000000000..d9ed0e3286 --- /dev/null +++ b/java/com/facebook/yoga/YogaFlexDirection.kt @@ -0,0 +1,31 @@ +/* + * 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. + */ + +// @generated by enums.py + +package com.facebook.yoga + +public enum class YogaFlexDirection(public val intValue: Int) { + COLUMN(0), + COLUMN_REVERSE(1), + ROW(2), + ROW_REVERSE(3); + + public fun intValue(): Int = intValue + + public companion object { + @JvmStatic + public fun fromInt(value: Int): YogaFlexDirection = + when (value) { + 0 -> COLUMN + 1 -> COLUMN_REVERSE + 2 -> ROW + 3 -> ROW_REVERSE + else -> throw IllegalArgumentException("Unknown enum value: $value") + } + } +} diff --git a/java/com/facebook/yoga/YogaGridTrackType.java b/java/com/facebook/yoga/YogaGridTrackType.java deleted file mode 100644 index e3d22d25be..0000000000 --- a/java/com/facebook/yoga/YogaGridTrackType.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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. - */ - -// @generated by enums.py - -package com.facebook.yoga; - -public enum YogaGridTrackType { - AUTO(0), - POINTS(1), - PERCENT(2), - FR(3), - MINMAX(4); - - private final int mIntValue; - - YogaGridTrackType(int intValue) { - mIntValue = intValue; - } - - public int intValue() { - return mIntValue; - } - - public static YogaGridTrackType fromInt(int value) { - switch (value) { - case 0: return AUTO; - case 1: return POINTS; - case 2: return PERCENT; - case 3: return FR; - case 4: return MINMAX; - default: throw new IllegalArgumentException("Unknown enum value: " + value); - } - } -} diff --git a/java/com/facebook/yoga/YogaGridTrackType.kt b/java/com/facebook/yoga/YogaGridTrackType.kt new file mode 100644 index 0000000000..61dc615590 --- /dev/null +++ b/java/com/facebook/yoga/YogaGridTrackType.kt @@ -0,0 +1,33 @@ +/* + * 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. + */ + +// @generated by enums.py + +package com.facebook.yoga + +public enum class YogaGridTrackType(public val intValue: Int) { + AUTO(0), + POINTS(1), + PERCENT(2), + FR(3), + MINMAX(4); + + public fun intValue(): Int = intValue + + public companion object { + @JvmStatic + public fun fromInt(value: Int): YogaGridTrackType = + when (value) { + 0 -> AUTO + 1 -> POINTS + 2 -> PERCENT + 3 -> FR + 4 -> MINMAX + else -> throw IllegalArgumentException("Unknown enum value: $value") + } + } +} diff --git a/java/com/facebook/yoga/YogaGutter.java b/java/com/facebook/yoga/YogaGutter.java deleted file mode 100644 index 2b4be1c08d..0000000000 --- a/java/com/facebook/yoga/YogaGutter.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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. - */ - -// @generated by enums.py - -package com.facebook.yoga; - -public enum YogaGutter { - COLUMN(0), - ROW(1), - ALL(2); - - private final int mIntValue; - - YogaGutter(int intValue) { - mIntValue = intValue; - } - - public int intValue() { - return mIntValue; - } - - public static YogaGutter fromInt(int value) { - switch (value) { - case 0: return COLUMN; - case 1: return ROW; - case 2: return ALL; - default: throw new IllegalArgumentException("Unknown enum value: " + value); - } - } -} diff --git a/java/com/facebook/yoga/YogaGutter.kt b/java/com/facebook/yoga/YogaGutter.kt new file mode 100644 index 0000000000..873423a589 --- /dev/null +++ b/java/com/facebook/yoga/YogaGutter.kt @@ -0,0 +1,29 @@ +/* + * 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. + */ + +// @generated by enums.py + +package com.facebook.yoga + +public enum class YogaGutter(public val intValue: Int) { + COLUMN(0), + ROW(1), + ALL(2); + + public fun intValue(): Int = intValue + + public companion object { + @JvmStatic + public fun fromInt(value: Int): YogaGutter = + when (value) { + 0 -> COLUMN + 1 -> ROW + 2 -> ALL + else -> throw IllegalArgumentException("Unknown enum value: $value") + } + } +} diff --git a/java/com/facebook/yoga/YogaJustify.java b/java/com/facebook/yoga/YogaJustify.java deleted file mode 100644 index 778238ec66..0000000000 --- a/java/com/facebook/yoga/YogaJustify.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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. - */ - -// @generated by enums.py - -package com.facebook.yoga; - -public enum YogaJustify { - AUTO(0), - FLEX_START(1), - CENTER(2), - FLEX_END(3), - SPACE_BETWEEN(4), - SPACE_AROUND(5), - SPACE_EVENLY(6), - STRETCH(7), - START(8), - END(9); - - private final int mIntValue; - - YogaJustify(int intValue) { - mIntValue = intValue; - } - - public int intValue() { - return mIntValue; - } - - public static YogaJustify fromInt(int value) { - switch (value) { - case 0: return AUTO; - case 1: return FLEX_START; - case 2: return CENTER; - case 3: return FLEX_END; - case 4: return SPACE_BETWEEN; - case 5: return SPACE_AROUND; - case 6: return SPACE_EVENLY; - case 7: return STRETCH; - case 8: return START; - case 9: return END; - default: throw new IllegalArgumentException("Unknown enum value: " + value); - } - } -} diff --git a/java/com/facebook/yoga/YogaJustify.kt b/java/com/facebook/yoga/YogaJustify.kt new file mode 100644 index 0000000000..731b034ef2 --- /dev/null +++ b/java/com/facebook/yoga/YogaJustify.kt @@ -0,0 +1,43 @@ +/* + * 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. + */ + +// @generated by enums.py + +package com.facebook.yoga + +public enum class YogaJustify(public val intValue: Int) { + AUTO(0), + FLEX_START(1), + CENTER(2), + FLEX_END(3), + SPACE_BETWEEN(4), + SPACE_AROUND(5), + SPACE_EVENLY(6), + STRETCH(7), + START(8), + END(9); + + public fun intValue(): Int = intValue + + public companion object { + @JvmStatic + public fun fromInt(value: Int): YogaJustify = + when (value) { + 0 -> AUTO + 1 -> FLEX_START + 2 -> CENTER + 3 -> FLEX_END + 4 -> SPACE_BETWEEN + 5 -> SPACE_AROUND + 6 -> SPACE_EVENLY + 7 -> STRETCH + 8 -> START + 9 -> END + else -> throw IllegalArgumentException("Unknown enum value: $value") + } + } +} diff --git a/java/com/facebook/yoga/YogaLogLevel.java b/java/com/facebook/yoga/YogaLogLevel.java deleted file mode 100644 index 761f302eb2..0000000000 --- a/java/com/facebook/yoga/YogaLogLevel.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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. - */ - -// @generated by enums.py - -package com.facebook.yoga; - -import com.facebook.yoga.annotations.DoNotStrip; - -@DoNotStrip -public enum YogaLogLevel { - ERROR(0), - WARN(1), - INFO(2), - DEBUG(3), - VERBOSE(4), - FATAL(5); - - private final int mIntValue; - - YogaLogLevel(int intValue) { - mIntValue = intValue; - } - - public int intValue() { - return mIntValue; - } - - @DoNotStrip - public static YogaLogLevel fromInt(int value) { - switch (value) { - case 0: return ERROR; - case 1: return WARN; - case 2: return INFO; - case 3: return DEBUG; - case 4: return VERBOSE; - case 5: return FATAL; - default: throw new IllegalArgumentException("Unknown enum value: " + value); - } - } -} diff --git a/java/com/facebook/yoga/YogaLogLevel.kt b/java/com/facebook/yoga/YogaLogLevel.kt new file mode 100644 index 0000000000..a8ff53fdd9 --- /dev/null +++ b/java/com/facebook/yoga/YogaLogLevel.kt @@ -0,0 +1,39 @@ +/* + * 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. + */ + +// @generated by enums.py + +package com.facebook.yoga + +import com.facebook.yoga.annotations.DoNotStrip + +@DoNotStrip +public enum class YogaLogLevel(public val intValue: Int) { + ERROR(0), + WARN(1), + INFO(2), + DEBUG(3), + VERBOSE(4), + FATAL(5); + + public fun intValue(): Int = intValue + + public companion object { + @DoNotStrip + @JvmStatic + public fun fromInt(value: Int): YogaLogLevel = + when (value) { + 0 -> ERROR + 1 -> WARN + 2 -> INFO + 3 -> DEBUG + 4 -> VERBOSE + 5 -> FATAL + else -> throw IllegalArgumentException("Unknown enum value: $value") + } + } +} diff --git a/java/com/facebook/yoga/YogaMeasureMode.java b/java/com/facebook/yoga/YogaMeasureMode.java deleted file mode 100644 index 0c77c23897..0000000000 --- a/java/com/facebook/yoga/YogaMeasureMode.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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. - */ - -// @generated by enums.py - -package com.facebook.yoga; - -public enum YogaMeasureMode { - UNDEFINED(0), - EXACTLY(1), - AT_MOST(2); - - private final int mIntValue; - - YogaMeasureMode(int intValue) { - mIntValue = intValue; - } - - public int intValue() { - return mIntValue; - } - - public static YogaMeasureMode fromInt(int value) { - switch (value) { - case 0: return UNDEFINED; - case 1: return EXACTLY; - case 2: return AT_MOST; - default: throw new IllegalArgumentException("Unknown enum value: " + value); - } - } -} diff --git a/java/com/facebook/yoga/YogaMeasureMode.kt b/java/com/facebook/yoga/YogaMeasureMode.kt new file mode 100644 index 0000000000..1a41cc6eec --- /dev/null +++ b/java/com/facebook/yoga/YogaMeasureMode.kt @@ -0,0 +1,29 @@ +/* + * 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. + */ + +// @generated by enums.py + +package com.facebook.yoga + +public enum class YogaMeasureMode(public val intValue: Int) { + UNDEFINED(0), + EXACTLY(1), + AT_MOST(2); + + public fun intValue(): Int = intValue + + public companion object { + @JvmStatic + public fun fromInt(value: Int): YogaMeasureMode = + when (value) { + 0 -> UNDEFINED + 1 -> EXACTLY + 2 -> AT_MOST + else -> throw IllegalArgumentException("Unknown enum value: $value") + } + } +} diff --git a/java/tests/com/facebook/yoga/TestParametrization.java b/java/tests/com/facebook/yoga/TestParametrization.java deleted file mode 100644 index a332c08f02..0000000000 --- a/java/tests/com/facebook/yoga/TestParametrization.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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. - */ - -package com.facebook.yoga; - -import java.util.Arrays; -import java.util.List; - -public class TestParametrization { - public static Iterable nodeFactories() { - NodeFactory nodeFactory = new NodeFactory() { - @Override - public YogaNode create() { - return YogaNodeFactory.create(); - } - - @Override - public YogaNode create(YogaConfig config) { - return YogaNodeFactory.create(config); - } - - @Override - public String toString() { - return "JNI"; - } - }; - return Arrays.asList(nodeFactory); - } - - - public interface NodeFactory { - YogaNode create(); - YogaNode create(YogaConfig config); - } -} diff --git a/java/tests/com/facebook/yoga/TestParametrization.kt b/java/tests/com/facebook/yoga/TestParametrization.kt new file mode 100644 index 0000000000..95b8c8d17c --- /dev/null +++ b/java/tests/com/facebook/yoga/TestParametrization.kt @@ -0,0 +1,29 @@ +/* + * 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. + */ + +package com.facebook.yoga + +object TestParametrization { + @JvmStatic + fun nodeFactories(): Iterable { + val nodeFactory = + object : NodeFactory { + override fun create(): YogaNode = YogaNodeFactory.create() + + override fun create(config: YogaConfig): YogaNode = YogaNodeFactory.create(config) + + override fun toString(): String = "JNI" + } + return listOf(nodeFactory) + } + + interface NodeFactory { + fun create(): YogaNode + + fun create(config: YogaConfig): YogaNode + } +} diff --git a/java/tests/com/facebook/yoga/YGAlignBaselineTest.java b/java/tests/com/facebook/yoga/YGAlignBaselineTest.java deleted file mode 100644 index d0eeb52f52..0000000000 --- a/java/tests/com/facebook/yoga/YGAlignBaselineTest.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * 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. - */ - -package com.facebook.yoga; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -@RunWith(Parameterized.class) -public class YGAlignBaselineTest { - @Parameterized.Parameters(name = "{0}") - public static Iterable nodeFactories() { - return TestParametrization.nodeFactories(); - } - - @Parameterized.Parameter public TestParametrization.NodeFactory mNodeFactory; - - private YogaBaselineFunction getBaselineFunc() { - return new YogaBaselineFunction() { - @Override - public float baseline(YogaNode node, float width, float height) { - return height / 2; - } - }; - } - - @Test - public void test_align_baseline_parent_using_child_in_column_as_reference() { - YogaConfig config = YogaConfigFactory.create(); - - final YogaNode root = createYGNode(config, YogaFlexDirection.ROW, 1000f, 1000f, true); - - final YogaNode root_child0 = createYGNode(config, YogaFlexDirection.COLUMN, 500f, 600f, false); - root.addChildAt(root_child0, 0); - - final YogaNode root_child1 = createYGNode(config, YogaFlexDirection.COLUMN, 500f, 800f, false); - root.addChildAt(root_child1, 1); - - final YogaNode root_child1_child0 = - createYGNode(config, YogaFlexDirection.COLUMN, 500f, 300f, false); - root_child1.addChildAt(root_child1_child0, 0); - - final YogaNode root_child1_child1 = - createYGNode(config, YogaFlexDirection.COLUMN, 500f, 400f, false); - root_child1_child1.setBaselineFunction(getBaselineFunc()); - root_child1_child1.setIsReferenceBaseline(true); - root_child1.addChildAt(root_child1_child1, 1); - - root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); - - assertEquals(0f, root_child0.getLayoutX(), 0.0f); - assertEquals(0f, root_child0.getLayoutY(), 0.0f); - - assertEquals(500f, root_child1.getLayoutX(), 0.0f); - assertEquals(100f, root_child1.getLayoutY(), 0.0f); - - assertEquals(0f, root_child1_child0.getLayoutX(), 0.0f); - assertEquals(0f, root_child1_child0.getLayoutY(), 0.0f); - - assertEquals(0f, root_child1_child1.getLayoutX(), 0.0f); - assertEquals(300f, root_child1_child1.getLayoutY(), 0.0f); - } - - @Test - public void test_align_baseline_parent_using_child_in_row_as_reference() { - YogaConfig config = YogaConfigFactory.create(); - - final YogaNode root = createYGNode(config, YogaFlexDirection.ROW, 1000f, 1000f, true); - - final YogaNode root_child0 = createYGNode(config, YogaFlexDirection.COLUMN, 500f, 600f, false); - root.addChildAt(root_child0, 0); - - final YogaNode root_child1 = createYGNode(config, YogaFlexDirection.ROW, 500f, 800f, true); - root.addChildAt(root_child1, 1); - - final YogaNode root_child1_child0 = - createYGNode(config, YogaFlexDirection.COLUMN, 500f, 500f, false); - root_child1.addChildAt(root_child1_child0, 0); - - final YogaNode root_child1_child1 = - createYGNode(config, YogaFlexDirection.COLUMN, 500f, 400f, false); - root_child1_child1.setBaselineFunction(getBaselineFunc()); - root_child1_child1.setIsReferenceBaseline(true); - root_child1.addChildAt(root_child1_child1, 1); - - root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); - - assertEquals(0f, root_child0.getLayoutX(), 0.0f); - assertEquals(0f, root_child0.getLayoutY(), 0.0f); - - assertEquals(500f, root_child1.getLayoutX(), 0.0f); - assertEquals(100f, root_child1.getLayoutY(), 0.0f); - - assertEquals(0f, root_child1_child0.getLayoutX(), 0.0f); - assertEquals(0f, root_child1_child0.getLayoutY(), 0.0f); - - assertEquals(500f, root_child1_child1.getLayoutX(), 0.0f); - assertEquals(300f, root_child1_child1.getLayoutY(), 0.0f); - } - - private YogaNode createYGNode( - YogaConfig config, - YogaFlexDirection flexDirection, - float width, - float height, - boolean alignBaseline) { - YogaNode node = mNodeFactory.create(config); - node.setFlexDirection(flexDirection); - node.setWidth(width); - node.setHeight(height); - if (alignBaseline) { - node.setAlignItems(YogaAlign.BASELINE); - } - return node; - } -} diff --git a/java/tests/com/facebook/yoga/YGAlignBaselineTest.kt b/java/tests/com/facebook/yoga/YGAlignBaselineTest.kt new file mode 100644 index 0000000000..587a89ec1b --- /dev/null +++ b/java/tests/com/facebook/yoga/YGAlignBaselineTest.kt @@ -0,0 +1,117 @@ +/* + * 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. + */ + +package com.facebook.yoga + +import org.junit.Assert.assertEquals +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.Parameterized + +@RunWith(Parameterized::class) +class YGAlignBaselineTest { + companion object { + @JvmStatic + @Parameterized.Parameters(name = "{0}") + fun nodeFactories(): Iterable = + TestParametrization.nodeFactories() + } + + @Parameterized.Parameter lateinit var mNodeFactory: TestParametrization.NodeFactory + + private fun getBaselineFunc(): YogaBaselineFunction = + YogaBaselineFunction { node, width, height -> + height / 2 + } + + @Test + fun test_align_baseline_parent_using_child_in_column_as_reference() { + val config = YogaConfigFactory.create() + + val root = createYGNode(config, YogaFlexDirection.ROW, 1000f, 1000f, true) + + val root_child0 = createYGNode(config, YogaFlexDirection.COLUMN, 500f, 600f, false) + root.addChildAt(root_child0, 0) + + val root_child1 = createYGNode(config, YogaFlexDirection.COLUMN, 500f, 800f, false) + root.addChildAt(root_child1, 1) + + val root_child1_child0 = createYGNode(config, YogaFlexDirection.COLUMN, 500f, 300f, false) + root_child1.addChildAt(root_child1_child0, 0) + + val root_child1_child1 = createYGNode(config, YogaFlexDirection.COLUMN, 500f, 400f, false) + root_child1_child1.setBaselineFunction(getBaselineFunc()) + root_child1_child1.setIsReferenceBaseline(true) + root_child1.addChildAt(root_child1_child1, 1) + + root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED) + + assertEquals(0f, root_child0.getLayoutX(), 0.0f) + assertEquals(0f, root_child0.getLayoutY(), 0.0f) + + assertEquals(500f, root_child1.getLayoutX(), 0.0f) + assertEquals(100f, root_child1.getLayoutY(), 0.0f) + + assertEquals(0f, root_child1_child0.getLayoutX(), 0.0f) + assertEquals(0f, root_child1_child0.getLayoutY(), 0.0f) + + assertEquals(0f, root_child1_child1.getLayoutX(), 0.0f) + assertEquals(300f, root_child1_child1.getLayoutY(), 0.0f) + } + + @Test + fun test_align_baseline_parent_using_child_in_row_as_reference() { + val config = YogaConfigFactory.create() + + val root = createYGNode(config, YogaFlexDirection.ROW, 1000f, 1000f, true) + + val root_child0 = createYGNode(config, YogaFlexDirection.COLUMN, 500f, 600f, false) + root.addChildAt(root_child0, 0) + + val root_child1 = createYGNode(config, YogaFlexDirection.ROW, 500f, 800f, true) + root.addChildAt(root_child1, 1) + + val root_child1_child0 = createYGNode(config, YogaFlexDirection.COLUMN, 500f, 500f, false) + root_child1.addChildAt(root_child1_child0, 0) + + val root_child1_child1 = createYGNode(config, YogaFlexDirection.COLUMN, 500f, 400f, false) + root_child1_child1.setBaselineFunction(getBaselineFunc()) + root_child1_child1.setIsReferenceBaseline(true) + root_child1.addChildAt(root_child1_child1, 1) + + root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED) + + assertEquals(0f, root_child0.getLayoutX(), 0.0f) + assertEquals(0f, root_child0.getLayoutY(), 0.0f) + + assertEquals(500f, root_child1.getLayoutX(), 0.0f) + assertEquals(100f, root_child1.getLayoutY(), 0.0f) + + assertEquals(0f, root_child1_child0.getLayoutX(), 0.0f) + assertEquals(0f, root_child1_child0.getLayoutY(), 0.0f) + + assertEquals(500f, root_child1_child1.getLayoutX(), 0.0f) + assertEquals(300f, root_child1_child1.getLayoutY(), 0.0f) + } + + private fun createYGNode( + config: YogaConfig, + flexDirection: YogaFlexDirection, + width: Float, + height: Float, + alignBaseline: Boolean, + ): YogaNode { + val node = mNodeFactory.create(config) + node.setFlexDirection(flexDirection) + node.setWidth(width) + node.setHeight(height) + if (alignBaseline) { + node.setAlignItems(YogaAlign.BASELINE) + } + return node + } +} diff --git a/java/tests/com/facebook/yoga/YogaExceptionTest.java b/java/tests/com/facebook/yoga/YogaExceptionTest.java deleted file mode 100644 index 058cf58911..0000000000 --- a/java/tests/com/facebook/yoga/YogaExceptionTest.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * 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. - */ - -package com.facebook.yoga; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.lang.ref.WeakReference; -import java.util.ArrayList; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicReference; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -@RunWith(Parameterized.class) -public class YogaExceptionTest { - @Parameterized.Parameters(name = "{0}") - public static Iterable nodeFactories() { - return TestParametrization.nodeFactories(); - } - - @Parameterized.Parameter public TestParametrization.NodeFactory mNodeFactory; - - @Test(expected = RuntimeException.class) - public void testBaselineThrows() { - final YogaNode root = createNode(); - root.setFlexDirection(YogaFlexDirection.ROW); - root.setAlignItems(YogaAlign.BASELINE); - - final YogaNode child1 = createNode(); - root.addChildAt(child1, 0); - - final YogaNode child2 = createNode(); - child2.setBaselineFunction(new YogaBaselineFunction() { - public float baseline(YogaNode node, float width, float height) { - throw new RuntimeException(); - } - }); - root.addChildAt(child2, 1); - - root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); - } - - @Test - public void testBaselineThrowsAndStops() { - final YogaNode root = createNode(); - root.setFlexDirection(YogaFlexDirection.ROW); - root.setAlignItems(YogaAlign.BASELINE); - - final YogaNode child1 = createNode(); - root.addChildAt(child1, 0); - - final YogaNode child2 = createNode(); - final AtomicReference expected = new AtomicReference(); - child2.setBaselineFunction(new YogaBaselineFunction() { - public float baseline(YogaNode node, float width, float height) { - RuntimeException e = new RuntimeException(); - expected.set(e); - throw e; - } - }); - root.addChildAt(child2, 1); - - final YogaNode child3 = createNode(); - final AtomicBoolean child3Called = new AtomicBoolean(); - child3.setBaselineFunction(new YogaBaselineFunction() { - public float baseline(YogaNode node, float width, float height) { - child3Called.set(true); - return 1.0f; - } - }); - root.addChildAt(child3, 2); - - try { - root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); - fail(); - } catch (RuntimeException e) { - assertEquals(expected.get(), e); - } - assertFalse(child3Called.get()); - } - - @Test(expected = RuntimeException.class) - public void testMeasureThrows() { - final YogaNode node = createNode(); - node.setMeasureFunction(new YogaMeasureFunction() { - public long measure( - YogaNode node, - float width, - YogaMeasureMode widthMode, - float height, - YogaMeasureMode heightMode) { - throw new RuntimeException(); - } - }); - node.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); - } - - @Test - public void testMeasureThrowsAndStops() { - final YogaNode root = createNode(); - root.setFlexDirection(YogaFlexDirection.ROW); - root.setAlignItems(YogaAlign.BASELINE); - - final YogaNode child1 = createNode(); - root.addChildAt(child1, 0); - - final YogaNode child2 = createNode(); - final AtomicReference expected = new AtomicReference(); - child2.setMeasureFunction(new YogaMeasureFunction() { - public long measure( - YogaNode node, - float width, - YogaMeasureMode widthMode, - float height, - YogaMeasureMode heightMode) { - RuntimeException e = new RuntimeException(); - expected.set(e); - throw e; - } - }); - root.addChildAt(child2, 1); - - final YogaNode child3 = createNode(); - final AtomicBoolean child3Called = new AtomicBoolean(); - child3.setMeasureFunction(new YogaMeasureFunction() { - public long measure( - YogaNode node, - float width, - YogaMeasureMode widthMode, - float height, - YogaMeasureMode heightMode) { - child3Called.set(true); - return 1; - } - }); - root.addChildAt(child3, 2); - - try { - root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); - fail(); - } catch (RuntimeException e) { - assertEquals(expected.get(), e); - } - assertFalse(child3Called.get()); - } - - private YogaNode createNode() { - return mNodeFactory.create(); - } - - private YogaNode createNode(YogaConfig config) { - return mNodeFactory.create(config); - } -} diff --git a/java/tests/com/facebook/yoga/YogaExceptionTest.kt b/java/tests/com/facebook/yoga/YogaExceptionTest.kt new file mode 100644 index 0000000000..1aa8cc1f2a --- /dev/null +++ b/java/tests/com/facebook/yoga/YogaExceptionTest.kt @@ -0,0 +1,140 @@ +/* + * 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. + */ + +package com.facebook.yoga + +import java.util.concurrent.atomic.AtomicBoolean +import java.util.concurrent.atomic.AtomicReference +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.fail +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.Parameterized + +@RunWith(Parameterized::class) +class YogaExceptionTest { + companion object { + @JvmStatic + @Parameterized.Parameters(name = "{0}") + fun nodeFactories(): Iterable = + TestParametrization.nodeFactories() + } + + @Parameterized.Parameter lateinit var mNodeFactory: TestParametrization.NodeFactory + + @Test(expected = RuntimeException::class) + fun testBaselineThrows() { + val root = createNode() + root.setFlexDirection(YogaFlexDirection.ROW) + root.setAlignItems(YogaAlign.BASELINE) + + val child1 = createNode() + root.addChildAt(child1, 0) + + val child2 = createNode() + child2.setBaselineFunction( + YogaBaselineFunction { node, width, height -> throw RuntimeException() } + ) + root.addChildAt(child2, 1) + + root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED) + } + + @Test + fun testBaselineThrowsAndStops() { + val root = createNode() + root.setFlexDirection(YogaFlexDirection.ROW) + root.setAlignItems(YogaAlign.BASELINE) + + val child1 = createNode() + root.addChildAt(child1, 0) + + val child2 = createNode() + val expected = AtomicReference() + child2.setBaselineFunction( + YogaBaselineFunction { node, width, height -> + val e = RuntimeException() + expected.set(e) + throw e + } + ) + root.addChildAt(child2, 1) + + val child3 = createNode() + val child3Called = AtomicBoolean() + child3.setBaselineFunction( + YogaBaselineFunction { node, width, height -> + child3Called.set(true) + 1.0f + } + ) + root.addChildAt(child3, 2) + + try { + root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED) + fail() + } catch (e: RuntimeException) { + assertEquals(expected.get(), e) + } + assertFalse(child3Called.get()) + } + + @Test(expected = RuntimeException::class) + fun testMeasureThrows() { + val node = createNode() + node.setMeasureFunction( + YogaMeasureFunction { node, width, widthMode, height, heightMode -> + throw RuntimeException() + } + ) + node.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED) + } + + @Test + fun testMeasureThrowsAndStops() { + val root = createNode() + root.setFlexDirection(YogaFlexDirection.ROW) + root.setAlignItems(YogaAlign.BASELINE) + + val child1 = createNode() + root.addChildAt(child1, 0) + + val child2 = createNode() + val expected = AtomicReference() + child2.setMeasureFunction( + YogaMeasureFunction { node, width, widthMode, height, heightMode -> + val e = RuntimeException() + expected.set(e) + throw e + } + ) + root.addChildAt(child2, 1) + + val child3 = createNode() + val child3Called = AtomicBoolean() + child3.setMeasureFunction( + YogaMeasureFunction { node, width, widthMode, height, heightMode -> + child3Called.set(true) + 1L + } + ) + root.addChildAt(child3, 2) + + try { + root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED) + fail() + } catch (e: RuntimeException) { + assertEquals(expected.get(), e) + } + assertFalse(child3Called.get()) + } + + private fun createNode(): YogaNode = mNodeFactory.create() + + private fun createNode(config: YogaConfig): YogaNode = mNodeFactory.create(config) +} diff --git a/java/tests/com/facebook/yoga/YogaLoggerTest.java b/java/tests/com/facebook/yoga/YogaLoggerTest.java deleted file mode 100644 index aa33a2d509..0000000000 --- a/java/tests/com/facebook/yoga/YogaLoggerTest.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * 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. - */ - -package com.facebook.yoga; - -import org.junit.Test; -import java.lang.ref.WeakReference; -import java.util.List; -import java.util.ArrayList; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -public class YogaLoggerTest { - - @Test - public void testRemovingLoggerFromConfig() throws Exception { - final List logs = new ArrayList<>(); - - final YogaConfig config = YogaConfigFactory.create(); - YogaLogger logger = new YogaLogger() { - @Override - public void log(YogaLogLevel level, String message) { - logs.add(message); - } - }; - config.setLogger(logger); - - final YogaNode root = YogaNodeFactory.create(config); - root.setFlexDirection(YogaFlexDirection.ROW); - root.setAlignItems(YogaAlign.BASELINE); - - final YogaNode child1 = YogaNodeFactory.create(config); - root.addChildAt(child1, 0); - - final YogaNode child2 = YogaNodeFactory.create(config); - child2.setBaselineFunction(new YogaBaselineFunction() { - public float baseline(YogaNode node, float width, float height) { - return Float.NaN; - } - }); - root.addChildAt(child2, 1); - - assertEquals(logs.size(), 0); - try { - root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); - fail("Expected calculateLayout to throw"); - } catch (IllegalStateException e) { - } - - assertEquals(logs.size(), 1); - - config.setLogger(null); - - try { - root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); - fail("Expected calculateLayout to throw again"); - } catch (IllegalStateException e) { - } - - assertEquals(logs.size(), 1); - } - - @Test - public void testLoggerLeak() throws Exception { - final YogaConfig config = YogaConfigFactory.create(); - YogaLogger logger = new YogaLogger() { - @Override - public void log(YogaLogLevel level, String message) { - } - }; - config.setLogger(logger); - config.setLogger(null); - WeakReference ref = new WeakReference(logger); - // noinspection UnusedAssignment - logger = null; - // try and free for the next 5 seconds, usually it works after the - // first GC attempt. - for (int i=0; i < 50; i++) { - System.gc(); - if (ref.get() == null) { - // free successfully - return; - } - Thread.sleep(100); - } - fail("YogaLogger leaked"); - } -} diff --git a/java/tests/com/facebook/yoga/YogaLoggerTest.kt b/java/tests/com/facebook/yoga/YogaLoggerTest.kt new file mode 100644 index 0000000000..c747e28f0c --- /dev/null +++ b/java/tests/com/facebook/yoga/YogaLoggerTest.kt @@ -0,0 +1,73 @@ +/* + * 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. + */ + +package com.facebook.yoga + +import java.lang.ref.WeakReference +import org.junit.Assert.assertEquals +import org.junit.Assert.fail +import org.junit.Test + +class YogaLoggerTest { + + @Test + fun testRemovingLoggerFromConfig() { + val logs = mutableListOf() + + val config = YogaConfigFactory.create() + var logger: YogaLogger? = YogaLogger { level, message -> logs.add(message) } + config.setLogger(logger) + + val root = YogaNodeFactory.create(config) + root.setFlexDirection(YogaFlexDirection.ROW) + root.setAlignItems(YogaAlign.BASELINE) + + val child1 = YogaNodeFactory.create(config) + root.addChildAt(child1, 0) + + val child2 = YogaNodeFactory.create(config) + child2.setBaselineFunction(YogaBaselineFunction { node, width, height -> Float.NaN }) + root.addChildAt(child2, 1) + + assertEquals(0, logs.size) + try { + root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED) + fail("Expected calculateLayout to throw") + } catch (e: IllegalStateException) {} + + assertEquals(1, logs.size) + + config.setLogger(null) + + try { + root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED) + fail("Expected calculateLayout to throw again") + } catch (e: IllegalStateException) {} + + assertEquals(1, logs.size) + } + + @Test + @Throws(Exception::class) + fun testLoggerLeak() { + val config = YogaConfigFactory.create() + var logger: YogaLogger? = YogaLogger { level, message -> } + config.setLogger(logger) + config.setLogger(null) + val ref = WeakReference(logger) + @Suppress("UNUSED_VALUE") + logger = null + for (i in 0 until 50) { + System.gc() + if (ref.get() == null) { + return + } + Thread.sleep(100) + } + fail("YogaLogger leaked") + } +} diff --git a/java/tests/com/facebook/yoga/YogaNodeStylePropertiesTest.java b/java/tests/com/facebook/yoga/YogaNodeStylePropertiesTest.java deleted file mode 100644 index 553c3ca09c..0000000000 --- a/java/tests/com/facebook/yoga/YogaNodeStylePropertiesTest.java +++ /dev/null @@ -1,1016 +0,0 @@ -/* - * 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. - */ - -package com.facebook.yoga; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -@RunWith(Parameterized.class) -public class YogaNodeStylePropertiesTest { - - @Parameterized.Parameters(name = "{0}") - public static Iterable nodeFactories() { - return TestParametrization.nodeFactories(); - } - - @Parameterized.Parameter public TestParametrization.NodeFactory mNodeFactory; - - private static final float UNDEFINED = YogaValue.UNDEFINED.value; - - @Test - public void testDirectionDefault() { - final YogaNode node = createNode(); - - assertEquals(node.getStyleDirection(), YogaDirection.INHERIT); - assertEquals(node.getLayoutDirection(), YogaDirection.INHERIT); - } - - @Test - public void testDirectionAssignment() { - final YogaNode node = createNode(); - node.setDirection(YogaDirection.LTR); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(YogaDirection.LTR, node.getStyleDirection()); - assertEquals(YogaDirection.LTR, node.getLayoutDirection()); - } - - @Test - public void testDirectionAffectsLayout() { - final YogaNode node = - style().direction(YogaDirection.RTL).width(200).children(style().widthPercent(40)).node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(120, node.getChildAt(0).getLayoutX(), 0); - } - - @Test - public void testFlexDirectionDefault() { - final YogaNode node = createNode(); - - assertEquals(YogaFlexDirection.COLUMN, node.getFlexDirection()); - } - - @Test - public void testFlexDirectionAssignment() { - final YogaNode node = style().flexDirection(YogaFlexDirection.COLUMN_REVERSE).node(); - - assertEquals(YogaFlexDirection.COLUMN_REVERSE, node.getFlexDirection()); - } - - @Test - public void testFlexDirectionAffectsLayout() { - final YogaNode node = - style() - .flexDirection(YogaFlexDirection.ROW_REVERSE) - .width(200) - .children(style().widthPercent(40)) - .node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(120, node.getChildAt(0).getLayoutX(), 0); - } - - @Test - public void testJustifyContentDefault() { - final YogaNode node = createNode(); - - assertEquals(YogaJustify.FLEX_START, node.getJustifyContent()); - } - - @Test - public void testJustifyContentAssignment() { - final YogaNode node = createNode(); - node.setJustifyContent(YogaJustify.SPACE_EVENLY); - - assertEquals(YogaJustify.SPACE_EVENLY, node.getJustifyContent()); - } - - @Test - public void testJustifyContentAffectsLayout() { - final YogaNode node = - style() - .justifyContent(YogaJustify.CENTER) - .height(200) - .children(style().heightPercent(40)) - .node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(60, node.getChildAt(0).getLayoutY(), 0); - } - - @Test - public void testAlignItemsDefault() { - final YogaNode node = createNode(); - - assertEquals(YogaAlign.STRETCH, node.getAlignItems()); - } - - @Test - public void testAlignItemsAssignment() { - final YogaNode node = createNode(); - node.setAlignItems(YogaAlign.SPACE_AROUND); - - assertEquals(YogaAlign.SPACE_AROUND, node.getAlignItems()); - } - - @Test - public void testAlignItemsAffectsLayout() { - final YogaNode node = - style().alignItems(YogaAlign.CENTER).height(200).children(style().widthPercent(40)).node(); - node.calculateLayout(200, UNDEFINED); - - assertEquals(60, node.getChildAt(0).getLayoutX(), 0); - } - - @Test - public void testAlignSelfDefault() { - final YogaNode node = createNode(); - - assertEquals(YogaAlign.AUTO, node.getAlignSelf()); - } - - @Test - public void testAlignSelfAssignment() { - final YogaNode node = createNode(); - node.setAlignSelf(YogaAlign.FLEX_END); - - assertEquals(YogaAlign.FLEX_END, node.getAlignSelf()); - } - - @Test - public void testAlignSelfAffectsLayout() { - final YogaNode node = - style().height(200).children(style().alignSelf(YogaAlign.CENTER).widthPercent(40)).node(); - node.calculateLayout(200, UNDEFINED); - - assertEquals(60, node.getChildAt(0).getLayoutX(), 0); - } - - @Test - public void testAlignContentDefault() { - final YogaNode node = createNode(); - - assertEquals(YogaAlign.FLEX_START, node.getAlignContent()); - } - - @Test - public void testAlignContentAssignment() { - final YogaNode node = createNode(); - node.setAlignContent(YogaAlign.BASELINE); - - assertEquals(YogaAlign.BASELINE, node.getAlignContent()); - } - - @Test - public void testAlignContentAffectsLayout() { - final YogaNode node = - style() - .alignContent(YogaAlign.SPACE_AROUND) - .flexWrap(YogaWrap.WRAP) - .height(200) - .width(200) - .children( - style().widthPercent(20).heightPercent(60), - style().widthPercent(20).heightPercent(60)) - .node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(30, node.getChildAt(0).getLayoutX(), 0); - } - - @Test - public void testPositionTypeDefault() { - final YogaNode node = createNode(); - - assertEquals(YogaPositionType.RELATIVE, node.getPositionType()); - } - - @Test - public void testPositionTypeAssignment() { - final YogaNode node = createNode(); - node.setPositionType(YogaPositionType.ABSOLUTE); - - assertEquals(YogaPositionType.ABSOLUTE, node.getPositionType()); - } - - @Test - public void testPositionTypeAffectsLayout() { - final YogaNode node = - style() - .height(200) - .children( - style().height(100), style().height(100).positionType(YogaPositionType.ABSOLUTE)) - .node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(0, node.getChildAt(1).getLayoutY(), 0); - } - - @Test - public void testWrapAffectsLayout() { - final YogaNode node = - style() - .width(200) - .height(200) - .flexWrap(YogaWrap.WRAP_REVERSE) - .children(style().width(10).heightPercent(60), style().heightPercent(60)) - .node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(190, node.getChildAt(0).getLayoutX(), 0); - } - - @Test - public void testOverflowDefault() { - final YogaNode node = createNode(); - - assertEquals(YogaOverflow.VISIBLE, node.getOverflow()); - } - - @Test - public void testOverflowAssignment() { - final YogaNode node = createNode(); - node.setOverflow(YogaOverflow.SCROLL); - - assertEquals(YogaOverflow.SCROLL, node.getOverflow()); - } - - // TODO add testOverflowAffectsLayout() - - @Test - public void testDisplayDefault() { - final YogaNode node = createNode(); - - assertEquals(YogaDisplay.FLEX, node.getDisplay()); - } - - @Test - public void testDisplayAssignment() { - final YogaNode node = createNode(); - node.setDisplay(YogaDisplay.NONE); - - assertEquals(YogaDisplay.NONE, node.getDisplay()); - } - - @Test - public void testDisplayAffectsLayout() { - final YogaNode node = - style().children(style().flexGrow(1).display(YogaDisplay.NONE), style().flexGrow(1)).node(); - node.calculateLayout(200, 200); - - assertEquals(200, node.getChildAt(1).getLayoutHeight(), 0); - } - - @Test - public void testFlexAffectsLayoutGrowing() { - final YogaNode node = style().height(200).children(style().height(100).flex(1.25f)).node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(200, node.getChildAt(0).getLayoutHeight(), 0); - } - - @Test - public void testFlexAffectsLayoutShrinking() { - final YogaNode node = style().height(200).children(style().height(300).flex(1.25f)).node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(200, node.getChildAt(0).getLayoutHeight(), 0); - } - - @Test - public void testFlexGrowDefault() { - final YogaNode node = createNode(); - - assertEquals(0, node.getFlexGrow(), 0); - } - - @Test - public void testFlexGrowAssignment() { - final YogaNode node = createNode(); - node.setFlexGrow(2.5f); - - assertEquals(2.5f, node.getFlexGrow(), 0); - } - - @Test - public void testFlexGrowAffectsLayout() { - final YogaNode node = - style().height(200).children(style().height(50).flexGrow(1), style().height(50)).node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(150, node.getChildAt(0).getLayoutHeight(), 0); - } - - @Test - public void testFlexShrinkDefault() { - final YogaNode node = createNode(); - - assertEquals(0, node.getFlexShrink(), 0); - } - - @Test - public void testFlexShrinkAssignment() { - final YogaNode node = createNode(); - node.setFlexShrink(2.5f); - - assertEquals(2.5f, node.getFlexShrink(), 0); - } - - @Test - public void testFlexShrinkAffectsLayout() { - final YogaNode node = - style().height(200).children(style().height(150).flexShrink(1), style().height(150)).node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(50, node.getChildAt(0).getLayoutHeight(), 0); - } - - @Test - public void testFlexBasisDefault() { - final YogaNode node = createNode(); - - assertEquals(YogaValue.AUTO, node.getFlexBasis()); - } - - @Test - public void testFlexBasisAssignment() { - final YogaNode node = createNode(); - node.setFlexBasis(50); - assertEquals(new YogaValue(50, YogaUnit.POINT), node.getFlexBasis()); - - node.setFlexBasisPercent(20); - assertEquals(new YogaValue(20, YogaUnit.PERCENT), node.getFlexBasis()); - - node.setFlexBasisAuto(); - assertEquals(YogaValue.AUTO, node.getFlexBasis()); - } - - @Test - public void testFlexBasisAffectsLayout() { - final YogaNode node = - style() - .height(200) - .children(style().flexBasis(150).flexShrink(1), style().flexBasis(150).flexShrink(1)) - .node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(100, node.getChildAt(0).getLayoutHeight(), 0); - } - - @Test - public void testFlexBasisPercentAffectsLayout() { - final YogaNode node = - style() - .height(200) - .children(style().flexBasisPercent(60), style().flexBasisPercent(40)) - .node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(80, node.getChildAt(1).getLayoutHeight(), 0); - } - - @Test - public void testMarginDefault() { - final YogaNode node = createNode(); - for (YogaEdge edge : YogaEdge.values()) { - assertEquals(YogaValue.UNDEFINED, node.getMargin(edge)); - } - } - - @Test - public void testMarginAssignment() { - final YogaNode node = createNode(); - for (YogaEdge edge : YogaEdge.values()) { - node.setMargin(edge, 25); - assertEquals(new YogaValue(25, YogaUnit.POINT), node.getMargin(edge)); - - node.setMarginPercent(edge, 5); - assertEquals(new YogaValue(5, YogaUnit.PERCENT), node.getMargin(edge)); - - node.setMarginAuto(edge); - assertEquals(YogaValue.AUTO, node.getMargin(edge)); - } - } - - @Test - public void testNegativeMarginAssignment() { - final YogaNode node = createNode(); - for (YogaEdge edge : YogaEdge.values()) { - node.setMargin(edge, -25); - assertEquals(new YogaValue(-25, YogaUnit.POINT), node.getMargin(edge)); - - node.setMarginPercent(edge, -5); - assertEquals(new YogaValue(-5, YogaUnit.PERCENT), node.getMargin(edge)); - - node.setMarginAuto(edge); - assertEquals(YogaValue.AUTO, node.getMargin(edge)); - } - } - - @Test - public void testMarginPointAffectsLayout() { - final YogaNode node = style().margin(YogaEdge.TOP, 42).node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(42, node.getLayoutY(), 0); - } - - @Test - public void testMarginPercentAffectsLayout() { - final YogaNode node = - style().height(200).children(style().flexGrow(1).marginPercent(YogaEdge.TOP, 20)).node(); - node.calculateLayout(200, 200); - - assertEquals(40, node.getChildAt(0).getLayoutY(), 0); - } - - @Test - public void testMarginAutoAffectsLayout() { - final YogaNode node = - style() - .width(200) - .flexDirection(YogaFlexDirection.ROW) - .children(style().marginAuto(YogaEdge.LEFT).marginAuto(YogaEdge.RIGHT).width(100)) - .node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(50, node.getChildAt(0).getLayoutX(), 0); - } - - @Test - public void testPaddingDefault() { - final YogaNode node = createNode(); - for (YogaEdge edge : YogaEdge.values()) { - assertEquals(YogaValue.UNDEFINED, node.getPadding(edge)); - } - } - - @Test - public void testPaddingAssignment() { - final YogaNode node = createNode(); - for (YogaEdge edge : YogaEdge.values()) { - node.setPadding(edge, 25); - assertEquals(new YogaValue(25, YogaUnit.POINT), node.getPadding(edge)); - - node.setPaddingPercent(edge, 5); - assertEquals(new YogaValue(5, YogaUnit.PERCENT), node.getPadding(edge)); - } - } - - @Test - public void testPaddingPointAffectsLayout() { - final YogaNode node = style().padding(YogaEdge.TOP, 42).children(style()).node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(42, node.getChildAt(0).getLayoutY(), 0); - } - - @Test - public void testPaddingPercentAffectsLayout() { - final YogaNode node = - style().height(200).paddingPercent(YogaEdge.TOP, 20).children(style().flexGrow(1)).node(); - node.calculateLayout(200, 200); - - assertEquals(40, node.getChildAt(0).getLayoutY(), 0); - } - - @Test - public void testBorderDefault() { - final YogaNode node = createNode(); - for (YogaEdge edge : YogaEdge.values()) { - assertEquals(UNDEFINED, node.getBorder(edge), 0); - } - } - - @Test - public void testBorderAssignment() { - final YogaNode node = createNode(); - for (YogaEdge edge : YogaEdge.values()) { - node.setBorder(edge, 2.5f); - assertEquals(2.5f, node.getBorder(edge), 0); - } - } - - @Test - public void testBorderAffectsLayout() { - final YogaNode node = style().border(YogaEdge.TOP, 42).children(style()).node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(42, node.getChildAt(0).getLayoutY(), 0); - } - - @Test - public void testPositionDefault() { - final YogaNode node = createNode(); - for (YogaEdge edge : YogaEdge.values()) { - assertEquals(YogaValue.UNDEFINED, node.getPosition(edge)); - } - } - - @Test - public void testPositionAssignment() { - final YogaNode node = createNode(); - for (YogaEdge edge : YogaEdge.values()) { - node.setPosition(edge, 25); - assertEquals(new YogaValue(25, YogaUnit.POINT), node.getPosition(edge)); - - node.setPositionPercent(edge, 5); - assertEquals(new YogaValue(5, YogaUnit.PERCENT), node.getPosition(edge)); - } - } - - @Test - public void testPositionAffectsLayout() { - final YogaNode node = - style() - .height(100) - .children( - style() - .positionType(YogaPositionType.ABSOLUTE) - .position(YogaEdge.TOP, 11) - .position(YogaEdge.BOTTOM, 22)) - .node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(67, node.getChildAt(0).getLayoutHeight(), 0); - } - - @Test - public void testPositionPercentAffectsLayout() { - final YogaNode node = - style() - .width(100) - .children( - style() - .positionType(YogaPositionType.ABSOLUTE) - .positionPercent(YogaEdge.LEFT, 11) - .positionPercent(YogaEdge.RIGHT, 22)) - .node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(67, node.getChildAt(0).getLayoutWidth(), 0); - } - - @Test - public void testWidthDefault() { - final YogaNode node = createNode(); - - assertEquals(YogaValue.AUTO, node.getWidth()); - } - - @Test - public void testWidthAssignment() { - final YogaNode node = createNode(); - node.setWidth(123); - assertEquals(new YogaValue(123, YogaUnit.POINT), node.getWidth()); - - node.setWidthPercent(45); - assertEquals(new YogaValue(45, YogaUnit.PERCENT), node.getWidth()); - } - - @Test - public void testWidthAffectsLayout() { - final YogaNode node = style().width(123).node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(123, node.getLayoutWidth(), 0); - } - - @Test - public void testWidthPercentAffectsLayout() { - final YogaNode node = style().widthPercent(75).node(); - node.calculateLayout(200, UNDEFINED); - - assertEquals(150, node.getLayoutWidth(), 0); - } - - // TODO: testWidthAutoAffectsLayout - - @Test - public void testHeightDefault() { - final YogaNode node = createNode(); - - assertEquals(YogaValue.AUTO, node.getHeight()); - } - - @Test - public void testHeightAssignment() { - final YogaNode node = createNode(); - node.setHeight(123); - assertEquals(new YogaValue(123, YogaUnit.POINT), node.getHeight()); - - node.setHeightPercent(45); - assertEquals(new YogaValue(45, YogaUnit.PERCENT), node.getHeight()); - } - - @Test - public void testHeightAffectsLayout() { - final YogaNode node = style().height(123).node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(123, node.getLayoutHeight(), 0); - } - - @Test - public void testHeightPercentAffectsLayout() { - final YogaNode node = style().heightPercent(75).node(); - node.calculateLayout(UNDEFINED, 200); - - assertEquals(150, node.getLayoutHeight(), 0); - } - - // TODO: testHeightAutoAffectsLayout - - @Test - public void testMinWidthDefault() { - final YogaNode node = createNode(); - - assertEquals(YogaValue.UNDEFINED, node.getMinWidth()); - } - - @Test - public void testMinWidthAssignment() { - final YogaNode node = createNode(); - node.setMinWidth(123); - assertEquals(new YogaValue(123, YogaUnit.POINT), node.getMinWidth()); - - node.setMinWidthPercent(45); - assertEquals(new YogaValue(45, YogaUnit.PERCENT), node.getMinWidth()); - } - - @Test - public void testMinWidthAffectsLayout() { - final YogaNode node = style().minWidth(123).node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(123, node.getLayoutWidth(), 0); - } - - @Test - public void testMinWidthPercentAffectsLayout() { - final YogaNode node = style().minWidthPercent(120).node(); - node.calculateLayout(200, UNDEFINED); - - assertEquals(240, node.getLayoutWidth(), 0); - } - - @Test - public void testMinHeightDefault() { - final YogaNode node = createNode(); - - assertEquals(YogaValue.UNDEFINED, node.getMinHeight()); - } - - @Test - public void testMinHeightAssignment() { - final YogaNode node = createNode(); - node.setMinHeight(123); - assertEquals(new YogaValue(123, YogaUnit.POINT), node.getMinHeight()); - - node.setMinHeightPercent(45); - assertEquals(new YogaValue(45, YogaUnit.PERCENT), node.getMinHeight()); - } - - @Test - public void testMinHeightAffectsLayout() { - final YogaNode node = style().minHeight(123).node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(123, node.getLayoutHeight(), 0); - } - - @Test - public void testMinHeightPercentAffectsLayout() { - final YogaNode node = style().minHeightPercent(120).node(); - node.calculateLayout(UNDEFINED, 200); - - assertEquals(240, node.getLayoutHeight(), 0); - } - - @Test - public void testMaxWidthDefault() { - final YogaNode node = createNode(); - - assertEquals(YogaValue.UNDEFINED, node.getMaxWidth()); - } - - @Test - public void testMaxWidthAssignment() { - final YogaNode node = createNode(); - node.setMaxWidth(123); - assertEquals(new YogaValue(123, YogaUnit.POINT), node.getMaxWidth()); - - node.setMaxWidthPercent(45); - assertEquals(new YogaValue(45, YogaUnit.PERCENT), node.getMaxWidth()); - } - - @Test - public void testMaxWidthAffectsLayout() { - final YogaNode node = style().width(200).children(style().maxWidth(123)).node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(123, node.getChildAt(0).getLayoutWidth(), 0); - } - - @Test - public void testMaxWidthPercentAffectsLayout() { - final YogaNode node = style().width(200).children(style().maxWidthPercent(80)).node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(160, node.getChildAt(0).getLayoutWidth(), 0); - } - - @Test - public void testMaxHeightDefault() { - final YogaNode node = createNode(); - - assertEquals(YogaValue.UNDEFINED, node.getMaxHeight()); - } - - @Test - public void testMaxHeightAssignment() { - final YogaNode node = createNode(); - node.setMaxHeight(123); - assertEquals(new YogaValue(123, YogaUnit.POINT), node.getMaxHeight()); - - node.setMaxHeightPercent(45); - assertEquals(new YogaValue(45, YogaUnit.PERCENT), node.getMaxHeight()); - } - - @Test - public void testMaxHeightAffectsLayout() { - final YogaNode node = - style() - .height(200) - .flexDirection(YogaFlexDirection.ROW) - .children(style().maxHeight(123)) - .node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(123, node.getChildAt(0).getLayoutHeight(), 0); - } - - @Test - public void testMaxHeightPercentAffectsLayout() { - final YogaNode node = - style() - .flexDirection(YogaFlexDirection.ROW) - .height(200) - .children(style().maxHeightPercent(80)) - .node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(160, node.getChildAt(0).getLayoutHeight(), 0); - } - - @Test - public void testAspectRatioDefault() { - final YogaNode node = createNode(); - - assertEquals(UNDEFINED, node.getAspectRatio(), 0); - } - - @Test - public void testAspectRatioAssignment() { - final YogaNode node = createNode(); - node.setAspectRatio(2.75f); - - assertEquals(2.75f, node.getAspectRatio(), 0); - } - - @Test - public void aspectRatioAffectsLayoutWithGivenWidth() { - final YogaNode node = style().children(style().width(300).aspectRatio(1.5f)).node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(200, node.getChildAt(0).getLayoutHeight(), 0); - } - - @Test - public void aspectRatioAffectsLayoutWithGivenHeight() { - final YogaNode node = style().children(style().height(300).aspectRatio(1.5f)).node(); - node.calculateLayout(UNDEFINED, UNDEFINED); - - assertEquals(450, node.getChildAt(0).getLayoutWidth(), 0); - } - - private YogaNode createNode() { - return mNodeFactory.create(); - } - - private StyledNode style() { - return new StyledNode(mNodeFactory); - } - - private static class StyledNode { - - private YogaNode mNode; - - public StyledNode(TestParametrization.NodeFactory nodeFactory) { - mNode = nodeFactory.create(); - } - - YogaNode node() { - return mNode; - } - - StyledNode children(StyledNode... children) { - for (int i = mNode.getChildCount(); --i >= 0; ) { - mNode.removeChildAt(i); - } - for (int i = 0; i < children.length; i++) { - mNode.addChildAt(children[i].node(), i); - } - return this; - } - - StyledNode direction(YogaDirection direction) { - mNode.setDirection(direction); - return this; - } - - StyledNode width(float width) { - mNode.setWidth(width); - return this; - } - - StyledNode widthPercent(float width) { - mNode.setWidthPercent(width); - return this; - } - - StyledNode flexDirection(YogaFlexDirection direction) { - mNode.setFlexDirection(direction); - return this; - } - - StyledNode justifyContent(YogaJustify justify) { - mNode.setJustifyContent(justify); - return this; - } - - StyledNode height(float height) { - mNode.setHeight(height); - return this; - } - - StyledNode heightPercent(float height) { - mNode.setHeightPercent(height); - return this; - } - - StyledNode alignItems(YogaAlign align) { - mNode.setAlignItems(align); - return this; - } - - StyledNode alignSelf(YogaAlign align) { - mNode.setAlignSelf(align); - return this; - } - - StyledNode alignContent(YogaAlign align) { - mNode.setAlignContent(align); - return this; - } - - StyledNode flexWrap(YogaWrap wrap) { - mNode.setWrap(wrap); - return this; - } - - StyledNode positionType(YogaPositionType positionType) { - mNode.setPositionType(positionType); - return this; - } - - StyledNode overflow(YogaOverflow overflow) { - mNode.setOverflow(overflow); - return this; - } - - StyledNode flexShrink(float flexShrink) { - mNode.setFlexShrink(flexShrink); - return this; - } - - StyledNode display(YogaDisplay display) { - mNode.setDisplay(display); - return this; - } - - StyledNode flexGrow(float flexGrow) { - mNode.setFlexGrow(flexGrow); - return this; - } - - StyledNode flex(float flex) { - mNode.setFlex(flex); - return this; - } - - StyledNode flexBasis(float flexBasis) { - mNode.setFlexBasis(flexBasis); - return this; - } - - StyledNode flexBasisPercent(float flexBasis) { - mNode.setFlexBasisPercent(flexBasis); - return this; - } - - StyledNode margin(YogaEdge edge, float margin) { - mNode.setMargin(edge, margin); - return this; - } - - StyledNode marginPercent(YogaEdge edge, float margin) { - mNode.setMarginPercent(edge, margin); - return this; - } - - StyledNode marginAuto(YogaEdge edge) { - mNode.setMarginAuto(edge); - return this; - } - - StyledNode padding(YogaEdge edge, float padding) { - mNode.setPadding(edge, padding); - return this; - } - - StyledNode paddingPercent(YogaEdge edge, float padding) { - mNode.setPaddingPercent(edge, padding); - return this; - } - - StyledNode border(YogaEdge edge, float border) { - mNode.setBorder(edge, border); - return this; - } - - StyledNode position(YogaEdge edge, float position) { - mNode.setPosition(edge, position); - return this; - } - - StyledNode positionPercent(YogaEdge edge, float position) { - mNode.setPositionPercent(edge, position); - return this; - } - - StyledNode minWidth(float minWidth) { - mNode.setMinWidth(minWidth); - return this; - } - - StyledNode minWidthPercent(float minWidth) { - mNode.setMinWidthPercent(minWidth); - return this; - } - - StyledNode minHeight(float minHeight) { - mNode.setMinHeight(minHeight); - return this; - } - - StyledNode minHeightPercent(float minHeight) { - mNode.setMinHeightPercent(minHeight); - return this; - } - - StyledNode maxWidth(float maxWidth) { - mNode.setMaxWidth(maxWidth); - return this; - } - - StyledNode maxWidthPercent(float maxWidth) { - mNode.setMaxWidthPercent(maxWidth); - return this; - } - - StyledNode maxHeight(float maxHeight) { - mNode.setMaxHeight(maxHeight); - return this; - } - - StyledNode maxHeightPercent(float maxHeight) { - mNode.setMaxHeightPercent(maxHeight); - return this; - } - - StyledNode aspectRatio(float aspectRatio) { - mNode.setAspectRatio(aspectRatio); - return this; - } - } -} diff --git a/java/tests/com/facebook/yoga/YogaNodeStylePropertiesTest.kt b/java/tests/com/facebook/yoga/YogaNodeStylePropertiesTest.kt new file mode 100644 index 0000000000..e26acc8852 --- /dev/null +++ b/java/tests/com/facebook/yoga/YogaNodeStylePropertiesTest.kt @@ -0,0 +1,1020 @@ +/* + * 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. + */ + +package com.facebook.yoga + +import org.junit.Assert.assertEquals +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.Parameterized + +@RunWith(Parameterized::class) +class YogaNodeStylePropertiesTest { + + companion object { + @JvmStatic + @Parameterized.Parameters(name = "{0}") + fun nodeFactories(): Iterable = + TestParametrization.nodeFactories() + + private val UNDEFINED = YogaValue.UNDEFINED.value + } + + @Parameterized.Parameter lateinit var mNodeFactory: TestParametrization.NodeFactory + + @Test + fun testDirectionDefault() { + val node = createNode() + + assertEquals(node.getStyleDirection(), YogaDirection.INHERIT) + assertEquals(node.getLayoutDirection(), YogaDirection.INHERIT) + } + + @Test + fun testDirectionAssignment() { + val node = createNode() + node.setDirection(YogaDirection.LTR) + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(YogaDirection.LTR, node.getStyleDirection()) + assertEquals(YogaDirection.LTR, node.getLayoutDirection()) + } + + @Test + fun testDirectionAffectsLayout() { + val node = + style().direction(YogaDirection.RTL).width(200f).children(style().widthPercent(40f)).node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(120f, node.getChildAt(0).getLayoutX(), 0f) + } + + @Test + fun testFlexDirectionDefault() { + val node = createNode() + + assertEquals(YogaFlexDirection.COLUMN, node.getFlexDirection()) + } + + @Test + fun testFlexDirectionAssignment() { + val node = style().flexDirection(YogaFlexDirection.COLUMN_REVERSE).node() + + assertEquals(YogaFlexDirection.COLUMN_REVERSE, node.getFlexDirection()) + } + + @Test + fun testFlexDirectionAffectsLayout() { + val node = + style() + .flexDirection(YogaFlexDirection.ROW_REVERSE) + .width(200f) + .children(style().widthPercent(40f)) + .node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(120f, node.getChildAt(0).getLayoutX(), 0f) + } + + @Test + fun testJustifyContentDefault() { + val node = createNode() + + assertEquals(YogaJustify.FLEX_START, node.getJustifyContent()) + } + + @Test + fun testJustifyContentAssignment() { + val node = createNode() + node.setJustifyContent(YogaJustify.SPACE_EVENLY) + + assertEquals(YogaJustify.SPACE_EVENLY, node.getJustifyContent()) + } + + @Test + fun testJustifyContentAffectsLayout() { + val node = + style() + .justifyContent(YogaJustify.CENTER) + .height(200f) + .children(style().heightPercent(40f)) + .node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(60f, node.getChildAt(0).getLayoutY(), 0f) + } + + @Test + fun testAlignItemsDefault() { + val node = createNode() + + assertEquals(YogaAlign.STRETCH, node.getAlignItems()) + } + + @Test + fun testAlignItemsAssignment() { + val node = createNode() + node.setAlignItems(YogaAlign.SPACE_AROUND) + + assertEquals(YogaAlign.SPACE_AROUND, node.getAlignItems()) + } + + @Test + fun testAlignItemsAffectsLayout() { + val node = + style().alignItems(YogaAlign.CENTER).height(200f).children(style().widthPercent(40f)).node() + node.calculateLayout(200f, UNDEFINED) + + assertEquals(60f, node.getChildAt(0).getLayoutX(), 0f) + } + + @Test + fun testAlignSelfDefault() { + val node = createNode() + + assertEquals(YogaAlign.AUTO, node.getAlignSelf()) + } + + @Test + fun testAlignSelfAssignment() { + val node = createNode() + node.setAlignSelf(YogaAlign.FLEX_END) + + assertEquals(YogaAlign.FLEX_END, node.getAlignSelf()) + } + + @Test + fun testAlignSelfAffectsLayout() { + val node = + style().height(200f).children(style().alignSelf(YogaAlign.CENTER).widthPercent(40f)).node() + node.calculateLayout(200f, UNDEFINED) + + assertEquals(60f, node.getChildAt(0).getLayoutX(), 0f) + } + + @Test + fun testAlignContentDefault() { + val node = createNode() + + assertEquals(YogaAlign.FLEX_START, node.getAlignContent()) + } + + @Test + fun testAlignContentAssignment() { + val node = createNode() + node.setAlignContent(YogaAlign.BASELINE) + + assertEquals(YogaAlign.BASELINE, node.getAlignContent()) + } + + @Test + fun testAlignContentAffectsLayout() { + val node = + style() + .alignContent(YogaAlign.SPACE_AROUND) + .flexWrap(YogaWrap.WRAP) + .height(200f) + .width(200f) + .children( + style().widthPercent(20f).heightPercent(60f), + style().widthPercent(20f).heightPercent(60f), + ) + .node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(30f, node.getChildAt(0).getLayoutX(), 0f) + } + + @Test + fun testPositionTypeDefault() { + val node = createNode() + + assertEquals(YogaPositionType.RELATIVE, node.getPositionType()) + } + + @Test + fun testPositionTypeAssignment() { + val node = createNode() + node.setPositionType(YogaPositionType.ABSOLUTE) + + assertEquals(YogaPositionType.ABSOLUTE, node.getPositionType()) + } + + @Test + fun testPositionTypeAffectsLayout() { + val node = + style() + .height(200f) + .children( + style().height(100f), + style().height(100f).positionType(YogaPositionType.ABSOLUTE), + ) + .node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(0f, node.getChildAt(1).getLayoutY(), 0f) + } + + @Test + fun testWrapAffectsLayout() { + val node = + style() + .width(200f) + .height(200f) + .flexWrap(YogaWrap.WRAP_REVERSE) + .children(style().width(10f).heightPercent(60f), style().heightPercent(60f)) + .node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(190f, node.getChildAt(0).getLayoutX(), 0f) + } + + @Test + fun testOverflowDefault() { + val node = createNode() + + assertEquals(YogaOverflow.VISIBLE, node.getOverflow()) + } + + @Test + fun testOverflowAssignment() { + val node = createNode() + node.setOverflow(YogaOverflow.SCROLL) + + assertEquals(YogaOverflow.SCROLL, node.getOverflow()) + } + + // TODO add testOverflowAffectsLayout() + + @Test + fun testDisplayDefault() { + val node = createNode() + + assertEquals(YogaDisplay.FLEX, node.getDisplay()) + } + + @Test + fun testDisplayAssignment() { + val node = createNode() + node.setDisplay(YogaDisplay.NONE) + + assertEquals(YogaDisplay.NONE, node.getDisplay()) + } + + @Test + fun testDisplayAffectsLayout() { + val node = + style() + .children(style().flexGrow(1f).display(YogaDisplay.NONE), style().flexGrow(1f)) + .node() + node.calculateLayout(200f, 200f) + + assertEquals(200f, node.getChildAt(1).getLayoutHeight(), 0f) + } + + @Test + fun testFlexAffectsLayoutGrowing() { + val node = style().height(200f).children(style().height(100f).flex(1.25f)).node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(200f, node.getChildAt(0).getLayoutHeight(), 0f) + } + + @Test + fun testFlexAffectsLayoutShrinking() { + val node = style().height(200f).children(style().height(300f).flex(1.25f)).node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(200f, node.getChildAt(0).getLayoutHeight(), 0f) + } + + @Test + fun testFlexGrowDefault() { + val node = createNode() + + assertEquals(0f, node.getFlexGrow(), 0f) + } + + @Test + fun testFlexGrowAssignment() { + val node = createNode() + node.setFlexGrow(2.5f) + + assertEquals(2.5f, node.getFlexGrow(), 0f) + } + + @Test + fun testFlexGrowAffectsLayout() { + val node = + style().height(200f).children(style().height(50f).flexGrow(1f), style().height(50f)).node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(150f, node.getChildAt(0).getLayoutHeight(), 0f) + } + + @Test + fun testFlexShrinkDefault() { + val node = createNode() + + assertEquals(0f, node.getFlexShrink(), 0f) + } + + @Test + fun testFlexShrinkAssignment() { + val node = createNode() + node.setFlexShrink(2.5f) + + assertEquals(2.5f, node.getFlexShrink(), 0f) + } + + @Test + fun testFlexShrinkAffectsLayout() { + val node = + style() + .height(200f) + .children(style().height(150f).flexShrink(1f), style().height(150f)) + .node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(50f, node.getChildAt(0).getLayoutHeight(), 0f) + } + + @Test + fun testFlexBasisDefault() { + val node = createNode() + + assertEquals(YogaValue.AUTO, node.getFlexBasis()) + } + + @Test + fun testFlexBasisAssignment() { + val node = createNode() + node.setFlexBasis(50f) + assertEquals(YogaValue(50f, YogaUnit.POINT), node.getFlexBasis()) + + node.setFlexBasisPercent(20f) + assertEquals(YogaValue(20f, YogaUnit.PERCENT), node.getFlexBasis()) + + node.setFlexBasisAuto() + assertEquals(YogaValue.AUTO, node.getFlexBasis()) + } + + @Test + fun testFlexBasisAffectsLayout() { + val node = + style() + .height(200f) + .children( + style().flexBasis(150f).flexShrink(1f), + style().flexBasis(150f).flexShrink(1f), + ) + .node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(100f, node.getChildAt(0).getLayoutHeight(), 0f) + } + + @Test + fun testFlexBasisPercentAffectsLayout() { + val node = + style() + .height(200f) + .children(style().flexBasisPercent(60f), style().flexBasisPercent(40f)) + .node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(80f, node.getChildAt(1).getLayoutHeight(), 0f) + } + + @Test + fun testMarginDefault() { + val node = createNode() + for (edge in YogaEdge.values()) { + assertEquals(YogaValue.UNDEFINED, node.getMargin(edge)) + } + } + + @Test + fun testMarginAssignment() { + val node = createNode() + for (edge in YogaEdge.values()) { + node.setMargin(edge, 25f) + assertEquals(YogaValue(25f, YogaUnit.POINT), node.getMargin(edge)) + + node.setMarginPercent(edge, 5f) + assertEquals(YogaValue(5f, YogaUnit.PERCENT), node.getMargin(edge)) + + node.setMarginAuto(edge) + assertEquals(YogaValue.AUTO, node.getMargin(edge)) + } + } + + @Test + fun testNegativeMarginAssignment() { + val node = createNode() + for (edge in YogaEdge.values()) { + node.setMargin(edge, -25f) + assertEquals(YogaValue(-25f, YogaUnit.POINT), node.getMargin(edge)) + + node.setMarginPercent(edge, -5f) + assertEquals(YogaValue(-5f, YogaUnit.PERCENT), node.getMargin(edge)) + + node.setMarginAuto(edge) + assertEquals(YogaValue.AUTO, node.getMargin(edge)) + } + } + + @Test + fun testMarginPointAffectsLayout() { + val node = style().margin(YogaEdge.TOP, 42f).node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(42f, node.getLayoutY(), 0f) + } + + @Test + fun testMarginPercentAffectsLayout() { + val node = + style().height(200f).children(style().flexGrow(1f).marginPercent(YogaEdge.TOP, 20f)).node() + node.calculateLayout(200f, 200f) + + assertEquals(40f, node.getChildAt(0).getLayoutY(), 0f) + } + + @Test + fun testMarginAutoAffectsLayout() { + val node = + style() + .width(200f) + .flexDirection(YogaFlexDirection.ROW) + .children(style().marginAuto(YogaEdge.LEFT).marginAuto(YogaEdge.RIGHT).width(100f)) + .node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(50f, node.getChildAt(0).getLayoutX(), 0f) + } + + @Test + fun testPaddingDefault() { + val node = createNode() + for (edge in YogaEdge.values()) { + assertEquals(YogaValue.UNDEFINED, node.getPadding(edge)) + } + } + + @Test + fun testPaddingAssignment() { + val node = createNode() + for (edge in YogaEdge.values()) { + node.setPadding(edge, 25f) + assertEquals(YogaValue(25f, YogaUnit.POINT), node.getPadding(edge)) + + node.setPaddingPercent(edge, 5f) + assertEquals(YogaValue(5f, YogaUnit.PERCENT), node.getPadding(edge)) + } + } + + @Test + fun testPaddingPointAffectsLayout() { + val node = style().padding(YogaEdge.TOP, 42f).children(style()).node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(42f, node.getChildAt(0).getLayoutY(), 0f) + } + + @Test + fun testPaddingPercentAffectsLayout() { + val node = + style().height(200f).paddingPercent(YogaEdge.TOP, 20f).children(style().flexGrow(1f)).node() + node.calculateLayout(200f, 200f) + + assertEquals(40f, node.getChildAt(0).getLayoutY(), 0f) + } + + @Test + fun testBorderDefault() { + val node = createNode() + for (edge in YogaEdge.values()) { + assertEquals(UNDEFINED, node.getBorder(edge), 0f) + } + } + + @Test + fun testBorderAssignment() { + val node = createNode() + for (edge in YogaEdge.values()) { + node.setBorder(edge, 2.5f) + assertEquals(2.5f, node.getBorder(edge), 0f) + } + } + + @Test + fun testBorderAffectsLayout() { + val node = style().border(YogaEdge.TOP, 42f).children(style()).node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(42f, node.getChildAt(0).getLayoutY(), 0f) + } + + @Test + fun testPositionDefault() { + val node = createNode() + for (edge in YogaEdge.values()) { + assertEquals(YogaValue.UNDEFINED, node.getPosition(edge)) + } + } + + @Test + fun testPositionAssignment() { + val node = createNode() + for (edge in YogaEdge.values()) { + node.setPosition(edge, 25f) + assertEquals(YogaValue(25f, YogaUnit.POINT), node.getPosition(edge)) + + node.setPositionPercent(edge, 5f) + assertEquals(YogaValue(5f, YogaUnit.PERCENT), node.getPosition(edge)) + } + } + + @Test + fun testPositionAffectsLayout() { + val node = + style() + .height(100f) + .children( + style() + .positionType(YogaPositionType.ABSOLUTE) + .position(YogaEdge.TOP, 11f) + .position(YogaEdge.BOTTOM, 22f) + ) + .node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(67f, node.getChildAt(0).getLayoutHeight(), 0f) + } + + @Test + fun testPositionPercentAffectsLayout() { + val node = + style() + .width(100f) + .children( + style() + .positionType(YogaPositionType.ABSOLUTE) + .positionPercent(YogaEdge.LEFT, 11f) + .positionPercent(YogaEdge.RIGHT, 22f) + ) + .node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(67f, node.getChildAt(0).getLayoutWidth(), 0f) + } + + @Test + fun testWidthDefault() { + val node = createNode() + + assertEquals(YogaValue.AUTO, node.getWidth()) + } + + @Test + fun testWidthAssignment() { + val node = createNode() + node.setWidth(123f) + assertEquals(YogaValue(123f, YogaUnit.POINT), node.getWidth()) + + node.setWidthPercent(45f) + assertEquals(YogaValue(45f, YogaUnit.PERCENT), node.getWidth()) + } + + @Test + fun testWidthAffectsLayout() { + val node = style().width(123f).node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(123f, node.getLayoutWidth(), 0f) + } + + @Test + fun testWidthPercentAffectsLayout() { + val node = style().widthPercent(75f).node() + node.calculateLayout(200f, UNDEFINED) + + assertEquals(150f, node.getLayoutWidth(), 0f) + } + + // TODO: testWidthAutoAffectsLayout + + @Test + fun testHeightDefault() { + val node = createNode() + + assertEquals(YogaValue.AUTO, node.getHeight()) + } + + @Test + fun testHeightAssignment() { + val node = createNode() + node.setHeight(123f) + assertEquals(YogaValue(123f, YogaUnit.POINT), node.getHeight()) + + node.setHeightPercent(45f) + assertEquals(YogaValue(45f, YogaUnit.PERCENT), node.getHeight()) + } + + @Test + fun testHeightAffectsLayout() { + val node = style().height(123f).node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(123f, node.getLayoutHeight(), 0f) + } + + @Test + fun testHeightPercentAffectsLayout() { + val node = style().heightPercent(75f).node() + node.calculateLayout(UNDEFINED, 200f) + + assertEquals(150f, node.getLayoutHeight(), 0f) + } + + // TODO: testHeightAutoAffectsLayout + + @Test + fun testMinWidthDefault() { + val node = createNode() + + assertEquals(YogaValue.UNDEFINED, node.getMinWidth()) + } + + @Test + fun testMinWidthAssignment() { + val node = createNode() + node.setMinWidth(123f) + assertEquals(YogaValue(123f, YogaUnit.POINT), node.getMinWidth()) + + node.setMinWidthPercent(45f) + assertEquals(YogaValue(45f, YogaUnit.PERCENT), node.getMinWidth()) + } + + @Test + fun testMinWidthAffectsLayout() { + val node = style().minWidth(123f).node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(123f, node.getLayoutWidth(), 0f) + } + + @Test + fun testMinWidthPercentAffectsLayout() { + val node = style().minWidthPercent(120f).node() + node.calculateLayout(200f, UNDEFINED) + + assertEquals(240f, node.getLayoutWidth(), 0f) + } + + @Test + fun testMinHeightDefault() { + val node = createNode() + + assertEquals(YogaValue.UNDEFINED, node.getMinHeight()) + } + + @Test + fun testMinHeightAssignment() { + val node = createNode() + node.setMinHeight(123f) + assertEquals(YogaValue(123f, YogaUnit.POINT), node.getMinHeight()) + + node.setMinHeightPercent(45f) + assertEquals(YogaValue(45f, YogaUnit.PERCENT), node.getMinHeight()) + } + + @Test + fun testMinHeightAffectsLayout() { + val node = style().minHeight(123f).node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(123f, node.getLayoutHeight(), 0f) + } + + @Test + fun testMinHeightPercentAffectsLayout() { + val node = style().minHeightPercent(120f).node() + node.calculateLayout(UNDEFINED, 200f) + + assertEquals(240f, node.getLayoutHeight(), 0f) + } + + @Test + fun testMaxWidthDefault() { + val node = createNode() + + assertEquals(YogaValue.UNDEFINED, node.getMaxWidth()) + } + + @Test + fun testMaxWidthAssignment() { + val node = createNode() + node.setMaxWidth(123f) + assertEquals(YogaValue(123f, YogaUnit.POINT), node.getMaxWidth()) + + node.setMaxWidthPercent(45f) + assertEquals(YogaValue(45f, YogaUnit.PERCENT), node.getMaxWidth()) + } + + @Test + fun testMaxWidthAffectsLayout() { + val node = style().width(200f).children(style().maxWidth(123f)).node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(123f, node.getChildAt(0).getLayoutWidth(), 0f) + } + + @Test + fun testMaxWidthPercentAffectsLayout() { + val node = style().width(200f).children(style().maxWidthPercent(80f)).node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(160f, node.getChildAt(0).getLayoutWidth(), 0f) + } + + @Test + fun testMaxHeightDefault() { + val node = createNode() + + assertEquals(YogaValue.UNDEFINED, node.getMaxHeight()) + } + + @Test + fun testMaxHeightAssignment() { + val node = createNode() + node.setMaxHeight(123f) + assertEquals(YogaValue(123f, YogaUnit.POINT), node.getMaxHeight()) + + node.setMaxHeightPercent(45f) + assertEquals(YogaValue(45f, YogaUnit.PERCENT), node.getMaxHeight()) + } + + @Test + fun testMaxHeightAffectsLayout() { + val node = + style() + .height(200f) + .flexDirection(YogaFlexDirection.ROW) + .children(style().maxHeight(123f)) + .node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(123f, node.getChildAt(0).getLayoutHeight(), 0f) + } + + @Test + fun testMaxHeightPercentAffectsLayout() { + val node = + style() + .flexDirection(YogaFlexDirection.ROW) + .height(200f) + .children(style().maxHeightPercent(80f)) + .node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(160f, node.getChildAt(0).getLayoutHeight(), 0f) + } + + @Test + fun testAspectRatioDefault() { + val node = createNode() + + assertEquals(UNDEFINED, node.getAspectRatio(), 0f) + } + + @Test + fun testAspectRatioAssignment() { + val node = createNode() + node.setAspectRatio(2.75f) + + assertEquals(2.75f, node.getAspectRatio(), 0f) + } + + @Test + fun aspectRatioAffectsLayoutWithGivenWidth() { + val node = style().children(style().width(300f).aspectRatio(1.5f)).node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(200f, node.getChildAt(0).getLayoutHeight(), 0f) + } + + @Test + fun aspectRatioAffectsLayoutWithGivenHeight() { + val node = style().children(style().height(300f).aspectRatio(1.5f)).node() + node.calculateLayout(UNDEFINED, UNDEFINED) + + assertEquals(450f, node.getChildAt(0).getLayoutWidth(), 0f) + } + + private fun createNode(): YogaNode = mNodeFactory.create() + + private fun style(): StyledNode = StyledNode(mNodeFactory) + + private class StyledNode(nodeFactory: TestParametrization.NodeFactory) { + private val mNode: YogaNode = nodeFactory.create() + + fun node(): YogaNode = mNode + + fun children(vararg children: StyledNode): StyledNode { + var i = mNode.getChildCount() + while (--i >= 0) { + mNode.removeChildAt(i) + } + for (j in children.indices) { + mNode.addChildAt(children[j].node(), j) + } + return this + } + + fun direction(direction: YogaDirection): StyledNode { + mNode.setDirection(direction) + return this + } + + fun width(width: Float): StyledNode { + mNode.setWidth(width) + return this + } + + fun widthPercent(width: Float): StyledNode { + mNode.setWidthPercent(width) + return this + } + + fun flexDirection(direction: YogaFlexDirection): StyledNode { + mNode.setFlexDirection(direction) + return this + } + + fun justifyContent(justify: YogaJustify): StyledNode { + mNode.setJustifyContent(justify) + return this + } + + fun height(height: Float): StyledNode { + mNode.setHeight(height) + return this + } + + fun heightPercent(height: Float): StyledNode { + mNode.setHeightPercent(height) + return this + } + + fun alignItems(align: YogaAlign): StyledNode { + mNode.setAlignItems(align) + return this + } + + fun alignSelf(align: YogaAlign): StyledNode { + mNode.setAlignSelf(align) + return this + } + + fun alignContent(align: YogaAlign): StyledNode { + mNode.setAlignContent(align) + return this + } + + fun flexWrap(wrap: YogaWrap): StyledNode { + mNode.setWrap(wrap) + return this + } + + fun positionType(positionType: YogaPositionType): StyledNode { + mNode.setPositionType(positionType) + return this + } + + fun overflow(overflow: YogaOverflow): StyledNode { + mNode.setOverflow(overflow) + return this + } + + fun flexShrink(flexShrink: Float): StyledNode { + mNode.setFlexShrink(flexShrink) + return this + } + + fun display(display: YogaDisplay): StyledNode { + mNode.setDisplay(display) + return this + } + + fun flexGrow(flexGrow: Float): StyledNode { + mNode.setFlexGrow(flexGrow) + return this + } + + fun flex(flex: Float): StyledNode { + mNode.setFlex(flex) + return this + } + + fun flexBasis(flexBasis: Float): StyledNode { + mNode.setFlexBasis(flexBasis) + return this + } + + fun flexBasisPercent(flexBasis: Float): StyledNode { + mNode.setFlexBasisPercent(flexBasis) + return this + } + + fun margin(edge: YogaEdge, margin: Float): StyledNode { + mNode.setMargin(edge, margin) + return this + } + + fun marginPercent(edge: YogaEdge, margin: Float): StyledNode { + mNode.setMarginPercent(edge, margin) + return this + } + + fun marginAuto(edge: YogaEdge): StyledNode { + mNode.setMarginAuto(edge) + return this + } + + fun padding(edge: YogaEdge, padding: Float): StyledNode { + mNode.setPadding(edge, padding) + return this + } + + fun paddingPercent(edge: YogaEdge, padding: Float): StyledNode { + mNode.setPaddingPercent(edge, padding) + return this + } + + fun border(edge: YogaEdge, border: Float): StyledNode { + mNode.setBorder(edge, border) + return this + } + + fun position(edge: YogaEdge, position: Float): StyledNode { + mNode.setPosition(edge, position) + return this + } + + fun positionPercent(edge: YogaEdge, position: Float): StyledNode { + mNode.setPositionPercent(edge, position) + return this + } + + fun minWidth(minWidth: Float): StyledNode { + mNode.setMinWidth(minWidth) + return this + } + + fun minWidthPercent(minWidth: Float): StyledNode { + mNode.setMinWidthPercent(minWidth) + return this + } + + fun minHeight(minHeight: Float): StyledNode { + mNode.setMinHeight(minHeight) + return this + } + + fun minHeightPercent(minHeight: Float): StyledNode { + mNode.setMinHeightPercent(minHeight) + return this + } + + fun maxWidth(maxWidth: Float): StyledNode { + mNode.setMaxWidth(maxWidth) + return this + } + + fun maxWidthPercent(maxWidth: Float): StyledNode { + mNode.setMaxWidthPercent(maxWidth) + return this + } + + fun maxHeight(maxHeight: Float): StyledNode { + mNode.setMaxHeight(maxHeight) + return this + } + + fun maxHeightPercent(maxHeight: Float): StyledNode { + mNode.setMaxHeightPercent(maxHeight) + return this + } + + fun aspectRatio(aspectRatio: Float): StyledNode { + mNode.setAspectRatio(aspectRatio) + return this + } + } +} diff --git a/java/tests/com/facebook/yoga/YogaNodeTest.java b/java/tests/com/facebook/yoga/YogaNodeTest.java deleted file mode 100644 index 8341d1cf5f..0000000000 --- a/java/tests/com/facebook/yoga/YogaNodeTest.java +++ /dev/null @@ -1,360 +0,0 @@ -/* - * 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. - */ - -package com.facebook.yoga; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.lang.ref.WeakReference; -import java.util.ArrayList; -import java.util.concurrent.atomic.AtomicBoolean; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -@RunWith(Parameterized.class) -public class YogaNodeTest { - @Parameterized.Parameters(name = "{0}") - public static Iterable nodeFactories() { - return TestParametrization.nodeFactories(); - } - - @Parameterized.Parameter public TestParametrization.NodeFactory mNodeFactory; - - @Test - public void testInit() { - final YogaNode node = createNode(); - } - - @Test - public void testBaseline() { - final YogaNode root = createNode(); - root.setFlexDirection(YogaFlexDirection.ROW); - root.setAlignItems(YogaAlign.BASELINE); - root.setWidth(100); - root.setHeight(100); - - final YogaNode child1 = createNode(); - child1.setWidth(40); - child1.setHeight(40); - root.addChildAt(child1, 0); - - final YogaNode child2 = createNode(); - child2.setWidth(40); - child2.setHeight(40); - child2.setBaselineFunction(new YogaBaselineFunction() { - public float baseline(YogaNode node, float width, float height) { - return 0; - } - }); - root.addChildAt(child2, 1); - - root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); - - assertEquals(0, (int) child1.getLayoutY()); - assertEquals(40, (int) child2.getLayoutY()); - } - - @Test - public void testMeasure() { - final YogaNode node = createNode(); - node.setMeasureFunction(new YogaMeasureFunction() { - public long measure( - YogaNode node, - float width, - YogaMeasureMode widthMode, - float height, - YogaMeasureMode heightMode) { - return YogaMeasureOutput.make(100, 100); - } - }); - node.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); - assertEquals(100, (int) node.getLayoutWidth()); - assertEquals(100, (int) node.getLayoutHeight()); - } - - @Test - public void testMeasureFloat() { - final YogaNode node = createNode(); - node.setMeasureFunction(new YogaMeasureFunction() { - public long measure( - YogaNode node, - float width, - YogaMeasureMode widthMode, - float height, - YogaMeasureMode heightMode) { - return YogaMeasureOutput.make(100.5f, 100.5f); - } - }); - node.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); - assertEquals(101f, node.getLayoutWidth(), 0.01f); - assertEquals(101f, node.getLayoutHeight(), 0.01f); - } - - @Test - public void testMeasureFloatMin() { - final YogaNode node = createNode(); - node.setMeasureFunction(new YogaMeasureFunction() { - public long measure( - YogaNode node, - float width, - YogaMeasureMode widthMode, - float height, - YogaMeasureMode heightMode) { - return YogaMeasureOutput.make(Float.MIN_VALUE, Float.MIN_VALUE); - } - }); - node.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); - assertEquals(Float.MIN_VALUE, node.getLayoutWidth(), 0.01f); - assertEquals(Float.MIN_VALUE, node.getLayoutHeight(), 0.01f); - } - - @Test - public void testMeasureFloatBigNumber() { - final YogaNode node = createNode(); - final float bigNumber = (float) 10E5; - node.setMeasureFunction( - new YogaMeasureFunction() { - public long measure( - YogaNode node, - float width, - YogaMeasureMode widthMode, - float height, - YogaMeasureMode heightMode) { - return YogaMeasureOutput.make(bigNumber, bigNumber); - } - }); - node.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); - assertEquals(bigNumber, node.getLayoutWidth(), 0.01f); - assertEquals(bigNumber, node.getLayoutHeight(), 0.01f); - } - - @Test - public void testCopyStyle() { - final YogaNode node0 = createNode(); - assertTrue(YogaConstants.isUndefined(node0.getMaxHeight())); - - final YogaNode node1 = createNode(); - node1.setMaxHeight(100); - - node0.copyStyle(node1); - assertEquals(100, (int) node0.getMaxHeight().value); - } - - @Test - public void testLayoutMargin() { - final YogaNode node = createNode(); - node.setWidth(100); - node.setHeight(100); - node.setMargin(YogaEdge.START, 1); - node.setMargin(YogaEdge.END, 2); - node.setMargin(YogaEdge.TOP, 3); - node.setMargin(YogaEdge.BOTTOM, 4); - node.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); - - assertEquals(1, (int) node.getLayoutMargin(YogaEdge.LEFT)); - assertEquals(2, (int) node.getLayoutMargin(YogaEdge.RIGHT)); - assertEquals(3, (int) node.getLayoutMargin(YogaEdge.TOP)); - assertEquals(4, (int) node.getLayoutMargin(YogaEdge.BOTTOM)); - } - - @Test - public void testLayoutPadding() { - final YogaNode node = createNode(); - node.setWidth(100); - node.setHeight(100); - node.setPadding(YogaEdge.START, 1); - node.setPadding(YogaEdge.END, 2); - node.setPadding(YogaEdge.TOP, 3); - node.setPadding(YogaEdge.BOTTOM, 4); - node.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); - - assertEquals(1, (int) node.getLayoutPadding(YogaEdge.LEFT)); - assertEquals(2, (int) node.getLayoutPadding(YogaEdge.RIGHT)); - assertEquals(3, (int) node.getLayoutPadding(YogaEdge.TOP)); - assertEquals(4, (int) node.getLayoutPadding(YogaEdge.BOTTOM)); - } - - @Test - public void testLayoutBorder() { - final YogaNode node = createNode(); - node.setWidth(100); - node.setHeight(100); - node.setBorder(YogaEdge.START, 1); - node.setBorder(YogaEdge.END, 2); - node.setBorder(YogaEdge.TOP, 3); - node.setBorder(YogaEdge.BOTTOM, 4); - node.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); - - assertEquals(1, (int) node.getLayoutBorder(YogaEdge.LEFT)); - assertEquals(2, (int) node.getLayoutBorder(YogaEdge.RIGHT)); - assertEquals(3, (int) node.getLayoutBorder(YogaEdge.TOP)); - assertEquals(4, (int) node.getLayoutBorder(YogaEdge.BOTTOM)); - } - - @Test - public void testUseWebDefaults() { - final YogaConfig config = YogaConfigFactory.create(); - config.setUseWebDefaults(true); - final YogaNode node = createNode(config); - assertEquals(YogaFlexDirection.ROW, node.getFlexDirection()); - } - - @Test - public void testPercentPaddingOnRoot() { - final YogaNode node = createNode(); - node.setPaddingPercent(YogaEdge.ALL, 10); - node.calculateLayout(50, 50); - - assertEquals(5, (int) node.getLayoutPadding(YogaEdge.LEFT)); - assertEquals(5, (int) node.getLayoutPadding(YogaEdge.RIGHT)); - assertEquals(5, (int) node.getLayoutPadding(YogaEdge.TOP)); - assertEquals(5, (int) node.getLayoutPadding(YogaEdge.BOTTOM)); - } - - @Test - public void testDefaultEdgeValues() { - final YogaNode node = createNode(); - - for (YogaEdge edge : YogaEdge.values()) { - assertEquals(YogaUnit.UNDEFINED, node.getMargin(edge).unit); - assertEquals(YogaUnit.UNDEFINED, node.getPadding(edge).unit); - assertEquals(YogaUnit.UNDEFINED, node.getPosition(edge).unit); - assertTrue(YogaConstants.isUndefined(node.getBorder(edge))); - } - } - - @Test - public void initiallyHasNewLayout() { - YogaNode root = createNode(); - assertTrue(root.hasNewLayout()); - } - - @Test - public void initialLayoutCanBeMarkedSeen() { - YogaNode root = createNode(); - root.markLayoutSeen(); - assertFalse(root.hasNewLayout()); - } - - @Test - public void calculatingLayoutMarksLayoutAsUnseen() { - YogaNode root = createNode(); - root.markLayoutSeen(); - root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); - assertTrue(root.hasNewLayout()); - } - - @Test - public void calculatedLayoutCanBeMarkedSeen() { - YogaNode root = createNode(); - root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); - root.markLayoutSeen(); - assertFalse(root.hasNewLayout()); - } - - @Test - public void recalculatingLayoutDoesMarkAsUnseen() { - YogaNode root = createNode(); - root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); - root.markLayoutSeen(); - root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); - assertTrue(root.hasNewLayout()); - } - - @Test - public void resetAlsoResetsLayoutSeen() { - YogaNode root = createNode(); - root.markLayoutSeen(); - root.reset(); - assertTrue(root.hasNewLayout()); - } - - @Test - public void directionIsPassedThrough() { - YogaNode root = createNode(); - - root.setDirection(YogaDirection.RTL); - root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); - - assertEquals(root.getLayoutDirection(), YogaDirection.RTL); - } - - @Test - public void testResetApiShouldResetAllLayoutOutputs() { - YogaConfig config = YogaConfigFactory.create(); - config.setErrata(YogaErrata.STRETCH_FLEX_BASIS); - YogaNode node = createNode(config); - node.setWidth(100); - node.setHeight(100); - node.setMargin(YogaEdge.START, 1); - node.setMargin(YogaEdge.END, 2); - node.setMargin(YogaEdge.TOP, 3); - node.setMargin(YogaEdge.BOTTOM, 4); - node.setPadding(YogaEdge.START, 1); - node.setPadding(YogaEdge.END, 2); - node.setPadding(YogaEdge.TOP, 3); - node.setPadding(YogaEdge.BOTTOM, 4); - node.setBorder(YogaEdge.START, 1); - node.setBorder(YogaEdge.END, 2); - node.setBorder(YogaEdge.TOP, 3); - node.setBorder(YogaEdge.BOTTOM, 4); - node.setDirection(YogaDirection.RTL); - node.markLayoutSeen(); - node.setMeasureFunction(new YogaMeasureFunction(){ - @Override - public long measure(YogaNode node, float width, YogaMeasureMode widthMode, float height, - YogaMeasureMode heightMode) { - return YogaMeasureOutput.make(100, 100); - } - }); - node.setBaselineFunction(new YogaBaselineFunction(){ - - @Override - public float baseline(YogaNode node, float width, float height) { - return height; - } - }); - node.setData(new ArrayList<>()); - - node.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); - node.reset(); - - assertEquals(0, (int) node.getLayoutHeight()); - assertEquals(0, (int) node.getLayoutWidth()); - assertEquals(0, (int) node.getLayoutMargin(YogaEdge.LEFT)); - assertEquals(0, (int) node.getLayoutMargin(YogaEdge.RIGHT)); - assertEquals(0, (int) node.getLayoutMargin(YogaEdge.TOP)); - assertEquals(0, (int) node.getLayoutMargin(YogaEdge.BOTTOM)); - assertEquals(0, (int) node.getLayoutPadding(YogaEdge.LEFT)); - assertEquals(0, (int) node.getLayoutPadding(YogaEdge.RIGHT)); - assertEquals(0, (int) node.getLayoutPadding(YogaEdge.TOP)); - assertEquals(0, (int) node.getLayoutPadding(YogaEdge.BOTTOM)); - assertEquals(0, (int) node.getLayoutBorder(YogaEdge.LEFT)); - assertEquals(0, (int) node.getLayoutBorder(YogaEdge.RIGHT)); - assertEquals(0, (int) node.getLayoutBorder(YogaEdge.TOP)); - assertEquals(0, (int) node.getLayoutBorder(YogaEdge.BOTTOM)); - assertEquals(node.getLayoutDirection(), YogaDirection.INHERIT); - assertTrue(node.hasNewLayout()); - assertFalse(node.isMeasureDefined()); - assertFalse(node.isBaselineDefined()); - assertEquals(null, node.getData()); - } - - private YogaNode createNode() { - return mNodeFactory.create(); - } - - private YogaNode createNode(YogaConfig config) { - return mNodeFactory.create(config); - } -} diff --git a/java/tests/com/facebook/yoga/YogaNodeTest.kt b/java/tests/com/facebook/yoga/YogaNodeTest.kt new file mode 100644 index 0000000000..62bc69fdf0 --- /dev/null +++ b/java/tests/com/facebook/yoga/YogaNodeTest.kt @@ -0,0 +1,319 @@ +/* + * 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. + */ + +package com.facebook.yoga + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.Parameterized + +@RunWith(Parameterized::class) +class YogaNodeTest { + companion object { + @JvmStatic + @Parameterized.Parameters(name = "{0}") + fun nodeFactories(): Iterable = + TestParametrization.nodeFactories() + } + + @Parameterized.Parameter lateinit var mNodeFactory: TestParametrization.NodeFactory + + @Test + fun testInit() { + val node = createNode() + } + + @Test + fun testBaseline() { + val root = createNode() + root.setFlexDirection(YogaFlexDirection.ROW) + root.setAlignItems(YogaAlign.BASELINE) + root.setWidth(100f) + root.setHeight(100f) + + val child1 = createNode() + child1.setWidth(40f) + child1.setHeight(40f) + root.addChildAt(child1, 0) + + val child2 = createNode() + child2.setWidth(40f) + child2.setHeight(40f) + child2.setBaselineFunction(YogaBaselineFunction { node, width, height -> 0f }) + root.addChildAt(child2, 1) + + root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED) + + assertEquals(0, child1.getLayoutY().toInt()) + assertEquals(40, child2.getLayoutY().toInt()) + } + + @Test + fun testMeasure() { + val node = createNode() + node.setMeasureFunction( + YogaMeasureFunction { node, width, widthMode, height, heightMode -> + YogaMeasureOutput.make(100, 100) + } + ) + node.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED) + assertEquals(100, node.getLayoutWidth().toInt()) + assertEquals(100, node.getLayoutHeight().toInt()) + } + + @Test + fun testMeasureFloat() { + val node = createNode() + node.setMeasureFunction( + YogaMeasureFunction { node, width, widthMode, height, heightMode -> + YogaMeasureOutput.make(100.5f, 100.5f) + } + ) + node.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED) + assertEquals(101f, node.getLayoutWidth(), 0.01f) + assertEquals(101f, node.getLayoutHeight(), 0.01f) + } + + @Test + fun testMeasureFloatMin() { + val node = createNode() + node.setMeasureFunction( + YogaMeasureFunction { node, width, widthMode, height, heightMode -> + YogaMeasureOutput.make(Float.MIN_VALUE, Float.MIN_VALUE) + } + ) + node.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED) + assertEquals(Float.MIN_VALUE, node.getLayoutWidth(), 0.01f) + assertEquals(Float.MIN_VALUE, node.getLayoutHeight(), 0.01f) + } + + @Test + fun testMeasureFloatBigNumber() { + val node = createNode() + val bigNumber = 10E5.toFloat() + node.setMeasureFunction( + YogaMeasureFunction { node, width, widthMode, height, heightMode -> + YogaMeasureOutput.make(bigNumber, bigNumber) + } + ) + node.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED) + assertEquals(bigNumber, node.getLayoutWidth(), 0.01f) + assertEquals(bigNumber, node.getLayoutHeight(), 0.01f) + } + + @Test + fun testCopyStyle() { + val node0 = createNode() + assertTrue(YogaConstants.isUndefined(node0.getMaxHeight())) + + val node1 = createNode() + node1.setMaxHeight(100f) + + node0.copyStyle(node1) + assertEquals(100, node0.getMaxHeight().value.toInt()) + } + + @Test + fun testLayoutMargin() { + val node = createNode() + node.setWidth(100f) + node.setHeight(100f) + node.setMargin(YogaEdge.START, 1f) + node.setMargin(YogaEdge.END, 2f) + node.setMargin(YogaEdge.TOP, 3f) + node.setMargin(YogaEdge.BOTTOM, 4f) + node.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED) + + assertEquals(1, node.getLayoutMargin(YogaEdge.LEFT).toInt()) + assertEquals(2, node.getLayoutMargin(YogaEdge.RIGHT).toInt()) + assertEquals(3, node.getLayoutMargin(YogaEdge.TOP).toInt()) + assertEquals(4, node.getLayoutMargin(YogaEdge.BOTTOM).toInt()) + } + + @Test + fun testLayoutPadding() { + val node = createNode() + node.setWidth(100f) + node.setHeight(100f) + node.setPadding(YogaEdge.START, 1f) + node.setPadding(YogaEdge.END, 2f) + node.setPadding(YogaEdge.TOP, 3f) + node.setPadding(YogaEdge.BOTTOM, 4f) + node.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED) + + assertEquals(1, node.getLayoutPadding(YogaEdge.LEFT).toInt()) + assertEquals(2, node.getLayoutPadding(YogaEdge.RIGHT).toInt()) + assertEquals(3, node.getLayoutPadding(YogaEdge.TOP).toInt()) + assertEquals(4, node.getLayoutPadding(YogaEdge.BOTTOM).toInt()) + } + + @Test + fun testLayoutBorder() { + val node = createNode() + node.setWidth(100f) + node.setHeight(100f) + node.setBorder(YogaEdge.START, 1f) + node.setBorder(YogaEdge.END, 2f) + node.setBorder(YogaEdge.TOP, 3f) + node.setBorder(YogaEdge.BOTTOM, 4f) + node.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED) + + assertEquals(1, node.getLayoutBorder(YogaEdge.LEFT).toInt()) + assertEquals(2, node.getLayoutBorder(YogaEdge.RIGHT).toInt()) + assertEquals(3, node.getLayoutBorder(YogaEdge.TOP).toInt()) + assertEquals(4, node.getLayoutBorder(YogaEdge.BOTTOM).toInt()) + } + + @Test + fun testUseWebDefaults() { + val config = YogaConfigFactory.create() + config.setUseWebDefaults(true) + val node = createNode(config) + assertEquals(YogaFlexDirection.ROW, node.getFlexDirection()) + } + + @Test + fun testPercentPaddingOnRoot() { + val node = createNode() + node.setPaddingPercent(YogaEdge.ALL, 10f) + node.calculateLayout(50f, 50f) + + assertEquals(5, node.getLayoutPadding(YogaEdge.LEFT).toInt()) + assertEquals(5, node.getLayoutPadding(YogaEdge.RIGHT).toInt()) + assertEquals(5, node.getLayoutPadding(YogaEdge.TOP).toInt()) + assertEquals(5, node.getLayoutPadding(YogaEdge.BOTTOM).toInt()) + } + + @Test + fun testDefaultEdgeValues() { + val node = createNode() + + for (edge in YogaEdge.values()) { + assertEquals(YogaUnit.UNDEFINED, node.getMargin(edge).unit) + assertEquals(YogaUnit.UNDEFINED, node.getPadding(edge).unit) + assertEquals(YogaUnit.UNDEFINED, node.getPosition(edge).unit) + assertTrue(YogaConstants.isUndefined(node.getBorder(edge))) + } + } + + @Test + fun initiallyHasNewLayout() { + val root = createNode() + assertTrue(root.hasNewLayout()) + } + + @Test + fun initialLayoutCanBeMarkedSeen() { + val root = createNode() + root.markLayoutSeen() + assertFalse(root.hasNewLayout()) + } + + @Test + fun calculatingLayoutMarksLayoutAsUnseen() { + val root = createNode() + root.markLayoutSeen() + root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED) + assertTrue(root.hasNewLayout()) + } + + @Test + fun calculatedLayoutCanBeMarkedSeen() { + val root = createNode() + root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED) + root.markLayoutSeen() + assertFalse(root.hasNewLayout()) + } + + @Test + fun recalculatingLayoutDoesMarkAsUnseen() { + val root = createNode() + root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED) + root.markLayoutSeen() + root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED) + assertTrue(root.hasNewLayout()) + } + + @Test + fun resetAlsoResetsLayoutSeen() { + val root = createNode() + root.markLayoutSeen() + root.reset() + assertTrue(root.hasNewLayout()) + } + + @Test + fun directionIsPassedThrough() { + val root = createNode() + + root.setDirection(YogaDirection.RTL) + root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED) + + assertEquals(root.getLayoutDirection(), YogaDirection.RTL) + } + + @Test + fun testResetApiShouldResetAllLayoutOutputs() { + val config = YogaConfigFactory.create() + config.setErrata(YogaErrata.STRETCH_FLEX_BASIS) + val node = createNode(config) + node.setWidth(100f) + node.setHeight(100f) + node.setMargin(YogaEdge.START, 1f) + node.setMargin(YogaEdge.END, 2f) + node.setMargin(YogaEdge.TOP, 3f) + node.setMargin(YogaEdge.BOTTOM, 4f) + node.setPadding(YogaEdge.START, 1f) + node.setPadding(YogaEdge.END, 2f) + node.setPadding(YogaEdge.TOP, 3f) + node.setPadding(YogaEdge.BOTTOM, 4f) + node.setBorder(YogaEdge.START, 1f) + node.setBorder(YogaEdge.END, 2f) + node.setBorder(YogaEdge.TOP, 3f) + node.setBorder(YogaEdge.BOTTOM, 4f) + node.setDirection(YogaDirection.RTL) + node.markLayoutSeen() + node.setMeasureFunction( + YogaMeasureFunction { node, width, widthMode, height, heightMode -> + YogaMeasureOutput.make(100, 100) + } + ) + node.setBaselineFunction(YogaBaselineFunction { node, width, height -> height }) + node.setData(ArrayList()) + + node.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED) + node.reset() + + assertEquals(0, node.getLayoutHeight().toInt()) + assertEquals(0, node.getLayoutWidth().toInt()) + assertEquals(0, node.getLayoutMargin(YogaEdge.LEFT).toInt()) + assertEquals(0, node.getLayoutMargin(YogaEdge.RIGHT).toInt()) + assertEquals(0, node.getLayoutMargin(YogaEdge.TOP).toInt()) + assertEquals(0, node.getLayoutMargin(YogaEdge.BOTTOM).toInt()) + assertEquals(0, node.getLayoutPadding(YogaEdge.LEFT).toInt()) + assertEquals(0, node.getLayoutPadding(YogaEdge.RIGHT).toInt()) + assertEquals(0, node.getLayoutPadding(YogaEdge.TOP).toInt()) + assertEquals(0, node.getLayoutPadding(YogaEdge.BOTTOM).toInt()) + assertEquals(0, node.getLayoutBorder(YogaEdge.LEFT).toInt()) + assertEquals(0, node.getLayoutBorder(YogaEdge.RIGHT).toInt()) + assertEquals(0, node.getLayoutBorder(YogaEdge.TOP).toInt()) + assertEquals(0, node.getLayoutBorder(YogaEdge.BOTTOM).toInt()) + assertEquals(node.getLayoutDirection(), YogaDirection.INHERIT) + assertTrue(node.hasNewLayout()) + assertFalse(node.isMeasureDefined()) + assertFalse(node.isBaselineDefined()) + assertNull(node.getData()) + } + + private fun createNode(): YogaNode = mNodeFactory.create() + + private fun createNode(config: YogaConfig): YogaNode = mNodeFactory.create(config) +} diff --git a/java/tests/com/facebook/yoga/YogaValueTest.java b/java/tests/com/facebook/yoga/YogaValueTest.java deleted file mode 100644 index 5c708b9135..0000000000 --- a/java/tests/com/facebook/yoga/YogaValueTest.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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. - */ - -package com.facebook.yoga; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class YogaValueTest { - - @Test - public void testEquals() { - assertEquals(new YogaValue(0, YogaUnit.UNDEFINED), new YogaValue(0, YogaUnit.UNDEFINED)); - assertEquals(new YogaValue(0, YogaUnit.POINT), new YogaValue(0, YogaUnit.POINT)); - assertEquals(new YogaValue(0, YogaUnit.PERCENT), new YogaValue(0, YogaUnit.PERCENT)); - assertEquals(new YogaValue(0, YogaUnit.UNDEFINED), new YogaValue(1, YogaUnit.UNDEFINED)); - assertEquals(new YogaValue(Float.NaN, YogaUnit.POINT), new YogaValue(Float.NaN, YogaUnit.POINT)); - } -} diff --git a/java/tests/com/facebook/yoga/YogaValueTest.kt b/java/tests/com/facebook/yoga/YogaValueTest.kt new file mode 100644 index 0000000000..b2ec7aa038 --- /dev/null +++ b/java/tests/com/facebook/yoga/YogaValueTest.kt @@ -0,0 +1,23 @@ +/* + * 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. + */ + +package com.facebook.yoga + +import org.junit.Assert.assertEquals +import org.junit.Test + +class YogaValueTest { + + @Test + fun testEquals() { + assertEquals(YogaValue(0f, YogaUnit.UNDEFINED), YogaValue(0f, YogaUnit.UNDEFINED)) + assertEquals(YogaValue(0f, YogaUnit.POINT), YogaValue(0f, YogaUnit.POINT)) + assertEquals(YogaValue(0f, YogaUnit.PERCENT), YogaValue(0f, YogaUnit.PERCENT)) + assertEquals(YogaValue(0f, YogaUnit.UNDEFINED), YogaValue(1f, YogaUnit.UNDEFINED)) + assertEquals(YogaValue(Float.NaN, YogaUnit.POINT), YogaValue(Float.NaN, YogaUnit.POINT)) + } +} diff --git a/java/tests/com/facebook/yoga/utils/TestUtils.java b/java/tests/com/facebook/yoga/utils/TestUtils.java deleted file mode 100644 index 4ba6b3dbd4..0000000000 --- a/java/tests/com/facebook/yoga/utils/TestUtils.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * 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. - */ - -package com.facebook.yoga.utils; - -import com.facebook.yoga.YogaMeasureMode; -import com.facebook.yoga.YogaMeasureOutput; -import com.facebook.yoga.YogaMeasureFunction; -import com.facebook.yoga.YogaNode; -import com.facebook.yoga.YogaFlexDirection; - -public class TestUtils { - - public static class intrinsicMeasureFunction implements YogaMeasureFunction { - - private static float widthPerChar = 10; - private static float heightPerChar = 10; - - @Override - public long measure( - YogaNode node, - float width, - YogaMeasureMode widthMode, - float height, - YogaMeasureMode heightMode) { - String text = (String) node.getData(); - float measuredWidth; - float measuredHeight; - - if (widthMode == YogaMeasureMode.EXACTLY) { - measuredWidth = width; - } else if (widthMode == YogaMeasureMode.AT_MOST) { - measuredWidth = Math.min(text.length() * widthPerChar, width); - } else { - measuredWidth = text.length() * widthPerChar; - } - - if (heightMode == YogaMeasureMode.EXACTLY) { - measuredHeight = height; - } else if (heightMode == YogaMeasureMode.AT_MOST) { - measuredHeight = - Math.min( - caclulateHeight(text, node.getFlexDirection() == YogaFlexDirection.COLUMN - ? measuredWidth - : Math.max(measuredWidth, getWidestWordWidth(text))), height); - } else { - measuredHeight = caclulateHeight(text, node.getFlexDirection() == YogaFlexDirection.COLUMN - ? measuredWidth - : Math.max(measuredWidth, getWidestWordWidth(text))); - } - - return YogaMeasureOutput.make(measuredWidth, measuredHeight); - } - - static float getWidestWordWidth(String text) { - int widestWordLength = 0; - String[] words = text.split(" "); - for (String word : words) { - int wordLength = word.length(); - if (widestWordLength < wordLength) { - widestWordLength = wordLength; - } - } - return (float) widestWordLength * widthPerChar; - } - - static float caclulateHeight(String text, float measuredWidth) { - if (text.length() * widthPerChar <= measuredWidth) { - return heightPerChar; - } - - String[] words = text.split(" "); - float lines = 1; - float currentLineLength = 0; - for (String word : words) { - float wordWidth = word.length() * widthPerChar; - if (wordWidth > measuredWidth) { - if (currentLineLength > 0) { - lines++; - } - lines++; - currentLineLength = 0; - } else if (currentLineLength + wordWidth <= measuredWidth) { - currentLineLength += wordWidth + widthPerChar; - } else { - lines++; - currentLineLength = wordWidth + widthPerChar; - } - } - return (currentLineLength == 0 ? lines - 1 : lines) * heightPerChar; - } - } -} diff --git a/java/tests/com/facebook/yoga/utils/TestUtils.kt b/java/tests/com/facebook/yoga/utils/TestUtils.kt new file mode 100644 index 0000000000..546a1c2eed --- /dev/null +++ b/java/tests/com/facebook/yoga/utils/TestUtils.kt @@ -0,0 +1,99 @@ +/* + * 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. + */ + +package com.facebook.yoga.utils + +import com.facebook.yoga.YogaFlexDirection +import com.facebook.yoga.YogaMeasureFunction +import com.facebook.yoga.YogaMeasureMode +import com.facebook.yoga.YogaMeasureOutput +import com.facebook.yoga.YogaNode + +class TestUtils { + class intrinsicMeasureFunction : YogaMeasureFunction { + override fun measure( + node: YogaNode, + width: Float, + widthMode: YogaMeasureMode, + height: Float, + heightMode: YogaMeasureMode, + ): Long { + val text = node.data as String + val measuredWidth: Float = + if (widthMode == YogaMeasureMode.EXACTLY) { + width + } else if (widthMode == YogaMeasureMode.AT_MOST) { + Math.min(text.length * widthPerChar, width) + } else { + text.length * widthPerChar + } + + val effectiveWidth = + if (node.flexDirection == YogaFlexDirection.COLUMN) { + measuredWidth + } else { + Math.max(measuredWidth, getWidestWordWidth(text)) + } + + val measuredHeight: Float = + if (heightMode == YogaMeasureMode.EXACTLY) { + height + } else if (heightMode == YogaMeasureMode.AT_MOST) { + Math.min(caclulateHeight(text, effectiveWidth), height) + } else { + caclulateHeight(text, effectiveWidth) + } + + return YogaMeasureOutput.make(measuredWidth, measuredHeight) + } + + companion object { + private const val widthPerChar: Float = 10f + private const val heightPerChar: Float = 10f + + @JvmStatic + fun getWidestWordWidth(text: String): Float { + var widestWordLength = 0 + val words = text.split(" ") + for (word in words) { + val wordLength = word.length + if (widestWordLength < wordLength) { + widestWordLength = wordLength + } + } + return widestWordLength.toFloat() * widthPerChar + } + + @JvmStatic + fun caclulateHeight(text: String, measuredWidth: Float): Float { + if (text.length * widthPerChar <= measuredWidth) { + return heightPerChar + } + + val words = text.split(" ") + var lines = 1f + var currentLineLength = 0f + for (word in words) { + val wordWidth = word.length * widthPerChar + if (wordWidth > measuredWidth) { + if (currentLineLength > 0) { + lines++ + } + lines++ + currentLineLength = 0f + } else if (currentLineLength + wordWidth <= measuredWidth) { + currentLineLength += wordWidth + widthPerChar + } else { + lines++ + currentLineLength = wordWidth + widthPerChar + } + } + return (if (currentLineLength == 0f) lines - 1 else lines) * heightPerChar + } + } + } +}