Skip to content

Commit

Permalink
Fixup some failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
squarejesse committed Oct 6, 2023
1 parent 3e9e90a commit 75d3d4f
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ abstract class AbstractFlexContainerTest<T : Any> {
val flexDirection = flexDirectionEnum.value
val container = flexContainer(flexDirection)
container.crossAxisAlignment(CrossAxisAlignment.Start)
container.onEndChanges()
verifySnapshot(container)
}

Expand All @@ -68,6 +69,7 @@ abstract class AbstractFlexContainerTest<T : Any> {
container.width(width)
container.height(height)
container.add(widget(movies.first()))
container.onEndChanges()
verifySnapshot(container)
}

Expand All @@ -81,6 +83,7 @@ abstract class AbstractFlexContainerTest<T : Any> {
movies.take(5).forEach { movie ->
container.add(widget(movie))
}
container.onEndChanges()
verifySnapshot(container)
}

Expand All @@ -94,6 +97,7 @@ abstract class AbstractFlexContainerTest<T : Any> {
movies.forEach { movie ->
container.add(widget(movie))
}
container.onEndChanges()
verifySnapshot(container)
}

Expand All @@ -115,6 +119,7 @@ abstract class AbstractFlexContainerTest<T : Any> {
}
container.add(widget(movie, modifier))
}
container.onEndChanges()
verifySnapshot(container)
}

Expand All @@ -132,6 +137,7 @@ abstract class AbstractFlexContainerTest<T : Any> {
movies.forEach { movie ->
container.add(widget(movie))
}
container.onEndChanges()
verifySnapshot(container)
}

