Skip to content

Commit

Permalink
Fix flaky network image tests and refactor test packages
Browse files Browse the repository at this point in the history
  • Loading branch information
takahirom committed Aug 8, 2024
1 parent e63c08b commit 2df87ec
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package io.github.droidkaigi.confsched.testing

import com.github.takahirom.roborazzi.ComposePreviewTester
import com.github.takahirom.roborazzi.ComposePreviewTester.Options
import com.github.takahirom.roborazzi.ComposePreviewTester.Options.JUnit4TestLifecycleOptions
import com.github.takahirom.roborazzi.captureRoboImage
import io.github.droidkaigi.confsched.testing.rules.CoilRule
import sergio.sastre.composable.preview.scanner.core.preview.ComposablePreview
import sergio.sastre.composable.preview.scanner.jvm.JvmAnnotationScanner
import sergio.sastre.composable.preview.scanner.jvm.JvmAnnotationScanner.DesktopPreviewInfo

class DroidKaigiKmpPreviewTester : ComposePreviewTester<JvmAnnotationScanner.DesktopPreviewInfo> {
override fun options(): Options {
return super.options().copy(
testLifecycleOptions = JUnit4TestLifecycleOptions {
CoilRule()
},
)
}
override fun previews(): List<ComposablePreview<DesktopPreviewInfo>> {
return JvmAnnotationScanner("org.jetbrains.compose.ui.tooling.preview.Preview")
.scanPackageTrees(*options().scanOptions.packages.toTypedArray())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.droidkaigi.confsched.testing
package io.github.droidkaigi.confsched.testing.robot

import androidx.compose.ui.test.junit4.ComposeTestRule
import com.github.takahirom.roborazzi.provideRoborazziContext
Expand All @@ -14,7 +14,6 @@ import io.github.droidkaigi.confsched.data.sponsors.SponsorsApiClient
import io.github.droidkaigi.confsched.data.staff.FakeStaffApiClient
import io.github.droidkaigi.confsched.data.staff.StaffApiClient
import io.github.droidkaigi.confsched.testing.SponsorsServerRobot.ServerStatus
import io.github.droidkaigi.confsched.testing.SponsorsServerRobot.ServerStatus.Error
import io.github.droidkaigi.confsched.testing.SponsorsServerRobot.ServerStatus.Operational
import io.github.droidkaigi.confsched.testing.coroutines.runTestWithLogging
import kotlinx.coroutines.test.TestDispatcher
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.droidkaigi.confsched.testing.rules

import android.graphics.drawable.ColorDrawable
import androidx.test.core.app.ApplicationProvider
import coil3.ImageLoader
import coil3.SingletonImageLoader
import coil3.test.FakeImageLoaderEngine
import coil3.test.default
import org.junit.rules.TestWatcher
import org.junit.runner.Description

class CoilRule : TestWatcher() {
override fun starting(description: Description?) {
super.starting(description)
val engine = FakeImageLoaderEngine.Builder()
.default(ColorDrawable(android.graphics.Color.BLUE))
.build()
val imageLoader =
ImageLoader.Builder(ApplicationProvider.getApplicationContext())
.components { add(engine) }
.build()
SingletonImageLoader.setUnsafe(imageLoader)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.droidkaigi.confsched.testing
package io.github.droidkaigi.confsched.testing.rules

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.droidkaigi.confsched.testing
package io.github.droidkaigi.confsched.testing.rules

import dagger.hilt.android.testing.HiltAndroidRule
import org.junit.rules.RuleChain
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.droidkaigi.confsched.testing
package io.github.droidkaigi.confsched.testing.rules

import dagger.hilt.android.testing.HiltAndroidRule
import org.junit.rules.TestWatcher
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.droidkaigi.confsched.testing
package io.github.droidkaigi.confsched.testing.rules

import android.content.Intent
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.compose.runtime.Composable
Expand All @@ -13,10 +12,6 @@ import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.rules.ActivityScenarioRule
import co.touchlab.kermit.CommonWriter
import co.touchlab.kermit.Logger
import coil3.ImageLoader
import coil3.SingletonImageLoader
import coil3.test.FakeImageLoaderEngine
import coil3.test.default
import com.github.takahirom.roborazzi.RoborazziOptions
import com.github.takahirom.roborazzi.RoborazziOptions.CompareOptions
import com.github.takahirom.roborazzi.RoborazziOptions.PixelBitConfig
Expand All @@ -29,6 +24,7 @@ import dagger.hilt.EntryPoints
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import io.github.droidkaigi.confsched.data.di.RepositoryProvider
import io.github.droidkaigi.confsched.testing.HiltTestActivity
import org.junit.rules.RuleChain
import org.junit.rules.TestRule
import org.junit.rules.TestWatcher
Expand Down Expand Up @@ -113,19 +109,7 @@ class RobotTestRule(
),
),
)
.around(object : TestWatcher() {
override fun starting(description: Description?) {
super.starting(description)
val engine = FakeImageLoaderEngine.Builder()
.default(ColorDrawable(android.graphics.Color.BLUE))
.build()
val imageLoader =
ImageLoader.Builder(ApplicationProvider.getApplicationContext())
.components { add(engine) }
.build()
SingletonImageLoader.setUnsafe(imageLoader)
}
})
.around(CoilRule())
.around(composeTestRule)
.apply(base, description)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.droidkaigi.confsched.testing
package io.github.droidkaigi.confsched.testing.rules

import org.junit.rules.TestWatcher
import org.junit.runner.Description
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ kover = "0.7.6"
androidxLifecycleProcess = "2.8.4"
skie = "0.8.4"
composablePreviewScanner = "0.1.3"
coil = "3.0.0-alpha06"
coil = "3.0.0-alpha10"

[libraries]
androidGradlePlugin = { group = "com.android.tools.build", name = "gradle", version.ref = "androidGradlePlugin" }
Expand Down Expand Up @@ -110,7 +110,7 @@ coreBundle = { module = "org.jetbrains.androidx.core:core-bundle", version = "1.
composeHiltNavigtation = { module = "androidx.hilt:hilt-navigation-compose", version.ref = "composeHiltNavigatiaon" }
composeLintCheck = { module = "com.slack.lint.compose:compose-lint-checks", version = "1.3.1" }
coil = { module = "io.coil-kt.coil3:coil-compose", version.ref = "coil" }
coilNetwork = { module = "io.coil-kt.coil3:coil-network-ktor", version.ref = "coil" }
coilNetwork = { module = "io.coil-kt.coil3:coil-network-ktor2", version.ref = "coil" }
coilTest = { module = "io.coil-kt.coil3:coil-test", version.ref = "coil" }
composeShimmer = { module = "com.valentinilk.shimmer:compose-shimmer", version = "1.0.5" }
rin = { module = "io.github.takahirom.rin:rin", version.ref = "rin" }
Expand Down

0 comments on commit 2df87ec

Please sign in to comment.