Skip to content

Commit

Permalink
Extract a common Snapshotter interface (#2325)
Browse files Browse the repository at this point in the history
* Extract a common Snapshotter interface

I couldn't copy-paste this stuff a third time.

* Use compileOnly for the Paparazzi dependency

Otherwise we get errors like this:

    Duplicate class androidx.annotation.ChecksSdkIntAtLeast found in modules annotation-jvm-1.7.1.jar -> annotation-jvm-1.7.1 (androidx.annotation:annotation-jvm:1.7.1) and layoutlib-native-jdk11-2022.3.1-5e972ea.jar -> layoutlib-native-jdk11-2022.3.1-5e972ea (app.cash.paparazzi:layoutlib-native-jdk11:2022.3.1-5e972ea)
  • Loading branch information
squarejesse authored Sep 25, 2024
1 parent e0989f3 commit f7b43c0
Show file tree
Hide file tree
Showing 40 changed files with 90 additions and 189 deletions.
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ androidx-activity = "1.9.2"
androidx-compose-ui = "1.7.2"
jbCompose = "1.6.11"
lint = "31.6.1"
paparazzi = "1.3.2"
zipline = "1.17.0"
coil = "3.0.0-alpha10"
okio = "3.9.1"
Expand Down Expand Up @@ -78,7 +79,8 @@ paging-compose-common = { module = "app.cash.paging:paging-compose-common", vers
zipline = { module = "app.cash.zipline:zipline", version.ref = "zipline" }
zipline-gradlePlugin = { module = "app.cash.zipline:zipline-gradle-plugin", version.ref = "zipline" }
zipline-loader = { module = "app.cash.zipline:zipline-loader", version.ref = "zipline" }
paparazzi-gradlePlugin = "app.cash.paparazzi:paparazzi-gradle-plugin:1.3.2"
paparazzi-gradlePlugin = { module = "app.cash.paparazzi:paparazzi-gradle-plugin", version.ref = "paparazzi" }
paparazzi = { module = "app.cash.paparazzi:paparazzi", version.ref = "paparazzi" }
jimfs = "com.google.jimfs:jimfs:1.3.0"
coil-compose = { module = "io.coil-kt.coil3:coil-compose-core", version.ref = "coil" }
coil-core = { module = "io.coil-kt.coil3:coil", version.ref = "coil" }
Expand Down
1 change: 1 addition & 0 deletions redwood-layout-composeui/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ kotlin {
androidUnitTest {
dependencies {
implementation projects.redwoodLayoutSharedTest
implementation projects.redwoodSnapshotTesting
implementation libs.testParameterInjector
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import app.cash.redwood.layout.AbstractBoxTest
import app.cash.redwood.layout.Color as ColorWidget
import app.cash.redwood.layout.Text
import app.cash.redwood.layout.widget.Box
import app.cash.redwood.snapshot.testing.ComposeSnapshotter
import com.android.resources.LayoutDirection
import com.google.testing.junit.testparameterinjector.TestParameter
import com.google.testing.junit.testparameterinjector.TestParameterInjector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import app.cash.paparazzi.Paparazzi
import app.cash.redwood.layout.AbstractFlexContainerTest
import app.cash.redwood.layout.TestFlexContainer
import app.cash.redwood.layout.widget.FlexContainer
import app.cash.redwood.snapshot.testing.ComposeSnapshotter
import app.cash.redwood.ui.Px
import app.cash.redwood.widget.Widget
import app.cash.redwood.widget.compose.ComposeWidgetChildren
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import app.cash.paparazzi.DeviceConfig
import app.cash.paparazzi.Paparazzi
import app.cash.redwood.layout.AbstractSpacerTest
import app.cash.redwood.layout.widget.Spacer
import app.cash.redwood.snapshot.testing.ComposeSnapshotter
import app.cash.redwood.widget.Widget
import com.android.ide.common.rendering.api.SessionParams
import org.junit.Rule
Expand Down
1 change: 1 addition & 0 deletions redwood-layout-shared-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ kotlin {
api projects.redwoodLayoutModifiers
api projects.redwoodLayoutWidget
api projects.redwoodRuntime
api projects.redwoodSnapshotTesting
api projects.redwoodWidget
api projects.redwoodYoga
api libs.kotlin.test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import app.cash.redwood.Modifier
import app.cash.redwood.layout.api.Constraint
import app.cash.redwood.layout.api.CrossAxisAlignment
import app.cash.redwood.layout.widget.Box
import app.cash.redwood.snapshot.testing.Snapshotter
import app.cash.redwood.ui.Dp
import app.cash.redwood.ui.Margin
import app.cash.redwood.ui.dp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import app.cash.redwood.layout.api.MainAxisAlignment
import app.cash.redwood.layout.api.Overflow
import app.cash.redwood.layout.widget.Column
import app.cash.redwood.layout.widget.Row
import app.cash.redwood.snapshot.testing.Snapshotter
import app.cash.redwood.ui.Margin
import app.cash.redwood.ui.Px
import app.cash.redwood.ui.dp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package app.cash.redwood.layout

import app.cash.redwood.layout.widget.Spacer
import app.cash.redwood.snapshot.testing.Snapshotter
import app.cash.redwood.ui.dp
import app.cash.redwood.widget.Widget
import kotlin.test.Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import RedwoodLayoutUIViewTestKt
import SnapshotTesting
import UIKit

final class SnapshotTestingCallback : UIViewSnapshotCallback {
final class SnapshotTestingCallback : Redwood_snapshot_testingUIViewSnapshotCallback {
private let testName: String
private let fileName: StaticString

Expand Down
1 change: 1 addition & 0 deletions redwood-layout-uiview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ kotlin {
commonTest {
dependencies {
api projects.redwoodLayoutSharedTest
api projects.redwoodSnapshotTesting
api libs.kotlin.test
implementation libs.assertk
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import app.cash.redwood.layout.AbstractBoxTest
import app.cash.redwood.layout.Color
import app.cash.redwood.layout.Text
import app.cash.redwood.layout.widget.Box
import app.cash.redwood.snapshot.testing.UIViewSnapshotCallback
import app.cash.redwood.snapshot.testing.UIViewSnapshotter
import assertk.assertThat
import kotlin.test.Test
import platform.CoreGraphics.CGSizeMake
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import app.cash.redwood.layout.api.Constraint
import app.cash.redwood.layout.api.CrossAxisAlignment
import app.cash.redwood.layout.toUIColor
import app.cash.redwood.layout.widget.FlexContainer
import app.cash.redwood.snapshot.testing.UIViewSnapshotCallback
import app.cash.redwood.snapshot.testing.UIViewSnapshotter
import app.cash.redwood.ui.Px
import app.cash.redwood.widget.ChangeListener
import app.cash.redwood.widget.ResizableWidget
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package app.cash.redwood.layout.uiview

import app.cash.redwood.layout.AbstractSpacerTest
import app.cash.redwood.snapshot.testing.UIViewSnapshotCallback
import app.cash.redwood.snapshot.testing.UIViewSnapshotter
import app.cash.redwood.widget.Widget
import kotlinx.cinterop.readValue
import kotlinx.cinterop.useContents
Expand Down
1 change: 1 addition & 0 deletions redwood-layout-view/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies {
implementation projects.redwoodYoga
implementation libs.androidx.core
testImplementation projects.redwoodLayoutSharedTest
testImplementation projects.redwoodSnapshotTesting
testImplementation libs.testParameterInjector
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import app.cash.paparazzi.DeviceConfig
import app.cash.paparazzi.Paparazzi
import app.cash.redwood.layout.AbstractBoxTest
import app.cash.redwood.layout.Color
import app.cash.redwood.layout.Snapshotter
import app.cash.redwood.layout.Text
import app.cash.redwood.layout.widget.Box
import app.cash.redwood.snapshot.testing.Snapshotter
import app.cash.redwood.snapshot.testing.ViewSnapshotter
import com.android.resources.LayoutDirection
import com.google.testing.junit.testparameterinjector.TestParameter
import com.google.testing.junit.testparameterinjector.TestParameterInjector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import app.cash.paparazzi.Paparazzi
import app.cash.redwood.layout.AbstractFlexContainerTest
import app.cash.redwood.layout.TestFlexContainer
import app.cash.redwood.layout.widget.FlexContainer
import app.cash.redwood.snapshot.testing.ViewSnapshotter
import app.cash.redwood.ui.Px
import app.cash.redwood.widget.ChangeListener
import app.cash.redwood.widget.ViewGroupChildren
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import app.cash.paparazzi.DeviceConfig
import app.cash.paparazzi.Paparazzi
import app.cash.redwood.layout.AbstractSpacerTest
import app.cash.redwood.layout.widget.Spacer
import app.cash.redwood.snapshot.testing.ViewSnapshotter
import app.cash.redwood.widget.Widget
import com.android.ide.common.rendering.api.SessionParams
import org.junit.Rule
Expand Down
1 change: 1 addition & 0 deletions redwood-lazylayout-composeui/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ kotlin {
dependencies {
implementation projects.redwoodLayoutComposeui
implementation projects.redwoodLayoutSharedTest
implementation projects.redwoodSnapshotTesting
implementation libs.testParameterInjector
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import app.cash.redwood.layout.widget.Column
import app.cash.redwood.layout.widget.Row
import app.cash.redwood.lazylayout.composeui.ComposeUiLazyList
import app.cash.redwood.lazylayout.widget.LazyList
import app.cash.redwood.snapshot.testing.ComposeSnapshotter
import app.cash.redwood.ui.Px
import app.cash.redwood.widget.Widget
import app.cash.redwood.widget.compose.ComposeWidgetChildren
Expand Down
1 change: 1 addition & 0 deletions redwood-lazylayout-shared-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ kotlin {
dependencies {
api projects.redwoodLazylayoutApi
api projects.redwoodLazylayoutWidget
api projects.redwoodSnapshotTesting
api projects.redwoodRuntime
api projects.redwoodWidget
api projects.redwoodYoga
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import app.cash.redwood.layout.api.Constraint
import app.cash.redwood.layout.api.CrossAxisAlignment
import app.cash.redwood.lazylayout.api.ScrollItemIndex
import app.cash.redwood.lazylayout.widget.LazyList
import app.cash.redwood.snapshot.testing.Snapshotter
import app.cash.redwood.ui.Margin
import app.cash.redwood.ui.dp
import app.cash.redwood.widget.ChangeListener
import app.cash.redwood.widget.Widget
import kotlin.test.Test

abstract class AbstractLazyListTest<T : Any> {
Expand Down Expand Up @@ -59,62 +59,63 @@ abstract class AbstractLazyListTest<T : Any> {
return result
}

abstract fun verifySnapshot(
container: Widget<T>,
name: String? = null,
)
abstract fun snapshotter(widget: T): Snapshotter

@Test fun testHappyPath() {
@Test
fun testHappyPath() {
val lazyList = defaultLazyList()

for ((index, value) in movies.take(5).withIndex()) {
lazyList.items.insert(index, coloredText(text = value))
}
(lazyList as? ChangeListener)?.onEndChanges()

verifySnapshot(lazyList)
snapshotter(lazyList.value).snapshot()
}

@Test fun testPlaceholderToLoadedAndLoadedToPlaceholder() {
@Test
fun testPlaceholderToLoadedAndLoadedToPlaceholder() {
val lazyList = defaultLazyList()
val snapshotter = snapshotter(lazyList.value)

(lazyList as? ChangeListener)?.onEndChanges()
verifySnapshot(lazyList, "0 empty")
snapshotter.snapshot("0 empty")

lazyList.itemsBefore(0)
lazyList.itemsAfter(10)
(lazyList as? ChangeListener)?.onEndChanges()
verifySnapshot(lazyList, "1 placeholders")
snapshotter.snapshot("1 placeholders")

lazyList.itemsBefore(0)
lazyList.itemsAfter(0)
for ((index, value) in movies.take(10).withIndex()) {
lazyList.items.insert(index, coloredText(text = value))
}
(lazyList as? ChangeListener)?.onEndChanges()
verifySnapshot(lazyList, "2 loaded")
snapshotter.snapshot("2 loaded")

lazyList.itemsBefore(0)
lazyList.itemsAfter(10)
lazyList.items.remove(0, 10)
(lazyList as? ChangeListener)?.onEndChanges()
verifySnapshot(lazyList, "3 placeholders")
snapshotter.snapshot("3 placeholders")

lazyList.itemsBefore(0)
lazyList.itemsAfter(0)
(lazyList as? ChangeListener)?.onEndChanges()
verifySnapshot(lazyList, "4 empty")
snapshotter.snapshot("4 empty")
}

@Test fun testPlaceholdersExhausted() {
@Test
fun testPlaceholdersExhausted() {
val lazyList = defaultLazyList()

lazyList.itemsBefore(11)
for ((index, value) in movies.take(1).withIndex()) {
lazyList.items.insert(index, coloredText(text = value))
}
(lazyList as? ChangeListener)?.onEndChanges()
verifySnapshot(lazyList)
snapshotter(lazyList.value).snapshot()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import RedwoodLazylayoutUIViewTestKt
import SnapshotTesting
import UIKit

final class SnapshotTestingCallback : UIViewSnapshotCallback {
final class SnapshotTestingCallback : Redwood_snapshot_testingUIViewSnapshotCallback {
private let testName: String
private let fileName: StaticString

Expand Down
Loading

0 comments on commit f7b43c0

Please sign in to comment.