Expand All @@ -144,6 +150,7 @@ abstract class AbstractFlexContainerTest<T : Any> {
movies.forEach { movie ->
container.add(widget(movie))
}
container.onEndChanges()
verifySnapshot(container, "Center")
container.crossAxisAlignment(CrossAxisAlignment.End)
container.onEndChanges()
Expand All @@ -163,6 +170,7 @@ abstract class AbstractFlexContainerTest<T : Any> {
movies.forEach { movie ->
container.add(widget(movie))
}
container.onEndChanges()
verifySnapshot(container)
}

Expand All @@ -174,6 +182,7 @@ abstract class AbstractFlexContainerTest<T : Any> {
repeat(10) { index ->
container.add(widget("$index", WidthImpl(50.dp)))
}
container.onEndChanges()
verifySnapshot(container)
}

Expand All @@ -185,6 +194,7 @@ abstract class AbstractFlexContainerTest<T : Any> {
repeat(10) { index ->
container.add(widget("$index", HeightImpl(50.dp)))
}
container.onEndChanges()
verifySnapshot(container)
}

Expand All @@ -196,6 +206,7 @@ abstract class AbstractFlexContainerTest<T : Any> {
repeat(10) { index ->
container.add(widget("$index", SizeImpl(50.dp, 50.dp)))
}
container.onEndChanges()
verifySnapshot(container)
}

Expand All @@ -206,8 +217,10 @@ abstract class AbstractFlexContainerTest<T : Any> {
container.crossAxisAlignment(CrossAxisAlignment.Start)
val widget = widget("")
container.add(widget)
container.onEndChanges()
verifySnapshot(container, "initial")
widget.text(movies.first())
container.onEndChanges()
verifySnapshot(container, "updated")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,12 @@ internal open class ViewLazyList private constructor(

override fun crossAxisAlignment(crossAxisAlignment: CrossAxisAlignment) {
this.crossAxisAlignment = crossAxisAlignment
adapter.notifyItemRangeChanged(0, processor.size)

// Layout params are invalid when crossAxisAlignment changes.
for (binding in processor.bindings) {
val view = binding.view ?: continue
view.content?.value?.layoutParams = createLayoutParams()
}
}

override fun onViewportChanged(onViewportChanged: (Int, Int) -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import app.cash.redwood.layout.TestFlexContainer
import app.cash.redwood.layout.Text
import app.cash.redwood.layout.api.MainAxisAlignment
import app.cash.redwood.lazylayout.widget.LazyList
import app.cash.redwood.widget.ChangeListener
import app.cash.redwood.widget.Widget
import app.cash.redwood.yoga.FlexDirection
import com.android.resources.LayoutDirection
Expand Down Expand Up @@ -70,7 +71,7 @@ class ViewLazyListTest(

class ViewTestFlexContainer private constructor(
private val delegate: ViewLazyList,
) : TestFlexContainer<View>, LazyList<View> by delegate {
) : TestFlexContainer<View>, LazyList<View> by delegate, ChangeListener {
private var childCount = 0

constructor(context: Context, direction: FlexDirection) : this(
Expand All @@ -88,6 +89,7 @@ class ViewLazyListTest(
}

override fun onEndChanges() {
delegate.onEndChanges()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ import app.cash.redwood.widget.Widget
*/
public abstract class LazyListUpdateProcessor<V : BoundView<W>, W : Any> {

public val bindings: List<Binding<V, W>>
get() = itemsBefore.nonNullElements + loadedItems + itemsAfter.nonNullElements

/** Pool of placeholder widgets. */
private val placeholdersQueue = ArrayDeque<Widget<W>>()

Expand Down Expand Up @@ -342,9 +345,14 @@ public abstract class LazyListUpdateProcessor<V : BoundView<W>, W : Any> {
internal val processor: LazyListUpdateProcessor<V, W>,
internal var isPlaceholder: Boolean = false,
) {
internal var view: V? = null
/** The currently-bound view. Null if this is not on-screen. */
public var view: V? = null
private set

/**
* The currently-bound content. This should be `lateinit`, but it can't be because of the
* side-effect in set().
*/
internal var content: Widget<W>? = null
set(value) {
field = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package app.cash.redwood.lazylayout.widget

internal class SparseList<T> : AbstractList<T?>() {
/** The non-null elements of this sparse list. */
private val elements = mutableListOf<T>()
private val elements = mutableListOf<T & Any>()

/**
* This contains the external indexes of the values in [elements]. The last element is the size of
Expand All @@ -37,6 +37,10 @@ internal class SparseList<T> : AbstractList<T?>() {
override val size: Int
get() = externalIndexes.last()

/** Returns a snapshot of the non-null elements in this list. */
val nonNullElements: List<T & Any>
get() = elements.toList()

override fun get(index: Int): T? {
require(index in 0 until size)
val internalIndex = externalIndexes.binarySearch(index)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package app.cash.redwood.lazylayout.widget

import assertk.assertThat
import assertk.assertions.containsExactly
import assertk.assertions.isEmpty
import assertk.assertions.isEqualTo
import kotlin.test.Test

Expand Down Expand Up @@ -291,4 +292,41 @@ class SparseListTest {
assertThat(list.size).isEqualTo(2)
assertThat(list.toList()).containsExactly(null, "C")
}

@Test
fun nonNullElements() {
val list = SparseList<String?>()
assertThat(list.nonNullElements).isEmpty()

list.add(null)
assertThat(list.nonNullElements).isEmpty()

list.add("A")
val a = list.nonNullElements
assertThat(a).containsExactly("A")

list.add(null)
assertThat(list.nonNullElements).containsExactly("A")

list.add("B")
assertThat(list.nonNullElements).containsExactly("A", "B")
assertThat(a).containsExactly("A") // Confirm each access returns a snapshot.
}

@Test
fun getOrCreateCreates() {
val list = SparseList<String?>()
list.addNulls(0, 10)

assertThat(list.getOrCreate(3) { "A" }).isEqualTo("A")
assertThat(list[3]).isEqualTo("A")
}

@Test
fun getOrCreateGets() {
val list = SparseList<String?>()
list.addNulls(0, 10)
list.set(3, "A")
assertThat(list.getOrCreate(3) { error("boom") }).isEqualTo("A")
}
}

0 comments on commit 75d3d4f

Please sign in to comment.