From 051d5b8f89c7769f870511077bf15f0249a569ef Mon Sep 17 00:00:00 2001 From: kenken <43767445+tkhs0604@users.noreply.github.com> Date: Tue, 19 Sep 2023 23:07:31 +0900 Subject: [PATCH] inject clock provider --- .../droidkaigi/confsched2023/AppModule.kt | 11 +++++++++ .../droidkaigi/confsched2023/MainActivity.kt | 17 +++++++++---- .../droidkaigi/confsched2023/FakeAppModule.kt | 24 +++++++++++++++++++ .../testing/robot/TimetableScreenRobot.kt | 17 +++++-------- 4 files changed, 54 insertions(+), 15 deletions(-) create mode 100644 app-android/src/test/java/io/github/droidkaigi/confsched2023/FakeAppModule.kt diff --git a/app-android/src/main/java/io/github/droidkaigi/confsched2023/AppModule.kt b/app-android/src/main/java/io/github/droidkaigi/confsched2023/AppModule.kt index 06b477e0d..1b555fd33 100644 --- a/app-android/src/main/java/io/github/droidkaigi/confsched2023/AppModule.kt +++ b/app-android/src/main/java/io/github/droidkaigi/confsched2023/AppModule.kt @@ -6,6 +6,7 @@ import dagger.hilt.InstallIn import dagger.hilt.components.SingletonComponent import io.github.droidkaigi.confsched2023.data.di.AppAndroidBuildConfig import io.github.droidkaigi.confsched2023.model.BuildConfigProvider +import kotlinx.datetime.Clock import javax.inject.Singleton @InstallIn(SingletonComponent::class) @@ -15,9 +16,19 @@ class AppModule { @Singleton @AppAndroidBuildConfig fun provideBuildConfigProvider(): BuildConfigProvider = AppBuildConfigProvider() + + @Provides + @Singleton + fun provideClockProvider(): ClockProvider = object : ClockProvider { + override fun clock(): Clock = Clock.System + } } class AppBuildConfigProvider( override val versionName: String = BuildConfig.VERSION_NAME, override val debugBuild: Boolean = BuildConfig.DEBUG, ) : BuildConfigProvider + +interface ClockProvider { + fun clock(): Clock +} diff --git a/app-android/src/main/java/io/github/droidkaigi/confsched2023/MainActivity.kt b/app-android/src/main/java/io/github/droidkaigi/confsched2023/MainActivity.kt index 83aff3ba3..e7d5bb352 100644 --- a/app-android/src/main/java/io/github/droidkaigi/confsched2023/MainActivity.kt +++ b/app-android/src/main/java/io/github/droidkaigi/confsched2023/MainActivity.kt @@ -10,6 +10,7 @@ import androidx.activity.enableEdgeToEdge import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass import androidx.compose.runtime.Composable +import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.getValue import androidx.compose.runtime.produceState import androidx.compose.runtime.remember @@ -19,12 +20,18 @@ import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen import androidx.window.layout.DisplayFeature import androidx.window.layout.WindowInfoTracker import dagger.hilt.android.AndroidEntryPoint +import io.github.droidkaigi.confsched2023.ui.compositionlocal.LocalClock import kotlinx.collections.immutable.PersistentList import kotlinx.collections.immutable.persistentListOf import kotlinx.collections.immutable.toPersistentList +import javax.inject.Inject @AndroidEntryPoint class MainActivity : ComponentActivity() { + + @Inject + lateinit var clockProvider: ClockProvider + @OptIn(ExperimentalMaterial3WindowSizeClassApi::class) override fun onCreate(savedInstanceState: Bundle?) { installSplashScreen() @@ -55,10 +62,12 @@ class MainActivity : ComponentActivity() { setContent { val windowSize = calculateWindowSizeClass(this) val displayFeatures = calculateDisplayFeatures(this) - KaigiApp( - windowSize = windowSize, - displayFeatures = displayFeatures, - ) + CompositionLocalProvider(LocalClock provides clockProvider.clock()) { + KaigiApp( + windowSize = windowSize, + displayFeatures = displayFeatures, + ) + } } } } diff --git a/app-android/src/test/java/io/github/droidkaigi/confsched2023/FakeAppModule.kt b/app-android/src/test/java/io/github/droidkaigi/confsched2023/FakeAppModule.kt new file mode 100644 index 000000000..90ddc1f3b --- /dev/null +++ b/app-android/src/test/java/io/github/droidkaigi/confsched2023/FakeAppModule.kt @@ -0,0 +1,24 @@ +package io.github.droidkaigi.confsched2023 + +import dagger.Module +import dagger.Provides +import dagger.hilt.components.SingletonComponent +import dagger.hilt.testing.TestInstallIn +import kotlinx.datetime.Clock +import kotlinx.datetime.Instant +import javax.inject.Singleton + +@TestInstallIn( + components = [SingletonComponent::class], + replaces = [AppModule::class], +) +@Module +class FakeAppModule { + @Provides + @Singleton + fun provideClockProvider(): ClockProvider = object : ClockProvider { + override fun clock(): Clock = object : Clock { + override fun now() = Instant.parse("2023-09-14T10:00:00.00Z") + } + } +} diff --git a/core/testing/src/main/java/io/github/droidkaigi/confsched2023/testing/robot/TimetableScreenRobot.kt b/core/testing/src/main/java/io/github/droidkaigi/confsched2023/testing/robot/TimetableScreenRobot.kt index e68f65f2a..1bea2ec3e 100644 --- a/core/testing/src/main/java/io/github/droidkaigi/confsched2023/testing/robot/TimetableScreenRobot.kt +++ b/core/testing/src/main/java/io/github/droidkaigi/confsched2023/testing/robot/TimetableScreenRobot.kt @@ -1,6 +1,5 @@ package io.github.droidkaigi.confsched2023.testing.robot -import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.hasTestTag import androidx.compose.ui.test.isRoot @@ -26,8 +25,6 @@ import io.github.droidkaigi.confsched2023.sessions.component.TimetableUiTypeChan import io.github.droidkaigi.confsched2023.sessions.section.TimetableTabTestTag import io.github.droidkaigi.confsched2023.testing.RobotTestRule import io.github.droidkaigi.confsched2023.testing.coroutines.runTestWithLogging -import io.github.droidkaigi.confsched2023.ui.compositionlocal.FakeClock -import io.github.droidkaigi.confsched2023.ui.compositionlocal.LocalClock import kotlinx.coroutines.test.TestDispatcher import javax.inject.Inject import kotlin.time.Duration.Companion.seconds @@ -53,14 +50,12 @@ class TimetableScreenRobot @Inject constructor( fun setupTimetableScreenContent() { composeTestRule.setContent { - CompositionLocalProvider(LocalClock provides FakeClock) { - KaigiTheme { - TimetableScreen( - onSearchClick = { }, - onTimetableItemClick = { }, - onBookmarkIconClick = { }, - ) - } + KaigiTheme { + TimetableScreen( + onSearchClick = { }, + onTimetableItemClick = { }, + onBookmarkIconClick = { }, + ) } } waitUntilIdle()