diff --git a/app/build.gradle b/app/build.gradle index 15a1ba7d..6353ecb9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -70,5 +70,6 @@ dependencies { implementation "com.jakewharton.timber:timber:$timber_version" testImplementation "junit:junit:$junit_version" + testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_test_version" debugImplementation "com.squareup.leakcanary:leakcanary-android:$leak_canary_version" } diff --git a/app/src/main/java/com/rtbishop/look4sat/di/DispatchersModule.kt b/app/src/main/java/com/rtbishop/look4sat/di/DispatchersModule.kt new file mode 100644 index 00000000..416330e5 --- /dev/null +++ b/app/src/main/java/com/rtbishop/look4sat/di/DispatchersModule.kt @@ -0,0 +1,57 @@ +/******************************************************************************* +Look4Sat. Amateur radio satellite tracker and pass predictor. +Copyright (C) 2019, 2020 Arty Bishop (bishop.arty@gmail.com) + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + ******************************************************************************/ + +package com.rtbishop.look4sat.di + +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers +import javax.inject.Qualifier + +@Module +@InstallIn(SingletonComponent::class) +object DispatchersModule { + + @Provides + @DefaultDispatcher + fun provideDefaultDispatcher(): CoroutineDispatcher = Dispatchers.Default + + @Provides + @IoDispatcher + fun provideIoDispatcher(): CoroutineDispatcher = Dispatchers.IO + + @Provides + @MainDispatcher + fun provideMainDispatcher(): CoroutineDispatcher = Dispatchers.Main +} + +@Qualifier +@Retention(AnnotationRetention.BINARY) +annotation class DefaultDispatcher + +@Qualifier +@Retention(AnnotationRetention.BINARY) +annotation class IoDispatcher + +@Qualifier +@Retention(AnnotationRetention.BINARY) +annotation class MainDispatcher \ No newline at end of file diff --git a/app/src/test/java/com/rtbishop/look4sat/CoroutineTestRule.kt b/app/src/test/java/com/rtbishop/look4sat/CoroutineTestRule.kt new file mode 100644 index 00000000..0d8d5bfd --- /dev/null +++ b/app/src/test/java/com/rtbishop/look4sat/CoroutineTestRule.kt @@ -0,0 +1,44 @@ +/******************************************************************************* +Look4Sat. Amateur radio satellite tracker and pass predictor. +Copyright (C) 2019, 2020 Arty Bishop (bishop.arty@gmail.com) + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + ******************************************************************************/ + +package com.rtbishop.look4sat + +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.TestCoroutineDispatcher +import kotlinx.coroutines.test.resetMain +import kotlinx.coroutines.test.setMain +import org.junit.rules.TestWatcher +import org.junit.runner.Description + +@ExperimentalCoroutinesApi +class CoroutineTestRule(private val dispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()) : + TestWatcher() { + + override fun starting(description: Description?) { + super.starting(description) + Dispatchers.setMain(dispatcher) + } + + override fun finished(description: Description?) { + super.finished(description) + Dispatchers.resetMain() + dispatcher.cleanupTestCoroutines() + } +} \ No newline at end of file diff --git a/build.gradle b/build.gradle index c9df13c5..034d737f 100644 --- a/build.gradle +++ b/build.gradle @@ -16,6 +16,7 @@ buildscript { timber_version = '4.7.1' predict4java_version = '1.3.1' junit_version = '4.13.2' + coroutines_test_version = '1.3.2' leak_canary_version = '2.5' } repositories {