Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't nest lazy layouts in tests
Browse files Browse the repository at this point in the history
Sometimes I want to test a Column, and sometimes I want to use
a Column as a facet in a test. This separates those two cases.
squarejesse committed Nov 20, 2023
1 parent fbd720a commit 35a04d5
Showing 27 changed files with 70 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@ import app.cash.redwood.Modifier as RedwoodModifier
import app.cash.redwood.layout.AbstractFlexContainerTest
import app.cash.redwood.layout.TestFlexContainer
import app.cash.redwood.layout.Text
import app.cash.redwood.layout.widget.Column
import app.cash.redwood.layout.widget.FlexContainer
import app.cash.redwood.widget.Widget
import app.cash.redwood.yoga.FlexDirection
@@ -72,7 +73,10 @@ class ComposeUiFlexContainerTest(
}
}

override fun verifySnapshot(container: TestFlexContainer<@Composable () -> Unit>, name: String?) {
override fun column(): Column<@Composable () -> Unit> =
ComposeUiFlexContainer(FlexDirection.Column)

override fun verifySnapshot(container: Widget<@Composable () -> Unit>, name: String?) {
paparazzi.snapshot(name) {
container.value()
}
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
@@ -26,6 +26,7 @@ import app.cash.redwood.layout.modifier.Shrink
import app.cash.redwood.layout.modifier.Size
import app.cash.redwood.layout.modifier.VerticalAlignment
import app.cash.redwood.layout.modifier.Width
import app.cash.redwood.layout.widget.Column
import app.cash.redwood.ui.Dp
import app.cash.redwood.ui.Margin
import app.cash.redwood.ui.dp
@@ -38,7 +39,8 @@ import kotlin.test.assertTrue
abstract class AbstractFlexContainerTest<T : Any> {
abstract fun flexContainer(direction: FlexDirection): TestFlexContainer<T>
abstract fun widget(): Text<T>
abstract fun verifySnapshot(container: TestFlexContainer<T>, name: String? = null)
abstract fun column(): Column<T>
abstract fun verifySnapshot(container: Widget<T>, name: String? = null)

private fun widget(text: String, modifier: Modifier = Modifier): Text<T> = widget().apply {
text(text)
@@ -439,12 +441,13 @@ abstract class AbstractFlexContainerTest<T : Any> {

@Test
fun testDynamicContainerSize() {
val parent = flexContainer(FlexDirection.Column).apply {
val parent = column().apply {
width(Constraint.Fill)
height(Constraint.Fill)
}

parent.add(
parent.children.insert(
0,
flexContainer(FlexDirection.Column).apply {
modifier = GrowImpl(1.0)
width(Constraint.Fill)
@@ -464,7 +467,8 @@ abstract class AbstractFlexContainerTest<T : Any> {
},
)

parent.add(
parent.children.insert(
1,
flexContainer(FlexDirection.Column).apply {
modifier = GrowImpl(1.0)
width(Constraint.Fill)
@@ -487,7 +491,7 @@ abstract class AbstractFlexContainerTest<T : Any> {

verifySnapshot(parent, "both")

parent.removeAt(1)
parent.children.remove(index = 1, count = 1)
verifySnapshot(parent, "single")
}

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
@@ -19,6 +19,7 @@ import app.cash.redwood.Modifier
import app.cash.redwood.layout.AbstractFlexContainerTest
import app.cash.redwood.layout.TestFlexContainer
import app.cash.redwood.layout.Text
import app.cash.redwood.layout.widget.Column
import app.cash.redwood.layout.widget.FlexContainer
import app.cash.redwood.widget.ChangeListener
import app.cash.redwood.widget.Widget
@@ -51,6 +52,8 @@ class UIViewFlexContainerTest(
}
}

override fun column(): Column<UIView> = UIViewFlexContainer(FlexDirection.Column)

class UIViewTestFlexContainer internal constructor(
private val delegate: UIViewFlexContainer,
) : TestFlexContainer<UIView>, FlexContainer<UIView> by delegate, ChangeListener by delegate {
@@ -75,7 +78,7 @@ class UIViewFlexContainerTest(
}
}

override fun verifySnapshot(container: TestFlexContainer<UIView>, name: String?) {
override fun verifySnapshot(container: Widget<UIView>, name: String?) {
val screenSize = CGRectMake(0.0, 0.0, 390.0, 844.0) // iPhone 14.
container.value.setFrame(screenSize)

Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ import app.cash.redwood.Modifier
import app.cash.redwood.layout.AbstractFlexContainerTest
import app.cash.redwood.layout.TestFlexContainer
import app.cash.redwood.layout.Text
import app.cash.redwood.layout.widget.Column
import app.cash.redwood.layout.widget.FlexContainer
import app.cash.redwood.widget.ChangeListener
import app.cash.redwood.widget.Widget
@@ -67,7 +68,9 @@ class ViewFlexContainerTest(
}
}

override fun verifySnapshot(container: TestFlexContainer<View>, name: String?) {
override fun column(): Column<View> = ViewFlexContainer(paparazzi.context, FlexDirection.Column)

override fun verifySnapshot(container: Widget<View>, name: String?) {
paparazzi.snapshot(container.value, name)
}

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.
1 change: 1 addition & 0 deletions redwood-lazylayout-composeui/build.gradle
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ kotlin {

androidUnitTest {
dependencies {
implementation projects.redwoodLayoutComposeui
implementation projects.redwoodLayoutSharedTest
implementation libs.testParameterInjector
}
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@ import app.cash.redwood.layout.AbstractFlexContainerTest
import app.cash.redwood.layout.TestFlexContainer
import app.cash.redwood.layout.Text
import app.cash.redwood.layout.api.MainAxisAlignment
import app.cash.redwood.layout.widget.Column
import app.cash.redwood.lazylayout.composeui.ComposeUiLazyList
import app.cash.redwood.lazylayout.widget.LazyList
import app.cash.redwood.widget.Widget
@@ -74,7 +75,10 @@ class ComposeUiLazyListTest(
}
}

override fun verifySnapshot(container: TestFlexContainer<@Composable () -> Unit>, name: String?) {
override fun column(): Column<@Composable () -> Unit> =
ComposeUiRedwoodLayoutWidgetFactory().Column()

override fun verifySnapshot(container: Widget<@Composable () -> Unit>, name: String?) {
paparazzi.snapshot(name) {
container.value()
}
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.
1 change: 1 addition & 0 deletions redwood-lazylayout-view/build.gradle
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ dependencies {
implementation libs.androidx.swipeRefreshLayout
implementation libs.kotlinx.coroutines.android
testImplementation projects.redwoodLayoutSharedTest
testImplementation projects.redwoodLayoutView
testImplementation libs.testParameterInjector
}

Original file line number Diff line number Diff line change
@@ -27,6 +27,8 @@ import app.cash.redwood.layout.AbstractFlexContainerTest
import app.cash.redwood.layout.TestFlexContainer
import app.cash.redwood.layout.Text
import app.cash.redwood.layout.api.MainAxisAlignment
import app.cash.redwood.layout.view.ViewRedwoodLayoutWidgetFactory
import app.cash.redwood.layout.widget.Column
import app.cash.redwood.lazylayout.widget.LazyList
import app.cash.redwood.widget.ChangeListener
import app.cash.redwood.widget.Widget
@@ -65,7 +67,9 @@ class ViewLazyListTest(
}
}

override fun verifySnapshot(container: TestFlexContainer<View>, name: String?) {
override fun column(): Column<View> = ViewRedwoodLayoutWidgetFactory(paparazzi.context).Column()

override fun verifySnapshot(container: Widget<View>, name: String?) {
paparazzi.snapshot(container.value, name)
}

0 comments on commit 35a04d5

Please sign in to comment.