Skip to content

Commit

Permalink
Remove child helpers from TestFlexContainer interface
Browse files Browse the repository at this point in the history
We can implement these generally now that the backing widget list provides the existing count for new insertion positions.
  • Loading branch information
JakeWharton committed Oct 24, 2024
1 parent 28da06e commit b672ecc
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import app.cash.redwood.layout.widget.Spacer
import app.cash.redwood.snapshot.testing.ComposeSnapshotter
import app.cash.redwood.snapshot.testing.ComposeUiTestWidgetFactory
import app.cash.redwood.ui.Px
import app.cash.redwood.widget.Widget
import app.cash.redwood.widget.compose.ComposeWidgetChildren
import app.cash.redwood.yoga.FlexDirection
import com.android.resources.LayoutDirection
Expand Down Expand Up @@ -72,8 +71,6 @@ class ComposeUiFlexContainerTest(
private val delegate: ComposeUiFlexContainer,
) : TestFlexContainer<@Composable () -> Unit>,
YogaFlexContainer<@Composable () -> Unit> by delegate {
private var childCount = 0

override val children: ComposeWidgetChildren = delegate.children

constructor(direction: FlexDirection, backgroundColor: Int) : this(
Expand All @@ -92,20 +89,6 @@ class ComposeUiFlexContainerTest(
}
}

override fun add(widget: Widget<@Composable () -> Unit>) {
addAt(childCount, widget)
}

override fun addAt(index: Int, widget: Widget<@Composable () -> Unit>) {
delegate.children.insert(index, widget)
childCount++
}

override fun removeAt(index: Int) {
delegate.children.remove(index = index, count = 1)
childCount--
}

override fun onEndChanges() {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ abstract class AbstractFlexContainerTest<T : Any> {
onScroll(null)
}

protected fun <T : Any> TestFlexContainer<T>.add(widget: Widget<T>) {
addAt(children.widgets.size, widget)
}

protected fun <T : Any> TestFlexContainer<T>.addAt(index: Int, widget: Widget<T>) {
children.insert(index, widget)
}

protected fun <T : Any> TestFlexContainer<T>.removeAt(index: Int) {
children.remove(index, 1)
}

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

Expand Down Expand Up @@ -959,9 +971,6 @@ interface TestFlexContainer<T : Any> :
fun overflow(overflow: Overflow)
fun onScroll(onScroll: ((Px) -> Unit)?)
fun scroll(offset: Px)
fun add(widget: Widget<T>)
fun addAt(index: Int, widget: Widget<T>)
fun removeAt(index: Int)
}

private val movies = listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ class UIViewFlexContainerTest(
ResizableWidget<UIView>,
YogaFlexContainer<UIView> by delegate,
ChangeListener by delegate {
private var childCount = 0

override var sizeListener: SizeListener? by delegate::sizeListener

override val children: Widget.Children<UIView> = delegate.children
Expand All @@ -100,20 +98,6 @@ class UIViewFlexContainerTest(
override fun scroll(offset: Px) {
(delegate.value as UIScrollView).setContentOffset(cValue { y = offset.value }, false)
}

override fun add(widget: Widget<UIView>) {
addAt(childCount, widget)
}

override fun addAt(index: Int, widget: Widget<UIView>) {
delegate.children.insert(index, widget)
childCount++
}

override fun removeAt(index: Int) {
delegate.children.remove(index = index, count = 1)
childCount--
}
}

override fun snapshotter(widget: UIView) = UIViewSnapshotter.framed(callback, widget)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import app.cash.redwood.snapshot.testing.ViewTestWidgetFactory
import app.cash.redwood.ui.Px
import app.cash.redwood.widget.ChangeListener
import app.cash.redwood.widget.ViewGroupChildren
import app.cash.redwood.widget.Widget
import app.cash.redwood.yoga.FlexDirection
import com.android.resources.LayoutDirection
import org.junit.Rule
Expand Down Expand Up @@ -76,31 +75,12 @@ class ViewFlexContainerTest(
) : TestFlexContainer<View>,
YogaFlexContainer<View> by delegate,
ChangeListener by delegate {
private var childCount = 0
private var onScroll: ((Px) -> Unit)? = null

override val children: ViewGroupChildren = delegate.children

override fun onScroll(onScroll: ((Px) -> Unit)?) {
this.onScroll = onScroll
}

override fun scroll(offset: Px) {
onScroll?.invoke(offset)
}

override fun add(widget: Widget<View>) {
addAt(childCount, widget)
}

override fun addAt(index: Int, widget: Widget<View>) {
delegate.children.insert(index, widget)
childCount++
}

override fun removeAt(index: Int) {
delegate.children.remove(index = index, count = 1)
childCount--
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import app.cash.redwood.lazylayout.widget.LazyList
import app.cash.redwood.snapshot.testing.ComposeSnapshotter
import app.cash.redwood.snapshot.testing.ComposeUiTestWidgetFactory
import app.cash.redwood.ui.Px
import app.cash.redwood.widget.Widget
import app.cash.redwood.widget.compose.ComposeWidgetChildren
import app.cash.redwood.yoga.FlexDirection
import com.android.resources.LayoutDirection
Expand Down Expand Up @@ -85,7 +84,6 @@ class ComposeUiLazyListTest(
// Work around https://youtrack.jetbrains.com/issue/KT-68850
override val value: @Composable () -> Unit get() = delegate.value

private var childCount = 0
private var onScroll: ((Px) -> Unit)? = null

constructor(direction: FlexDirection, backgroundColor: Int) : this(
Expand All @@ -111,20 +109,6 @@ class ComposeUiLazyListTest(
override fun overflow(overflow: Overflow) {
}

override fun add(widget: Widget<@Composable () -> Unit>) {
addAt(childCount, widget)
}

override fun addAt(index: Int, widget: Widget<@Composable () -> Unit>) {
delegate.items.insert(index, widget)
childCount++
}

override fun removeAt(index: Int) {
delegate.items.remove(index = index, count = 1)
childCount--
}

override fun onEndChanges() {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ class UIViewLazyListAsFlexContainerTest(
private val delegate: LazyList<UIView>,
) : TestFlexContainer<UIView>,
LazyList<UIView> by delegate {
private var childCount = 0
private var onScroll: ((Px) -> Unit)? = null

constructor(delegate: LazyList<UIView>, direction: FlexDirection, backgroundColor: Int) : this(
Expand All @@ -88,20 +87,6 @@ class UIViewLazyListAsFlexContainerTest(
override fun overflow(overflow: Overflow) {
}

override fun add(widget: Widget<UIView>) {
addAt(childCount, widget)
}

override fun addAt(index: Int, widget: Widget<UIView>) {
delegate.items.insert(index, widget)
childCount++
}

override fun removeAt(index: Int) {
delegate.items.remove(index = index, count = 1)
childCount--
}

override fun onEndChanges() {
(delegate as ChangeListener).onEndChanges()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ class ViewLazyListAsFlexContainerTest(
) : TestFlexContainer<View>,
LazyList<View> by delegate,
ChangeListener by delegate {
private var childCount = 0
private var onScroll: ((Px) -> Unit)? = null

constructor(context: Context, direction: FlexDirection, backgroundColor: Int) : this(
Expand All @@ -108,19 +107,5 @@ class ViewLazyListAsFlexContainerTest(

override fun overflow(overflow: Overflow) {
}

override fun add(widget: Widget<View>) {
addAt(childCount, widget)
}

override fun addAt(index: Int, widget: Widget<View>) {
delegate.items.insert(index, widget)
childCount++
}

override fun removeAt(index: Int) {
delegate.items.remove(index = index, count = 1)
childCount--
}
}
}

0 comments on commit b672ecc

Please sign in to comment.