Skip to content

Commit

Permalink
Explicitly configure layouts in snapshot tests (#2389)
Browse files Browse the repository at this point in the history
We weren't fully configuring Row and Column layouts, which caused
the (undocumented) yoga defaults to be used in tests.
  • Loading branch information
squarejesse authored Oct 17, 2024
1 parent 6799c4d commit aeb1fb8
Show file tree
Hide file tree
Showing 72 changed files with 186 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class ComposeUiFlexContainerTest(
backgroundColor: Int,
): ComposeTestFlexContainer {
return ComposeTestFlexContainer(direction, backgroundColor)
.apply { applyDefaults() }
}

override fun row() = flexContainer(FlexDirection.Row)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ abstract class AbstractFlexContainerTest<T : Any> {
backgroundColor: Int = argb(51, 0, 0, 255),
): TestFlexContainer<T>

/**
* Yoga node’s default values for properties like alignment are different from Redwood's default
* values, so we explicitly apply those defaults here. This is only necessary in tests; in
* production the framework explicitly sets every property.
*/
protected fun TestFlexContainer<*>.applyDefaults() {
width(Constraint.Wrap)
height(Constraint.Wrap)
margin(Margin.Zero)
overflow(Overflow.Clip)
crossAxisAlignment(CrossAxisAlignment.Start)
mainAxisAlignment(MainAxisAlignment.Start)
onScroll(null)
}

/** Returns a non-lazy flex container row, even if the test is for a LazyList. */
abstract fun row(): Row<T>

Expand Down Expand Up @@ -119,7 +134,14 @@ abstract class AbstractFlexContainerTest<T : Any> {
val container = flexContainer(flexDirection)
container.width(width)
container.height(height)
container.add(widgetFactory.text(movies.first()))
container.add(
widgetFactory.text(
text = movies.first(),
modifier = Modifier
.then(HorizontalAlignmentImpl(CrossAxisAlignment.Stretch))
.then(VerticalAlignmentImpl(CrossAxisAlignment.Stretch)),
),
)
container.onEndChanges()
snapshotter(container.value).snapshot()
}
Expand Down Expand Up @@ -484,7 +506,10 @@ abstract class AbstractFlexContainerTest<T : Any> {
parent.children.insert(
0,
flexContainer(FlexDirection.Column).apply {
modifier = GrowImpl(1.0)
modifier = Modifier
.then(GrowImpl(1.0))
.then(HorizontalAlignmentImpl(CrossAxisAlignment.Stretch))
.then(VerticalAlignmentImpl(CrossAxisAlignment.Stretch))
width(Constraint.Fill)
mainAxisAlignment(MainAxisAlignment.SpaceBetween)
add(
Expand All @@ -505,7 +530,10 @@ abstract class AbstractFlexContainerTest<T : Any> {
parent.children.insert(
1,
flexContainer(FlexDirection.Column).apply {
modifier = GrowImpl(1.0)
modifier = Modifier
.then(GrowImpl(1.0))
.then(HorizontalAlignmentImpl(CrossAxisAlignment.Stretch))
.then(VerticalAlignmentImpl(CrossAxisAlignment.Stretch))
width(Constraint.Fill)
mainAxisAlignment(MainAxisAlignment.SpaceBetween)
add(
Expand Down Expand Up @@ -777,12 +805,20 @@ abstract class AbstractFlexContainerTest<T : Any> {
}
.also { column.add(it) }

val rowA1 = widgetFactory.text("A1 ".repeat(50))
.apply { modifier = FlexImpl(1.0) }
.also { rowA.children.insert(0, it) }
val rowA2 = widgetFactory.text("A-TWO ".repeat(50))
.apply { modifier = FlexImpl(1.0) }
.also { rowA.children.insert(1, it) }
val rowA1 = widgetFactory.text(
text = "A1 ".repeat(50),
modifier = Modifier
.then(FlexImpl(1.0))
.then(HorizontalAlignmentImpl(CrossAxisAlignment.Stretch))
.then(VerticalAlignmentImpl(CrossAxisAlignment.Stretch)),
).also { rowA.children.insert(0, it) }
val rowA2 = widgetFactory.text(
text = "A-TWO ".repeat(50),
modifier = Modifier
.then(FlexImpl(1.0))
.then(HorizontalAlignmentImpl(CrossAxisAlignment.Stretch))
.then(VerticalAlignmentImpl(CrossAxisAlignment.Stretch)),
).also { rowA.children.insert(1, it) }

val rowB = widgetFactory.text("B1 ".repeat(5))
.apply {
Expand Down Expand Up @@ -811,13 +847,12 @@ abstract class AbstractFlexContainerTest<T : Any> {
height(Constraint.Fill)
}

val alignStart = HorizontalAlignmentImpl(CrossAxisAlignment.Start)
flexContainer(FlexDirection.Column)
.apply {
width(Constraint.Fill)
modifier = WidthImpl(25.dp)
add(widgetFactory.text("ok", alignStart)) // This is under 25.dp in width.
add(widgetFactory.text("1 2 3 4", alignStart)) // Each character is under 25.dp in width.
add(widgetFactory.text("ok")) // This is under 25.dp in width.
add(widgetFactory.text("1 2 3 4")) // Each character is under 25.dp in width.
onEndChanges()
}
.also { fullWidthParent.children.insert(0, it) }
Expand All @@ -826,8 +861,8 @@ abstract class AbstractFlexContainerTest<T : Any> {
.apply {
width(Constraint.Fill)
modifier = WidthImpl(25.dp)
add(widgetFactory.text("overflows parent", alignStart)) // This is over 25.dp in width.
add(widgetFactory.text("1 2 3 4", alignStart)) // Each character is under 25.dp in width.
add(widgetFactory.text("overflows parent")) // This is over 25.dp in width.
add(widgetFactory.text("1 2 3 4")) // Each character is under 25.dp in width.
onEndChanges()
}
.also { fullWidthParent.children.insert(1, it) }
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class UIViewFlexContainerTest(
override fun invalidateSize() {
}
}

applyDefaults()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class ViewFlexContainerTest(
value.setBackgroundColor(backgroundColor)
}
return ViewTestFlexContainer(delegate)
.apply { applyDefaults() }
}

override fun row() = flexContainer(FlexDirection.Row)
Expand Down
Loading

0 comments on commit aeb1fb8

Please sign in to comment.